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 | |
c9d2e0a2 |
9 | use POSIX 'strftime'; |
10 | use File::Copy; |
79923569 |
11 | use File::Spec; |
843f8ecd |
12 | |
d4f16b21 |
13 | sub _dbh_last_insert_id { |
14 | my ($self, $dbh, $source, $col) = @_; |
15 | $dbh->func('last_insert_rowid'); |
843f8ecd |
16 | } |
17 | |
c9d2e0a2 |
18 | sub backup |
19 | { |
8795fefb |
20 | my ($self, $dir) = @_; |
21 | $dir ||= './'; |
c9d2e0a2 |
22 | |
23 | ## Where is the db file? |
12c9beea |
24 | my $dsn = $self->_dbi_connect_info()->[0]; |
c9d2e0a2 |
25 | |
26 | my $dbname = $1 if($dsn =~ /dbname=([^;]+)/); |
27 | if(!$dbname) |
28 | { |
29 | $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i); |
30 | } |
31 | $self->throw_exception("Cannot determine name of SQLite db file") |
32 | if(!$dbname || !-f $dbname); |
33 | |
34 | # print "Found database: $dbname\n"; |
79923569 |
35 | # my $dbfile = file($dbname); |
8795fefb |
36 | my ($vol, $dbdir, $file) = File::Spec->splitpath($dbname); |
79923569 |
37 | # my $file = $dbfile->basename(); |
a32c360c |
38 | $file = strftime("%Y-%m-%d-%H_%M_%S", localtime()) . $file; |
c9d2e0a2 |
39 | $file = "B$file" while(-f $file); |
8795fefb |
40 | |
41 | mkdir($dir) unless -f $dir; |
42 | my $backupfile = File::Spec->catfile($dir, $file); |
43 | |
44 | my $res = copy($dbname, $backupfile); |
c9d2e0a2 |
45 | $self->throw_exception("Backup failed! ($!)") if(!$res); |
46 | |
8795fefb |
47 | return $backupfile; |
c9d2e0a2 |
48 | } |
49 | |
2361982d |
50 | sub deployment_statements { |
51 | my $self = shift;; |
52 | my ($schema, $type, $version, $dir, $sqltargs, @rest) = @_; |
53 | |
54 | $sqltargs ||= {}; |
55 | |
56 | my $sqlite_version = $self->_get_dbh->{sqlite_version}; |
57 | |
58 | # numify, SQLT does a numeric comparison |
f05ba46f |
59 | $sqlite_version =~ s/^(\d+) \. (\d+) (?: \. (\d+))? .*/${1}.${2}/x; |
2361982d |
60 | |
61 | $sqltargs->{producer_args}{sqlite_version} = $sqlite_version; |
62 | |
63 | $self->next::method($schema, $type, $version, $dir, $sqltargs, @rest); |
64 | } |
65 | |
40f75181 |
66 | sub datetime_parser_type { return "DateTime::Format::SQLite"; } |
67 | |
843f8ecd |
68 | 1; |
69 | |
75d07914 |
70 | =head1 NAME |
843f8ecd |
71 | |
8e766a13 |
72 | DBIx::Class::Storage::DBI::SQLite - Automatic primary key class for SQLite |
843f8ecd |
73 | |
74 | =head1 SYNOPSIS |
75 | |
76 | # In your table classes |
77254782 |
77 | __PACKAGE__->load_components(qw/PK::Auto Core/); |
843f8ecd |
78 | __PACKAGE__->set_primary_key('id'); |
79 | |
80 | =head1 DESCRIPTION |
81 | |
82 | This class implements autoincrements for SQLite. |
83 | |
84 | =head1 AUTHORS |
85 | |
86 | Matt S. Trout <mst@shadowcatsystems.co.uk> |
87 | |
88 | =head1 LICENSE |
89 | |
90 | You may distribute this code under the same terms as Perl itself. |
91 | |
92 | =cut |