Massive rewrite of bind handling, and overall simplification of ::Storage::DBI
[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 __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');
13
14 sub backup {
15
16   require File::Spec;
17   require File::Copy;
18   require POSIX;
19
20   my ($self, $dir) = @_;
21   $dir ||= './';
22
23   ## Where is the db file?
24   my $dsn = $self->_dbi_connect_info()->[0];
25
26   my $dbname = $1 if($dsn =~ /dbname=([^;]+)/);
27   if(!$dbname)
28   {
29     $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i);
30   }
31   $self->throw_exception("Cannot determine name of SQLite db file")
32     if(!$dbname || !-f $dbname);
33
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);
40
41   mkdir($dir) unless -f $dir;
42   my $backupfile = File::Spec->catfile($dir, $file);
43
44   my $res = File::Copy::copy($dbname, $backupfile);
45   $self->throw_exception("Backup failed! ($!)") if(!$res);
46
47   return $backupfile;
48 }
49
50 sub deployment_statements {
51   my $self = shift;
52   my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
53
54   $sqltargs ||= {};
55
56   if (
57     ! exists $sqltargs->{producer_args}{sqlite_version}
58       and
59     my $dver = $self->_server_info->{normalized_dbms_version}
60   ) {
61     $sqltargs->{producer_args}{sqlite_version} = $dver;
62   }
63
64   $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
65 }
66
67 sub bind_attribute_by_data_type {
68   $_[1] =~ /^ (?: int(?:eger)? | (?:tiny|small|medium|big)int ) $/ix
69     ? do { require DBI; DBI::SQL_INTEGER() }
70     : undef
71   ;
72 }
73
74 =head2 connect_call_use_foreign_keys
75
76 Used as:
77
78     on_connect_call => 'use_foreign_keys'
79
80 In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to turn on foreign key
81 (including cascading) support for recent versions of SQLite and L<DBD::SQLite>.
82
83 Executes:
84
85   PRAGMA foreign_keys = ON 
86
87 See L<http://www.sqlite.org/foreignkeys.html> for more information.
88
89 =cut
90
91 sub connect_call_use_foreign_keys {
92   my $self = shift;
93
94   $self->_do_query(
95     'PRAGMA foreign_keys = ON'
96   );
97 }
98
99 1;
100
101 =head1 NAME
102
103 DBIx::Class::Storage::DBI::SQLite - Automatic primary key class for SQLite
104
105 =head1 SYNOPSIS
106
107   # In your table classes
108   use base 'DBIx::Class::Core';
109   __PACKAGE__->set_primary_key('id');
110
111 =head1 DESCRIPTION
112
113 This class implements autoincrements for SQLite.
114
115 =head1 AUTHORS
116
117 Matt S. Trout <mst@shadowcatsystems.co.uk>
118
119 =head1 LICENSE
120
121 You may distribute this code under the same terms as Perl itself.
122
123 =cut