blib/lib/DBD/SQLite/VirtualTable/PerlData.pm

filter: Statementstmt 84.6% 110 / 130 Branchbran 67.3% 35 / 52 Conditioncond 82.3% 14 / 17 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 91.6% 22 / 24 Podpod 100% 4 / 4 totaltotal 79.7% 189 / 237 CC 34 SCAR 17.0
CC
34
Cov
80%
BEST_INDEX
12.2
NEW
8.4
UPDATE
6.0
CRAP 5.5
« blib/lib/DBD/SQLite/VirtualTable/FileContent.pm ↑ Summary blib/lib/DBD/SQLite/GetInfo.pm »
1 #======================================================================
2 package DBD::SQLite::VirtualTable::PerlData;
3 #======================================================================
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use base 'DBD::SQLite::VirtualTable';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use DBD::SQLite;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use constant SQLITE_3010000 => $DBD::SQLite::sqlite_version_number >= 3010000 ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$DBD::SQLite::sqlite_version_number >= 3010000 ? : 5 0 5
9 5 ▶use constant SQLITE_3021000 => $DBD::SQLite::sqlite_version_number >= 3021000 ? 1 : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$DBD::SQLite::sqlite_version_number >= 3021000 ? : 5 0 5
10  
11 # private data for translating comparison operators from Sqlite to Perl
12 5 my $TXT = 0;
13 5 my $NUM = 1;
14 5 my %SQLOP2PERLOP = (
15 # TXT NUM
16 '=' => [ 'eq', '==' ],
17 '<' => [ 'lt', '<' ],
18 '<=' => [ 'le', '<=' ],
19 '>' => [ 'gt', '>' ],
20 '>=' => [ 'ge', '>=' ],
21 'MATCH' => [ '=~', '=~' ],
22 (SQLITE_3010000 ? (
23 'LIKE' => [ 'DBD::SQLite::strlike', 'DBD::SQLite::strlike' ],
24 'GLOB' => [ 'DBD::SQLite::strglob', 'DBD::SQLite::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 11 ▶sub NEW {
Subroutine NEW: called CC=8 SCAR=21.3
Pod documented
41 11 my $class = shift;
42 11 my $self = $class->_PREPARE_SELF(@_);
43  
44 # verifications
45 11 my $n_cols = @{$self->{columns}};
46 11 ▶ $n_cols > 0
Branch: 1 branch
BranchTrueFalseTotal
$n_cols > 0 or die "${class}: no declared columns" 11 0 11
47 or die "$class: no declared columns";
48 11 ▶ !$self->{options}{colref} || $n_cols == 1
Branch: 1 branch
BranchTrueFalseTotal
not $self->{"options"}{"colref"} or $n_cols == 1 or die "${class}: must have exactly 1 column when using 'colref'" 11 0 11
Condition66%2/3
not $self->{"options"}{"colref"} or $n_cols == 1
l!l&&r!l&&!r
560
MC/DC0%0/2
not $self->{"options"}{"colref"} or $n_cols == 1
not $self->{"options"}{"colref"} $n_cols == 1
Truth table66%2/3
not(A) or B
A= not $self->{"options"}{"colref"}B= $n_cols == 1
ABresult
1X1
011
000
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 11 ▶ or die "$class: missing option 'arrayrefs' or 'hashrefs' or 'colref'";
Branch: 1 branch
BranchTrueFalseTotal
my $symbolic_ref = $self->{"options"}{"arrayrefs"} || $self->{"options"}{"hashrefs"} || $self->{"options"}{"colref"} or die "${class}: missing option 'arrayrefs' or 'hashrefs' or 'colref'" 11 0 11
Condition83%5/6
$self->{'options'}{'arrayrefs'} || $self->{'options'}{'hashrefs'} || $self->{'options'}{'colref'}
l!l&&r!l&&!r
560
$self->{"options"}{"arrayrefs"} or $self->{"options"}{"hashrefs"}
l!l&&r!l&&!r
236
MC/DC0%0/3
$self->{'options'}{'arrayrefs'} || $self->{'options'}{'hashrefs'} || $self->{'options'}{'colref'}
$self->{"options"}{"arrayrefs"} $self->{"options"}{"hashrefs"} $self->{'options'}{'colref'}
Truth table75%3/4
A or B || C
A= $self->{"options"}{"arrayrefs"}B= $self->{"options"}{"hashrefs"}C= $self->{'options'}{'colref'}
ABCresult
1XX1
01X1
0011
0000
54  
55 # bind to the Perl variable
56 5 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
57 11 ▶ defined ${$symbolic_ref}
Branch: 1 branch
BranchTrueFalseTotal
defined ${$symbolic_ref;} or die "${class}: can't find global variable \$$symbolic_ref" 11 0 11
58 or die "$class: can't find global variable \$$symbolic_ref";
59 11 $self->{rows} = \ ${$symbolic_ref};
60  
61 11 bless $self, $class;
62 }
63  
64 11 ▶sub _build_headers_optypes {
Subroutine _build_headers_optypes: called CC=2 SCAR=6.9
65 11 my $self = shift;
66  
67 11 my $cols = $self->sqlite_table_info;
68  
69 # headers : names of columns, without type information
70 11 $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 11 ▶ = [ map {$_->{type} =~ /INT|REAL|FLOA|DOUB/i ? $NUM : $TXT} @$cols ];
Branch: 1 branch
BranchTrueFalseTotal
$_->{'type'} =~ /INT|REAL|FLOA|DOUB/i ? : 10 19 29
76 }
77  
78 #----------------------------------------------------------------------
79 # method for initiating a search
80 #----------------------------------------------------------------------
81  
82 67 ▶sub BEST_INDEX {
Subroutine BEST_INDEX: called CC=12 SCAR=25.0
Pod documented
83 67 my ($self, $constraints, $order_by) = @_;
84  
85 67 ▶ $self->_build_headers_optypes if !$self->{headers};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"headers"} 11 56 67
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 67 my @conditions;
90 67 my $ix = 0;
91 67 ▶ foreach my $constraint (grep {$_->{usable} and exists $SQLOP2PERLOP{ $_->{op} } } @$constraints) {
Condition66%2/3
$_->{'usable'} && exists $SQLOP2PERLOP{$_->{'op'}}
!ll&&!rl&&r
8054
MC/DC50%1/2
$_->{'usable'} && exists $SQLOP2PERLOP{$_->{'op'}}
$_->{'usable'} exists $SQLOP2PERLOP{$_->{'op'}}
Truth table66%2/3
A && B
A= $_->{'usable'}B= exists $SQLOP2PERLOP{$_->{'op'}}
ABresult
0X0
100
111
92 54 my $col = $constraint->{col};
93 54 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 54 ▶ if ($col == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col == -1) { } 0 54 54
98 # constraint on rowid
99 0 $member = '$i';
100 0 $optype = $NUM;
101 }
102 else {
103 # constraint on regular column
104 54 my $opts = $self->{options};
105 $member = $opts->{arrayrefs} ? "\$row->[$col]"
106 : $opts->{hashrefs} ? "\$row->{$self->{headers}[$col]}"
107 54 ▶ : $opts->{colref} ? "\$row"
Branch: 3 branches
BranchTrueFalseTotal
$opts->{'arrayrefs'} ? : 37 17 54
$opts->{'hashrefs'} ? : 10 7 17
$opts->{'colref'} ? : 7 0 7
108 : die "corrupted data in ->{options}";
109 54 $optype = $self->{optypes}[$col];
110 }
111 54 my $op = $SQLOP2PERLOP{$constraint->{op}}[$optype];
112 54 ▶ if (SQLITE_3021000 && $op =~ /defined/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op =~ /defined/) { } 8 46 54
elsif ($op =~ /str/) { } 1 45 46
113 8 ▶ if ($constraint->{op} =~ /NULL/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($constraint->{'op'} =~ /NULL/) { } 6 2 8
114 6 push @conditions,
115 "($op($member))";
116 } else {
117 2 push @conditions,
118 "($op($member) && !defined(\$vals[$ix]))";
119 }
120 } elsif (SQLITE_3010000 && $op =~ /str/) {
121 1 push @conditions,
122 "(defined($member) && defined(\$vals[$ix]) && !$op(\$vals[$ix], $member))";
123 } else {
124 45 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 54 $constraint->{argvIndex} = $ix++;
132 54 $constraint->{omit} = 1;
133 }
134  
135 # further info for the SQLite core
136 67 ▶ my $outputs = {
Condition100%2/2
join(' && ', @conditions) || '1'
l!l
5017
MC/DC100%1/1
join(' && ', @conditions) || '1'
join(' && ', @conditions)
Truth table100%2/2
A
A= join(' && ', @conditions)
Aresult
11
00
137 idxNum => 1,
138 idxStr => (join(" && ", @conditions) || "1"),
139 orderByConsumed => 0,
140 estimatedCost => 1.0,
141 estimatedRows => undef,
142 };
143  
144 67 return $outputs;
145 }
146  
147  
148 #----------------------------------------------------------------------
149 # methods for data update
150 #----------------------------------------------------------------------
151  
152 5 ▶sub _build_new_row {
Subroutine _build_new_row: called CC=4 SCAR=15.6
153 5 my ($self, $values) = @_;
154  
155 5 my $opts = $self->{options};
156 return $opts->{arrayrefs} ? $values
157 0 : $opts->{hashrefs} ? { map {$self->{headers}->[$_], $values->[$_]}
158 0 ▶ (0 .. @{$self->{headers}} - 1) }
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'colref'} ? : 4 0 4
159 5 ▶ : $opts->{colref} ? $values->[0]
Branch: 2 branches
BranchTrueFalseTotal
$opts->{'arrayrefs'} ? : 1 4 5
$opts->{'hashrefs'} ? : 0 4 4
160 : die "corrupted data in ->{options}";
161 }
162  
163 5 ▶sub INSERT {
Subroutine INSERT: called CC=3 SCAR=13.6
Pod documented
164 5 my ($self, $new_rowid, @values) = @_;
165  
166 5 my $new_row = $self->_build_new_row(\@values);
167  
168 5 ▶ if (defined $new_rowid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_rowid) { } 0 5 5
169 0 ▶ not ${$self->{rows}}->[$new_rowid]
Branch: 1 branch
BranchTrueFalseTotal
${$$self{"rows"};}->[$new_rowid] and die "can't INSERT : rowid $new_rowid already in use" 0 0 0
170 or die "can't INSERT : rowid $new_rowid already in use";
171 0 ${$self->{rows}}->[$new_rowid] = $new_row;
172 }
173 else {
174 5 push @${$self->{rows}}, $new_row;
175 5 return $#${$self->{rows}};
176 }
177 }
178  
179 0 ▶sub DELETE {
Subroutine DELETE: not called CC=1 SCAR=6.9
180 0 my ($self, $old_rowid) = @_;
181  
182 0 delete ${$self->{rows}}->[$old_rowid];
183 }
184  
185 0 ▶sub UPDATE {
Subroutine UPDATE: not called CC=2 SCAR=17.9
Pod documented
186 0 my ($self, $old_rowid, $new_rowid, @values) = @_;
187  
188 0 my $new_row = $self->_build_new_row(\@values);
189  
190 0 ▶ if ($new_rowid == $old_rowid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_rowid == $old_rowid) { } 0 0 0
191 0 ${$self->{rows}}->[$old_rowid] = $new_row;
192 }
193 else {
194 0 delete ${$self->{rows}}->[$old_rowid];
195 0 ${$self->{rows}}->[$new_rowid] = $new_row;
196 }
197 }
198  
199  
200 #======================================================================
201 package DBD::SQLite::VirtualTable::PerlData::Cursor;
202 #======================================================================
203 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
204 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
205 5 ▶use base "DBD::SQLite::VirtualTable::Cursor";
Subroutine BEGIN: called CC=1 SCAR=0.0
206  
207  
208 1815 ▶sub row {
Subroutine row: called CC=1 SCAR=0.0
209 1815 my ($self, $i) = @_;
210 1815 return ${$self->{vtable}{rows}}->[$i];
211 }
212  
213 69 ▶sub FILTER {
Subroutine FILTER: called CC=2 SCAR=6.9
214 69 my ($self, $idxNum, $idxStr, @vals) = @_;
215  
216 # build a method coderef to fetch matching rows
217 69 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 5 ▶ $self->{is_wanted_row} = do { no warnings; eval $perl_code }
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$self->{"is_wanted_row"} = do { no warnings; eval $perl_code } or die "couldn't eval q{$perl_code} : $@" 69 0 69
224 or die "couldn't eval q{$perl_code} : $@";
225  
226 # position the cursor to the first matching row (or to eof)
227 69 $self->{row_ix} = -1;
228 69 $self->NEXT;
229 }
230  
231  
232 1346 ▶sub EOF {
Subroutine EOF: called CC=1 SCAR=0.0
233 1346 my ($self) = @_;
234  
235 1346 return $self->{row_ix} > $#${$self->{vtable}{rows}};
236 }
237  
238 211 ▶sub NEXT {
Subroutine NEXT: called CC=3 SCAR=11.0
239 211 my ($self) = @_;
240  
241 do {
242 1135 $self->{row_ix} += 1
243 } until $self->EOF
244 211 ▶ || eval {$self->{is_wanted_row}->($self, $self->{row_ix})};
Branch: 1 branch
BranchTrueFalseTotal
($self->EOF or eval { do { $self->{"is_wanted_row"}->($self, $self->{"row_ix"}) } }) or do { do { $self->{"row_ix"} += 1 } } 211 924 1135
Condition100%3/3
$self->EOF or eval { do { $self->{"is_wanted_row"}->($self, $self->{"row_ix"}) } }
l!l&&r!l&&!r
64147924
MC/DC100%2/2
$self->EOF or eval { do { $self->{"is_wanted_row"}->($self, $self->{"row_ix"}) } }
$self->EOF eval { do { $self->{"is_wanted_row"}->($self, $self->{"row_ix"}) } }
Truth table100%3/3
A or B
A= $self->EOFB= eval { do { $self->{"is_wanted_row"}->($self, $self->{"row_ix"}) } }
ABresult
1X1
011
000
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 744 ▶sub COLUMN {
Subroutine COLUMN: called CC=4 SCAR=13.9
255 744 my ($self, $idxCol) = @_;
256  
257 744 my $row = $self->row($self->{row_ix});
258  
259 744 my $opts = $self->{vtable}{options};
260 return $opts->{arrayrefs} ? $row->[$idxCol]
261 : $opts->{hashrefs} ? $row->{$self->{vtable}{headers}[$idxCol]}
262 744 ▶ : $opts->{colref} ? $row
Branch: 3 branches
BranchTrueFalseTotal
$opts->{'arrayrefs'} ? : 76 668 744
$opts->{'hashrefs'} ? : 201 467 668
$opts->{'colref'} ? : 467 0 467
263 : die "corrupted data in ->{options}";
264 }
265  
266 8 ▶sub ROWID {
Subroutine ROWID: called CC=1 SCAR=0.0
267 8 my ($self) = @_;
268  
269 8 return $self->{row_ix} + 1; # rowids start at 1 in SQLite
270 }
271  
272  
273 5 1;
274  
275 __END__
« blib/lib/DBD/SQLite/VirtualTable/FileContent.pm ↑ Summary blib/lib/DBD/SQLite/GetInfo.pm »