No nested transactions in SQLite.
[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, $dir) = @_;
19   $dir ||= './';
20
21   ## Where is the db file?
22   my $dsn = $self->connect_info()->[0];
23
24   my $dbname = $1 if($dsn =~ /dbname=([^;]+)/);
25   if(!$dbname)
26   {
27     $dbname = $1 if($dsn =~ /^dbi:SQLite:(.+)$/i);
28   }
29   $self->throw_exception("Cannot determine name of SQLite db file") 
30     if(!$dbname || !-f $dbname);
31
32 #  print "Found database: $dbname\n";
33 #  my $dbfile = file($dbname);
34   my ($vol, $dbdir, $file) = File::Spec->splitpath($dbname);
35 #  my $file = $dbfile->basename();
36   $file = strftime("%y%m%d%h%M%s", localtime()) . $file; 
37   $file = "B$file" while(-f $file);
38
39   mkdir($dir) unless -f $dir;
40   my $backupfile = File::Spec->catfile($dir, $file);
41
42   my $res = copy($dbname, $backupfile);
43   $self->throw_exception("Backup failed! ($!)") if(!$res);
44
45   return $backupfile;
46 }
47
48 sub txn_begin
49 {
50   my $self = shift;
51   return if(!$self->{transaction_depth});
52
53   $self->next::method(@_);
54 }
55
56
57 1;
58
59 =head1 NAME
60
61 DBIx::Class::PK::Auto::SQLite - Automatic primary key class for SQLite
62
63 =head1 SYNOPSIS
64
65   # In your table classes
66   __PACKAGE__->load_components(qw/PK::Auto Core/);
67   __PACKAGE__->set_primary_key('id');
68
69 =head1 DESCRIPTION
70
71 This class implements autoincrements for SQLite.
72
73 =head1 AUTHORS
74
75 Matt S. Trout <mst@shadowcatsystems.co.uk>
76
77 =head1 LICENSE
78
79 You may distribute this code under the same terms as Perl itself.
80
81 =cut