Commit | Line | Data |
843f8ecd |
1 | package DBIx::Class::Storage::DBI::SQLite; |
2 | |
3 | use strict; |
4 | use warnings; |
2ad62d97 |
5 | |
6 | use base qw/DBIx::Class::Storage::DBI/; |
7 | use mro 'c3'; |
8 | |
b5ce6748 |
9 | use SQL::Abstract 'is_plain_value'; |
ddcc02d1 |
10 | use DBIx::Class::_Util qw(modver_gt_or_eq sigwarn_silencer dbic_internal_try); |
632d1e0f |
11 | use DBIx::Class::Carp; |
8d1fb3e2 |
12 | use Try::Tiny; |
632d1e0f |
13 | use namespace::clean; |
14 | |
d5dedbd6 |
15 | __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::SQLite'); |
6a247f33 |
16 | __PACKAGE__->sql_limit_dialect ('LimitOffset'); |
2b8cc2f2 |
17 | __PACKAGE__->sql_quote_char ('"'); |
6f7a118e |
18 | __PACKAGE__->datetime_parser_type ('DateTime::Format::SQLite'); |
09cedb88 |
19 | |
2fa97c7d |
20 | =head1 NAME |
21 | |
22 | DBIx::Class::Storage::DBI::SQLite - Automatic primary key class for SQLite |
23 | |
24 | =head1 SYNOPSIS |
25 | |
26 | # In your table classes |
27 | use base 'DBIx::Class::Core'; |
28 | __PACKAGE__->set_primary_key('id'); |
29 | |
30 | =head1 DESCRIPTION |
31 | |
32 | This class implements autoincrements for SQLite. |
33 | |
75a1d824 |
34 | =head2 Known Issues |
35 | |
36 | =over |
37 | |
38 | =item RT79576 |
39 | |
40 | NOTE - This section applies to you only if ALL of these are true: |
41 | |
42 | * You are or were using DBD::SQLite with a version lesser than 1.38_01 |
43 | |
44 | * You are or were using DBIx::Class versions between 0.08191 and 0.08209 |
45 | (inclusive) or between 0.08240-TRIAL and 0.08242-TRIAL (also inclusive) |
46 | |
47 | * You use objects with overloaded stringification and are feeding them |
48 | to DBIC CRUD methods directly |
49 | |
50 | An unfortunate chain of events led to DBIx::Class silently hitting the problem |
51 | described in L<RT#79576|https://rt.cpan.org/Public/Bug/Display.html?id=79576>. |
52 | |
53 | In order to trigger the bug condition one needs to supply B<more than one> |
4a0eed52 |
54 | bind value that is an object with overloaded stringification (numification |
75a1d824 |
55 | is not relevant, only stringification is). When this is the case the internal |
56 | DBIx::Class call to C<< $sth->bind_param >> would be executed in a way that |
57 | triggers the above-mentioned DBD::SQLite bug. As a result all the logs and |
58 | tracers will contain the expected values, however SQLite will receive B<all> |
59 | these bind positions being set to the value of the B<last> supplied |
60 | stringifiable object. |
61 | |
62 | Even if you upgrade DBIx::Class (which works around the bug starting from |
63 | version 0.08210) you may still have corrupted/incorrect data in your database. |
cff17b97 |
64 | DBIx::Class warned about this condition for several years, hoping to give |
65 | anyone affected sufficient notice of the potential issues. The warning was |
66 | removed in version 0.082900. |
75a1d824 |
67 | |
68 | =back |
69 | |
2fa97c7d |
70 | =head1 METHODS |
71 | |
72 | =cut |
73 | |
357eb92c |
74 | sub backup { |
75 | |
76 | require File::Spec; |
77 | require File::Copy; |
78 | require POSIX; |
79 | |
8795fefb |
80 | my ($self, $dir) = @_; |
81 | $dir ||= './'; |
c9d2e0a2 |
82 | |
83 | ## Where is the db file? |
12c9beea |
84 | my $dsn = $self->_dbi_connect_info()->[0]; |
c9d2e0a2 |
85 | |
86 | my $dbname = $1 if($dsn =~ /dbname=([^;]+)/); |
87 | if(!$dbname) |
88 | { |
89 | $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i); |
90 | } |
357eb92c |
91 | $self->throw_exception("Cannot determine name of SQLite db file") |
c9d2e0a2 |
92 | if(!$dbname || !-f $dbname); |
93 | |
94 | # print "Found database: $dbname\n"; |
79923569 |
95 | # my $dbfile = file($dbname); |
8795fefb |
96 | my ($vol, $dbdir, $file) = File::Spec->splitpath($dbname); |
79923569 |
97 | # my $file = $dbfile->basename(); |
357eb92c |
98 | $file = POSIX::strftime("%Y-%m-%d-%H_%M_%S", localtime()) . $file; |
c9d2e0a2 |
99 | $file = "B$file" while(-f $file); |
8795fefb |
100 | |
101 | mkdir($dir) unless -f $dir; |
102 | my $backupfile = File::Spec->catfile($dir, $file); |
103 | |
357eb92c |
104 | my $res = File::Copy::copy($dbname, $backupfile); |
c9d2e0a2 |
105 | $self->throw_exception("Backup failed! ($!)") if(!$res); |
106 | |
8795fefb |
107 | return $backupfile; |
c9d2e0a2 |
108 | } |
109 | |
86a51471 |
110 | sub _exec_svp_begin { |
111 | my ($self, $name) = @_; |
112 | |
113 | $self->_dbh->do("SAVEPOINT $name"); |
114 | } |
115 | |
116 | sub _exec_svp_release { |
117 | my ($self, $name) = @_; |
118 | |
119 | $self->_dbh->do("RELEASE SAVEPOINT $name"); |
120 | } |
121 | |
122 | sub _exec_svp_rollback { |
123 | my ($self, $name) = @_; |
124 | |
398215b1 |
125 | $self->_dbh->do("ROLLBACK TO SAVEPOINT $name"); |
126 | } |
127 | |
128 | # older SQLite has issues here too - both of these are in fact |
129 | # completely benign warnings (or at least so say the tests) |
130 | sub _exec_txn_rollback { |
131 | local $SIG{__WARN__} = sigwarn_silencer( qr/rollback ineffective/ ) |
132 | unless $DBD::SQLite::__DBIC_TXN_SYNC_SANE__; |
133 | |
134 | shift->next::method(@_); |
135 | } |
136 | |
137 | sub _exec_txn_commit { |
138 | local $SIG{__WARN__} = sigwarn_silencer( qr/commit ineffective/ ) |
139 | unless $DBD::SQLite::__DBIC_TXN_SYNC_SANE__; |
86a51471 |
140 | |
398215b1 |
141 | shift->next::method(@_); |
86a51471 |
142 | } |
143 | |
8d1fb3e2 |
144 | sub _ping { |
145 | my $self = shift; |
2aeb3c7f |
146 | |
147 | # Be extremely careful what we do here. SQLite is notoriously bad at |
148 | # synchronizing its internal transaction state with {AutoCommit} |
149 | # https://metacpan.org/source/ADAMK/DBD-SQLite-1.37/lib/DBD/SQLite.pm#L921 |
150 | # There is a function http://www.sqlite.org/c3ref/get_autocommit.html |
151 | # but DBD::SQLite does not expose it (nor does it seem to properly use it) |
152 | |
153 | # Therefore only execute a "ping" when we have no other choice *AND* |
154 | # scrutinize the thrown exceptions to make sure we are where we think we are |
155 | my $dbh = $self->_dbh or return undef; |
156 | return undef unless $dbh->FETCH('Active'); |
157 | return undef unless $dbh->ping; |
158 | |
ab0b0a09 |
159 | my $ping_fail; |
160 | |
161 | # older DBD::SQLite does not properly synchronize commit state between |
162 | # the libsqlite and the $dbh |
163 | unless (defined $DBD::SQLite::__DBIC_TXN_SYNC_SANE__) { |
b1dbf716 |
164 | $DBD::SQLite::__DBIC_TXN_SYNC_SANE__ = modver_gt_or_eq('DBD::SQLite', '1.38_02'); |
ab0b0a09 |
165 | } |
2aeb3c7f |
166 | |
ab0b0a09 |
167 | # fallback to travesty |
168 | unless ($DBD::SQLite::__DBIC_TXN_SYNC_SANE__) { |
169 | # since we do not have access to sqlite3_get_autocommit(), do a trick |
170 | # to attempt to *safely* determine what state are we *actually* in. |
ddcc02d1 |
171 | |
172 | my $really_not_in_txn; |
173 | |
174 | # not assigning RV directly to env above, because this causes a bizarre |
175 | # leak of the catch{} cref on older perls... wtf |
176 | dbic_internal_try { |
ab0b0a09 |
177 | |
178 | # older versions of DBD::SQLite do not properly detect multiline BEGIN/COMMIT |
179 | # statements to adjust their {AutoCommit} state. Hence use such a statement |
180 | # pair here as well, in order to escape from poking {AutoCommit} needlessly |
181 | # https://rt.cpan.org/Public/Bug/Display.html?id=80087 |
ddcc02d1 |
182 | # |
183 | # will fail instantly if already in a txn |
184 | $dbh->do("-- multiline\nBEGIN"); |
185 | $dbh->do("-- multiline\nCOMMIT"); |
186 | |
187 | $really_not_in_txn = 1; |
188 | } |
189 | catch { |
190 | $really_not_in_txn = ( $_[0] =~ qr/transaction within a transaction/ |
191 | ? 0 |
192 | : undef |
193 | ); |
2aeb3c7f |
194 | }; |
2aeb3c7f |
195 | |
ab0b0a09 |
196 | # if we were unable to determine this - we may very well be dead |
197 | if (not defined $really_not_in_txn) { |
198 | $ping_fail = 1; |
199 | } |
200 | # check the AC sync-state |
201 | elsif ($really_not_in_txn xor $dbh->{AutoCommit}) { |
202 | carp_unique (sprintf |
203 | 'Internal transaction state of handle %s (apparently %s a transaction) does not seem to ' |
204 | . 'match its AutoCommit attribute setting of %s - this is an indication of a ' |
205 | . 'potentially serious bug in your transaction handling logic', |
206 | $dbh, |
207 | $really_not_in_txn ? 'NOT in' : 'in', |
208 | $dbh->{AutoCommit} ? 'TRUE' : 'FALSE', |
209 | ); |
210 | |
211 | # it is too dangerous to execute anything else in this state |
212 | # assume everything works (safer - worst case scenario next statement throws) |
213 | return 1; |
214 | } |
2aeb3c7f |
215 | } |
216 | |
ab0b0a09 |
217 | # do the actual test and return on no failure |
ddcc02d1 |
218 | ( $ping_fail ||= ! dbic_internal_try { $dbh->do('SELECT * FROM sqlite_master LIMIT 1'); 1 } ) |
ab0b0a09 |
219 | or return 1; # the actual RV of _ping() |
220 | |
221 | # ping failed (or so it seems) - need to do some cleanup |
222 | # it is possible to have a proper "connection", and have "ping" return |
223 | # false anyway (e.g. corrupted file). In such cases DBD::SQLite still |
224 | # keeps the actual file handle open. We don't really want this to happen, |
225 | # so force-close the handle via DBI itself |
226 | # |
ddcc02d1 |
227 | dbic_internal_try { $dbh->disconnect }; # if it fails - it fails |
ab0b0a09 |
228 | undef; # the actual RV of _ping() |
8d1fb3e2 |
229 | } |
230 | |
2361982d |
231 | sub deployment_statements { |
96736321 |
232 | my $self = shift; |
2361982d |
233 | my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_; |
234 | |
235 | $sqltargs ||= {}; |
236 | |
96736321 |
237 | if ( |
238 | ! exists $sqltargs->{producer_args}{sqlite_version} |
239 | and |
240 | my $dver = $self->_server_info->{normalized_dbms_version} |
241 | ) { |
242 | $sqltargs->{producer_args}{sqlite_version} = $dver; |
6d766626 |
243 | } |
2361982d |
244 | |
245 | $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest); |
246 | } |
247 | |
0e773352 |
248 | sub bind_attribute_by_data_type { |
04ab4eb1 |
249 | |
250 | # According to http://www.sqlite.org/datatype3.html#storageclasses |
251 | # all numeric types are dynamically allocated up to 8 bytes per |
252 | # individual value |
253 | # Thus it should be safe and non-wasteful to bind everything as |
254 | # SQL_BIGINT and have SQLite deal with storage/comparisons however |
255 | # it deems correct |
256 | $_[1] =~ /^ (?: int(?:[1248]|eger)? | (?:tiny|small|medium|big)int ) $/ix |
257 | ? DBI::SQL_BIGINT() |
0e773352 |
258 | : undef |
259 | ; |
260 | } |
261 | |
04ab4eb1 |
262 | # FIXME - what the flying fuck... work around RT#76395 |
263 | # DBD::SQLite warns on binding >32 bit values with 32 bit IVs |
264 | sub _dbh_execute { |
1363f0f5 |
265 | if ( |
00882d2c |
266 | ( |
267 | DBIx::Class::_ENV_::IV_SIZE < 8 |
268 | or |
269 | DBIx::Class::_ENV_::OS_NAME eq 'MSWin32' |
270 | ) |
1363f0f5 |
271 | and |
272 | ! defined $DBD::SQLite::__DBIC_CHECK_dbd_mishandles_bound_BIGINT |
273 | ) { |
274 | $DBD::SQLite::__DBIC_CHECK_dbd_mishandles_bound_BIGINT = ( |
275 | modver_gt_or_eq('DBD::SQLite', '1.37') |
276 | ) ? 1 : 0; |
04ab4eb1 |
277 | } |
278 | |
1363f0f5 |
279 | local $SIG{__WARN__} = sigwarn_silencer( qr/ |
280 | \Qdatatype mismatch: bind\E \s (?: |
281 | param \s+ \( \d+ \) \s+ [-+]? \d+ (?: \. 0*)? \Q as integer\E |
282 | | |
283 | \d+ \s type \s @{[ DBI::SQL_BIGINT() ]} \s as \s [-+]? \d+ (?: \. 0*)? |
284 | ) |
00882d2c |
285 | /x ) if ( |
286 | ( |
287 | DBIx::Class::_ENV_::IV_SIZE < 8 |
288 | or |
289 | DBIx::Class::_ENV_::OS_NAME eq 'MSWin32' |
290 | ) |
291 | and |
292 | $DBD::SQLite::__DBIC_CHECK_dbd_mishandles_bound_BIGINT |
293 | ); |
1363f0f5 |
294 | |
04ab4eb1 |
295 | shift->next::method(@_); |
296 | } |
297 | |
632d1e0f |
298 | # DBD::SQLite (at least up to version 1.31 has a bug where it will |
4a0eed52 |
299 | # non-fatally numify a string value bound as an integer, resulting |
632d1e0f |
300 | # in insertions of '0' into supposed-to-be-numeric fields |
301 | # Since this can result in severe data inconsistency, remove the |
4a0eed52 |
302 | # bind attr if such a situation is detected |
632d1e0f |
303 | # |
304 | # FIXME - when a DBD::SQLite version is released that eventually fixes |
4a0eed52 |
305 | # this situation (somehow) - no-op this override once a proper DBD |
632d1e0f |
306 | # version is detected |
307 | sub _dbi_attrs_for_bind { |
308 | my ($self, $ident, $bind) = @_; |
75a1d824 |
309 | |
632d1e0f |
310 | my $bindattrs = $self->next::method($ident, $bind); |
311 | |
04ab4eb1 |
312 | if (! defined $DBD::SQLite::__DBIC_CHECK_dbd_can_bind_bigint_values) { |
215102b9 |
313 | $DBD::SQLite::__DBIC_CHECK_dbd_can_bind_bigint_values |
314 | = modver_gt_or_eq('DBD::SQLite', '1.37') ? 1 : 0; |
04ab4eb1 |
315 | } |
316 | |
49f7b6c7 |
317 | for my $i (0.. $#$bindattrs) { |
632d1e0f |
318 | if ( |
49f7b6c7 |
319 | defined $bindattrs->[$i] |
632d1e0f |
320 | and |
49f7b6c7 |
321 | defined $bind->[$i][1] |
632d1e0f |
322 | and |
d830d9f4 |
323 | grep { $bindattrs->[$i] eq $_ } ( |
324 | DBI::SQL_INTEGER(), DBI::SQL_TINYINT(), DBI::SQL_SMALLINT(), DBI::SQL_BIGINT() |
325 | ) |
632d1e0f |
326 | ) { |
04ab4eb1 |
327 | if ( $bind->[$i][1] !~ /^ [\+\-]? [0-9]+ (?: \. 0* )? $/x ) { |
328 | carp_unique( sprintf ( |
329 | "Non-integer value supplied for column '%s' despite the integer datatype", |
330 | $bind->[$i][0]{dbic_colname} || "# $i" |
331 | ) ); |
332 | undef $bindattrs->[$i]; |
333 | } |
334 | elsif ( |
335 | ! $DBD::SQLite::__DBIC_CHECK_dbd_can_bind_bigint_values |
215102b9 |
336 | ) { |
04ab4eb1 |
337 | # unsigned 32 bit ints have a range of −2,147,483,648 to 2,147,483,647 |
338 | # alternatively expressed as the hexadecimal numbers below |
339 | # the comparison math will come out right regardless of ivsize, since |
340 | # we are operating within 31 bits |
341 | # P.S. 31 because one bit is lost for the sign |
215102b9 |
342 | if ($bind->[$i][1] > 0x7fff_ffff or $bind->[$i][1] < -0x8000_0000) { |
343 | carp_unique( sprintf ( |
344 | "An integer value occupying more than 32 bits was supplied for column '%s' " |
345 | . 'which your version of DBD::SQLite (%s) can not bind properly so DBIC ' |
346 | . 'will treat it as a string instead, consider upgrading to at least ' |
347 | . 'DBD::SQLite version 1.37', |
348 | $bind->[$i][0]{dbic_colname} || "# $i", |
349 | DBD::SQLite->VERSION, |
350 | ) ); |
351 | undef $bindattrs->[$i]; |
352 | } |
353 | else { |
354 | $bindattrs->[$i] = DBI::SQL_INTEGER() |
355 | } |
04ab4eb1 |
356 | } |
632d1e0f |
357 | } |
358 | } |
359 | |
360 | return $bindattrs; |
361 | } |
362 | |
732e4282 |
363 | =head2 connect_call_use_foreign_keys |
364 | |
365 | Used as: |
366 | |
367 | on_connect_call => 'use_foreign_keys' |
368 | |
8384a713 |
369 | In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to turn on foreign key |
370 | (including cascading) support for recent versions of SQLite and L<DBD::SQLite>. |
732e4282 |
371 | |
372 | Executes: |
373 | |
8273e845 |
374 | PRAGMA foreign_keys = ON |
732e4282 |
375 | |
376 | See L<http://www.sqlite.org/foreignkeys.html> for more information. |
377 | |
378 | =cut |
379 | |
380 | sub connect_call_use_foreign_keys { |
381 | my $self = shift; |
382 | |
383 | $self->_do_query( |
384 | 'PRAGMA foreign_keys = ON' |
385 | ); |
386 | } |
387 | |
a2bd3796 |
388 | =head1 FURTHER QUESTIONS? |
843f8ecd |
389 | |
a2bd3796 |
390 | Check the list of L<additional DBIC resources|DBIx::Class/GETTING HELP/SUPPORT>. |
843f8ecd |
391 | |
a2bd3796 |
392 | =head1 COPYRIGHT AND LICENSE |
843f8ecd |
393 | |
a2bd3796 |
394 | This module is free software L<copyright|DBIx::Class/COPYRIGHT AND LICENSE> |
395 | by the L<DBIx::Class (DBIC) authors|DBIx::Class/AUTHORS>. You can |
396 | redistribute it and/or modify it under the same terms as the |
397 | L<DBIx::Class library|DBIx::Class/COPYRIGHT AND LICENSE>. |
843f8ecd |
398 | |
399 | =cut |
a2bd3796 |
400 | |
401 | 1; |