6943c77de5b085fcd82f892c47468e71f2427f14
[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 =head1 NAME
19
20 DBIx::Class::Storage::DBI::SQLite - Automatic primary key class for SQLite
21
22 =head1 SYNOPSIS
23
24   # In your table classes
25   use base 'DBIx::Class::Core';
26   __PACKAGE__->set_primary_key('id');
27
28 =head1 DESCRIPTION
29
30 This class implements autoincrements for SQLite.
31
32 =head1 METHODS
33
34 =cut
35
36 sub backup {
37
38   require File::Spec;
39   require File::Copy;
40   require POSIX;
41
42   my ($self, $dir) = @_;
43   $dir ||= './';
44
45   ## Where is the db file?
46   my $dsn = $self->_dbi_connect_info()->[0];
47
48   my $dbname = $1 if($dsn =~ /dbname=([^;]+)/);
49   if(!$dbname)
50   {
51     $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i);
52   }
53   $self->throw_exception("Cannot determine name of SQLite db file")
54     if(!$dbname || !-f $dbname);
55
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);
62
63   mkdir($dir) unless -f $dir;
64   my $backupfile = File::Spec->catfile($dir, $file);
65
66   my $res = File::Copy::copy($dbname, $backupfile);
67   $self->throw_exception("Backup failed! ($!)") if(!$res);
68
69   return $backupfile;
70 }
71
72 sub _exec_svp_begin {
73   my ($self, $name) = @_;
74
75   $self->_dbh->do("SAVEPOINT $name");
76 }
77
78 sub _exec_svp_release {
79   my ($self, $name) = @_;
80
81   $self->_dbh->do("RELEASE SAVEPOINT $name");
82 }
83
84 sub _exec_svp_rollback {
85   my ($self, $name) = @_;
86
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};
90
91   $self->_dbh->do("ROLLBACK TRANSACTION TO SAVEPOINT $name");
92 }
93
94 sub deployment_statements {
95   my $self = shift;
96   my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
97
98   $sqltargs ||= {};
99
100   if (
101     ! exists $sqltargs->{producer_args}{sqlite_version}
102       and
103     my $dver = $self->_server_info->{normalized_dbms_version}
104   ) {
105     $sqltargs->{producer_args}{sqlite_version} = $dver;
106   }
107
108   $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
109 }
110
111 sub bind_attribute_by_data_type {
112   $_[1] =~ /^ (?: int(?:eger)? | (?:tiny|small|medium)int ) $/ix
113     ? do { require DBI; DBI::SQL_INTEGER() }
114     : undef
115   ;
116 }
117
118 # DBD::SQLite (at least up to version 1.31 has a bug where it will
119 # non-fatally nummify a string value bound as an integer, resulting
120 # in insertions of '0' into supposed-to-be-numeric fields
121 # Since this can result in severe data inconsistency, remove the
122 # bind attr if such a sitation is detected
123 #
124 # FIXME - when a DBD::SQLite version is released that eventually fixes
125 # this sutiation (somehow) - no-op this override once a proper DBD
126 # version is detected
127 sub _dbi_attrs_for_bind {
128   my ($self, $ident, $bind) = @_;
129   my $bindattrs = $self->next::method($ident, $bind);
130
131   for (0.. $#$bindattrs) {
132     if (
133       defined $bindattrs->[$_]
134         and
135       defined $bind->[$_][1]
136         and
137       $bindattrs->[$_] eq DBI::SQL_INTEGER()
138         and
139       ! looks_like_number ($bind->[$_][1])
140     ) {
141       carp_unique( sprintf (
142         "Non-numeric value supplied for column '%s' despite the numeric datatype",
143         $bind->[$_][0]{dbic_colname} || "# $_"
144       ) );
145       undef $bindattrs->[$_];
146     }
147   }
148
149   return $bindattrs;
150 }
151
152 =head2 connect_call_use_foreign_keys
153
154 Used as:
155
156     on_connect_call => 'use_foreign_keys'
157
158 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to turn on foreign key
159 (including cascading) support for recent versions of SQLite and L<DBD::SQLite>.
160
161 Executes:
162
163   PRAGMA foreign_keys = ON
164
165 See L<http://www.sqlite.org/foreignkeys.html> for more information.
166
167 =cut
168
169 sub connect_call_use_foreign_keys {
170   my $self = shift;
171
172   $self->_do_query(
173     'PRAGMA foreign_keys = ON'
174   );
175 }
176
177 1;
178
179 =head1 AUTHORS
180
181 Matt S. Trout <mst@shadowcatsystems.co.uk>
182
183 =head1 LICENSE
184
185 You may distribute this code under the same terms as Perl itself.
186
187 =cut