Trailing WS crusade - got to save them bits
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / SQLite.pm
1 package DBIx::Class::Storage::DBI::SQLite;
2
3 use strict;
4 use warnings;
5
6 use base qw/DBIx::Class::Storage::DBI/;
7 use mro 'c3';
8
9 use DBIx::Class::Carp;
10 use Scalar::Util 'looks_like_number';
11 use namespace::clean;
12
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');
17
18 sub backup {
19
20   require File::Spec;
21   require File::Copy;
22   require POSIX;
23
24   my ($self, $dir) = @_;
25   $dir ||= './';
26
27   ## Where is the db file?
28   my $dsn = $self->_dbi_connect_info()->[0];
29
30   my $dbname = $1 if($dsn =~ /dbname=([^;]+)/);
31   if(!$dbname)
32   {
33     $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i);
34   }
35   $self->throw_exception("Cannot determine name of SQLite db file")
36     if(!$dbname || !-f $dbname);
37
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);
44
45   mkdir($dir) unless -f $dir;
46   my $backupfile = File::Spec->catfile($dir, $file);
47
48   my $res = File::Copy::copy($dbname, $backupfile);
49   $self->throw_exception("Backup failed! ($!)") if(!$res);
50
51   return $backupfile;
52 }
53
54 sub _exec_svp_begin {
55   my ($self, $name) = @_;
56
57   $self->_dbh->do("SAVEPOINT $name");
58 }
59
60 sub _exec_svp_release {
61   my ($self, $name) = @_;
62
63   $self->_dbh->do("RELEASE SAVEPOINT $name");
64 }
65
66 sub _exec_svp_rollback {
67   my ($self, $name) = @_;
68
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};
72
73   $self->_dbh->do("ROLLBACK TRANSACTION TO SAVEPOINT $name");
74 }
75
76 sub deployment_statements {
77   my $self = shift;
78   my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
79
80   $sqltargs ||= {};
81
82   if (
83     ! exists $sqltargs->{producer_args}{sqlite_version}
84       and
85     my $dver = $self->_server_info->{normalized_dbms_version}
86   ) {
87     $sqltargs->{producer_args}{sqlite_version} = $dver;
88   }
89
90   $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
91 }
92
93 sub bind_attribute_by_data_type {
94   $_[1] =~ /^ (?: int(?:eger)? | (?:tiny|small|medium)int ) $/ix
95     ? do { require DBI; DBI::SQL_INTEGER() }
96     : undef
97   ;
98 }
99
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
105 #
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);
112
113   for (0.. $#$bindattrs) {
114     if (
115       defined $bindattrs->[$_]
116         and
117       defined $bind->[$_][1]
118         and
119       $bindattrs->[$_] eq DBI::SQL_INTEGER()
120         and
121       ! looks_like_number ($bind->[$_][1])
122     ) {
123       carp_unique( sprintf (
124         "Non-numeric value supplied for column '%s' despite the numeric datatype",
125         $bind->[$_][0]{dbic_colname} || "# $_"
126       ) );
127       undef $bindattrs->[$_];
128     }
129   }
130
131   return $bindattrs;
132 }
133
134 =head2 connect_call_use_foreign_keys
135
136 Used as:
137
138     on_connect_call => 'use_foreign_keys'
139
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>.
142
143 Executes:
144
145   PRAGMA foreign_keys = ON
146
147 See L<http://www.sqlite.org/foreignkeys.html> for more information.
148
149 =cut
150
151 sub connect_call_use_foreign_keys {
152   my $self = shift;
153
154   $self->_do_query(
155     'PRAGMA foreign_keys = ON'
156   );
157 }
158
159 1;
160
161 =head1 NAME
162
163 DBIx::Class::Storage::DBI::SQLite - Automatic primary key class for SQLite
164
165 =head1 SYNOPSIS
166
167   # In your table classes
168   use base 'DBIx::Class::Core';
169   __PACKAGE__->set_primary_key('id');
170
171 =head1 DESCRIPTION
172
173 This class implements autoincrements for SQLite.
174
175 =head1 AUTHORS
176
177 Matt S. Trout <mst@shadowcatsystems.co.uk>
178
179 =head1 LICENSE
180
181 You may distribute this code under the same terms as Perl itself.
182
183 =cut