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

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