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 | |
632d1e0f |
9 | use DBIx::Class::Carp; |
8d1fb3e2 |
10 | use Try::Tiny; |
632d1e0f |
11 | use namespace::clean; |
12 | |
d5dedbd6 |
13 | __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::SQLite'); |
6a247f33 |
14 | __PACKAGE__->sql_limit_dialect ('LimitOffset'); |
2b8cc2f2 |
15 | __PACKAGE__->sql_quote_char ('"'); |
6f7a118e |
16 | __PACKAGE__->datetime_parser_type ('DateTime::Format::SQLite'); |
09cedb88 |
17 | |
2fa97c7d |
18 | =head1 NAME |
19 | |
20 | DBIx::Class::Storage::DBI::SQLite - Automatic primary key class for SQLite |
21 | |
22 | =head1 SYNOPSIS |
23 | |
24 | # In your table classes |
25 | use base 'DBIx::Class::Core'; |
26 | __PACKAGE__->set_primary_key('id'); |
27 | |
28 | =head1 DESCRIPTION |
29 | |
30 | This class implements autoincrements for SQLite. |
31 | |
32 | =head1 METHODS |
33 | |
34 | =cut |
35 | |
357eb92c |
36 | sub backup { |
37 | |
38 | require File::Spec; |
39 | require File::Copy; |
40 | require POSIX; |
41 | |
8795fefb |
42 | my ($self, $dir) = @_; |
43 | $dir ||= './'; |
c9d2e0a2 |
44 | |
45 | ## Where is the db file? |
12c9beea |
46 | my $dsn = $self->_dbi_connect_info()->[0]; |
c9d2e0a2 |
47 | |
48 | my $dbname = $1 if($dsn =~ /dbname=([^;]+)/); |
49 | if(!$dbname) |
50 | { |
51 | $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i); |
52 | } |
357eb92c |
53 | $self->throw_exception("Cannot determine name of SQLite db file") |
c9d2e0a2 |
54 | if(!$dbname || !-f $dbname); |
55 | |
56 | # print "Found database: $dbname\n"; |
79923569 |
57 | # my $dbfile = file($dbname); |
8795fefb |
58 | my ($vol, $dbdir, $file) = File::Spec->splitpath($dbname); |
79923569 |
59 | # my $file = $dbfile->basename(); |
357eb92c |
60 | $file = POSIX::strftime("%Y-%m-%d-%H_%M_%S", localtime()) . $file; |
c9d2e0a2 |
61 | $file = "B$file" while(-f $file); |
8795fefb |
62 | |
63 | mkdir($dir) unless -f $dir; |
64 | my $backupfile = File::Spec->catfile($dir, $file); |
65 | |
357eb92c |
66 | my $res = File::Copy::copy($dbname, $backupfile); |
c9d2e0a2 |
67 | $self->throw_exception("Backup failed! ($!)") if(!$res); |
68 | |
8795fefb |
69 | return $backupfile; |
c9d2e0a2 |
70 | } |
71 | |
86a51471 |
72 | sub _exec_svp_begin { |
73 | my ($self, $name) = @_; |
74 | |
75 | $self->_dbh->do("SAVEPOINT $name"); |
76 | } |
77 | |
78 | sub _exec_svp_release { |
79 | my ($self, $name) = @_; |
80 | |
81 | $self->_dbh->do("RELEASE SAVEPOINT $name"); |
82 | } |
83 | |
84 | sub _exec_svp_rollback { |
85 | my ($self, $name) = @_; |
86 | |
87 | # For some reason this statement changes the value of $dbh->{AutoCommit}, so |
88 | # we localize it here to preserve the original value. |
89 | local $self->_dbh->{AutoCommit} = $self->_dbh->{AutoCommit}; |
90 | |
91 | $self->_dbh->do("ROLLBACK TRANSACTION TO SAVEPOINT $name"); |
92 | } |
93 | |
8d1fb3e2 |
94 | sub _ping { |
95 | my $self = shift; |
2aeb3c7f |
96 | |
97 | # Be extremely careful what we do here. SQLite is notoriously bad at |
98 | # synchronizing its internal transaction state with {AutoCommit} |
99 | # https://metacpan.org/source/ADAMK/DBD-SQLite-1.37/lib/DBD/SQLite.pm#L921 |
100 | # There is a function http://www.sqlite.org/c3ref/get_autocommit.html |
101 | # but DBD::SQLite does not expose it (nor does it seem to properly use it) |
102 | |
103 | # Therefore only execute a "ping" when we have no other choice *AND* |
104 | # scrutinize the thrown exceptions to make sure we are where we think we are |
105 | my $dbh = $self->_dbh or return undef; |
106 | return undef unless $dbh->FETCH('Active'); |
107 | return undef unless $dbh->ping; |
108 | |
109 | # since we do not have access to sqlite3_get_autocommit(), do a trick |
110 | # to attempt to *safely* determine what state are we *actually* in. |
111 | # FIXME |
112 | # also using T::T here leads to bizarre leaks - will figure it out later |
113 | my $really_not_in_txn = do { |
114 | local $@; |
115 | |
116 | # older versions of DBD::SQLite do not properly detect multiline BEGIN/COMMIT |
117 | # statements to adjust their {AutoCommit} state. Hence use such a statement |
118 | # pair here as well, in order to escape from poking {AutoCommit} needlessly |
119 | # https://rt.cpan.org/Public/Bug/Display.html?id=80087 |
120 | eval { |
121 | # will fail instantly if already in a txn |
122 | $dbh->do("-- multiline\nBEGIN"); |
123 | $dbh->do("-- multiline\nCOMMIT"); |
124 | 1; |
125 | } or do { |
126 | ($@ =~ /transaction within a transaction/) |
127 | ? 0 |
128 | : undef |
129 | ; |
130 | }; |
131 | }; |
132 | |
133 | my $ping_fail; |
134 | |
135 | # if we were unable to determine this - we may very well be dead |
136 | if (not defined $really_not_in_txn) { |
137 | $ping_fail = 1; |
138 | } |
139 | # check the AC sync-state |
140 | elsif ($really_not_in_txn xor $dbh->{AutoCommit}) { |
141 | carp_unique (sprintf |
142 | 'Internal transaction state of handle %s (apparently %s a transaction) does not seem to ' |
143 | . 'match its AutoCommit attribute setting of %s - this is an indication of a ' |
144 | . 'potentially serious bug in your transaction handling logic', |
145 | $dbh, |
146 | $really_not_in_txn ? 'NOT in' : 'in', |
147 | $dbh->{AutoCommit} ? 'TRUE' : 'FALSE', |
148 | ); |
149 | |
150 | # it is too dangerous to execute anything else in this state |
151 | # assume everything works (safer - worst case scenario next statement throws) |
152 | return 1; |
153 | } |
154 | else { |
155 | # do the actual test |
156 | $ping_fail = ! try { $dbh->do('SELECT * FROM sqlite_master LIMIT 1'); 1 }; |
157 | } |
158 | |
159 | if ($ping_fail) { |
160 | # it is possible to have a proper "connection", and have "ping" return |
161 | # false anyway (e.g. corrupted file). In such cases DBD::SQLite still |
162 | # keeps the actual file handle open. We don't really want this to happen, |
163 | # so force-close the handle via DBI itself |
164 | # |
165 | local $@; # so that we do not clober the real error as set above |
166 | eval { $dbh->disconnect }; # if it fails - it fails |
167 | return undef # the actual RV of _ping() |
168 | } |
169 | else { |
170 | return 1; |
171 | } |
8d1fb3e2 |
172 | } |
173 | |
2361982d |
174 | sub deployment_statements { |
96736321 |
175 | my $self = shift; |
2361982d |
176 | my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_; |
177 | |
178 | $sqltargs ||= {}; |
179 | |
96736321 |
180 | if ( |
181 | ! exists $sqltargs->{producer_args}{sqlite_version} |
182 | and |
183 | my $dver = $self->_server_info->{normalized_dbms_version} |
184 | ) { |
185 | $sqltargs->{producer_args}{sqlite_version} = $dver; |
6d766626 |
186 | } |
2361982d |
187 | |
188 | $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest); |
189 | } |
190 | |
0e773352 |
191 | sub bind_attribute_by_data_type { |
67b35a45 |
192 | $_[1] =~ /^ (?: int(?:eger)? | (?:tiny|small|medium)int ) $/ix |
ad7c50fc |
193 | ? DBI::SQL_INTEGER() |
0e773352 |
194 | : undef |
195 | ; |
196 | } |
197 | |
632d1e0f |
198 | # DBD::SQLite (at least up to version 1.31 has a bug where it will |
199 | # non-fatally nummify a string value bound as an integer, resulting |
200 | # in insertions of '0' into supposed-to-be-numeric fields |
201 | # Since this can result in severe data inconsistency, remove the |
202 | # bind attr if such a sitation is detected |
203 | # |
204 | # FIXME - when a DBD::SQLite version is released that eventually fixes |
205 | # this sutiation (somehow) - no-op this override once a proper DBD |
206 | # version is detected |
207 | sub _dbi_attrs_for_bind { |
208 | my ($self, $ident, $bind) = @_; |
209 | my $bindattrs = $self->next::method($ident, $bind); |
210 | |
211 | for (0.. $#$bindattrs) { |
212 | if ( |
213 | defined $bindattrs->[$_] |
214 | and |
215 | defined $bind->[$_][1] |
216 | and |
217 | $bindattrs->[$_] eq DBI::SQL_INTEGER() |
218 | and |
445bc0cd |
219 | $bind->[$_][1] !~ /^ [\+\-]? [0-9]+ (?: \. 0* )? $/x |
632d1e0f |
220 | ) { |
221 | carp_unique( sprintf ( |
445bc0cd |
222 | "Non-integer value supplied for column '%s' despite the integer datatype", |
632d1e0f |
223 | $bind->[$_][0]{dbic_colname} || "# $_" |
224 | ) ); |
225 | undef $bindattrs->[$_]; |
226 | } |
227 | } |
228 | |
229 | return $bindattrs; |
230 | } |
231 | |
732e4282 |
232 | =head2 connect_call_use_foreign_keys |
233 | |
234 | Used as: |
235 | |
236 | on_connect_call => 'use_foreign_keys' |
237 | |
8384a713 |
238 | In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to turn on foreign key |
239 | (including cascading) support for recent versions of SQLite and L<DBD::SQLite>. |
732e4282 |
240 | |
241 | Executes: |
242 | |
8273e845 |
243 | PRAGMA foreign_keys = ON |
732e4282 |
244 | |
245 | See L<http://www.sqlite.org/foreignkeys.html> for more information. |
246 | |
247 | =cut |
248 | |
249 | sub connect_call_use_foreign_keys { |
250 | my $self = shift; |
251 | |
252 | $self->_do_query( |
253 | 'PRAGMA foreign_keys = ON' |
254 | ); |
255 | } |
256 | |
843f8ecd |
257 | 1; |
258 | |
0c11ad0e |
259 | =head1 AUTHOR AND CONTRIBUTORS |
843f8ecd |
260 | |
0c11ad0e |
261 | See L<AUTHOR|DBIx::Class/AUTHOR> and L<CONTRIBUTORS|DBIx::Class/CONTRIBUTORS> in DBIx::Class |
843f8ecd |
262 | |
263 | =head1 LICENSE |
264 | |
265 | You may distribute this code under the same terms as Perl itself. |
266 | |
267 | =cut |