add ::DBI::_is_integer_type and use for SQLite
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / SQLite.pm
CommitLineData
843f8ecd 1package DBIx::Class::Storage::DBI::SQLite;
2
3use strict;
4use warnings;
2ad62d97 5
6use base qw/DBIx::Class::Storage::DBI/;
7use mro 'c3';
8
632d1e0f 9use DBIx::Class::Carp;
10use Scalar::Util 'looks_like_number';
11use 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
20DBIx::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
30This class implements autoincrements for SQLite.
31
32=head1 METHODS
33
34=cut
35
357eb92c 36sub 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 72sub _exec_svp_begin {
73 my ($self, $name) = @_;
74
75 $self->_dbh->do("SAVEPOINT $name");
76}
77
78sub _exec_svp_release {
79 my ($self, $name) = @_;
80
81 $self->_dbh->do("RELEASE SAVEPOINT $name");
82}
83
84sub _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
2361982d 94sub deployment_statements {
96736321 95 my $self = shift;
2361982d 96 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
97
98 $sqltargs ||= {};
99
96736321 100 if (
101 ! exists $sqltargs->{producer_args}{sqlite_version}
102 and
103 my $dver = $self->_server_info->{normalized_dbms_version}
104 ) {
105 $sqltargs->{producer_args}{sqlite_version} = $dver;
6d766626 106 }
2361982d 107
108 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
109}
110
c50a1dbf 111# In addition to checking for integer data_types, we need to check for generic
112# NUMBER data_types with a zero scale, such as NUMBER(38,0) in Oracle, which are
113# integer types. Because bind_attribute_by_data_type cannot check the
114# column_info size, and the regexes are very hairy, we do the checking in
115# _resolve_bindattrs and cache the result of the check in the column_info.
116#
117sub _resolve_bindattrs {
118 my $self = shift;
119 my ($ident, $bind, $colinfos) = @_;
120
121 $colinfos = $self->_resolve_column_info($ident)
122 unless keys %$colinfos;
123
124 my $binds = $self->next::method(@_);
125
126 BIND: foreach my $bind (@$binds) {
127 my $attrs = $bind->[0];
128 my $col = $attrs->{dbic_colname};
129
130 if (exists $colinfos->{$col}{_is_integer_data_type}
131 && $colinfos->{$col}{_is_integer_data_type} == 1) { # cached
132
133 $attrs->{dbd_attrs} = do { require DBI; DBI::SQL_INTEGER() };
134 next BIND;
135 }
136
137 my $data_type = $attrs->{sqlt_datatype};
138 my $size = $attrs->{sqlt_size};
139
140 my $is_int_type = $self->_is_integer_type($data_type) ? 1 : 0;
141
142 # This should really live in ::DBI and cache in column_info, but currently
143 # only needed in SQLite.
144 if ((not $is_int_type)
145 && (ref $size eq 'ARRAY' && $size->[1] == 0)
146 && $data_type =~ /^(?:real|float|double(?:\s+precision)?|dec(?:imal)?|numeric|number|fixed|money|currency)(?:\s+unsigned)?\z/i) {
147
148 $is_int_type = 1;
149 }
150
151 if ($is_int_type) {
152 $attrs->{dbd_attrs} = do { require DBI; DBI::SQL_INTEGER() };
153
154 $colinfos->{$col}{_is_integer_data_type} = 1; # cache
155 }
156 }
157
158 return $binds;
0e773352 159}
160
632d1e0f 161# DBD::SQLite (at least up to version 1.31 has a bug where it will
162# non-fatally nummify a string value bound as an integer, resulting
163# in insertions of '0' into supposed-to-be-numeric fields
164# Since this can result in severe data inconsistency, remove the
165# bind attr if such a sitation is detected
166#
167# FIXME - when a DBD::SQLite version is released that eventually fixes
168# this sutiation (somehow) - no-op this override once a proper DBD
169# version is detected
c50a1dbf 170#
632d1e0f 171sub _dbi_attrs_for_bind {
172 my ($self, $ident, $bind) = @_;
c50a1dbf 173
632d1e0f 174 my $bindattrs = $self->next::method($ident, $bind);
175
176 for (0.. $#$bindattrs) {
177 if (
178 defined $bindattrs->[$_]
179 and
180 defined $bind->[$_][1]
181 and
182 $bindattrs->[$_] eq DBI::SQL_INTEGER()
183 and
184 ! looks_like_number ($bind->[$_][1])
185 ) {
186 carp_unique( sprintf (
187 "Non-numeric value supplied for column '%s' despite the numeric datatype",
188 $bind->[$_][0]{dbic_colname} || "# $_"
189 ) );
190 undef $bindattrs->[$_];
191 }
192 }
193
194 return $bindattrs;
195}
196
732e4282 197=head2 connect_call_use_foreign_keys
198
199Used as:
200
201 on_connect_call => 'use_foreign_keys'
202
8384a713 203In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to turn on foreign key
204(including cascading) support for recent versions of SQLite and L<DBD::SQLite>.
732e4282 205
206Executes:
207
8273e845 208 PRAGMA foreign_keys = ON
732e4282 209
210See L<http://www.sqlite.org/foreignkeys.html> for more information.
211
212=cut
213
214sub connect_call_use_foreign_keys {
215 my $self = shift;
216
217 $self->_do_query(
218 'PRAGMA foreign_keys = ON'
219 );
220}
221
843f8ecd 2221;
223
843f8ecd 224=head1 AUTHORS
225
226Matt S. Trout <mst@shadowcatsystems.co.uk>
227
228=head1 LICENSE
229
230You may distribute this code under the same terms as Perl itself.
231
232=cut