Refactor the version handling
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / Storage / DBI / SQLite.pm
CommitLineData
843f8ecd 1package DBIx::Class::Storage::DBI::SQLite;
2
3use strict;
4use warnings;
2ad62d97 5
6use base qw/DBIx::Class::Storage::DBI/;
7use mro 'c3';
8
c9d2e0a2 9use POSIX 'strftime';
10use File::Copy;
79923569 11use File::Spec;
843f8ecd 12
09cedb88 13__PACKAGE__->sql_maker_class('DBIx::Class::SQLAHacks::SQLite');
14
c9d2e0a2 15sub backup
16{
8795fefb 17 my ($self, $dir) = @_;
18 $dir ||= './';
c9d2e0a2 19
20 ## Where is the db file?
12c9beea 21 my $dsn = $self->_dbi_connect_info()->[0];
c9d2e0a2 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";
79923569 32# my $dbfile = file($dbname);
8795fefb 33 my ($vol, $dbdir, $file) = File::Spec->splitpath($dbname);
79923569 34# my $file = $dbfile->basename();
a32c360c 35 $file = strftime("%Y-%m-%d-%H_%M_%S", localtime()) . $file;
c9d2e0a2 36 $file = "B$file" while(-f $file);
8795fefb 37
38 mkdir($dir) unless -f $dir;
39 my $backupfile = File::Spec->catfile($dir, $file);
40
41 my $res = copy($dbname, $backupfile);
c9d2e0a2 42 $self->throw_exception("Backup failed! ($!)") if(!$res);
43
8795fefb 44 return $backupfile;
c9d2e0a2 45}
46
2361982d 47sub deployment_statements {
48 my $self = shift;;
49 my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_;
50
51 $sqltargs ||= {};
52
6d766626 53 # it'd be cool to use the normalized perl-style version but this needs sqlt hacking as well
54 if (my $sqlite_version = $self->_server_info->{dbms_version}) {
55 # numify, SQLT does a numeric comparison
56 $sqlite_version =~ s/^(\d+) \. (\d+) (?: \. (\d+))? .*/${1}.${2}/x;
2361982d 57
6d766626 58 $sqltargs->{producer_args}{sqlite_version} = $sqlite_version if $sqlite_version;
59 }
2361982d 60
61 $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest);
62}
63
40f75181 64sub datetime_parser_type { return "DateTime::Format::SQLite"; }
65
732e4282 66=head2 connect_call_use_foreign_keys
67
68Used as:
69
70 on_connect_call => 'use_foreign_keys'
71
8384a713 72In L<connect_info|DBIx::Class::Storage::DBI/connect_info> to turn on foreign key
73(including cascading) support for recent versions of SQLite and L<DBD::SQLite>.
732e4282 74
75Executes:
76
77 PRAGMA foreign_keys = ON
78
79See L<http://www.sqlite.org/foreignkeys.html> for more information.
80
81=cut
82
83sub connect_call_use_foreign_keys {
84 my $self = shift;
85
86 $self->_do_query(
87 'PRAGMA foreign_keys = ON'
88 );
89}
90
843f8ecd 911;
92
75d07914 93=head1 NAME
843f8ecd 94
8e766a13 95DBIx::Class::Storage::DBI::SQLite - Automatic primary key class for SQLite
843f8ecd 96
97=head1 SYNOPSIS
98
99 # In your table classes
d88ecca6 100 use base 'DBIx::Class::Core';
843f8ecd 101 __PACKAGE__->set_primary_key('id');
102
103=head1 DESCRIPTION
104
105This class implements autoincrements for SQLite.
106
107=head1 AUTHORS
108
109Matt S. Trout <mst@shadowcatsystems.co.uk>
110
111=head1 LICENSE
112
113You may distribute this code under the same terms as Perl itself.
114
115=cut