blib/lib/DBD/SQLeet.pm

filter: Statementstmt 95.8% 325 / 339 Branchbran 75.2% 173 / 230 Conditioncond 69.3% 95 / 137 MC/DCmcdc 43.0% 28 / 65 Subroutinesub 90.0% 27 / 30 Podpod 0.0% 0 / 1 totaltotal 80.8% 648 / 802 CC 175 SCAR 32.0
CC
175
Cov
84%
table_info
42.8
foreign_key_info
33.2
statistics_info
31.7
CRAP 24.6
↑ Summary dbdimp.h »
1 package DBD::SQLeet;
2  
3 101 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 101 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 101 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 101 ▶use DBI 1.57 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 101 ▶use DynaLoader ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 101 our $VERSION = '0.26.0';
11 101 our @ISA = 'DynaLoader';
12  
13 # sqlite_version cache (set in the XS bootstrap)
14 101 our ($sqlite_version, $sqlite_version_number);
15  
16 # not sure if we still need these...
17 101 our ($err, $errstr);
18  
19 101 __PACKAGE__->bootstrap($VERSION);
20  
21 # New or old API?
22 101 ▶use constant NEWAPI => ($DBI::VERSION >= 1.608);
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # global registry of collation functions, initialized with 2 builtins
25 101 our %COLLATION;
26 101 tie %COLLATION, 'DBD::SQLeet::_WriteOnceHash';
27 192 ▶$COLLATION{perl} = sub { $_[0] cmp $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
28 101 ▶$COLLATION{perllocale} = sub { use locale; $_[0] cmp $_[1] };
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
29  
30 101 our $drh;
31 101 my $methods_are_installed = 0;
32  
33 95 ▶sub driver {
Subroutine driver: called CC=4 SCAR=13.9
Pod undocumented
34 95 ▶ return $drh if $drh;
Branch: 1 branch
BranchTrueFalseTotal
if $DBD::SQLeet::drh 0 95 95
35  
36 95 ▶ if (!$methods_are_installed && DBD::SQLeet::NEWAPI ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $methods_are_installed and !!1) 95 0 95
Condition50%1/2
not $methods_are_installed and !!1
!ll
095
MC/DC0%0/1
not $methods_are_installed and !!1
not $methods_are_installed
Truth table50%1/2
not(A)
A= not $methods_are_installed
Aresult
00
11
37 95 DBI->setup_driver('DBD::SQLeet');
38  
39 95 DBD::SQLeet::db->install_method('sqlite_last_insert_rowid');
40 95 DBD::SQLeet::db->install_method('sqlite_busy_timeout');
41 95 DBD::SQLeet::db->install_method('sqlite_create_function');
42 95 DBD::SQLeet::db->install_method('sqlite_create_aggregate');
43 95 DBD::SQLeet::db->install_method('sqlite_create_collation');
44 95 DBD::SQLeet::db->install_method('sqlite_collation_needed');
45 95 DBD::SQLeet::db->install_method('sqlite_progress_handler');
46 95 DBD::SQLeet::db->install_method('sqlite_commit_hook');
47 95 DBD::SQLeet::db->install_method('sqlite_rollback_hook');
48 95 DBD::SQLeet::db->install_method('sqlite_update_hook');
49 95 DBD::SQLeet::db->install_method('sqlite_set_authorizer');
50 95 DBD::SQLeet::db->install_method('sqlite_backup_from_file');
51 95 DBD::SQLeet::db->install_method('sqlite_backup_to_file');
52 95 DBD::SQLeet::db->install_method('sqlite_enable_load_extension');
53 95 DBD::SQLeet::db->install_method('sqlite_load_extension');
54 95 DBD::SQLeet::db->install_method('sqlite_register_fts3_perl_tokenizer');
55 95 DBD::SQLeet::db->install_method('sqlite_trace', { O => 0x0004 });
56 95 DBD::SQLeet::db->install_method('sqlite_profile', { O => 0x0004 });
57 95 DBD::SQLeet::db->install_method('sqlite_table_column_metadata', { O => 0x0004 });
58 95 DBD::SQLeet::db->install_method('sqlite_db_filename', { O => 0x0004 });
59 95 DBD::SQLeet::db->install_method('sqlite_db_status', { O => 0x0004 });
60 95 DBD::SQLeet::st->install_method('sqlite_st_status', { O => 0x0004 });
61 95 DBD::SQLeet::db->install_method('sqlite_create_module');
62  
63 95 $methods_are_installed++;
64 }
65  
66 95 $drh = DBI::_new_drh( "$_[0]::dr", {
67 Name => 'SQLite',
68 Version => $VERSION,
69 Attribution => 'DBD::SQLeet by Dimitar D. Mitov et al',
70 } );
71  
72 95 return $drh;
73 }
74  
75 0 ▶sub CLONE {
Subroutine CLONE: not called CC=1 SCAR=6.9
76 0 undef $drh;
77 }
78  
79  
80 package # hide from PAUSE
81 DBD::SQLeet::dr;
82  
83 253 ▶sub connect {
Subroutine connect: called CC=14 SCAR=29.9
84 253 my ($drh, $dbname, $user, $auth, $attr) = @_;
85  
86 # Default PrintWarn to the value of $^W
87 # unless ( defined $attr->{PrintWarn} ) {
88 # $attr->{PrintWarn} = $^W ? 1 : 0;
89 # }
90  
91 253 my $dbh = DBI::_new_dbh( $drh, {
92 Name => $dbname,
93 } );
94  
95 253 my $real = $dbname;
96 253 ▶ if ($dbname =~ /=/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dbname =~ /=/) 238 15 253
97 238 foreach my $attrib (split(/;/, $dbname)) {
98 240 my ($key, $value) = split(/=/, $attrib, 2);
99 240 ▶ if ($key =~ /^(?:db(?:name)?|database)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($key =~ /^(?:db(?:name)?|database)$/) { } 227 13 240
elsif ($key eq 'uri') { } 6 7 13
100 227 $real = $value;
101 } elsif ($key eq 'uri') {
102 6 $real = $value;
103 6 $attr->{sqlite_open_flags} |= DBD::SQLeet::OPEN_URI();
104 } else {
105 7 $attr->{$key} = $value;
106 }
107 }
108 }
109  
110 253 ▶ if (my $flags = $attr->{sqlite_open_flags}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $flags = $attr->{"sqlite_open_flags"}) 18 235 253
111 18 ▶ unless ($flags & (DBD::SQLeet::OPEN_READONLY() | DBD::SQLeet::OPEN_READWRITE())) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($flags & (&DBD::SQLeet::OPEN_READONLY() | &DBD::SQLeet::OPEN_READWRITE())) 12 6 18
112 12 $attr->{sqlite_open_flags} |= DBD::SQLeet::OPEN_READWRITE() | DBD::SQLeet::OPEN_CREATE();
113 }
114 }
115  
116 # To avoid unicode and long file name problems on Windows,
117 # convert to the shortname if the file (or parent directory) exists.
118 253 ▶ if ($^O =~ /MSWin32/ and $real ne ':memory:' and $real ne '' and $real !~ /^file:/ and !-f $real) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O =~ /MSWin32/ and $real ne ":memory:" and $real ne "" and not $real =~ /^file:/ and not -f $real) 0 253 253
Condition33%4/12
$^O =~ /MSWin32/ and $real ne ":memory:" and $real ne "" and not $real =~ /^file:/ and not -f $real
!ll&&!rl&&r
25300
$^O =~ /MSWin32/ and $real ne ":memory:" and $real ne "" and not $real =~ /^file:/
!ll&&!rl&&r
25300
$^O =~ /MSWin32/ and $real ne ":memory:" and $real ne ""
!ll&&!rl&&r
25300
$^O =~ /MSWin32/ and $real ne ":memory:"
!ll&&!rl&&r
25300
MC/DC0%0/5
$^O =~ /MSWin32/ and $real ne ":memory:" and $real ne "" and not $real =~ /^file:/ and not -f $real
$^O =~ /MSWin32/ $real ne ":memory:" $real ne "" not $real =~ /^file:/ not -f $real
Truth table16%1/6
A and B and C and not(D) and not(E)
A= $^O =~ /MSWin32/B= $real ne ":memory:"C= $real ne ""D= not $real =~ /^file:/E= not -f $real
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
119 0 require File::Basename;
120 0 my ($file, $dir, $suffix) = File::Basename::fileparse($real);
121 # We are creating a new file.
122 # Does the directory it's in at least exist?
123 0 ▶ if (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) { } 0 0 0
124 0 require Win32;
125 0 $real = join '', grep { defined } Win32::GetShortPathName($dir), $file, $suffix;
126 } else {
127 # SQLite can't do mkpath anyway.
128 # So let it go through as it and fail.
129 }
130 }
131  
132 # Hand off to the actual login function
133 253 ▶ DBD::SQLeet::db::_login($dbh, $real, $user, $auth, $attr) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
DBD::SQLeet::db::_login($dbh, $real, $user, $auth, $attr) or return undef 247 6 253
134  
135 # Register the on-demand collation installer, REGEXP function and
136 # perl tokenizer
137 247 if (DBD::SQLeet::NEWAPI) {
138 247 $dbh->sqlite_collation_needed( \&install_collation );
139 247 $dbh->sqlite_create_function( "REGEXP", 2, \&regexp );
140 247 $dbh->sqlite_register_fts3_perl_tokenizer();
141 } else {
142 $dbh->func(\&install_collation, "collation_needed");
143 $dbh->func("REGEXP", 2, \&regexp, "create_function");
144 $dbh->func("register_fts3_perl_tokenizer");
145 }
146  
147 # HACK: Since PrintWarn = 0 doesn't seem to actually prevent warnings
148 # in DBD::SQLeet we set Warn to false if PrintWarn is false.
149  
150 # NOTE: According to the explanation by timbunce,
151 # "Warn is meant to report on bad practices or problems with
152 # the DBI itself (hence always on by default), while PrintWarn
153 # is meant to report warnings coming from the database."
154 # That is, if you want to disable an ineffective rollback warning
155 # etc (due to bad practices), you should turn off Warn,
156 # and to silence other warnings, turn off PrintWarn.
157 # Warn and PrintWarn are independent, and turning off PrintWarn
158 # does not silence those warnings that should be controlled by
159 # Warn.
160  
161 # unless ( $attr->{PrintWarn} ) {
162 # $attr->{Warn} = 0;
163 # }
164  
165 247 return $dbh;
166 }
167  
168 12 ▶sub install_collation {
Subroutine install_collation: called CC=3 SCAR=13.0
169 12 my $dbh = shift;
170 12 my $name = shift;
171 12 my $collation = $DBD::SQLeet::COLLATION{$name};
172 12 ▶ unless ($collation) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($collation) 0 12 12
173 0 ▶ warn "Can't install unknown collation: $name" if $dbh->{PrintWarn};
Branch: 1 branch
BranchTrueFalseTotal
if $dbh->{"PrintWarn"} 0 0 0
174 0 return;
175 }
176 12 if (DBD::SQLeet::NEWAPI) {
177 12 $dbh->sqlite_create_collation( $name => $collation );
178 } else {
179 $dbh->func($name => $collation, "create_collation");
180 }
181 }
182  
183 # default implementation for sqlite 'REGEXP' infix operator.
184 # Note : args are reversed, i.e. "a REGEXP b" calls REGEXP(b, a)
185 # (see http://www.sqlite.org/vtab.html#xfindfunction)
186 392 ▶sub regexp {
Subroutine regexp: called CC=3 SCAR=11.0
187 101 ▶ use locale;
Subroutine BEGIN: called CC=1 SCAR=0.0
188 392 ▶ return if !defined $_[0] || !defined $_[1];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and defined $_[1] 128 264 392
Condition66%2/3
defined $_[0] and defined $_[1]
!ll&&!rl&&r
1280264
MC/DC50%1/2
defined $_[0] and defined $_[1]
defined $_[0] defined $_[1]
Truth table66%2/3
A and B
A= defined $_[0]B= defined $_[1]
ABresult
0X0
100
111
189 264 return scalar($_[1] =~ $_[0]);
190 }
191  
192 package # hide from PAUSE
193 DBD::SQLeet::db;
194  
195 1117 ▶sub prepare {
Subroutine prepare: called CC=3 SCAR=11.0
196 1117 my $dbh = shift;
197 1117 my $sql = shift;
198 1117 ▶ $sql = '' unless defined $sql;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sql 0 1117 1117
199  
200 1117 my $sth = DBI::_new_sth( $dbh, {
201 Statement => $sql,
202 } );
203  
204 1117 ▶ DBD::SQLeet::st::_prepare($sth, $sql, @_) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
DBD::SQLeet::st::_prepare($sth, $sql, @_) or return undef 1107 10 1117
205  
206 1107 return $sth;
207 }
208  
209 2877 ▶sub do {
Subroutine do: called CC=11 SCAR=24.0
210 2877 my ($dbh, $statement, $attr, @bind_values) = @_;
211  
212 # shortcut
213 2877 my $allow_multiple_statements = $dbh->FETCH('sqlite_allow_multiple_statements');
214 2877 ▶ if (defined $statement && !defined $attr && !@bind_values) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $statement and not defined $attr and not @bind_values) 2778 99 2877
Condition100%6/6
defined $statement and not defined $attr and not @bind_values
!ll&&!rl&&r
42572778
defined $statement and not defined $attr
!ll&&!rl&&r
1412835
MC/DC100%3/3
defined $statement and not defined $attr and not @bind_values
defined $statement not defined $attr not @bind_values
Truth table100%4/4
A and not(B) and not(C)
A= defined $statementB= not defined $attrC= not @bind_values
ABCresult
0XX0
10X0
1100
1111
215 # _do() (i.e. sqlite3_exec()) runs semicolon-separate SQL
216 # statements, which is handy but insecure sometimes.
217 # Use this only when it's safe or explicitly allowed.
218 2778 ▶ if (index($statement, ';') == -1 or $allow_multiple_statements) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($statement, ";") == -1 or $allow_multiple_statements) 2755 23 2778
Condition66%2/3
index($statement, ";") == -1 or $allow_multiple_statements
l!l&&r!l&&!r
2755023
MC/DC50%1/2
index($statement, ";") == -1 or $allow_multiple_statements
index($statement, ";") == -1 $allow_multiple_statements
Truth table66%2/3
A or B
A= index($statement, ";") == -1B= $allow_multiple_statements
ABresult
1X1
011
000
219 2755 return DBD::SQLeet::db::_do($dbh, $statement);
220 }
221 }
222  
223 122 my @copy = @{[@bind_values]};
224 122 my $rows = 0;
225  
226 122 ▶ while ($statement) {
Branch: 1 branch
BranchTrueFalseTotal
if ($statement) 127 3 130
227 127 ▶ my $sth = $dbh->prepare($statement, $attr) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare($statement, $attr) or return undef 119 8 127
228 119 ▶ $sth->execute(splice @copy, 0, $sth->{NUM_OF_PARAMS}) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute(splice(@copy, 0, $sth->{"NUM_OF_PARAMS"})) or return undef 115 4 119
229 115 $rows += $sth->rows;
230 # XXX: not sure why but $dbh->{sqlite...} wouldn't work here
231 115 ▶ last unless $allow_multiple_statements;
Branch: 1 branch
BranchTrueFalseTotal
unless $allow_multiple_statements 107 8 115
232 8 $statement = $sth->{sqlite_unprepared_statements};
233 }
234  
235 # always return true if no error
236 110 ▶ return ($rows == 0) ? "0E0" : $rows;
Branch: 1 branch
BranchTrueFalseTotal
$rows == 0 ? : 16 94 110
237 }
238  
239 1 ▶sub ping {
Subroutine ping: called CC=4 SCAR=15.6
240 1 my $dbh = shift;
241  
242 # $file may be undef (ie. in-memory/temporary database)
243 1 my $file = DBD::SQLeet::NEWAPI ? $dbh->sqlite_db_filename
244 : $dbh->func("db_filename");
245  
246 1 ▶ return 0 if $file && !-f $file;
Branch: 1 branch
BranchTrueFalseTotal
if $file and not -f $file 0 1 1
Condition33%1/3
$file and not -f $file
!ll&&!rl&&r
100
MC/DC0%0/2
$file and not -f $file
$file not -f $file
Truth table33%1/3
A and not(B)
A= $fileB= not -f $file
ABresult
0X0
100
111
247 1 ▶ return $dbh->FETCH('Active') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$dbh->FETCH('Active') ? : 1 0 1
248 }
249  
250 0 ▶sub _get_version {
Subroutine _get_version: not called CC=1 SCAR=6.9
251 0 return (DBD::SQLeet::db::FETCH($_[0], 'sqlite_version'));
252 }
253  
254 101 my %info = (
255 17 => 'SQLite', # SQL_DBMS_NAME
256 18 => \&_get_version, # SQL_DBMS_VER
257 29 => '"', # SQL_IDENTIFIER_QUOTE_CHAR
258 );
259  
260 63 ▶sub get_info {
Subroutine get_info: called CC=2 SCAR=7.0
261 63 my($dbh, $info_type) = @_;
262 63 my $v = $info{int($info_type)};
263 63 ▶ $v = $v->($dbh) if ref $v eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "CODE" 0 63 63
264 63 return $v;
265 }
266  
267 21 ▶sub _attached_database_list {
Subroutine _attached_database_list: called CC=5 SCAR=16.2
268 21 my $dbh = shift;
269 21 my @attached;
270  
271 21 ▶ my $sth_databases = $dbh->prepare('PRAGMA database_list') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sth_databases = $dbh->prepare("PRAGMA database_list") or return 21 0 21
272 21 ▶ $sth_databases->execute or return;
Branch: 1 branch
BranchTrueFalseTotal
$sth_databases->execute or return 21 0 21
273 21 ▶ while (my $db_info = $sth_databases->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $db_info = $sth_databases->fetchrow_hashref) 47 21 68
274 47 ▶ push @attached, $db_info->{name} if $db_info->{seq} >= 2;
Branch: 1 branch
BranchTrueFalseTotal
if $db_info->{"seq"} >= 2 11 36 47
275 }
276 21 return @attached;
277 }
278  
279 # SQL/CLI (ISO/IEC JTC 1/SC 32 N 0595), 6.63 Tables
280 # Based on DBD::Oracle's
281 # See also http://www.ch-werner.de/sqliteodbc/html/sqlite3odbc_8c.html#a213
282 16 ▶sub table_info {
Subroutine table_info: called CC=34 SCAR=37.6
283 16 my ($dbh, $cat_val, $sch_val, $tbl_val, $typ_val, $attr) = @_;
284  
285 16 my @where = ();
286 16 my $sql;
287 16 ▶ if (defined($cat_val) && $cat_val eq '%'
Branch: 3 branches
BranchTrueFalseTotal
if (defined $cat_val and $cat_val eq '%' and defined $sch_val and $sch_val eq '' and defined $tbl_val and $tbl_val eq '') { } 1 15 16
elsif (defined $cat_val and $cat_val eq '' and defined $sch_val and $sch_val eq '%' and defined $tbl_val and $tbl_val eq '') { } 1 14 15
elsif (defined $cat_val and $cat_val eq '' and defined $sch_val and $sch_val eq '' and defined $tbl_val and $tbl_val eq '' and defined $typ_val and $typ_val eq '%') { } 1 13 14
Condition73%11/15
defined $cat_val and $cat_val eq "%" and defined $sch_val and $sch_val eq "" and defined $tbl_val and $tbl_val eq ""
!ll&&!rl&&r
1501
defined $cat_val and $cat_val eq "%" and defined $sch_val and $sch_val eq "" and defined $tbl_val
!ll&&!rl&&r
1501
defined $cat_val and $cat_val eq "%" and defined $sch_val and $sch_val eq ""
!ll&&!rl&&r
1501
defined $cat_val and $cat_val eq "%" and defined $sch_val
!ll&&!rl&&r
1501
defined $cat_val and $cat_val eq "%"
!ll&&!rl&&r
1321
MC/DC33%2/6
defined $cat_val and $cat_val eq "%" and defined $sch_val and $sch_val eq "" and defined $tbl_val and $tbl_val eq ""
defined $cat_val $cat_val eq "%" defined $sch_val $sch_val eq "" defined $tbl_val $tbl_val eq ""
Truth table42%3/7
A and B and C and D and E and F
A= defined $cat_valB= $cat_val eq "%"C= defined $sch_valD= $sch_val eq ""E= defined $tbl_valF= $tbl_val eq ""
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
288 && defined($sch_val) && $sch_val eq ''
289 && defined($tbl_val) && $tbl_val eq '') { # Rule 19a
290 1 $sql = <<'END_SQL';
291 SELECT NULL TABLE_CAT
292 , NULL TABLE_SCHEM
293 , NULL TABLE_NAME
294 , NULL TABLE_TYPE
295 , NULL REMARKS
296 END_SQL
297 ▶ } elsif ( defined($cat_val) && $cat_val eq ''
Condition73%11/15
defined $cat_val and $cat_val eq "" and defined $sch_val and $sch_val eq "%" and defined $tbl_val and $tbl_val eq ""
!ll&&!rl&&r
1401
defined $cat_val and $cat_val eq "" and defined $sch_val and $sch_val eq "%" and defined $tbl_val
!ll&&!rl&&r
1401
defined $cat_val and $cat_val eq "" and defined $sch_val and $sch_val eq "%"
!ll&&!rl&&r
1311
defined $cat_val and $cat_val eq "" and defined $sch_val
!ll&&!rl&&r
1302
defined $cat_val and $cat_val eq ""
!ll&&!rl&&r
1302
MC/DC33%2/6
defined $cat_val and $cat_val eq "" and defined $sch_val and $sch_val eq "%" and defined $tbl_val and $tbl_val eq ""
defined $cat_val $cat_val eq "" defined $sch_val $sch_val eq "%" defined $tbl_val $tbl_val eq ""
Truth table42%3/7
A and B and C and D and E and F
A= defined $cat_valB= $cat_val eq ""C= defined $sch_valD= $sch_val eq "%"E= defined $tbl_valF= $tbl_val eq ""
ABCDEFresult
0XXXXX0
10XXXX0
110XXX0
1110XX0
11110X0
1111100
1111111
298 && defined($sch_val) && $sch_val eq '%'
299 && defined($tbl_val) && $tbl_val eq '') { # Rule 19b
300 1 $sql = <<'END_SQL';
301 SELECT NULL TABLE_CAT
302 , t.tn TABLE_SCHEM
303 , NULL TABLE_NAME
304 , NULL TABLE_TYPE
305 , NULL REMARKS
306 FROM (
307 SELECT 'main' tn
308 UNION SELECT 'temp' tn
309 END_SQL
310 1 for my $db_name (_attached_database_list($dbh)) {
311 0 $sql .= " UNION SELECT '$db_name' tn\n";
312 }
313 1 $sql .= ") t\n";
314 ▶ } elsif (defined($cat_val) && $cat_val eq ''
Condition66%14/21
defined $cat_val and $cat_val eq "" and defined $sch_val and $sch_val eq "" and defined $tbl_val and $tbl_val eq "" and defined $typ_val and $typ_val eq "%"
!ll&&!rl&&r
1301
defined $cat_val and $cat_val eq "" and defined $sch_val and $sch_val eq "" and defined $tbl_val and $tbl_val eq "" and defined $typ_val
!ll&&!rl&&r
1301
defined $cat_val and $cat_val eq "" and defined $sch_val and $sch_val eq "" and defined $tbl_val and $tbl_val eq ""
!ll&&!rl&&r
1301
defined $cat_val and $cat_val eq "" and defined $sch_val and $sch_val eq "" and defined $tbl_val
!ll&&!rl&&r
1301
defined $cat_val and $cat_val eq "" and defined $sch_val and $sch_val eq ""
!ll&&!rl&&r
1301
defined $cat_val and $cat_val eq "" and defined $sch_val
!ll&&!rl&&r
1301
defined $cat_val and $cat_val eq ""
!ll&&!rl&&r
1301
MC/DC12%1/8
defined $cat_val and $cat_val eq "" and defined $sch_val and $sch_val eq "" and defined $tbl_val and $tbl_val eq "" and defined $typ_val and $typ_val eq "%"
defined $cat_val $cat_val eq "" defined $sch_val $sch_val eq "" defined $tbl_val $tbl_val eq "" defined $typ_val $typ_val eq "%"
Truth table22%2/9
A and B and C and D and E and F and G and H
A= defined $cat_valB= $cat_val eq ""C= defined $sch_valD= $sch_val eq ""E= defined $tbl_valF= $tbl_val eq ""G= defined $typ_valH= $typ_val eq "%"
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
110XXXXX0
1110XXXX0
11110XXX0
111110XX0
1111110X0
111111100
111111111
315 && defined($sch_val) && $sch_val eq ''
316 && defined($tbl_val) && $tbl_val eq ''
317 && defined($typ_val) && $typ_val eq '%') { # Rule 19c
318 1 $sql = <<'END_SQL';
319 SELECT NULL TABLE_CAT
320 , NULL TABLE_SCHEM
321 , NULL TABLE_NAME
322 , t.tt TABLE_TYPE
323 , NULL REMARKS
324 FROM (
325 SELECT 'TABLE' tt UNION
326 SELECT 'VIEW' tt UNION
327 SELECT 'LOCAL TEMPORARY' tt UNION
328 SELECT 'SYSTEM TABLE' tt
329 ) t
330 ORDER BY TABLE_TYPE
331 END_SQL
332 } else {
333 13 $sql = <<'END_SQL';
334 SELECT *
335 FROM
336 (
337 SELECT NULL TABLE_CAT
338 , TABLE_SCHEM
339 , tbl_name TABLE_NAME
340 , TABLE_TYPE
341 , NULL REMARKS
342 , sql sqlite_sql
343 FROM (
344 SELECT 'main' TABLE_SCHEM, tbl_name, upper(type) TABLE_TYPE, sql
345 FROM sqlite_master
346 UNION ALL
347 SELECT 'temp' TABLE_SCHEM, tbl_name, 'LOCAL TEMPORARY' TABLE_TYPE, sql
348 FROM sqlite_temp_master
349 END_SQL
350  
351 13 for my $db_name (_attached_database_list($dbh)) {
352 9 $sql .= <<"END_SQL";
353 UNION ALL
354 SELECT '$db_name' TABLE_SCHEM, tbl_name, upper(type) TABLE_TYPE, sql
355 FROM "$db_name".sqlite_master
356 END_SQL
357 }
358  
359 13 $sql .= <<'END_SQL';
360 UNION ALL
361 SELECT 'main' TABLE_SCHEM, 'sqlite_master' tbl_name, 'SYSTEM TABLE' TABLE_TYPE, NULL sql
362 UNION ALL
363 SELECT 'temp' TABLE_SCHEM, 'sqlite_temp_master' tbl_name, 'SYSTEM TABLE' TABLE_TYPE, NULL sql
364 )
365 )
366 END_SQL
367 13 ▶ $attr = {} unless ref $attr eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attr eq "HASH" 13 0 13
368 13 ▶ my $escape = defined $attr->{Escape} ? " ESCAPE '$attr->{Escape}'" : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $attr->{'Escape'} ? : 0 13 13
369 13 ▶ if (defined $sch_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sch_val) 3 10 13
370 3 push @where, "TABLE_SCHEM LIKE '$sch_val'$escape";
371 }
372  
373 13 ▶ if (defined $tbl_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tbl_val) 11 2 13
374 11 push @where, "TABLE_NAME LIKE '$tbl_val'$escape";
375 }
376  
377 13 ▶ if (defined $typ_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $typ_val) 1 12 13
378 1 my $table_type_list;
379 1 $typ_val =~ s/^\s+//;
380 1 $typ_val =~ s/\s+$//;
381 1 my @ttype_list = split (/\s*,\s*/, $typ_val);
382 1 foreach my $table_type (@ttype_list) {
383 1 ▶ if ($table_type !~ /^'.*'$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table_type =~ /^'.*'$/) 1 0 1
384 1 $table_type = "'" . $table_type . "'";
385 }
386 }
387 1 $table_type_list = join(', ', @ttype_list);
388 1 ▶ push @where, "TABLE_TYPE IN (\U$table_type_list)" if $table_type_list;
Branch: 1 branch
BranchTrueFalseTotal
if $table_type_list 1 0 1
389 }
390 13 ▶ $sql .= ' WHERE ' . join("\n AND ", @where ) . "\n" if @where;
Branch: 1 branch
BranchTrueFalseTotal
if @where 12 1 13
391 13 $sql .= " ORDER BY TABLE_TYPE, TABLE_SCHEM, TABLE_NAME\n";
392 }
393 16 ▶ my $sth = $dbh->prepare($sql) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare($sql) or return undef 16 0 16
394 16 ▶ $sth->execute or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return undef 16 0 16
395 16 $sth;
396 }
397  
398 33 ▶sub primary_key_info {
Subroutine primary_key_info: called CC=26 SCAR=32.8
399 33 my ($dbh, $catalog, $schema, $table, $attr) = @_;
400  
401 33 my $databases = $dbh->selectall_arrayref("PRAGMA database_list", {Slice => {}});
402  
403 33 my @pk_info;
404 33 for my $database (@$databases) {
405 49 my $dbname = $database->{name};
406 49 ▶ next if defined $schema && $schema ne '%' && $schema ne $dbname;
Branch: 1 branch
BranchTrueFalseTotal
if defined $schema and $schema ne "%" and $schema ne $dbname 6 43 49
Condition83%5/6
defined $schema and $schema ne "%" and $schema ne $dbname
!ll&&!rl&&r
4036
defined $schema and $schema ne "%"
!ll&&!rl&&r
4009
MC/DC66%2/3
defined $schema and $schema ne "%" and $schema ne $dbname
defined $schema $schema ne "%" $schema ne $dbname
Truth table75%3/4
A and B and C
A= defined $schemaB= $schema ne "%"C= $schema ne $dbname
ABCresult
0XX0
10X0
1100
1111
407  
408 43 my $quoted_dbname = $dbh->quote_identifier($dbname);
409  
410 43 ▶ my $master_table =
Branch: 2 branches
BranchTrueFalseTotal
$dbname eq 'main' ? : 31 12 43
$dbname eq 'temp' ? : 8 4 12
411 ($dbname eq 'main') ? 'sqlite_master' :
412 ($dbname eq 'temp') ? 'sqlite_temp_master' :
413 $quoted_dbname.'.sqlite_master';
414  
415 43 ▶ my $sth = $dbh->prepare("SELECT name, sql FROM $master_table WHERE type = ?") or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("SELECT name, sql FROM $master_table WHERE type = ?") or return 43 0 43
416 43 ▶ $sth->execute("table") or return;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute("table") or return 43 0 43
417 43 ▶ while(my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 55 43 98
418 55 my $tbname = $row->{name};
419 55 ▶ next if defined $table && $table ne '%' && $table ne $tbname;
Branch: 1 branch
BranchTrueFalseTotal
if defined $table and $table ne "%" and $table ne $tbname 22 33 55
Condition83%5/6
defined $table and $table ne "%" and $table ne $tbname
!ll&&!rl&&r
33022
defined $table and $table ne "%"
!ll&&!rl&&r
3052
MC/DC66%2/3
defined $table and $table ne "%" and $table ne $tbname
defined $table $table ne "%" $table ne $tbname
Truth table75%3/4
A and B and C
A= defined $tableB= $table ne "%"C= $table ne $tbname
ABCresult
0XX0
10X0
1100
1111
420  
421 33 my $quoted_tbname = $dbh->quote_identifier($tbname);
422 33 ▶ my $t_sth = $dbh->prepare("PRAGMA $quoted_dbname.table_info($quoted_tbname)") or return;
Branch: 1 branch
BranchTrueFalseTotal
my $t_sth = $dbh->prepare("PRAGMA $quoted_dbname.table_info($quoted_tbname)") or return 33 0 33
423 33 ▶ $t_sth->execute or return;
Branch: 1 branch
BranchTrueFalseTotal
$t_sth->execute or return 33 0 33
424 33 my @pk;
425 33 ▶ while(my $col = $t_sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $col = $t_sth->fetchrow_hashref) 52 33 85
426 52 ▶ push @pk, $col->{name} if $col->{pk};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"pk"} 38 14 52
427 }
428  
429 # If there're multiple primary key columns, we need to
430 # find their order from one of the auto-generated unique
431 # indices (note that single column integer primary key
432 # doesn't create an index).
433 33 ▶ if (@pk > 1 and $row->{sql} =~ /\bPRIMARY\s+KEY\s*\(\s*
Branch: 1 branch
BranchTrueFalseTotal
if (@pk > 1 and $row->{"sql"} =~ /\bPRIMARY\s+KEY\s*\(\s* ( (?: ( [a-z_][a-z0-9_]* | (["'`])(?:\3\3|(?!\3).)+?\3(?!\3) | \[[^\]]+\] ) \s*,\s* )+ ( [a-z_][a-z0-9_]* | (["'`])(?:\5\5|(?!\5).)+?\5(?!\5) | \[[^\]]+\] ) ) \s*\)/six) 4 29 33
Condition66%2/3
@pk > 1 and $row->{"sql"} =~ /\bPRIMARY\s+KEY\s*\(\s* ( (?: ( [a-z_][a-z0-9_]* | (["'`])(?:\3\3|(?!\3).)+?\3(?!\3) | \[[^\]]+\] ) \s*,\s* )+ ( [a-z_][a-z0-9_]* | (["'`])(?:\5\5|(?!\5).)+?\5(?!\5) | \[[^\]]+\] ) ) \s*\)/six
!ll&&!rl&&r
2904
MC/DC50%1/2
@pk > 1 and $row->{"sql"} =~ /\bPRIMARY\s+KEY\s*\(\s* ( (?: ( [a-z_][a-z0-9_]* | (["'`])(?:\3\3|(?!\3).)+?\3(?!\3) | \[[^\]]+\] ) \s*,\s* )+ ( [a-z_][a-z0-9_]* | (["'`])(?:\5\5|(?!\5).)+?\5(?!\5) | \[[^\]]+\] ) ) \s*\)/six
@pk > 1 $row->{"sql"} =~ /\bPRIMARY\s+KEY\s*\(\s* ( (?: ( [a-z_][a-z0-9_]* | (["'`])(?:\3\3|(?!\3).)+?\3(?!\3) | \[[^\]]+\] ) \s*,\s* )+ ( [a-z_][a-z0-9_]* | (["'`])(?:\5\5|(?!\5).)+?\5(?!\5) | \[[^\]]+\] ) ) \s*\)/six
Truth table66%2/3
A and B
A= @pk > 1B= $row->{"sql"} =~ /\bPRIMARY\s+KEY\s*\(\s* ( (?: ( [a-z_][a-z0-9_]* | (["'`])(?:\3\3|(?!\3).)+?\3(?!\3) | \[[^\]]+\] ) \s*,\s* )+ ( [a-z_][a-z0-9_]* | (["'`])(?:\5\5|(?!\5).)+?\5(?!\5) | \[[^\]]+\] ) ) \s*\)/six
ABresult
0X0
100
111
434 (
435 (?:
436 (
437 [a-z_][a-z0-9_]*
438 | (["'`])(?:\3\3|(?!\3).)+?\3(?!\3)
439 | \[[^\]]+\]
440 )
441 \s*,\s*
442 )+
443 (
444 [a-z_][a-z0-9_]*
445 | (["'`])(?:\5\5|(?!\5).)+?\5(?!\5)
446 | \[[^\]]+\]
447 )
448 )
449 \s*\)/six) {
450 4 my $pk_sql = $1;
451 4 @pk = ();
452 4 ▶ while($pk_sql =~ /
Branch: 1 branch
BranchTrueFalseTotal
if ($pk_sql =~ / ( [a-z_][a-z0-9_]* | (["'`])(?:\2\2|(?!\2).)+?\2(?!\2) | \[([^\]]+)\] ) (?:\s*,\s*|$) /gisx) 9 4 13
453 (
454 [a-z_][a-z0-9_]*
455 | (["'`])(?:\2\2|(?!\2).)+?\2(?!\2)
456 | \[([^\]]+)\]
457 )
458 (?:\s*,\s*|$)
459 /sixg) {
460 9 my($col, $quote, $brack) = ($1, $2, $3);
461 9 ▶ if (defined $quote) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $quote) { } 2 7 9
elsif (defined $brack) { } 1 6 7
462 # Dequote "'`
463 2 $col = substr $col, 1, -1;
464 2 $col =~ s/$quote$quote/$quote/g;
465 } elsif (defined $brack) {
466 # Dequote []
467 1 $col = $brack;
468 }
469 9 push @pk, $col;
470 }
471 }
472  
473 33 ▶ my $key_name = $row->{sql} =~ /\bCONSTRAINT\s+(\S+|"[^"]+")\s+PRIMARY\s+KEY\s*\(/i ? $1 : 'PRIMARY KEY';
Branch: 1 branch
BranchTrueFalseTotal
$row->{'sql'} =~ /\bCONSTRAINT\s+(\S+|"[^"]+")\s+PRIMARY\s+KEY\s*\(/i ? : 1 32 33
474 33 my $key_seq = 0;
475 33 foreach my $pk_field (@pk) {
476 38 push @pk_info, {
477 TABLE_SCHEM => $dbname,
478 TABLE_NAME => $tbname,
479 COLUMN_NAME => $pk_field,
480 KEY_SEQ => ++$key_seq,
481 PK_NAME => $key_name,
482 };
483 }
484 }
485 }
486  
487 33 ▶ my $sponge = DBI->connect("DBI:Sponge:", '','')
Branch: 1 branch
BranchTrueFalseTotal
my $sponge = "DBI"->connect("DBI:Sponge:", "", "") or return $dbh->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr") 33 0 33
488 or return $dbh->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr");
489 33 my @names = qw(TABLE_CAT TABLE_SCHEM TABLE_NAME COLUMN_NAME KEY_SEQ PK_NAME);
490 my $sth = $sponge->prepare( "primary_key_info", {
491 33 ▶ rows => [ map { [ @{$_}{@names} ] } @pk_info ],
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $sponge->prepare("primary_key_info", {"rows", [map({[@{$_;}{@names}];} @pk_info)], "NUM_OF_FIELDS", scalar @names, "NAME", \@names}) or return $dbh->DBI::set_err($sponge->err, $sponge->errstr) 33 0 33
492 NUM_OF_FIELDS => scalar @names,
493 NAME => \@names,
494 }) or return $dbh->DBI::set_err(
495 $sponge->err,
496 $sponge->errstr,
497 );
498 33 return $sth;
499 }
500  
501 101 our %DBI_code_for_rule = (
502 # from DBI doc; curiously, they are not exported
503 # by the DBI module.
504 # codes for update/delete constraints
505 'CASCADE' => 0,
506 'RESTRICT' => 1,
507 'SET NULL' => 2,
508 'NO ACTION' => 3,
509 'SET DEFAULT' => 4,
510  
511 # codes for deferrability
512 'INITIALLY DEFERRED' => 5,
513 'INITIALLY IMMEDIATE' => 6,
514 'NOT DEFERRABLE' => 7,
515 );
516  
517 101 my @FOREIGN_KEY_INFO_ODBC = (
518 'PKTABLE_CAT', # The primary (unique) key table catalog identifier.
519 'PKTABLE_SCHEM', # The primary (unique) key table schema identifier.
520 'PKTABLE_NAME', # The primary (unique) key table identifier.
521 'PKCOLUMN_NAME', # The primary (unique) key column identifier.
522 'FKTABLE_CAT', # The foreign key table catalog identifier.
523 'FKTABLE_SCHEM', # The foreign key table schema identifier.
524 'FKTABLE_NAME', # The foreign key table identifier.
525 'FKCOLUMN_NAME', # The foreign key column identifier.
526 'KEY_SEQ', # The column sequence number (starting with 1).
527 'UPDATE_RULE', # The referential action for the UPDATE rule.
528 'DELETE_RULE', # The referential action for the DELETE rule.
529 'FK_NAME', # The foreign key name.
530 'PK_NAME', # The primary (unique) key name.
531 'DEFERRABILITY', # The deferrability of the foreign key constraint.
532 'UNIQUE_OR_PRIMARY', # qualifies the key referenced by the foreign key
533 );
534  
535 # Column names below are not used, but listed just for completeness's sake.
536 # Maybe we could add an option so that the user can choose which field
537 # names will be returned; the DBI spec is not very clear about ODBC vs. CLI.
538 101 my @FOREIGN_KEY_INFO_SQL_CLI = qw(
539 UK_TABLE_CAT
540 UK_TABLE_SCHEM
541 UK_TABLE_NAME
542 UK_COLUMN_NAME
543 FK_TABLE_CAT
544 FK_TABLE_SCHEM
545 FK_TABLE_NAME
546 FK_COLUMN_NAME
547 ORDINAL_POSITION
548 UPDATE_RULE
549 DELETE_RULE
550 FK_NAME
551 UK_NAME
552 DEFERABILITY
553 UNIQUE_OR_PRIMARY
554 );
555  
556 6 ▶sub foreign_key_info {
Subroutine foreign_key_info: called CC=31 SCAR=35.0
557 6 my ($dbh, $pk_catalog, $pk_schema, $pk_table, $fk_catalog, $fk_schema, $fk_table) = @_;
558  
559 6 ▶ my $databases = $dbh->selectall_arrayref("PRAGMA database_list", {Slice => {}}) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $databases = $dbh->selectall_arrayref("PRAGMA database_list", {"Slice", {}}) or return 6 0 6
560  
561 6 my @fk_info;
562 my %table_info;
563 6 for my $database (@$databases) {
564 12 my $dbname = $database->{name};
565 12 ▶ next if defined $fk_schema && $fk_schema ne '%' && $fk_schema ne $dbname;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fk_schema and $fk_schema ne "%" and $fk_schema ne $dbname 1 11 12
Condition83%5/6
defined $fk_schema and $fk_schema ne "%" and $fk_schema ne $dbname
!ll&&!rl&&r
1011
defined $fk_schema and $fk_schema ne "%"
!ll&&!rl&&r
1002
MC/DC66%2/3
defined $fk_schema and $fk_schema ne "%" and $fk_schema ne $dbname
defined $fk_schema $fk_schema ne "%" $fk_schema ne $dbname
Truth table75%3/4
A and B and C
A= defined $fk_schemaB= $fk_schema ne "%"C= $fk_schema ne $dbname
ABCresult
0XX0
10X0
1100
1111
566  
567 11 my $quoted_dbname = $dbh->quote_identifier($dbname);
568 11 ▶ my $master_table =
Branch: 2 branches
BranchTrueFalseTotal
$dbname eq 'main' ? : 5 6 11
$dbname eq 'temp' ? : 0 6 6
569 ($dbname eq 'main') ? 'sqlite_master' :
570 ($dbname eq 'temp') ? 'sqlite_temp_master' :
571 $quoted_dbname.'.sqlite_master';
572  
573 11 ▶ my $tables = $dbh->selectall_arrayref("SELECT name FROM $master_table WHERE type = ?", undef, "table") or return;
Branch: 1 branch
BranchTrueFalseTotal
my $tables = $dbh->selectall_arrayref("SELECT name FROM $master_table WHERE type = ?", undef, "table") or return 11 0 11
574 11 for my $table (@$tables) {
575 26 my $tbname = $table->[0];
576 26 ▶ next if defined $fk_table && $fk_table ne '%' && $fk_table ne $tbname;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fk_table and $fk_table ne "%" and $fk_table ne $tbname 16 10 26
Condition83%5/6
defined $fk_table and $fk_table ne "%" and $fk_table ne $tbname
!ll&&!rl&&r
6416
defined $fk_table and $fk_table ne "%"
!ll&&!rl&&r
6020
MC/DC66%2/3
defined $fk_table and $fk_table ne "%" and $fk_table ne $tbname
defined $fk_table $fk_table ne "%" $fk_table ne $tbname
Truth table75%3/4
A and B and C
A= defined $fk_tableB= $fk_table ne "%"C= $fk_table ne $tbname
ABCresult
0XX0
10X0
1100
1111
577  
578 10 my $quoted_tbname = $dbh->quote_identifier($tbname);
579 10 ▶ my $sth = $dbh->prepare("PRAGMA $quoted_dbname.foreign_key_list($quoted_tbname)") or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("PRAGMA $quoted_dbname.foreign_key_list($quoted_tbname)") or return 10 0 10
580 10 ▶ $sth->execute or return;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return 10 0 10
581 10 ▶ while(my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 14 10 24
582 14 ▶ next if defined $pk_table && $pk_table ne '%' && $pk_table ne $row->{table};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pk_table and $pk_table ne "%" and $pk_table ne $row->{"table"} 3 11 14
Condition83%5/6
defined $pk_table and $pk_table ne "%" and $pk_table ne $row->{"table"}
!ll&&!rl&&r
1013
defined $pk_table and $pk_table ne "%"
!ll&&!rl&&r
1004
MC/DC66%2/3
defined $pk_table and $pk_table ne "%" and $pk_table ne $row->{"table"}
defined $pk_table $pk_table ne "%" $pk_table ne $row->{"table"}
Truth table75%3/4
A and B and C
A= defined $pk_tableB= $pk_table ne "%"C= $pk_table ne $row->{"table"}
ABCresult
0XX0
10X0
1100
1111
583  
584 11 ▶ unless ($table_info{$row->{table}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($table_info{$row->{"table"}}) 9 2 11
585 9 my $quoted_tb = $dbh->quote_identifier($row->{table});
586 9 for my $db (@$databases) {
587 11 my $quoted_db = $dbh->quote_identifier($db->{name});
588 11 ▶ my $t_sth = $dbh->prepare("PRAGMA $quoted_db.table_info($quoted_tb)") or return;
Branch: 1 branch
BranchTrueFalseTotal
my $t_sth = $dbh->prepare("PRAGMA $quoted_db.table_info($quoted_tb)") or return 11 0 11
589 11 ▶ $t_sth->execute or return;
Branch: 1 branch
BranchTrueFalseTotal
$t_sth->execute or return 11 0 11
590 11 my $cols = {};
591 11 ▶ while(my $r = $t_sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $t_sth->fetchrow_hashref) 22 11 33
592 22 $cols->{$r->{name}} = $r->{pk};
593 }
594 11 ▶ if (keys %$cols) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$cols) 9 2 11
595 $table_info{$row->{table}} = {
596 schema => $db->{name},
597 9 columns => $cols,
598 };
599 9 last;
600 }
601 }
602 }
603  
604 11 ▶ next if defined $pk_schema && $pk_schema ne '%' && $pk_schema ne $table_info{$row->{table}}{schema};
Branch: 1 branch
BranchTrueFalseTotal
if defined $pk_schema and $pk_schema ne "%" and $pk_schema ne $table_info{$row->{"table"}}{"schema"} 2 9 11
Condition83%5/6
defined $pk_schema and $pk_schema ne "%" and $pk_schema ne $table_info{$row->{"table"}}{"schema"}
!ll&&!rl&&r
722
defined $pk_schema and $pk_schema ne "%"
!ll&&!rl&&r
704
MC/DC66%2/3
defined $pk_schema and $pk_schema ne "%" and $pk_schema ne $table_info{$row->{"table"}}{"schema"}
defined $pk_schema $pk_schema ne "%" $pk_schema ne $table_info{$row->{"table"}}{"schema"}
Truth table75%3/4
A and B and C
A= defined $pk_schemaB= $pk_schema ne "%"C= $pk_schema ne $table_info{$row->{"table"}}{"schema"}
ABCresult
0XX0
10X0
1100
1111
605  
606 push @fk_info, {
607 PKTABLE_CAT => undef,
608 PKTABLE_SCHEM => $table_info{$row->{table}}{schema},
609 PKTABLE_NAME => $row->{table},
610 PKCOLUMN_NAME => $row->{to},
611 FKTABLE_CAT => undef,
612 FKTABLE_SCHEM => $dbname,
613 FKTABLE_NAME => $tbname,
614 FKCOLUMN_NAME => $row->{from},
615 KEY_SEQ => $row->{seq} + 1,
616 UPDATE_RULE => $DBI_code_for_rule{$row->{on_update}},
617 DELETE_RULE => $DBI_code_for_rule{$row->{on_delete}},
618 FK_NAME => undef,
619 PK_NAME => undef,
620 DEFERRABILITY => undef,
621 9 ▶ UNIQUE_OR_PRIMARY => $table_info{$row->{table}}{columns}{$row->{to}} ? 'PRIMARY' : 'UNIQUE',
Branch: 1 branch
BranchTrueFalseTotal
$table_info{$row->{'table'}}{'columns'}{$row->{'to'}} ? : 6 3 9
622 };
623 }
624 }
625 }
626  
627 6 ▶ my $sponge_dbh = DBI->connect("DBI:Sponge:", "", "")
Branch: 1 branch
BranchTrueFalseTotal
my $sponge_dbh = "DBI"->connect("DBI:Sponge:", "", "") or return $dbh->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr") 6 0 6
628 or return $dbh->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr");
629 my $sponge_sth = $sponge_dbh->prepare("foreign_key_info", {
630 NAME => \@FOREIGN_KEY_INFO_ODBC,
631 6 ▶ rows => [ map { [@{$_}{@FOREIGN_KEY_INFO_ODBC} ] } @fk_info ],
Branch: 1 branch
BranchTrueFalseTotal
my $sponge_sth = $sponge_dbh->prepare("foreign_key_info", {"NAME", \@FOREIGN_KEY_INFO_ODBC, "rows", [map({[@{$_;}{@FOREIGN_KEY_INFO_ODBC}];} @fk_info)], "NUM_OF_FIELDS", scalar @FOREIGN_KEY_INFO_ODBC}) or return $dbh->DBI::set_err($sponge_dbh->err, $sponge_dbh->errstr) 6 0 6
632 NUM_OF_FIELDS => scalar(@FOREIGN_KEY_INFO_ODBC),
633 }) or return $dbh->DBI::set_err(
634 $sponge_dbh->err,
635 $sponge_dbh->errstr,
636 );
637 6 return $sponge_sth;
638 }
639  
640 101 my @STATISTICS_INFO_ODBC = (
641 'TABLE_CAT', # The catalog identifier.
642 'TABLE_SCHEM', # The schema identifier.
643 'TABLE_NAME', # The table identifier.
644 'NON_UNIQUE', # Unique index indicator.
645 'INDEX_QUALIFIER', # Index qualifier identifier.
646 'INDEX_NAME', # The index identifier.
647 'TYPE', # The type of information being returned.
648 'ORDINAL_POSITION', # Column sequence number (starting with 1).
649 'COLUMN_NAME', # The column identifier.
650 'ASC_OR_DESC', # Column sort sequence.
651 'CARDINALITY', # Cardinality of the table or index.
652 'PAGES', # Number of storage pages used by this table or index.
653 'FILTER_CONDITION', # The index filter condition as a string.
654 );
655  
656 4 ▶sub statistics_info {
Subroutine statistics_info: called CC=25 SCAR=34.6
657 4 my ($dbh, $catalog, $schema, $table, $unique_only, $quick) = @_;
658  
659 4 ▶ my $databases = $dbh->selectall_arrayref("PRAGMA database_list", {Slice => {}}) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $databases = $dbh->selectall_arrayref("PRAGMA database_list", {"Slice", {}}) or return 4 0 4
660  
661 4 my @statistics_info;
662 4 for my $database (@$databases) {
663 8 my $dbname = $database->{name};
664 8 ▶ next if defined $schema && $schema ne '%' && $schema ne $dbname;
Branch: 1 branch
BranchTrueFalseTotal
if defined $schema and $schema ne "%" and $schema ne $dbname 0 8 8
Condition33%2/6
defined $schema and $schema ne "%" and $schema ne $dbname
!ll&&!rl&&r
800
defined $schema and $schema ne "%"
!ll&&!rl&&r
800
MC/DC0%0/3
defined $schema and $schema ne "%" and $schema ne $dbname
defined $schema $schema ne "%" $schema ne $dbname
Truth table25%1/4
A and B and C
A= defined $schemaB= $schema ne "%"C= $schema ne $dbname
ABCresult
0XX0
10X0
1100
1111
665  
666 8 my $quoted_dbname = $dbh->quote_identifier($dbname);
667 8 ▶ my $master_table =
Branch: 2 branches
BranchTrueFalseTotal
$dbname eq 'main' ? : 4 4 8
$dbname eq 'temp' ? : 0 4 4
668 ($dbname eq 'main') ? 'sqlite_master' :
669 ($dbname eq 'temp') ? 'sqlite_temp_master' :
670 $quoted_dbname.'.sqlite_master';
671  
672 8 ▶ my $tables = $dbh->selectall_arrayref("SELECT name FROM $master_table WHERE type = ?", undef, "table") or return;
Branch: 1 branch
BranchTrueFalseTotal
my $tables = $dbh->selectall_arrayref("SELECT name FROM $master_table WHERE type = ?", undef, "table") or return 8 0 8
673 8 for my $table_ref (@$tables) {
674 8 my $tbname = $table_ref->[0];
675 8 ▶ next if defined $table && $table ne '%' && uc($table) ne uc($tbname);
Branch: 1 branch
BranchTrueFalseTotal
if defined $table and $table ne "%" and uc $table ne uc $tbname 4 4 8
Condition50%3/6
defined $table and $table ne "%" and uc $table ne uc $tbname
!ll&&!rl&&r
044
defined $table and $table ne "%"
!ll&&!rl&&r
008
MC/DC33%1/3
defined $table and $table ne "%" and uc $table ne uc $tbname
defined $table $table ne "%" uc $table ne uc $tbname
Truth table50%2/4
A and B and C
A= defined $tableB= $table ne "%"C= uc $table ne uc $tbname
ABCresult
0XX0
10X0
1100
1111
676  
677 4 my $quoted_tbname = $dbh->quote_identifier($tbname);
678 4 ▶ my $sth = $dbh->prepare("PRAGMA $quoted_dbname.index_list($quoted_tbname)") or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare("PRAGMA $quoted_dbname.index_list($quoted_tbname)") or return 4 0 4
679 4 ▶ $sth->execute or return;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return 4 0 4
680 4 ▶ while(my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 16 4 20
681  
682 16 ▶ next if $unique_only && !$row->{unique};
Branch: 1 branch
BranchTrueFalseTotal
if $unique_only and not $row->{"unique"} 4 12 16
Condition100%3/3
$unique_only and not $row->{"unique"}
!ll&&!rl&&r
844
MC/DC100%2/2
$unique_only and not $row->{"unique"}
$unique_only not $row->{"unique"}
Truth table100%3/3
A and not(B)
A= $unique_onlyB= not $row->{"unique"}
ABresult
0X0
100
111
683 12 my $quoted_idx = $dbh->quote_identifier($row->{name});
684 12 for my $db (@$databases) {
685 24 my $quoted_db = $dbh->quote_identifier($db->{name});
686 24 ▶ my $i_sth = $dbh->prepare("PRAGMA $quoted_db.index_info($quoted_idx)") or return;
Branch: 1 branch
BranchTrueFalseTotal
my $i_sth = $dbh->prepare("PRAGMA $quoted_db.index_info($quoted_idx)") or return 24 0 24
687 24 ▶ $i_sth->execute or return;
Branch: 1 branch
BranchTrueFalseTotal
$i_sth->execute or return 24 0 24
688 24 my $cols = {};
689 24 ▶ while(my $info = $i_sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $info = $i_sth->fetchrow_hashref) 16 24 40
690 push @statistics_info, {
691 TABLE_CAT => undef,
692 TABLE_SCHEM => $db->{name},
693 TABLE_NAME => $tbname,
694 NON_UNIQUE => $row->{unique} ? 0 : 1,
695 INDEX_QUALIFIER => undef,
696 INDEX_NAME => $row->{name},
697 TYPE => 'btree', # see http://www.sqlite.org/version3.html esp. "Traditional B-trees are still used for indices"
698 ORDINAL_POSITION => $info->{seqno} + 1,
699 COLUMN_NAME => $info->{name},
700 16 ▶ ASC_OR_DESC => undef,
Branch: 1 branch
BranchTrueFalseTotal
$row->{'unique'} ? : 12 4 16
701 CARDINALITY => undef,
702 PAGES => undef,
703 FILTER_CONDITION => undef,
704 };
705 }
706 }
707 }
708 }
709 }
710  
711 4 ▶ my $sponge_dbh = DBI->connect("DBI:Sponge:", "", "")
Branch: 1 branch
BranchTrueFalseTotal
my $sponge_dbh = "DBI"->connect("DBI:Sponge:", "", "") or return $dbh->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr") 4 0 4
712 or return $dbh->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr");
713 my $sponge_sth = $sponge_dbh->prepare("statistics_info", {
714 NAME => \@STATISTICS_INFO_ODBC,
715 4 ▶ rows => [ map { [@{$_}{@STATISTICS_INFO_ODBC} ] } @statistics_info ],
Branch: 1 branch
BranchTrueFalseTotal
my $sponge_sth = $sponge_dbh->prepare("statistics_info", {"NAME", \@STATISTICS_INFO_ODBC, "rows", [map({[@{$_;}{@STATISTICS_INFO_ODBC}];} @statistics_info)], "NUM_OF_FIELDS", scalar @STATISTICS_INFO_ODBC}) or return $dbh->DBI::set_err($sponge_dbh->err, $sponge_dbh->errstr) 4 0 4
716 NUM_OF_FIELDS => scalar(@STATISTICS_INFO_ODBC),
717 }) or return $dbh->DBI::set_err(
718 $sponge_dbh->err,
719 $sponge_dbh->errstr,
720 );
721 4 return $sponge_sth;
722 }
723  
724 0 ▶sub type_info_all {
Subroutine type_info_all: not called CC=1 SCAR=6.9
725 0 return; # XXX code just copied from DBD::Oracle, not yet thought about
726 }
727  
728 101 my @COLUMN_INFO = qw(
729 TABLE_CAT
730 TABLE_SCHEM
731 TABLE_NAME
732 COLUMN_NAME
733 DATA_TYPE
734 TYPE_NAME
735 COLUMN_SIZE
736 BUFFER_LENGTH
737 DECIMAL_DIGITS
738 NUM_PREC_RADIX
739 NULLABLE
740 REMARKS
741 COLUMN_DEF
742 SQL_DATA_TYPE
743 SQL_DATETIME_SUB
744 CHAR_OCTET_LENGTH
745 ORDINAL_POSITION
746 IS_NULLABLE
747 );
748  
749 7 ▶sub column_info {
Subroutine column_info: called CC=22 SCAR=31.8
750 7 my ($dbh, $cat_val, $sch_val, $tbl_val, $col_val) = @_;
751  
752 7 ▶ if ( defined $col_val and $col_val eq '%' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col_val and $col_val eq "%") 1 6 7
Condition100%3/3
defined $col_val and $col_val eq "%"
!ll&&!rl&&r
151
MC/DC100%2/2
defined $col_val and $col_val eq "%"
defined $col_val $col_val eq "%"
Truth table100%3/3
A and B
A= defined $col_valB= $col_val eq "%"
ABresult
0X0
100
111
753 1 $col_val = undef;
754 }
755  
756 # Get a list of all tables ordered by TABLE_SCHEM, TABLE_NAME
757 7 my $sql = <<'END_SQL';
758 SELECT TABLE_SCHEM, tbl_name TABLE_NAME
759 FROM (
760 SELECT 'main' TABLE_SCHEM, tbl_name
761 FROM sqlite_master
762 WHERE type IN ('table','view')
763 UNION ALL
764 SELECT 'temp' TABLE_SCHEM, tbl_name
765 FROM sqlite_temp_master
766 WHERE type IN ('table','view')
767 END_SQL
768  
769 7 for my $db_name (_attached_database_list($dbh)) {
770 2 $sql .= <<"END_SQL";
771 UNION ALL
772 SELECT '$db_name' TABLE_SCHEM, tbl_name
773 FROM "$db_name".sqlite_master
774 WHERE type IN ('table','view')
775 END_SQL
776 }
777  
778 7 $sql .= <<'END_SQL';
779 UNION ALL
780 SELECT 'main' TABLE_SCHEM, 'sqlite_master' tbl_name
781 UNION ALL
782 SELECT 'temp' TABLE_SCHEM, 'sqlite_temp_master' tbl_name
783 )
784 END_SQL
785  
786 7 my @where;
787 7 ▶ if (defined $sch_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sch_val) 0 7 7
788 0 push @where, "TABLE_SCHEM LIKE '$sch_val'";
789 }
790 7 ▶ if (defined $tbl_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tbl_val) 7 0 7
791 7 push @where, "TABLE_NAME LIKE '$tbl_val'";
792 }
793 7 ▶ $sql .= ' WHERE ' . join("\n AND ", @where ) . "\n" if @where;
Branch: 1 branch
BranchTrueFalseTotal
if @where 7 0 7
794 7 $sql .= " ORDER BY TABLE_SCHEM, TABLE_NAME\n";
795 7 ▶ my $sth_tables = $dbh->prepare($sql) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $sth_tables = $dbh->prepare($sql) or return undef 7 0 7
796 7 ▶ $sth_tables->execute or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$sth_tables->execute or return undef 7 0 7
797  
798 # Taken from Fey::Loader::SQLite
799 7 my @cols;
800 7 ▶ while (my($schema, $table) = $sth_tables->fetchrow_array) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $sth_tables->fetchrow_array) 9 7 16
801 9 ▶ my $sth_columns = $dbh->prepare(qq{PRAGMA "$schema".table_info("$table")}) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $sth_columns = $dbh->prepare(qq[PRAGMA "$schema".table_info("$table")]) or return 9 0 9
802 9 ▶ $sth_columns->execute or return;
Branch: 1 branch
BranchTrueFalseTotal
$sth_columns->execute or return 9 0 9
803  
804 9 ▶ for (my $position = 1; my $col_info = $sth_columns->fetchrow_hashref; $position++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $col_info = $sth_columns->fetchrow_hashref) 20 9 29
805 20 ▶ if (defined $col_val) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col_val) 14 6 20
806 # This must do a LIKE comparison
807 14 ▶ my $sth = $dbh->prepare("SELECT '$col_info->{name}' LIKE '$col_val'") or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $sth = $dbh->prepare(qq[SELECT '$col_info->{"name"}' LIKE '${col_val}']) or return undef 14 0 14
808 14 ▶ $sth->execute or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$sth->execute or return undef 14 0 14
809 # Skip columns that don't match $col_val
810 14 ▶ next unless ($sth->fetchrow_array)[0];
Branch: 1 branch
BranchTrueFalseTotal
unless ($sth->fetchrow_array)[0] 7 7 14
811 }
812  
813 my %col = (
814 TABLE_SCHEM => $schema,
815 TABLE_NAME => $table,
816 COLUMN_NAME => $col_info->{name},
817 13 ORDINAL_POSITION => $position,
818 );
819  
820 13 my $type = $col_info->{type};
821 13 ▶ if ($type =~ s/(\w+)\s*\(\s*(\d+)(?:\s*,\s*(\d+))?\s*\)/$1/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ s/(\w+)\s*\(\s*(\d+)(?:\s*,\s*(\d+))?\s*\)/$1/) 7 6 13
822 7 $col{COLUMN_SIZE} = $2;
823 7 $col{DECIMAL_DIGITS} = $3;
824 }
825  
826 13 $col{TYPE_NAME} = $type;
827  
828 13 ▶ if ( defined $col_info->{dflt_value} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $col_info->{"dflt_value"}) 0 13 13
829 $col{COLUMN_DEF} = $col_info->{dflt_value}
830 0 }
831  
832 13 ▶ if ( $col_info->{notnull} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($col_info->{'notnull'}) { } 2 11 13
833 2 $col{NULLABLE} = 0;
834 2 $col{IS_NULLABLE} = 'NO';
835 } else {
836 11 $col{NULLABLE} = 1;
837 11 $col{IS_NULLABLE} = 'YES';
838 }
839  
840 13 push @cols, \%col;
841 }
842 9 $sth_columns->finish;
843 }
844 7 $sth_tables->finish;
845  
846 7 ▶ my $sponge = DBI->connect("DBI:Sponge:", '','')
Branch: 1 branch
BranchTrueFalseTotal
my $sponge = "DBI"->connect("DBI:Sponge:", "", "") or return $dbh->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr") 7 0 7
847 or return $dbh->DBI::set_err($DBI::err, "DBI::Sponge: $DBI::errstr");
848 $sponge->prepare( "column_info", {
849 7 ▶ rows => [ map { [ @{$_}{@COLUMN_INFO} ] } @cols ],
Branch: 1 branch
BranchTrueFalseTotal
$sponge->prepare("column_info", {"rows", [map({[@{$_;}{@COLUMN_INFO}];} @cols)], "NUM_OF_FIELDS", scalar @COLUMN_INFO, "NAME", [@COLUMN_INFO]}) or return $dbh->DBI::set_err($sponge->err, $sponge->errstr) 7 0 7
850 NUM_OF_FIELDS => scalar @COLUMN_INFO,
851 NAME => [ @COLUMN_INFO ],
852 } ) or return $dbh->DBI::set_err(
853 $sponge->err,
854 $sponge->errstr,
855 );
856 }
857  
858 #======================================================================
859 # An internal tied hash package used for %DBD::SQLeet::COLLATION, to
860 # prevent people from unintentionally overriding globally registered collations.
861  
862 package # hide from PAUSE
863 DBD::SQLeet::_WriteOnceHash;
864  
865 101 require Tie::Hash;
866  
867 101 our @ISA = qw(Tie::StdHash);
868  
869 101 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
870 101 bless {}, $_[0];
871 }
872  
873 207 ▶sub STORE {
Subroutine STORE: called CC=2 SCAR=6.9
874 207 ▶ ! exists $_[0]->{$_[1]} or die "entry $_[1] already registered";
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{$_[1]} and die "entry $_[1] already registered" 2 205 207
875 205 $_[0]->{$_[1]} = $_[2];
876 }
877  
878 1 ▶sub DELETE {
Subroutine DELETE: called CC=1 SCAR=0.0
879 1 die "deletion of entry $_[1] is forbidden";
880 }
881  
882 101 1;
883  
884 __END__
↑ Summary dbdimp.h »