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?
97 my ($table, $cond, @cols) = @_;
99 $cond = { id => $cond } unless ref $cond;
101 my @keys = keys %$cond;
102 my $where = join ' AND ', map { "`$_` = ?" } @keys;
104 return $self->{dbh}->selectall_arrayref(
105 "SELECT `@{[join '`,`', @cols ]}` FROM $table WHERE $where",
106 { Slice => {} }, @{$cond}{@keys},
114 my ($table, $id, %args) = @_;
116 my @keys = keys %args;
118 "REPLACE INTO $table ( `id`, "
119 . join( ',', map { "`$_`" } @keys )
121 . join( ',', ('?') x (@keys + 1) )
123 $self->{dbh}->do( $sql, undef, $id, @args{@keys} );
125 return $self->{dbh}->last_insert_id("", "", "", "");
130 my ($table, $cond) = @_;
132 $cond = { id => $cond } unless ref $cond;
134 my @keys = keys %$cond;
135 my $where = join ' AND ', map { "`$_` = ?" } @keys;
138 "DELETE FROM $table WHERE $where", undef, @{$cond}{@keys},
142 sub driver { $_[0]{driver} }
146 my $driver = $self->driver;
147 if ( $driver eq 'sqlite' ) {
150 elsif ( $driver eq 'mysql' ) {
154 die "rand_function undefined for $driver\n";