savepoints 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
d5dedbd6 9__PACKAGE__->sql_maker_class('DBIx::Class::SQLMaker::SQLite');
6a247f33 10__PACKAGE__->sql_limit_dialect ('LimitOffset');
2b8cc2f2 11__PACKAGE__->sql_quote_char ('"');
6f7a118e 12__PACKAGE__->datetime_parser_type ('DateTime::Format::SQLite');
09cedb88 13
357eb92c 14sub backup {
15
16 require File::Spec;
17 require File::Copy;
18 require POSIX;
19
8795fefb 20 my ($self, $dir) = @_;
21 $dir ||= './';
c9d2e0a2 22
23 ## Where is the db file?
12c9beea 24 my $dsn = $self->_dbi_connect_info()->[0];
c9d2e0a2 25
26 my $dbname = $1 if($dsn =~ /dbname=([^;]+)/);
27 if(!$dbname)
28 {
29 $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i);
30 }
357eb92c 31 $self->throw_exception("Cannot determine name of SQLite db file")
c9d2e0a2 32 if(!$dbname || !-f $dbname);
33
34# print "Found database: $dbname\n";
79923569 35# my $dbfile = file($dbname);
8795fefb 36 my ($vol, $dbdir, $file) = File::Spec->splitpath($dbname);
79923569 37# my $file = $dbfile->basename();
357eb92c 38 $file = POSIX::strftime("%Y-%m-%d-%H_%M_%S", localtime()) . $file;
c9d2e0a2 39 $file = "B$file" while(-f $file);
8795fefb 40
41 mkdir($dir) unless -f $dir;
42 my $backupfile = File::Spec->catfile($dir, $file);
43
357eb92c 44 my $res = File::Copy::copy($dbname, $backupfile);
c9d2e0a2 45 $self->throw_exception("Backup failed! ($!)") if(!$res);
46
8795fefb 47 return $backupfile;
c9d2e0a2 48}
49
86a51471 50sub _exec_svp_begin {
51 my ($self, $name) = @_;
52
53 $self->_dbh->do("SAVEPOINT $name");
54}
55
56sub _exec_svp_release {
57 my ($self, $name) = @_;
58
59 $self->_dbh->do("RELEASE SAVEPOINT $name");
60}
61
62sub _exec_svp_rollback {
63 my ($self, $name) = @_;
64
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};
68
69 $self->_dbh->do("ROLLBACK TRANSACTION TO SAVEPOINT $name");
70}
71
2361982d 72sub deployment_statements {
96736321 73 my $self = shift;
2361982d 74 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
75
76 $sqltargs ||= {};
77
96736321 78 if (
79 ! exists $sqltargs->{producer_args}{sqlite_version}
80 and
81 my $dver = $self->_server_info->{normalized_dbms_version}
82 ) {
83 $sqltargs->{producer_args}{sqlite_version} = $dver;
6d766626 84 }
2361982d 85
86 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
87}
88
0e773352 89sub bind_attribute_by_data_type {
90 $_[1] =~ /^ (?: int(?:eger)? | (?:tiny|small|medium|big)int ) $/ix
91 ? do { require DBI; DBI::SQL_INTEGER() }
92 : undef
93 ;
94}
95
732e4282 96=head2 connect_call_use_foreign_keys
97
98Used as:
99
100 on_connect_call => 'use_foreign_keys'
101
8384a713 102In 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>.
732e4282 104
105Executes:
106
107 PRAGMA foreign_keys = ON
108
109See L<http://www.sqlite.org/foreignkeys.html> for more information.
110
111=cut
112
113sub connect_call_use_foreign_keys {
114 my $self = shift;
115
116 $self->_do_query(
117 'PRAGMA foreign_keys = ON'
118 );
119}
120
843f8ecd 1211;
122
75d07914 123=head1 NAME
843f8ecd 124
8e766a13 125DBIx::Class::Storage::DBI::SQLite - Automatic primary key class for SQLite
843f8ecd 126
127=head1 SYNOPSIS
128
129 # In your table classes
d88ecca6 130 use base 'DBIx::Class::Core';
843f8ecd 131 __PACKAGE__->set_primary_key('id');
132
133=head1 DESCRIPTION
134
135This class implements autoincrements for SQLite.
136
137=head1 AUTHORS
138
139Matt S. Trout <mst@shadowcatsystems.co.uk>
140
141=head1 LICENSE
142
143You may distribute this code under the same terms as Perl itself.
144
145=cut