SQLite/Storage.pm

filter: Statementstmt 83.4% 126 / 151 Branchbran 52.8% 37 / 70 Conditioncond 44.4% 28 / 63 MC/DCmcdc 17.1% 6 / 35 Subroutinesub 96.6% 29 / 30 Podpod 83.3% 5 / 6 totaltotal 65.0% 231 / 355 CC 59 SCAR 31.7
CC
59
Cov
67%
_ping
83.4
_dbi_attrs_for_bind
26.0
_dbh_execute
22.4
CRAP 23.9
« SQLite/DDL.pm ↑ Summary SQLite/Diff.pm »
1 package DBIO::SQLite::Storage;
2 # ABSTRACT: SQLite storage driver for DBIO
3  
4 214 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 214 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 214 ▶use base qw/DBIO::Storage::DBI/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 214 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 214 __PACKAGE__->register_driver('SQLite' => __PACKAGE__);
11  
12 214 ▶use Context::Preserve 'preserve_context';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 214 ▶use DBIO::Util qw(file_path iv_size mkpath modver_gt_or_eq os_name sigwarn_silencer);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 214 ▶use DBIO::SQLite::Util qw(column_is_nullable);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 214 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 214 ▶use DBIO::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 214 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 214 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 214 ▶use File::Copy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 214 ▶use POSIX ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 214 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 214 __PACKAGE__->sql_maker_class('DBIO::SQLite::SQLMaker');
24 214 __PACKAGE__->sql_quote_char ('"');
25 214 __PACKAGE__->datetime_parser_type ('DateTime::Format::SQLite');
26  
27 # Must be defined AFTER `use namespace::clean` — a sub declared before it
28 # is wiped from the symbol table at end of compile, which would make
29 # DBIO::Schema/deploy fall back to the (unsupported) SQL::Translator path.
30 336 ▶sub dbio_deploy_class { 'DBIO::SQLite::Deploy' }
Subroutine dbio_deploy_class: called CC=1 SCAR=0.0
Pod undocumented
31  
32 28519 ▶sub sql_maker {
Subroutine sql_maker: called CC=3 SCAR=11.6
Pod documented
33 28519 my $self = shift;
34 28519 my $sm = $self->next::method(@_);
35 # SQLite always uses double-quote identifier quoting — ensure it is
36 # active even when the caller did not pass quote_names in connect_info.
37 28519 ▶ $sm->{quote_char} //= $self->sql_quote_char;
Condition66%2/3
$sm->{"quote_char"} //= $self->sql_quote_char
l!l&&r!l&&!r
2851720
MC/DC0%0/2
$sm->{"quote_char"} //= $self->sql_quote_char
$sm->{"quote_char"} $self->sql_quote_char
Truth table66%2/3
A //= B
A= $sm->{"quote_char"}B= $self->sql_quote_char
ABresult
1X1
011
000
38 28519 ▶ $sm->{name_sep} //= $self->sql_name_sep;
Condition33%1/3
$sm->{"name_sep"} //= $self->sql_name_sep
l!l&&r!l&&!r
2851900
MC/DC0%0/2
$sm->{"name_sep"} //= $self->sql_name_sep
$sm->{"name_sep"} $self->sql_name_sep
Truth table33%1/3
A //= B
A= $sm->{"name_sep"}B= $self->sql_name_sep
ABresult
1X1
011
000
39 28519 $sm;
40 }
41  
42 4 ▶sub _dbh_columns_info_for {
Subroutine _dbh_columns_info_for: called CC=6 SCAR=18.3
43 4 my ($self, $dbh, $table) = @_;
44  
45 4 ▶ my $result = $self->SUPER::_dbh_columns_info_for($dbh, $table) // {};
Condition50%1/2
$self->SUPER::_dbh_columns_info_for($dbh, $table) // {}
l!l
40
MC/DC0%0/1
$self->SUPER::_dbh_columns_info_for($dbh, $table) // {}
$self->SUPER::_dbh_columns_info_for($dbh, $table)
Truth table50%1/2
A
A= $self->SUPER::_dbh_columns_info_for($dbh, $table)
Aresult
11
00
46  
47 # DBD::SQLite's column_info (and its prepared-statement NULLABLE fallback)
48 # do not report is_nullable reliably -- PK columns come back as nullable,
49 # and columns in unique constraints may also be wrong. PRAGMA table_info
50 # is the authoritative source: notnull=1 means NOT NULL. Layer its truth
51 # over the parent's result so that PK columns are correctly NOT NULL.
52 4 my $quoted = $dbh->quote(do {
53 # Strip any "schema." prefix the caller may have supplied.
54 4 my $t = $table;
55 4 $t =~ s/^.*\.//;
56 4 $t;
57 });
58 4 my $sth = eval { $dbh->prepare(qq{PRAGMA table_info($quoted)}) };
59 4 ▶ return $result unless $sth;
Branch: 1 branch
BranchTrueFalseTotal
unless $sth 0 4 4
60 4 $sth->execute;
61 4 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 14 4 18
62 14 my $col = $row->{name};
63 14 ▶ next unless $col && exists $result->{$col};
Branch: 1 branch
BranchTrueFalseTotal
unless $col and exists $result->{$col} 0 14 14
Condition33%1/3
$col and exists $result->{$col}
!ll&&!rl&&r
0014
MC/DC0%0/2
$col and exists $result->{$col}
$col exists $result->{$col}
Truth table33%1/3
A and B
A= $colB= exists $result->{$col}
ABresult
0X0
100
111
64 # PRIMARY KEY columns are logically NOT NULL even when the DDL did
65 # not declare them as such (PRAGMA table_info reports notnull=0 for
66 # them -- a SQLite quirk). column_is_nullable is the single source of
67 # truth for that rule (shared with DBIO::SQLite::Introspect).
68 $result->{$col}{is_nullable} =
69 14 column_is_nullable($row->{notnull}, $row->{pk});
70 }
71 4 $sth->finish;
72  
73 4 return $result;
74 }
75  
76  
77  
78 3 ▶sub _determine_supports_multicolumn_in {
Subroutine _determine_supports_multicolumn_in: called CC=2 SCAR=7.6
79 3 ▶ ( shift->_server_info->{normalized_dbms_version} < '3.014' )
Branch: 1 branch
BranchTrueFalseTotal
+(shift())->_server_info->{'normalized_dbms_version'} < '3.014' ? : 0 3 3
80 ? 0
81 : 1
82 }
83  
84 2 ▶sub backup {
Subroutine backup: called CC=10 SCAR=24.3
Pod documented
85 2 my ($self, $dir) = @_;
86 2 ▶ $dir //= './';
Condition100%2/2
$dir //= "./"
l!l
11
MC/DC100%1/1
$dir //= "./"
$dir
Truth table100%2/2
A
A= $dir
Aresult
11
00
87  
88 ## Where is the db file?
89 2 my $dsn = $self->_dbi_connect_info()->[0];
90  
91 2 ▶ my $dbname = $1 if($dsn =~ /dbname=([^;]+)/);
Branch: 1 branch
BranchTrueFalseTotal
if $dsn =~ /dbname=([^;]+)/ 0 2 2
92 2 ▶ if(!$dbname)
Branch: 1 branch
BranchTrueFalseTotal
unless ($dbname) 1 1 2
93 {
94 1 ▶ $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i);
Branch: 1 branch
BranchTrueFalseTotal
if $dsn =~ /^dbi:SQLite:(.+)$/i 1 0 1
95 }
96 2 ▶ $self->throw_exception("Cannot determine name of SQLite db file")
Branch: 1 branch
BranchTrueFalseTotal
unless $dbname and -f $dbname 0 2 2
Condition33%1/3
$dbname and -f $dbname
!ll&&!rl&&r
002
MC/DC0%0/2
$dbname and -f $dbname
$dbname -f $dbname
Truth table33%1/3
A and B
A= $dbnameB= -f $dbname
ABresult
0X0
100
111
97 if(!$dbname || !-f $dbname);
98  
99 2 my $file = File::Basename::basename($dbname);
100 2 $file = POSIX::strftime("%Y-%m-%d-%H_%M_%S", localtime()) . $file;
101 2 ▶ $file = "B$file" while(-f $file);
Branch: 1 branch
BranchTrueFalseTotal
-f $file and do { $file = "B$file" } 0 2 2
102  
103 2 ▶ mkpath($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 2 2
104 2 my $backupfile = file_path($dir, $file);
105  
106 2 my $res = File::Copy::copy($dbname, $backupfile);
107 2 ▶ $self->throw_exception("Backup failed! ($!)") if(!$res);
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 2 2
108  
109 2 return $backupfile;
110 }
111  
112  
113 21 ▶sub _exec_svp_begin {
Subroutine _exec_svp_begin: called CC=1 SCAR=0.0
114 21 my ($self, $name) = @_;
115  
116 21 $self->_dbh->do("SAVEPOINT $name");
117 }
118  
119 9 ▶sub _exec_svp_release {
Subroutine _exec_svp_release: called CC=1 SCAR=0.0
120 9 my ($self, $name) = @_;
121  
122 9 $self->_dbh->do("RELEASE SAVEPOINT $name");
123 }
124  
125 14 ▶sub _exec_svp_rollback {
Subroutine _exec_svp_rollback: called CC=3 SCAR=14.2
126 14 my ($self, $name) = @_;
127  
128 14 $self->_dbh->do("ROLLBACK TO SAVEPOINT $name");
129  
130 # resync state for older DBD::SQLite (RT#67843)
131 # https://github.com/DBD-SQLite/DBD-SQLite/commit/9b3cdbf
132 14 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not &modver_gt_or_eq("DBD::SQLite", "1.33") and $self->_dbh->FETCH("AutoCommit")) 0 14 14
Condition33%1/3
not &modver_gt_or_eq("DBD::SQLite", "1.33") and $self->_dbh->FETCH("AutoCommit")
!ll&&!rl&&r
1400
MC/DC0%0/2
not &modver_gt_or_eq("DBD::SQLite", "1.33") and $self->_dbh->FETCH("AutoCommit")
not &modver_gt_or_eq("DBD::SQLite", "1.33") $self->_dbh->FETCH("AutoCommit")
Truth table33%1/3
not(A) and B
A= not &modver_gt_or_eq("DBD::SQLite", "1.33")B= $self->_dbh->FETCH("AutoCommit")
ABresult
0X0
100
111
133 ! modver_gt_or_eq('DBD::SQLite', '1.33')
134 and
135 $self->_dbh->FETCH('AutoCommit')
136 ) {
137 0 $self->_dbh->STORE('AutoCommit', 0);
138 0 $self->_dbh->STORE('BegunWork', 1);
139 }
140 }
141  
142 510 ▶sub _ping {
Subroutine _ping: called CC=15 SCAR=44.2
143 510 my $self = shift;
144  
145 # Be extremely careful what we do here. SQLite is notoriously bad at
146 # synchronizing its internal transaction state with {AutoCommit}
147 # https://metacpan.org/source/ADAMK/DBD-SQLite-1.37/lib/DBD/SQLite.pm#L921
148 # There is a function http://www.sqlite.org/c3ref/get_autocommit.html
149 # but DBD::SQLite does not expose it (nor does it seem to properly use it)
150  
151 # Therefore only execute a "ping" when we have no other choice *AND*
152 # scrutinize the thrown exceptions to make sure we are where we think we are
153 510 ▶ my $dbh = $self->_dbh or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $dbh = $self->_dbh or return undef 510 0 510
154 510 ▶ return undef unless $dbh->FETCH('Active');
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->FETCH("Active") 0 510 510
155 510 ▶ return undef unless $dbh->ping;
Branch: 1 branch
BranchTrueFalseTotal
unless $dbh->ping 0 510 510
156  
157 510 my $ping_fail;
158  
159 # older DBD::SQLite does not properly synchronize commit state between
160 # the libsqlite and the $dbh
161 510 ▶ unless (defined $DBD::SQLite::__DBIO_TXN_SYNC_SANE__) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $DBD::SQLite::__DBIO_TXN_SYNC_SANE__) 199 311 510
162 199 $DBD::SQLite::__DBIO_TXN_SYNC_SANE__ = modver_gt_or_eq('DBD::SQLite', '1.38_02');
163 }
164  
165 # fallback to travesty
166 510 ▶ unless ($DBD::SQLite::__DBIO_TXN_SYNC_SANE__) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($DBD::SQLite::__DBIO_TXN_SYNC_SANE__) 0 510 510
167 # since we do not have access to sqlite3_get_autocommit(), do a trick
168 # to attempt to *safely* determine what state are we *actually* in.
169 # FIXME
170 # also using T::T here leads to bizarre leaks - will figure it out later
171 0 my $really_not_in_txn = do {
172 0 local $@;
173  
174 # older versions of DBD::SQLite do not properly detect multiline BEGIN/COMMIT
175 # statements to adjust their {AutoCommit} state. Hence use such a statement
176 # pair here as well, in order to escape from poking {AutoCommit} needlessly
177 # https://rt.cpan.org/Public/Bug/Display.html?id=80087
178 eval {
179 # will fail instantly if already in a txn
180 0 $dbh->do("-- multiline\nBEGIN");
181 0 $dbh->do("-- multiline\nCOMMIT");
182 0 1;
183 0 ▶ } or do {
Condition0%0/3
eval { || do {
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { $dbh->do("-- multiline\nBEGIN"); $dbh->do("-- multiline\nCOMMIT"); 1 } } || do { $@ =~ /transaction within a transaction/ ? 0 : undef }
eval { do { $dbh->do("-- multiline\nBEGIN"); $dbh->do("-- multiline\nCOMMIT"); 1 } } do { $@ =~ /transaction within a transaction/ ? 0 : undef }
Truth table0%0/3
A || B
A= eval { do { $dbh->do("-- multiline\nBEGIN"); $dbh->do("-- multiline\nCOMMIT"); 1 } }B= do { $@ =~ /transaction within a transaction/ ? 0 : undef }
ABresult
1X1
011
000
184 0 ▶ ($@ =~ /transaction within a transaction/)
Branch: 1 branch
BranchTrueFalseTotal
$@ =~ /transaction within a transaction/ ? : 0 0 0
185 ? 0
186 : undef
187 ;
188 };
189 };
190  
191 # if we were unable to determine this - we may very well be dead
192 0 ▶ if (not defined $really_not_in_txn) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $really_not_in_txn) { } 0 0 0
elsif ($really_not_in_txn xor $dbh->{'AutoCommit'}) { } 0 0 0
193 0 $ping_fail = 1;
194 }
195 # check the AC sync-state
196 ▶ elsif ($really_not_in_txn xor $dbh->{AutoCommit}) {
Condition0%0/4
$really_not_in_txn xor $dbh->{"AutoCommit"}
l&&rl&&!r!l&&r!l&&!r
0000
MC/DC0%0/2
$really_not_in_txn xor $dbh->{"AutoCommit"}
$really_not_in_txn $dbh->{"AutoCommit"}
Truth table0%0/4
A xor B
A= $really_not_in_txnB= $dbh->{"AutoCommit"}
ABresult
110
101
011
000
197 carp_unique (sprintf
198 'Internal transaction state of handle %s (apparently %s a transaction) does not seem to '
199 . 'match its AutoCommit attribute setting of %s - this is an indication of a '
200 . 'potentially serious bug in your transaction handling logic',
201 $dbh,
202 $really_not_in_txn ? 'NOT in' : 'in',
203 0 ▶ $dbh->{AutoCommit} ? 'TRUE' : 'FALSE',
Branch: 2 branches
BranchTrueFalseTotal
$really_not_in_txn ? : 0 0 0
$dbh->{'AutoCommit'} ? : 0 0 0
204 );
205  
206 # it is too dangerous to execute anything else in this state
207 # assume everything works (safer - worst case scenario next statement throws)
208 0 return 1;
209 }
210 }
211  
212 # do the actual test and return on no failure
213 510 ▶ ( $ping_fail ||= ! try { $dbh->do('SELECT * FROM sqlite_master LIMIT 1'); 1 } )
Subroutine __ANON__: called CC=1 SCAR=0.0
214 510 ▶ or return 1; # the actual RV of _ping()
Branch: 1 branch
BranchTrueFalseTotal
$ping_fail ||= !&try(sub { $dbh->do("SELECT * FROM sqlite_master LIMIT 1"); 1; } ) or return 1 0 510 510
Condition33%1/3
$ping_fail ||= !&try(sub {
l!l&&r!l&&!r
00510
MC/DC0%0/2
$ping_fail ||= !&try(sub { $dbh->do("SELECT * FROM sqlite_master LIMIT 1"); 1; } )
$ping_fail !&try(sub { $dbh->do("SELECT * FROM sqlite_master LIMIT 1"); 1; } )
Truth table33%1/3
A ||= not(B)
A= $ping_failB= !&try(sub { $dbh->do("SELECT * FROM sqlite_master LIMIT 1"); 1; } )
ABresult
1X1
011
000
215  
216 # ping failed (or so it seems) - need to do some cleanup
217 # it is possible to have a proper "connection", and have "ping" return
218 # false anyway (e.g. corrupted file). In such cases DBD::SQLite still
219 # keeps the actual file handle open. We don't really want this to happen,
220 # so force-close the handle via DBI itself
221 #
222 0 local $@; # so that we do not clobber the real error as set above
223 0 eval { $dbh->disconnect }; # if it fails - it fails
224 0 undef; # the actual RV of _ping()
225 }
226  
227 2305 ▶sub bind_attribute_by_data_type {
Subroutine bind_attribute_by_data_type: called CC=2 SCAR=6.9
Pod documented
228  
229 # According to http://www.sqlite.org/datatype3.html#storageclasses
230 # all numeric types are dynamically allocated up to 8 bytes per
231 # individual value
232 # Thus it should be safe and non-wasteful to bind everything as
233 # SQL_BIGINT and have SQLite deal with storage/comparisons however
234 # it deems correct
235 2305 ▶ $_[1] =~ /^ (?: int(?:[1248]|eger)? | (?:tiny|small|medium|big)int ) $/ix
Branch: 1 branch
BranchTrueFalseTotal
$_[1] =~ /^ (?: int(?:[1248]|eger)? | (?:tiny|small|medium|big)int ) $/xi ? : 661 1644 2305
236 ? DBI::SQL_BIGINT()
237 : undef
238 ;
239 }
240  
241  
242 # FIXME - what the flying fuck... work around RT#76395
243 # DBD::SQLite warns on binding >32 bit values with 32 bit IVs
244 10248 ▶sub _dbh_execute {
Subroutine _dbh_execute: called CC=8 SCAR=31.1
245 10248 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (&iv_size() < 8 || &os_name() eq "MSWin32" and not defined $DBD::SQLite::__DBIO_CHECK_dbd_mishandles_bound_BIGINT) 0 10248 10248
Condition33%2/6
&iv_size() < 8 || &os_name() eq "MSWin32" and not defined $DBD::SQLite::__DBIO_CHECK_dbd_mishandles_bound_BIGINT
!ll&&!rl&&r
1024800
&iv_size() < 8 or &os_name() eq "MSWin32"
l!l&&r!l&&!r
0010248
MC/DC0%0/3
&iv_size() < 8 || &os_name() eq "MSWin32" and not defined $DBD::SQLite::__DBIO_CHECK_dbd_mishandles_bound_BIGINT
&iv_size() < 8 &os_name() eq "MSWin32" not defined $DBD::SQLite::__DBIO_CHECK_dbd_mishandles_bound_BIGINT
Truth table20%1/5
A or B and not(C)
A= &iv_size() < 8B= &os_name() eq "MSWin32"C= not defined $DBD::SQLite::__DBIO_CHECK_dbd_mishandles_bound_BIGINT
ABCresult
00X0
1X00
0100
1X11
0111
246 (
247 iv_size < 8
248 or
249 os_name eq 'MSWin32'
250 )
251 and
252 ! defined $DBD::SQLite::__DBIO_CHECK_dbd_mishandles_bound_BIGINT
253 ) {
254 0 ▶ $DBD::SQLite::__DBIO_CHECK_dbd_mishandles_bound_BIGINT = (
Branch: 1 branch
BranchTrueFalseTotal
&modver_gt_or_eq('DBD::SQLite', '1.37') ? : 0 0 0
255 modver_gt_or_eq('DBD::SQLite', '1.37')
256 ) ? 1 : 0;
257 }
258  
259 10248 ▶ local $SIG{__WARN__} = sigwarn_silencer( qr/
Branch: 1 branch
BranchTrueFalseTotal
if &iv_size() < 8 || &os_name() eq "MSWin32" and $DBD::SQLite::__DBIO_CHECK_dbd_mishandles_bound_BIGINT 0 10248 10248
Condition33%2/6
&iv_size() < 8 || &os_name() eq "MSWin32" and $DBD::SQLite::__DBIO_CHECK_dbd_mishandles_bound_BIGINT
!ll&&!rl&&r
1024800
&iv_size() < 8 or &os_name() eq "MSWin32"
l!l&&r!l&&!r
0010248
MC/DC0%0/3
&iv_size() < 8 || &os_name() eq "MSWin32" and $DBD::SQLite::__DBIO_CHECK_dbd_mishandles_bound_BIGINT
&iv_size() < 8 &os_name() eq "MSWin32" $DBD::SQLite::__DBIO_CHECK_dbd_mishandles_bound_BIGINT
Truth table20%1/5
A or B and C
A= &iv_size() < 8B= &os_name() eq "MSWin32"C= $DBD::SQLite::__DBIO_CHECK_dbd_mishandles_bound_BIGINT
ABCresult
00X0
1X00
0100
1X11
0111
260 \Qdatatype mismatch: bind\E \s (?:
261 param \s+ \( \d+ \) \s+ [-+]? \d+ (?: \. 0*)? \Q as integer\E
262 |
263 0 \d+ \s type \s @{[ DBI::SQL_BIGINT() ]} \s as \s [-+]? \d+ (?: \. 0*)?
264 )
265 /x ) if (
266 (
267 iv_size < 8
268 or
269 os_name eq 'MSWin32'
270 )
271 and
272 $DBD::SQLite::__DBIO_CHECK_dbd_mishandles_bound_BIGINT
273 );
274  
275 10248 shift->next::method(@_);
276 }
277  
278 # DBD::SQLite (at least up to version 1.31 has a bug where it will
279 # non-fatally numify a string value bound as an integer, resulting
280 # in insertions of '0' into supposed-to-be-numeric fields
281 # Since this can result in severe data inconsistency, remove the
282 # bind attr if such a situation is detected
283 #
284 # FIXME - when a DBD::SQLite version is released that eventually fixes
285 # this situation (somehow) - no-op this override once a proper DBD
286 # version is detected
287 17591 ▶sub _dbi_attrs_for_bind {
Subroutine _dbi_attrs_for_bind: called CC=18 SCAR=32.6
288 17591 my ($self, $ident, $bind) = @_;
289  
290 17591 my $bindattrs = $self->next::method($ident, $bind);
291  
292 17591 ▶ if (! defined $DBD::SQLite::__DBIO_CHECK_dbd_can_bind_bigint_values) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $DBD::SQLite::__DBIO_CHECK_dbd_can_bind_bigint_values) 195 17396 17591
293 195 ▶ $DBD::SQLite::__DBIO_CHECK_dbd_can_bind_bigint_values
Branch: 1 branch
BranchTrueFalseTotal
&modver_gt_or_eq('DBD::SQLite', '1.37') ? : 195 0 195
294 = modver_gt_or_eq('DBD::SQLite', '1.37') ? 1 : 0;
295 }
296  
297 17591 for my $i (0.. $#$bindattrs) {
298  
299 41347 ▶ if (defined $bindattrs->[$i]) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $bindattrs->[$i]) { } 30067 11280 41347
elsif (defined $bind->[$i][1] and not ref $bind->[$i][1] and &looks_like_number($bind->[$i][1]) and !$bind->[$i][0]{'dbic_colname'} || $bind->[$i][0]{'dbic_colname'} =~ /\(/) { } 34 21333 21367
300 # Validate existing integer bind attributes
301 30067 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (defined $bind->[$i][1] and grep {$bindattrs->[$i] eq $_;} &DBI::SQL_INTEGER(), &DBI::SQL_TINYINT(), &DBI::SQL_SMALLINT(), &DBI::SQL_BIGINT()) 29727 340 30067
Condition66%2/3
defined $bind->[$i][1] and grep {$bindattrs->[$i] eq $_;} &DBI::SQL_INTEGER(), &DBI::SQL_TINYINT(), &DBI::SQL_SMALLINT(), &DBI::SQL_BIGINT()
!ll&&!rl&&r
340029727
MC/DC50%1/2
defined $bind->[$i][1] and grep {$bindattrs->[$i] eq $_;} &DBI::SQL_INTEGER(), &DBI::SQL_TINYINT(), &DBI::SQL_SMALLINT(), &DBI::SQL_BIGINT()
defined $bind->[$i][1] grep {$bindattrs->[$i] eq $_;} &DBI::SQL_INTEGER(), &DBI::SQL_TINYINT(), &DBI::SQL_SMALLINT(), &DBI::SQL_BIGINT()
Truth table66%2/3
A and B
A= defined $bind->[$i][1]B= grep {$bindattrs->[$i] eq $_;} &DBI::SQL_INTEGER(), &DBI::SQL_TINYINT(), &DBI::SQL_SMALLINT(), &DBI::SQL_BIGINT()
ABresult
0X0
100
111
302 defined $bind->[$i][1]
303 and
304 118908 grep { $bindattrs->[$i] eq $_ } (
305 DBI::SQL_INTEGER(), DBI::SQL_TINYINT(), DBI::SQL_SMALLINT(), DBI::SQL_BIGINT()
306 )
307 ) {
308 29727 ▶ if ( $bind->[$i][1] !~ /^ [\+\-]? [0-9]+ (?: \. 0* )? $/x ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $bind->[$i][1] =~ /^ [\+\-]? [0-9]+ (?: \. 0* )? $/x) { } 3 29724 29727
elsif (not $DBD::SQLite::__DBIO_CHECK_dbd_can_bind_bigint_values) { } 0 29724 29724
309 carp_unique( sprintf (
310 "Non-integer value supplied for column '%s' despite the integer datatype",
311 3 ▶ $bind->[$i][0]{dbic_colname} || "# $i"
Condition50%1/2
$bind->[$i][0]{'dbic_colname'} || "# $i"
l!l
30
MC/DC0%0/1
$bind->[$i][0]{'dbic_colname'} || "# $i"
$bind->[$i][0]{'dbic_colname'}
Truth table50%1/2
A
A= $bind->[$i][0]{'dbic_colname'}
Aresult
11
00
312 ) );
313 3 undef $bindattrs->[$i];
314 }
315 elsif (
316 ! $DBD::SQLite::__DBIO_CHECK_dbd_can_bind_bigint_values
317 ) {
318 0 ▶ if ($bind->[$i][1] > 0x7fff_ffff or $bind->[$i][1] < -0x8000_0000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bind->[$i][1] > 2147483647 or $bind->[$i][1] < -2147483648) { } 0 0 0
Condition0%0/3
$bind->[$i][1] > 2147483647 or $bind->[$i][1] < '-2147483648'
l!l&&r!l&&!r
000
MC/DC0%0/2
$bind->[$i][1] > 2147483647 or $bind->[$i][1] < '-2147483648'
$bind->[$i][1] > 2147483647 $bind->[$i][1] < '-2147483648'
Truth table0%0/3
A or B
A= $bind->[$i][1] > 2147483647B= $bind->[$i][1] < '-2147483648'
ABresult
1X1
011
000
319 carp_unique( sprintf (
320 "An integer value occupying more than 32 bits was supplied for column '%s' "
321 . 'which your version of DBD::SQLite (%s) can not bind properly so DBIO '
322 . 'will treat it as a string instead, consider upgrading to at least '
323 . 'DBD::SQLite version 1.37',
324 0 ▶ $bind->[$i][0]{dbic_colname} || "# $i",
Condition0%0/2
$bind->[$i][0]{'dbic_colname'} || "# $i"
l!l
00
MC/DC0%0/1
$bind->[$i][0]{'dbic_colname'} || "# $i"
$bind->[$i][0]{'dbic_colname'}
Truth table0%0/2
A
A= $bind->[$i][0]{'dbic_colname'}
Aresult
11
00
325 DBD::SQLite->VERSION,
326 ) );
327 0 undef $bindattrs->[$i];
328 }
329 else {
330 0 $bindattrs->[$i] = DBI::SQL_INTEGER()
331 }
332 }
333 }
334 }
335 ▶ elsif (
Condition91%11/12
defined $bind->[$i][1] and not ref $bind->[$i][1] and &looks_like_number($bind->[$i][1]) and !$bind->[$i][0]{"dbic_colname"} || $bind->[$i][0]{"dbic_colname"} =~ /\(/
!ll&&!rl&&r
10087115934
defined $bind->[$i][1] and not ref $bind->[$i][1] and &looks_like_number($bind->[$i][1])
!ll&&!rl&&r
9899891193
defined $bind->[$i][1] and not ref $bind->[$i][1]
!ll&&!rl&&r
554311182
not $bind->[$i][0]{"dbic_colname"} or $bind->[$i][0]{"dbic_colname"} =~ /\(/
l!l&&r!l&&!r
3401159
MC/DC80%4/5
defined $bind->[$i][1] and not ref $bind->[$i][1] and &looks_like_number($bind->[$i][1]) and !$bind->[$i][0]{"dbic_colname"} || $bind->[$i][0]{"dbic_colname"} =~ /\(/
defined $bind->[$i][1] not ref $bind->[$i][1] &looks_like_number($bind->[$i][1]) not $bind->[$i][0]{"dbic_colname"} $bind->[$i][0]{"dbic_colname"} =~ /\(/
Truth table83%5/6
A and not(B) and C and not(D) or E
A= defined $bind->[$i][1]B= not ref $bind->[$i][1]C= &looks_like_number($bind->[$i][1])D= not $bind->[$i][0]{"dbic_colname"}E= $bind->[$i][0]{"dbic_colname"} =~ /\(/
ABCDEresult
0XXXX0
10XXX0
110XX0
111000
1111X1
111011
336 # SQLite binds all parameters as TEXT by default (sqlite3_bind_text).
337 # This causes cross-type comparison failures: INTEGER < TEXT is always
338 # true in SQLite's type sort order, so e.g. COUNT(x) > ? with a text
339 # bind always returns FALSE. Fix by hinting numeric values as SQL_INTEGER
340 # when no column-based type info is available.
341 #
342 # Apply to:
343 # - Anonymous binds (no dbic_colname) from literal SQL
344 # - Expression binds (colname contains parens, e.g. 'COUNT(x)')
345 # Do NOT apply to plain column aliases (e.g. 'newest_cd_year') as
346 # these may reference TEXT-typed columns containing numeric strings.
347 defined $bind->[$i][1]
348 and
349 ! ref $bind->[$i][1]
350 and
351 looks_like_number($bind->[$i][1])
352 and
353 (
354 ! $bind->[$i][0]{dbic_colname}
355 or
356 $bind->[$i][0]{dbic_colname} =~ /\(/ # expression (COUNT, MAX, etc.)
357 )
358 ) {
359 34 $bindattrs->[$i] = DBI::SQL_INTEGER();
360 }
361  
362 }
363  
364 17591 return $bindattrs;
365 }
366  
367 2 ▶sub with_deferred_fk_checks {
Subroutine with_deferred_fk_checks: called CC=1 SCAR=0.0
Pod documented
368 2 my ($self, $sub) = @_;
369  
370 2 my $txn_scope_guard = $self->txn_scope_guard;
371  
372 2 $self->_do_query('PRAGMA defer_foreign_keys = ON');
373  
374 2 ▶ return preserve_context { $sub->() } after => sub { $txn_scope_guard->commit };
Subroutine __ANON__: called CC=1 SCAR=0.0
375 }
376  
377  
378 0 ▶sub connect_call_use_foreign_keys {
Subroutine connect_call_use_foreign_keys: not called CC=1 SCAR=6.9
Pod documented
379 0 my $self = shift;
380  
381 0 $self->_do_query(
382 'PRAGMA foreign_keys = ON'
383 );
384 }
385  
386  
387  
388 214 1;
389  
390 __END__
391  
392 =pod
393  
394 =encoding UTF-8
395  
396 =head1 NAME
397  
398 DBIO::SQLite::Storage - SQLite storage driver for DBIO
399  
400 =head1 VERSION
401  
402 version 0.900001
403  
404 =head1 DESCRIPTION
405  
406 SQLite storage driver for DBIO. Extends L<DBIO::Storage::DBI> with
407 SQLite-specific behavior including savepoint support, foreign key pragma
408 helpers, database file backup, and workarounds for known L<DBD::SQLite> bugs.
409  
410 This class is registered as the driver for C<SQLite> and is loaded
411 automatically when connecting to a SQLite DSN. It uses
412 L<DBIO::SQLite::SQLMaker> for SQL generation, C<LimitOffset> pagination,
413 double-quote identifiers, and L<DateTime::Format::SQLite> for datetime
414 parsing.
415  
416 =head1 METHODS
417  
418 =head2 _dbh_columns_info_for
419  
420 Override the parent's C<column_info> lookup. DBD::SQLite does not
421 implement C<NULLABLE> correctly -- PRIMARY KEY columns and columns
422 constrained by C<UNIQUE ... NOT NULL> come back as nullable. This
423 override layers the truth from C<PRAGMA table_info> (C<notnull>) on
424 top of the parent method's result so that C<is_nullable> is correct
425 for downstream consumers like L<DBIO::Result/columns_info_for>.
426  
427 =head2 backup
428  
429 my $backupfile = $storage->backup($dir);
430 my $backupfile = $storage->backup; # defaults to './'
431  
432 Copy the live SQLite database file to C<$dir>. The backup filename includes
433 a timestamp prefix. Returns the full path to the backup file.
434  
435 Throws an exception if the database filename cannot be determined from the
436 DSN or if the file copy fails.
437  
438 =head2 bind_attribute_by_data_type
439  
440 Returns C<DBI::SQL_BIGINT> for any integer-family column type (C<int>,
441 C<integer>, C<tinyint>, C<smallint>, C<mediumint>, C<bigint>, C<int1>,
442 C<int2>, C<int4>, C<int8>). Returns C<undef> for all other types, deferring
443 to the default binding behavior.
444  
445 SQLite stores all numeric values as dynamically-sized integers up to 8 bytes,
446 so binding everything as C<SQL_BIGINT> is safe and avoids unnecessary type
447 coercions.
448  
449 =head2 with_deferred_fk_checks
450  
451 $storage->with_deferred_fk_checks(sub { ... });
452  
453 Execute C<$sub> inside a transaction with C<PRAGMA defer_foreign_keys = ON>.
454 Foreign key constraint checking is deferred until the transaction commits,
455 allowing bulk inserts or other operations that temporarily violate referential
456 integrity within the same transaction. The transaction is committed
457 automatically after C<$sub> returns.
458  
459 =head2 connect_call_use_foreign_keys
460  
461 A connect-time callback that executes C<PRAGMA foreign_keys = ON>. SQLite
462 does not enforce foreign key constraints by default. Enable it by passing
463 C<on_connect_call =E<gt> 'use_foreign_keys'> in your connection options:
464  
465 $schema->connect(
466 'dbi:SQLite:db/app.db', '', '',
467 { on_connect_call => 'use_foreign_keys' },
468 );
469  
470 =seealso
471  
472 =over
473  
474 =item * L<DBIO::SQLite> - Schema component that activates this storage
475  
476 =item * L<DBIO::SQLite::SQLMaker> - SQL generation for SQLite
477  
478 =item * L<DBIO::Storage::DBI> - Base DBI storage class
479  
480 =back
481  
482 =head1 AUTHOR
483  
484 DBIO & DBIx::Class Authors
485  
486 =head1 COPYRIGHT AND LICENSE
487  
488 Copyright (C) 2026 DBIO Authors
489 Portions Copyright (C) 2005-2025 DBIx::Class Authors
490 Based on DBIx::Class, heavily modified.
491  
492 This is free software; you can redistribute it and/or modify it under
493 the same terms as the Perl 5 programming language system itself.
494  
495 =cut
« SQLite/DDL.pm ↑ Summary SQLite/Diff.pm »