1bbfd1d9198707e7121ad6dc85a693a11c318815
[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 use POSIX 'strftime';
6 use File::Copy;
7 use File::Spec;
8
9 use base qw/DBIx::Class::Storage::DBI::MultiDistinctEmulation/;
10
11 sub _dbh_last_insert_id {
12   my ($self, $dbh, $source, $col) = @_;
13   $dbh->func('last_insert_rowid');
14 }
15
16 sub backup
17 {
18   my ($self) = @_;
19
20   ## Where is the db file?
21   my $dsn = $self->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, $dir, $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   my $res = copy($dbname, $file);
39   $self->throw_exception("Backup failed! ($!)") if(!$res);
40
41   return $file;
42 }
43
44 1;
45
46 =head1 NAME
47
48 DBIx::Class::PK::Auto::SQLite - Automatic primary key class for SQLite
49
50 =head1 SYNOPSIS
51
52   # In your table classes
53   __PACKAGE__->load_components(qw/PK::Auto Core/);
54   __PACKAGE__->set_primary_key('id');
55
56 =head1 DESCRIPTION
57
58 This class implements autoincrements for SQLite.
59
60 =head1 AUTHORS
61
62 Matt S. Trout <mst@shadowcatsystems.co.uk>
63
64 =head1 LICENSE
65
66 You may distribute this code under the same terms as Perl itself.
67
68 =cut