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; |
10 | use Scalar::Util 'looks_like_number'; |
96dfdf51 |
11 | use Scope::Guard (); |
12 | use Context::Preserve 'preserve_context'; |
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 | |
34 | =head1 METHODS |
35 | |
36 | =cut |
37 | |
357eb92c |
38 | sub backup { |
39 | |
40 | require File::Spec; |
41 | require File::Copy; |
42 | require POSIX; |
43 | |
8795fefb |
44 | my ($self, $dir) = @_; |
45 | $dir ||= './'; |
c9d2e0a2 |
46 | |
47 | ## Where is the db file? |
12c9beea |
48 | my $dsn = $self->_dbi_connect_info()->[0]; |
c9d2e0a2 |
49 | |
50 | my $dbname = $1 if($dsn =~ /dbname=([^;]+)/); |
51 | if(!$dbname) |
52 | { |
53 | $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i); |
54 | } |
357eb92c |
55 | $self->throw_exception("Cannot determine name of SQLite db file") |
c9d2e0a2 |
56 | if(!$dbname || !-f $dbname); |
57 | |
58 | # print "Found database: $dbname\n"; |
79923569 |
59 | # my $dbfile = file($dbname); |
8795fefb |
60 | my ($vol, $dbdir, $file) = File::Spec->splitpath($dbname); |
79923569 |
61 | # my $file = $dbfile->basename(); |
357eb92c |
62 | $file = POSIX::strftime("%Y-%m-%d-%H_%M_%S", localtime()) . $file; |
c9d2e0a2 |
63 | $file = "B$file" while(-f $file); |
8795fefb |
64 | |
65 | mkdir($dir) unless -f $dir; |
66 | my $backupfile = File::Spec->catfile($dir, $file); |
67 | |
357eb92c |
68 | my $res = File::Copy::copy($dbname, $backupfile); |
c9d2e0a2 |
69 | $self->throw_exception("Backup failed! ($!)") if(!$res); |
70 | |
8795fefb |
71 | return $backupfile; |
c9d2e0a2 |
72 | } |
73 | |
86a51471 |
74 | sub _exec_svp_begin { |
75 | my ($self, $name) = @_; |
76 | |
77 | $self->_dbh->do("SAVEPOINT $name"); |
78 | } |
79 | |
80 | sub _exec_svp_release { |
81 | my ($self, $name) = @_; |
82 | |
83 | $self->_dbh->do("RELEASE SAVEPOINT $name"); |
84 | } |
85 | |
86 | sub _exec_svp_rollback { |
87 | my ($self, $name) = @_; |
88 | |
89 | # For some reason this statement changes the value of $dbh->{AutoCommit}, so |
90 | # we localize it here to preserve the original value. |
91 | local $self->_dbh->{AutoCommit} = $self->_dbh->{AutoCommit}; |
92 | |
93 | $self->_dbh->do("ROLLBACK TRANSACTION TO SAVEPOINT $name"); |
94 | } |
95 | |
2361982d |
96 | sub deployment_statements { |
96736321 |
97 | my $self = shift; |
2361982d |
98 | my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_; |
99 | |
100 | $sqltargs ||= {}; |
101 | |
96736321 |
102 | if ( |
103 | ! exists $sqltargs->{producer_args}{sqlite_version} |
104 | and |
105 | my $dver = $self->_server_info->{normalized_dbms_version} |
106 | ) { |
107 | $sqltargs->{producer_args}{sqlite_version} = $dver; |
6d766626 |
108 | } |
2361982d |
109 | |
110 | $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest); |
111 | } |
112 | |
0e773352 |
113 | sub bind_attribute_by_data_type { |
67b35a45 |
114 | $_[1] =~ /^ (?: int(?:eger)? | (?:tiny|small|medium)int ) $/ix |
0e773352 |
115 | ? do { require DBI; DBI::SQL_INTEGER() } |
116 | : undef |
117 | ; |
118 | } |
119 | |
632d1e0f |
120 | # DBD::SQLite (at least up to version 1.31 has a bug where it will |
121 | # non-fatally nummify a string value bound as an integer, resulting |
122 | # in insertions of '0' into supposed-to-be-numeric fields |
123 | # Since this can result in severe data inconsistency, remove the |
124 | # bind attr if such a sitation is detected |
125 | # |
126 | # FIXME - when a DBD::SQLite version is released that eventually fixes |
127 | # this sutiation (somehow) - no-op this override once a proper DBD |
128 | # version is detected |
129 | sub _dbi_attrs_for_bind { |
130 | my ($self, $ident, $bind) = @_; |
131 | my $bindattrs = $self->next::method($ident, $bind); |
132 | |
133 | for (0.. $#$bindattrs) { |
134 | if ( |
135 | defined $bindattrs->[$_] |
136 | and |
137 | defined $bind->[$_][1] |
138 | and |
139 | $bindattrs->[$_] eq DBI::SQL_INTEGER() |
140 | and |
141 | ! looks_like_number ($bind->[$_][1]) |
142 | ) { |
143 | carp_unique( sprintf ( |
144 | "Non-numeric value supplied for column '%s' despite the numeric datatype", |
145 | $bind->[$_][0]{dbic_colname} || "# $_" |
146 | ) ); |
147 | undef $bindattrs->[$_]; |
148 | } |
149 | } |
150 | |
151 | return $bindattrs; |
152 | } |
153 | |
732e4282 |
154 | =head2 connect_call_use_foreign_keys |
155 | |
156 | Used as: |
157 | |
158 | on_connect_call => 'use_foreign_keys' |
159 | |
8384a713 |
160 | In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to turn on foreign key |
161 | (including cascading) support for recent versions of SQLite and L<DBD::SQLite>. |
732e4282 |
162 | |
163 | Executes: |
164 | |
8273e845 |
165 | PRAGMA foreign_keys = ON |
732e4282 |
166 | |
167 | See L<http://www.sqlite.org/foreignkeys.html> for more information. |
168 | |
169 | =cut |
170 | |
171 | sub connect_call_use_foreign_keys { |
172 | my $self = shift; |
173 | |
174 | $self->_do_query( |
175 | 'PRAGMA foreign_keys = ON' |
176 | ); |
177 | } |
178 | |
96dfdf51 |
179 | sub with_deferred_fk_checks { |
180 | my ($self, $sub) = @_; |
181 | |
182 | my ($fk_state) = $self->_get_dbh->selectrow_array('PRAGMA foreign_keys'); |
183 | $self->_do_query('PRAGMA foreign_keys = OFF'); |
184 | |
185 | my $txn_scope_guard = $self->txn_scope_guard; |
186 | |
187 | my $sg; |
188 | if ($fk_state) { |
189 | $sg = Scope::Guard->new(sub { |
190 | $self->_do_query('PRAGMA foreign_keys = ON') |
191 | }) |
192 | } |
193 | |
194 | return preserve_context { $sub->() } after => sub { $txn_scope_guard->commit }; |
195 | } |
196 | |
843f8ecd |
197 | 1; |
198 | |
843f8ecd |
199 | =head1 AUTHORS |
200 | |
201 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
202 | |
203 | =head1 LICENSE |
204 | |
205 | You may distribute this code under the same terms as Perl itself. |
206 | |
207 | =cut |