JAF/DBI/mysql.pm untested

filter: Statementstmt 0.0% 0 / 30 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 63 CC 4 SCAR 22.2
CC
4
Cov
0%
replace
20.0
last_insert_id
2.0
date_format
2.0
CRAP 9.2
1 package JAF::DBI::mysql;
2  
3 use strict;
4 use base qw( JAF::DBI );
5  
6 sub last_insert_id { $_[0]->{dbh}->{mysql_insertid} }
7  
8 sub date_format {
9 return sprintf "%04d-%02d-%02d", (shift =~ /(\d{1,2}).(\d{1,2}).(\d{4})/)[2,1,0];
10 }
11 sub datetime_format {
12 return sprintf "%04d-%02d-%02d %02d:%02d:%02d", (shift =~ /(\d{1,2}).(\d{1,2}).(\d{4})\s+(\d{2}).(\d{2}).?(\d{2})?/)[2,1,0,3,4,5];
13 }
14  
15 sub _replace_sql {
16 my ($self, $options) = @_;
17 my $cols = $options->{cols} || $self->{cols};
18 return "replace into $self->{table} (".(join ',', @{$cols}).") values (".(join ',', map {'?'} @{$cols}).")";
19 }
20  
21 sub replace {
22 my ($self, $params, $options) = @_;
23
24 my @cols = $options && $options->{cols} ? @{$options->{cols}} : @{$self->{cols}};
25 @cols = grep {exists $params->{$_}} @cols;
26 my $sql = $options->{sql} || $self->_replace_sql({%$options, cols => \@cols});
27  
28 my $return = $self->{dbh}->do($sql, undef, map {$params->{$_}} @cols);
29 $return ? $self->message(defined $options->{message} ? $options->{message} : $self->{insert_message}) : $self->error($self->{dbh}->errstr());
30 return $return;
31 }
32  
33 1;