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