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'); |
2b8cc2f2 |
11 | __PACKAGE__->sql_quote_char ('"'); |
09cedb88 |
12 | |
357eb92c |
13 | sub backup { |
14 | |
15 | require File::Spec; |
16 | require File::Copy; |
17 | require POSIX; |
18 | |
8795fefb |
19 | my ($self, $dir) = @_; |
20 | $dir ||= './'; |
c9d2e0a2 |
21 | |
22 | ## Where is the db file? |
12c9beea |
23 | my $dsn = $self->_dbi_connect_info()->[0]; |
c9d2e0a2 |
24 | |
25 | my $dbname = $1 if($dsn =~ /dbname=([^;]+)/); |
26 | if(!$dbname) |
27 | { |
28 | $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i); |
29 | } |
357eb92c |
30 | $self->throw_exception("Cannot determine name of SQLite db file") |
c9d2e0a2 |
31 | if(!$dbname || !-f $dbname); |
32 | |
33 | # print "Found database: $dbname\n"; |
79923569 |
34 | # my $dbfile = file($dbname); |
8795fefb |
35 | my ($vol, $dbdir, $file) = File::Spec->splitpath($dbname); |
79923569 |
36 | # my $file = $dbfile->basename(); |
357eb92c |
37 | $file = POSIX::strftime("%Y-%m-%d-%H_%M_%S", localtime()) . $file; |
c9d2e0a2 |
38 | $file = "B$file" while(-f $file); |
8795fefb |
39 | |
40 | mkdir($dir) unless -f $dir; |
41 | my $backupfile = File::Spec->catfile($dir, $file); |
42 | |
357eb92c |
43 | my $res = File::Copy::copy($dbname, $backupfile); |
c9d2e0a2 |
44 | $self->throw_exception("Backup failed! ($!)") if(!$res); |
45 | |
8795fefb |
46 | return $backupfile; |
c9d2e0a2 |
47 | } |
48 | |
2361982d |
49 | sub deployment_statements { |
96736321 |
50 | my $self = shift; |
2361982d |
51 | my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_; |
52 | |
53 | $sqltargs ||= {}; |
54 | |
96736321 |
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; |
6d766626 |
61 | } |
2361982d |
62 | |
63 | $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest); |
64 | } |
65 | |
357eb92c |
66 | sub datetime_parser_type { return "DateTime::Format::SQLite"; } |
40f75181 |
67 | |
732e4282 |
68 | =head2 connect_call_use_foreign_keys |
69 | |
70 | Used as: |
71 | |
72 | on_connect_call => 'use_foreign_keys' |
73 | |
8384a713 |
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>. |
732e4282 |
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 | |
843f8ecd |
93 | 1; |
94 | |
75d07914 |
95 | =head1 NAME |
843f8ecd |
96 | |
8e766a13 |
97 | DBIx::Class::Storage::DBI::SQLite - Automatic primary key class for SQLite |
843f8ecd |
98 | |
99 | =head1 SYNOPSIS |
100 | |
101 | # In your table classes |
d88ecca6 |
102 | use base 'DBIx::Class::Core'; |
843f8ecd |
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 |