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');
24 my ($self, $dir) = @_;
27 ## Where is the db file?
28 my $dsn = $self->_dbi_connect_info()->[0];
30 my $dbname = $1 if($dsn =~ /dbname=([^;]+)/);
33 $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i);
35 $self->throw_exception("Cannot determine name of SQLite db file")
36 if(!$dbname || !-f $dbname);
38 # print "Found database: $dbname\n";
39 # my $dbfile = file($dbname);
40 my ($vol, $dbdir, $file) = File::Spec->splitpath($dbname);
41 # my $file = $dbfile->basename();
42 $file = POSIX::strftime("%Y-%m-%d-%H_%M_%S", localtime()) . $file;
43 $file = "B$file" while(-f $file);
45 mkdir($dir) unless -f $dir;
46 my $backupfile = File::Spec->catfile($dir, $file);
48 my $res = File::Copy::copy($dbname, $backupfile);
49 $self->throw_exception("Backup failed! ($!)") if(!$res);
55 my ($self, $name) = @_;
57 $self->_dbh->do("SAVEPOINT $name");
60 sub _exec_svp_release {
61 my ($self, $name) = @_;
63 $self->_dbh->do("RELEASE SAVEPOINT $name");
66 sub _exec_svp_rollback {
67 my ($self, $name) = @_;
69 # For some reason this statement changes the value of $dbh->{AutoCommit}, so
70 # we localize it here to preserve the original value.
71 local $self->_dbh->{AutoCommit} = $self->_dbh->{AutoCommit};
73 $self->_dbh->do("ROLLBACK TRANSACTION TO SAVEPOINT $name");
76 sub deployment_statements {
78 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
83 ! exists $sqltargs->{producer_args}{sqlite_version}
85 my $dver = $self->_server_info->{normalized_dbms_version}
87 $sqltargs->{producer_args}{sqlite_version} = $dver;
90 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
93 sub bind_attribute_by_data_type {
94 $_[1] =~ /^ (?: int(?:eger)? | (?:tiny|small|medium)int ) $/ix
95 ? do { require DBI; DBI::SQL_INTEGER() }
100 # DBD::SQLite (at least up to version 1.31 has a bug where it will
101 # non-fatally nummify a string value bound as an integer, resulting
102 # in insertions of '0' into supposed-to-be-numeric fields
103 # Since this can result in severe data inconsistency, remove the
104 # bind attr if such a sitation is detected
106 # FIXME - when a DBD::SQLite version is released that eventually fixes
107 # this sutiation (somehow) - no-op this override once a proper DBD
108 # version is detected
109 sub _dbi_attrs_for_bind {
110 my ($self, $ident, $bind) = @_;
111 my $bindattrs = $self->next::method($ident, $bind);
113 for (0.. $#$bindattrs) {
115 defined $bindattrs->[$_]
117 defined $bind->[$_][1]
119 $bindattrs->[$_] eq DBI::SQL_INTEGER()
121 ! looks_like_number ($bind->[$_][1])
123 carp_unique( sprintf (
124 "Non-numeric value supplied for column '%s' despite the numeric datatype",
125 $bind->[$_][0]{dbic_colname} || "# $_"
127 undef $bindattrs->[$_];
134 =head2 connect_call_use_foreign_keys
138 on_connect_call => 'use_foreign_keys'
140 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to turn on foreign key
141 (including cascading) support for recent versions of SQLite and L<DBD::SQLite>.
145 PRAGMA foreign_keys = ON
147 See L<http://www.sqlite.org/foreignkeys.html> for more information.
151 sub connect_call_use_foreign_keys {
155 'PRAGMA foreign_keys = ON'
163 DBIx::Class::Storage::DBI::SQLite - Automatic primary key class for SQLite
167 # In your table classes
168 use base 'DBIx::Class::Core';
169 __PACKAGE__->set_primary_key('id');
173 This class implements autoincrements for SQLite.
177 Matt S. Trout <mst@shadowcatsystems.co.uk>
181 You may distribute this code under the same terms as Perl itself.