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