1 package DBIx::Class::Storage::DBI::SQLite;
6 use base qw/DBIx::Class::Storage::DBI/;
9 __PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::SQLite');
10 __PACKAGE__->sql_limit_dialect ('LimitOffset');
11 __PACKAGE__->sql_quote_char ('"');
12 __PACKAGE__->datetime_parser_type ('DateTime::Format::SQLite');
20 my ($self, $dir) = @_;
23 ## Where is the db file?
24 my $dsn = $self->_dbi_connect_info()->[0];
26 my $dbname = $1 if($dsn =~ /dbname=([^;]+)/);
29 $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i);
31 $self->throw_exception("Cannot determine name of SQLite db file")
32 if(!$dbname || !-f $dbname);
34 # print "Found database: $dbname\n";
35 # my $dbfile = file($dbname);
36 my ($vol, $dbdir, $file) = File::Spec->splitpath($dbname);
37 # my $file = $dbfile->basename();
38 $file = POSIX::strftime("%Y-%m-%d-%H_%M_%S", localtime()) . $file;
39 $file = "B$file" while(-f $file);
41 mkdir($dir) unless -f $dir;
42 my $backupfile = File::Spec->catfile($dir, $file);
44 my $res = File::Copy::copy($dbname, $backupfile);
45 $self->throw_exception("Backup failed! ($!)") if(!$res);
51 my ($self, $name) = @_;
53 $self->_dbh->do("SAVEPOINT $name");
56 sub _exec_svp_release {
57 my ($self, $name) = @_;
59 $self->_dbh->do("RELEASE SAVEPOINT $name");
62 sub _exec_svp_rollback {
63 my ($self, $name) = @_;
65 # For some reason this statement changes the value of $dbh->{AutoCommit}, so
66 # we localize it here to preserve the original value.
67 local $self->_dbh->{AutoCommit} = $self->_dbh->{AutoCommit};
69 $self->_dbh->do("ROLLBACK TRANSACTION TO SAVEPOINT $name");
72 sub deployment_statements {
74 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
79 ! exists $sqltargs->{producer_args}{sqlite_version}
81 my $dver = $self->_server_info->{normalized_dbms_version}
83 $sqltargs->{producer_args}{sqlite_version} = $dver;
86 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
89 sub bind_attribute_by_data_type {
90 $_[1] =~ /^ (?: int(?:eger)? | (?:tiny|small|medium|big)int ) $/ix
91 ? do { require DBI; DBI::SQL_INTEGER() }
96 =head2 connect_call_use_foreign_keys
100 on_connect_call => 'use_foreign_keys'
102 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to turn on foreign key
103 (including cascading) support for recent versions of SQLite and L<DBD::SQLite>.
107 PRAGMA foreign_keys = ON
109 See L<http://www.sqlite.org/foreignkeys.html> for more information.
113 sub connect_call_use_foreign_keys {
117 'PRAGMA foreign_keys = ON'
125 DBIx::Class::Storage::DBI::SQLite - Automatic primary key class for SQLite
129 # In your table classes
130 use base 'DBIx::Class::Core';
131 __PACKAGE__->set_primary_key('id');
135 This class implements autoincrements for SQLite.
139 Matt S. Trout <mst@shadowcatsystems.co.uk>
143 You may distribute this code under the same terms as Perl itself.