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};
28 $self->open unless $self->{dbh};
36 # TODO: Is this really what should happen?
37 return if $self->{dbh};
39 $self->{dbh} = DBI->connect(
40 $self->{dbi}{dsn}, $self->{dbi}{username}, $self->{dbi}{password}, {
44 %{ $self->{dbi}{connect_args} || {} },
53 $self->{dbh}->disconnect if $self->{dbh};
59 $self->close if ref $self;
62 # Is there a portable way of determining writability to a DBH?
82 my ($table, $cond, @cols) = @_;
84 $cond = { id => $cond } unless ref $cond;
86 my @keys = keys %$cond;
87 my $where = join ' AND ', map { "`$_` = ?" } @keys;
89 return $self->{dbh}->selectall_arrayref(
90 "SELECT `@{[join '`,`', @cols ]}` FROM $table WHERE $where",
91 { Slice => {} }, @{$cond}{@keys},
99 my ($table, $id, %args) = @_;
101 my @keys = keys %args;
103 "REPLACE INTO $table ( `id`, "
104 . join( ',', map { "`$_`" } @keys )
106 . join( ',', ('?') x (@keys + 1) )
110 #warn "@args{@keys}\n";
111 $self->{dbh}->do( $sql, undef, $id, @args{@keys} );
113 return $self->{dbh}{mysql_insertid};
118 my ($table, $cond) = @_;
120 $cond = { id => $cond } unless ref $cond;
122 my @keys = keys %$cond;
123 my $where = join ' AND ', map { "`$_` = ?" } @keys;
126 "DELETE FROM $table WHERE $where", undef, @{$cond}{@keys},