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