blib/lib/DBD/SQLcipher/VirtualTable/PerlData.pm untested

filter: Statementstmt 0.0% 0 / 136 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 29 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 235 CC 16 SCAR 28.0
CC
16
Cov
0%
BEST_INDEX
56.0
_build_new_row
20.0
COLUMN
20.0
CRAP 16.4
1 #======================================================================
2 package DBD::SQLcipher::VirtualTable::PerlData;
3 #======================================================================
4 use strict;
5 use warnings;
6 use base 'DBD::SQLcipher::VirtualTable';
7 use DBD::SQLcipher;
8 use constant SQLITE_3010000 => $DBD::SQLcipher::sqlite_version_number >= 3010000 ? 1 : 0;
9  
10 # private data for translating comparison operators from Sqlite to Perl
11 my $TXT = 0;
12 my $NUM = 1;
13 my %SQLOP2PERLOP = (
14 # TXT NUM
15 '=' => [ 'eq', '==' ],
16 '<' => [ 'lt', '<' ],
17 '<=' => [ 'le', '<=' ],
18 '>' => [ 'gt', '>' ],
19 '>=' => [ 'ge', '>=' ],
20 'MATCH' => [ '=~', '=~' ],
21 (SQLITE_3010000 ? (
22 'LIKE' => [ 'DBD::SQLcipher::strlike', 'DBD::SQLcipher::strlike' ],
23 'GLOB' => [ 'DBD::SQLcipher::strglob', 'DBD::SQLcipher::strglob' ],
24 'REGEXP'=> [ '=~', '=~' ],
25 ) : ()),
26 );
27  
28 #----------------------------------------------------------------------
29 # instanciation methods
30 #----------------------------------------------------------------------
31  
32 sub NEW {
33 my $class = shift;
34 my $self = $class->_PREPARE_SELF(@_);
35  
36 # verifications
37 my $n_cols = @{$self->{columns}};
38 $n_cols > 0
39 or die "$class: no declared columns";
40 !$self->{options}{colref} || $n_cols == 1
41 or die "$class: must have exactly 1 column when using 'colref'";
42 my $symbolic_ref = $self->{options}{arrayrefs}
43 || $self->{options}{hashrefs}
44 || $self->{options}{colref}
45 or die "$class: missing option 'arrayrefs' or 'hashrefs' or 'colref'";
46  
47 # bind to the Perl variable
48 no strict "refs";
49 defined ${$symbolic_ref}
50 or die "$class: can't find global variable \$$symbolic_ref";
51 $self->{rows} = \ ${$symbolic_ref};
52  
53 bless $self, $class;
54 }
55  
56 sub _build_headers_optypes {
57 my $self = shift;
58  
59 my $cols = $self->sqlite_table_info;
60  
61 # headers : names of columns, without type information
62 $self->{headers} = [ map {$_->{name}} @$cols ];
63  
64 # optypes : either $NUM or $TEXT for each column
65 # (applying algorithm from datatype3.html" for type affinity)
66 $self->{optypes}
67 = [ map {$_->{type} =~ /INT|REAL|FLOA|DOUB/i ? $NUM : $TXT} @$cols ];
68 }
69  
70 #----------------------------------------------------------------------
71 # method for initiating a search
72 #----------------------------------------------------------------------
73  
74 sub BEST_INDEX {
75 my ($self, $constraints, $order_by) = @_;
76  
77 $self->_build_headers_optypes if !$self->{headers};
78  
79 # for each constraint, build a Perl code fragment. Those will be gathered
80 # in FILTER() for deciding which rows match the constraints.
81 my @conditions;
82 my $ix = 0;
83 foreach my $constraint (grep {$_->{usable}} @$constraints) {
84 my $col = $constraint->{col};
85 my ($member, $optype);
86  
87 # build a Perl code fragment. Those fragments will be gathered
88 # and eval-ed in FILTER(), for deciding which rows match the constraints.
89 if ($col == -1) {
90 # constraint on rowid
91 $member = '$i';
92 $optype = $NUM;
93 }
94 else {
95 # constraint on regular column
96 my $opts = $self->{options};
97 $member = $opts->{arrayrefs} ? "\$row->[$col]"
98 : $opts->{hashrefs} ? "\$row->{$self->{headers}[$col]}"
99 : $opts->{colref} ? "\$row"
100 : die "corrupted data in ->{options}";
101 $optype = $self->{optypes}[$col];
102 }
103 my $op = $SQLOP2PERLOP{$constraint->{op}}[$optype];
104 if (SQLITE_3010000 && $op =~ /str/) {
105 push @conditions,
106 "(defined($member) && defined(\$vals[$ix]) && !$op(\$vals[$ix], $member))";
107 } else {
108 push @conditions,
109 "(defined($member) && defined(\$vals[$ix]) && $member $op \$vals[$ix])";
110 }
111 # Note : $vals[$ix] refers to an array of values passed to the
112 # FILTER method (see below); so the eval-ed perl code will be a
113 # closure on those values
114 # info passed back to the SQLite core -- see vtab.html in sqlite doc
115 $constraint->{argvIndex} = $ix++;
116 $constraint->{omit} = 1;
117 }
118  
119 # further info for the SQLite core
120 my $outputs = {
121 idxNum => 1,
122 idxStr => (join(" && ", @conditions) || "1"),
123 orderByConsumed => 0,
124 estimatedCost => 1.0,
125 estimatedRows => undef,
126 };
127  
128 return $outputs;
129 }
130  
131  
132 #----------------------------------------------------------------------
133 # methods for data update
134 #----------------------------------------------------------------------
135  
136 sub _build_new_row {
137 my ($self, $values) = @_;
138  
139 my $opts = $self->{options};
140 return $opts->{arrayrefs} ? $values
141 : $opts->{hashrefs} ? { map {$self->{headers}->[$_], $values->[$_]}
142 (0 .. @{$self->{headers}} - 1) }
143 : $opts->{colref} ? $values->[0]
144 : die "corrupted data in ->{options}";
145 }
146  
147 sub INSERT {
148 my ($self, $new_rowid, @values) = @_;
149  
150 my $new_row = $self->_build_new_row(\@values);
151  
152 if (defined $new_rowid) {
153 not ${$self->{rows}}->[$new_rowid]
154 or die "can't INSERT : rowid $new_rowid already in use";
155 ${$self->{rows}}->[$new_rowid] = $new_row;
156 }
157 else {
158 push @${$self->{rows}}, $new_row;
159 return $#${$self->{rows}};
160 }
161 }
162  
163 sub DELETE {
164 my ($self, $old_rowid) = @_;
165  
166 delete ${$self->{rows}}->[$old_rowid];
167 }
168  
169 sub UPDATE {
170 my ($self, $old_rowid, $new_rowid, @values) = @_;
171  
172 my $new_row = $self->_build_new_row(\@values);
173  
174 if ($new_rowid == $old_rowid) {
175 ${$self->{rows}}->[$old_rowid] = $new_row;
176 }
177 else {
178 delete ${$self->{rows}}->[$old_rowid];
179 ${$self->{rows}}->[$new_rowid] = $new_row;
180 }
181 }
182  
183  
184 #======================================================================
185 package DBD::SQLcipher::VirtualTable::PerlData::Cursor;
186 #======================================================================
187 use strict;
188 use warnings;
189 use base "DBD::SQLcipher::VirtualTable::Cursor";
190  
191  
192 sub row {
193 my ($self, $i) = @_;
194 return ${$self->{vtable}{rows}}->[$i];
195 }
196  
197 sub FILTER {
198 my ($self, $idxNum, $idxStr, @vals) = @_;
199  
200 # build a method coderef to fetch matching rows
201 my $perl_code = 'sub {my ($self, $i) = @_; my $row = $self->row($i); '
202 . $idxStr
203 . '}';
204  
205 # print STDERR "PERL CODE:\n", $perl_code, "\n";
206  
207 $self->{is_wanted_row} = do { no warnings; eval $perl_code }
208 or die "couldn't eval q{$perl_code} : $@";
209  
210 # position the cursor to the first matching row (or to eof)
211 $self->{row_ix} = -1;
212 $self->NEXT;
213 }
214  
215  
216 sub EOF {
217 my ($self) = @_;
218  
219 return $self->{row_ix} > $#${$self->{vtable}{rows}};
220 }
221  
222 sub NEXT {
223 my ($self) = @_;
224  
225 do {
226 $self->{row_ix} += 1
227 } until $self->EOF
228 || eval {$self->{is_wanted_row}->($self, $self->{row_ix})};
229  
230 # NOTE: the eval above is required for cases when user data, injected
231 # into Perl comparison operators, generates errors; for example
232 # WHERE col MATCH '(foo' will die because the regex is not well formed
233 # (no matching parenthesis). In such cases no row is selected and the
234 # query just returns an empty list.
235 }
236  
237  
238 sub COLUMN {
239 my ($self, $idxCol) = @_;
240  
241 my $row = $self->row($self->{row_ix});
242  
243 my $opts = $self->{vtable}{options};
244 return $opts->{arrayrefs} ? $row->[$idxCol]
245 : $opts->{hashrefs} ? $row->{$self->{vtable}{headers}[$idxCol]}
246 : $opts->{colref} ? $row
247 : die "corrupted data in ->{options}";
248 }
249  
250 sub ROWID {
251 my ($self) = @_;
252  
253 return $self->{row_ix} + 1; # rowids start at 1 in SQLite
254 }
255  
256  
257 1;
258  
259 __END__