JAF/DBI/Pg.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 84 CC 14 SCAR 45.6
CC
14
Cov
0%
_record_sql
156.0
insert
12.0
last_insert_id
2.0
CRAP 95.9
1 package JAF::DBI::Pg;
2  
3 use strict;
4 use base qw( JAF::DBI );
5  
6 sub last_insert_id {
7 shift->{last_insert_id}
8 }
9  
10 sub date_format {
11 return sprintf "%04d-%02d-%02d", (shift =~ /(\d{1,2}).(\d{1,2}).(\d{4})/)[2,1,0];
12 }
13 sub datetime_format {
14 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];
15 }
16  
17 sub insert {
18 my ($self, $params, $options) = @_;
19 if($self->{key} && !ref $self->{key}) {
20 $params->{$self->{key}} = $self->{dbh}->selectrow_array("select nextval('seq_$self->{table}')") unless($params->{$self->{key}});
21 $self->{last_insert_id} = $params->{$self->{key}};
22 }
23 return $self->SUPER::insert($params, $options)
24 }
25  
26 sub _record_sql {
27 my ($self, $options) = @_;
28
29 my @cols = $options && $options->{cols} ? @{$options->{cols}} : @{$self->{cols}};
30 foreach my $c(!$self->{key} ? () : ref $self->{key} eq 'ARRAY' ? @{$self->{key}} : ($self->{key})) {
31 push @cols, $c unless(grep {$_ eq $c} @cols)
32 }
33 my $criteria = $options->{criteria};
34
35 my $return = "select ".(join ',', @cols)." from $self->{table} ";
36 if ($criteria) {
37 $return .= " where ".(ref $criteria eq 'ARRAY' ? join ' and ', map {"$_ = ?"} @$criteria : "$criteria = ?");
38 }
39 if($options->{order_by}) {
40 if($options->{order_by} =~ /^(.+)(\+|\-)$/) {
41 $return .= " order by $1 ".($2 eq '-' ? 'desc' : 'asc')
42 } else {
43 $return .= " order by $options->{order_by}"
44 }
45 }
46 $return .= " offset $options->{offset}" if $options->{offset};
47 $return .= " limit $options->{limit}" if $options->{limit};
48 return $return;
49 }
50  
51 1;