1 package DBM::Deep::Storage::DBI;
6 use warnings FATAL => 'all';
8 use base 'DBM::Deep::Storage';
22 # Grab the parameters we want to use
23 foreach my $param ( keys %$self ) {
24 next unless exists $args->{$param};
25 $self->{$param} = $args->{$param};
29 $self->{driver} = lc $self->{dbh}->{Driver}->{Name};
35 # Foreign keys are turned off by default in SQLite3 (for now)
36 #q.v. http://search.cpan.org/~adamk/DBD-SQLite-1.27/lib/DBD/SQLite.pm#Foreign_Keys
38 if ( $self->driver eq 'sqlite' ) {
39 $self->{dbh}->do( 'PRAGMA foreign_keys = ON' );
48 return if $self->{dbh};
50 $self->{dbh} = DBI->connect(
51 $self->{dbi}{dsn}, $self->{dbi}{username}, $self->{dbi}{password}, {
55 %{ $self->{dbi}{connect_args} || {} },
59 # Should we use the same method as done in new() if passed a $dbh?
60 (undef, $self->{driver}) = map lc, DBI->parse_dsn( $self->{dbi}{dsn} );
67 $self->{dbh}->disconnect if $self->{dbh};
73 $self->close if ref $self;
76 # Is there a portable way of determining writability to a DBH?
92 # $self->{dbh}->commit;
97 # $self->{dbh}->begin_work;
102 # $self->{dbh}->commit;
107 # $self->{dbh}->rollback;
112 my ($table, $cond, @cols) = @_;
114 $cond = { id => $cond } unless ref $cond;
116 my @keys = keys %$cond;
117 my $where = join ' AND ', map { "`$_` = ?" } @keys;
119 return $self->{dbh}->selectall_arrayref(
120 "SELECT `@{[join '`,`', @cols ]}` FROM $table WHERE $where",
121 { Slice => {} }, @{$cond}{@keys},
129 my ($table, $id, %args) = @_;
131 my @keys = keys %args;
133 "REPLACE INTO $table ( `id`, "
134 . join( ',', map { "`$_`" } @keys )
136 . join( ',', ('?') x (@keys + 1) )
138 $self->{dbh}->do( $sql, undef, $id, @args{@keys} );
140 return $self->{dbh}->last_insert_id("", "", "", "");
145 my ($table, $cond) = @_;
147 $cond = { id => $cond } unless ref $cond;
149 my @keys = keys %$cond;
150 my $where = join ' AND ', map { "`$_` = ?" } @keys;
153 "DELETE FROM $table WHERE $where", undef, @{$cond}{@keys},
157 sub driver { $_[0]{driver} }
161 my $driver = $self->driver;
162 if ( $driver eq 'sqlite' ) {
165 elsif ( $driver eq 'mysql' ) {
169 die "rand_function undefined for $driver\n";