1275406672bedc0580e913c31ad7786f0034d8f5
[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 Path::Class;
8
9 use base qw/DBIx::Class::Storage::DBI::MultiDistinctEmulation/;
10
11 sub last_insert_id {
12   return $_[0]->dbh->func('last_insert_rowid');
13 }
14
15 sub backup
16 {
17   my ($self) = @_;
18
19   ## Where is the db file?
20   my $dsn = $self->connect_info()->[0];
21
22   my $dbname = $1 if($dsn =~ /dbname=([^;]+)/);
23   if(!$dbname)
24   {
25     $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i);
26   }
27   $self->throw_exception("Cannot determine name of SQLite db file") 
28     if(!$dbname || !-f $dbname);
29
30   print "Found database: $dbname\n";
31   my $dbfile = file($dbname);
32 #  my ($vol, $dir, $file) = File::Spec->splitpath($dbname);
33   my $file = $dbfile->basename();
34   $file = strftime("%y%m%d%h%M%s", localtime()) . $file; 
35   $file = "B$file" while(-f $file);
36   
37   my $res = copy($dbname, $file);
38   $self->throw_exception("Backup failed! ($!)") if(!$res);
39
40   return $file;
41 }
42
43 1;
44
45 =head1 NAME
46
47 DBIx::Class::PK::Auto::SQLite - Automatic primary key class for SQLite
48
49 =head1 SYNOPSIS
50
51   # In your table classes
52   __PACKAGE__->load_components(qw/PK::Auto Core/);
53   __PACKAGE__->set_primary_key('id');
54
55 =head1 DESCRIPTION
56
57 This class implements autoincrements for SQLite.
58
59 =head1 AUTHORS
60
61 Matt S. Trout <mst@shadowcatsystems.co.uk>
62
63 =head1 LICENSE
64
65 You may distribute this code under the same terms as Perl itself.
66
67 =cut