1 package DBIx::Class::Storage::DBI::SQLite;
6 use base qw/DBIx::Class::Storage::DBI/;
10 use Scalar::Util 'looks_like_number';
13 __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::SQLite');
14 __PACKAGE__->sql_limit_dialect ('LimitOffset');
15 __PACKAGE__->sql_quote_char ('"');
16 __PACKAGE__->datetime_parser_type ('DateTime::Format::SQLite');
20 DBIx::Class::Storage::DBI::SQLite - Automatic primary key class for SQLite
24 # In your table classes
25 use base 'DBIx::Class::Core';
26 __PACKAGE__->set_primary_key('id');
30 This class implements autoincrements for SQLite.
42 my ($self, $dir) = @_;
45 ## Where is the db file?
46 my $dsn = $self->_dbi_connect_info()->[0];
48 my $dbname = $1 if($dsn =~ /dbname=([^;]+)/);
51 $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i);
53 $self->throw_exception("Cannot determine name of SQLite db file")
54 if(!$dbname || !-f $dbname);
56 # print "Found database: $dbname\n";
57 # my $dbfile = file($dbname);
58 my ($vol, $dbdir, $file) = File::Spec->splitpath($dbname);
59 # my $file = $dbfile->basename();
60 $file = POSIX::strftime("%Y-%m-%d-%H_%M_%S", localtime()) . $file;
61 $file = "B$file" while(-f $file);
63 mkdir($dir) unless -f $dir;
64 my $backupfile = File::Spec->catfile($dir, $file);
66 my $res = File::Copy::copy($dbname, $backupfile);
67 $self->throw_exception("Backup failed! ($!)") if(!$res);
73 my ($self, $name) = @_;
75 $self->_dbh->do("SAVEPOINT $name");
78 sub _exec_svp_release {
79 my ($self, $name) = @_;
81 $self->_dbh->do("RELEASE SAVEPOINT $name");
84 sub _exec_svp_rollback {
85 my ($self, $name) = @_;
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};
91 $self->_dbh->do("ROLLBACK TRANSACTION TO SAVEPOINT $name");
94 sub deployment_statements {
96 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
101 ! exists $sqltargs->{producer_args}{sqlite_version}
103 my $dver = $self->_server_info->{normalized_dbms_version}
105 $sqltargs->{producer_args}{sqlite_version} = $dver;
108 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
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.
117 sub _resolve_bindattrs {
119 my ($ident, $bind, $colinfos) = @_;
121 $colinfos = $self->_resolve_column_info($ident)
122 unless keys %$colinfos;
124 my $binds = $self->next::method(@_);
126 BIND: foreach my $bind (@$binds) {
127 my $attrs = $bind->[0];
128 my $col = $attrs->{dbic_colname};
130 if (exists $colinfos->{$col}{_is_integer_data_type}
131 && $colinfos->{$col}{_is_integer_data_type} == 1) { # cached
133 $attrs->{dbd_attrs} = do { require DBI; DBI::SQL_INTEGER() };
137 my $data_type = $attrs->{sqlt_datatype};
138 my $size = $attrs->{sqlt_size};
140 my $is_int_type = $self->_is_integer_type($data_type) ? 1 : 0;
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) {
152 $attrs->{dbd_attrs} = do { require DBI; DBI::SQL_INTEGER() };
154 $colinfos->{$col}{_is_integer_data_type} = 1; # cache
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
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
171 sub _dbi_attrs_for_bind {
172 my ($self, $ident, $bind) = @_;
174 my $bindattrs = $self->next::method($ident, $bind);
176 for (0.. $#$bindattrs) {
178 defined $bindattrs->[$_]
180 defined $bind->[$_][1]
182 $bindattrs->[$_] eq DBI::SQL_INTEGER()
184 ! looks_like_number ($bind->[$_][1])
186 carp_unique( sprintf (
187 "Non-numeric value supplied for column '%s' despite the numeric datatype",
188 $bind->[$_][0]{dbic_colname} || "# $_"
190 undef $bindattrs->[$_];
197 =head2 connect_call_use_foreign_keys
201 on_connect_call => 'use_foreign_keys'
203 In 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>.
208 PRAGMA foreign_keys = ON
210 See L<http://www.sqlite.org/foreignkeys.html> for more information.
214 sub connect_call_use_foreign_keys {
218 'PRAGMA foreign_keys = ON'
226 Matt S. Trout <mst@shadowcatsystems.co.uk>
230 You may distribute this code under the same terms as Perl itself.