| 1 | package JAF::DBI; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use DBI (); | ||
| 5 | use Data::Dumper 'Dumper'; | ||
| 6 | |||
| 7 | sub new { | ||
| 8 | my ($class, $self) = @_; | ||
| 9 | return undef unless $self->{parent}; | ||
| 10 | bless $self, $class; | ||
| 11 | $self->_init(); | ||
| 12 | return $self; | ||
| 13 | } | ||
| 14 | |||
| 15 | sub _init { | ||
| 16 | my $self = shift; | ||
| 17 | |||
| 18 | $self->{insert_message} = "Record's inserted"; | ||
| 19 | $self->{update_message} = "Record's updated"; | ||
| 20 | $self->{delete_message} = "Record's deleted"; | ||
| 21 | } | ||
| 22 | |||
| 23 | sub error { shift()->{parent}->error(@_) } | ||
| 24 | sub message { shift()->{parent}->message(@_) } | ||
| 25 | |||
| 26 | sub fixup { { Columns => {} } } | ||
| 27 | |||
| 28 | sub _insert_sql { | ||
| 29 | my ($self, $options) = @_; | ||
| 30 | |||
| 31 | my $cols = $options->{cols} || $self->{cols}; | ||
| 32 | |||
| 33 | return "insert into $self->{table} (".(join ',', @{$cols}).") values (".(join ',', map {'?'} @{$cols}).")"; | ||
| 34 | } | ||
| 35 | |||
| 36 | sub insert { | ||
| 37 | my ($self, $params, $options) = @_; | ||
| 38 | |||
| 39 | my @cols = $options && $options->{cols} ? @{$options->{cols}} : @{$self->{cols}}; | ||
| 40 | @cols = grep {exists $params->{$_}} @cols; | ||
| 41 | my $sql = $options->{sql} || $self->_insert_sql({%$options, cols => \@cols}); | ||
| 42 | |||
| 43 | if ($options->{debug}) { | ||
| 44 | warn Dumper { sql => $sql, params => $params, options => $options }; | ||
| 45 | } | ||
| 46 | |||
| 47 | my $return = $self->{dbh}->do($sql, undef, map {$params->{$_}} @cols); | ||
| 48 | $return ? $self->message(defined $options->{message} ? $options->{message} : $self->{insert_message}) : $self->error($self->{dbh}->errstr()); | ||
| 49 | return $return; | ||
| 50 | } | ||
| 51 | |||
| 52 | sub _update_sql { | ||
| 53 | my ($self, $options) = @_; | ||
| 54 | |||
| 55 | my $cols = $options->{cols} || $self->{cols}; | ||
| 56 | my $criteria = $options->{criteria} || $self->{key}; | ||
| 57 | |||
| 58 | return "update $self->{table} set ".(join ',', map {"$_ = ?"} @$cols)." where ".(ref $criteria eq 'ARRAY' ? join ' and ', map {"$_ = ?"} @$criteria : "$criteria = ?"); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub update { | ||
| 62 | my ($self, $params, $options) = @_; | ||
| 63 | |||
| 64 | $options->{criteria} ||= $self->{key}; | ||
| 65 | |||
| 66 | my @cols = $options && $options->{cols} ? @{$options->{cols}} : @{$self->{cols}}; | ||
| 67 | @cols = grep {exists $params->{$_} && (!ref $self->{key} || $self->{key} ne $_)} @cols; | ||
| 68 | my $sql = $options->{sql} || $self->_update_sql({%$options, cols => \@cols}); | ||
| 69 | |||
| 70 | my $return = $self->{dbh}->do($sql, undef, (map {$params->{$_}} @cols), ref ($options->{criteria}) eq 'ARRAY' ? map {$params->{$_}} @{$options->{criteria}} : $params->{$options->{criteria}}); | ||
| 71 | $return ? $self->message(defined $options->{message} ? $options->{message} : $self->{update_message}) : $self->error($self->{dbh}->errstr()); | ||
| 72 | return $return; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub _delete_sql { | ||
| 76 | my ($self, $options) = @_; | ||
| 77 | |||
| 78 | my $criteria = $options->{criteria} || $self->{key}; | ||
| 79 | |||
| 80 | return "delete from $self->{table} where ".(ref $criteria eq 'ARRAY' ? join ' and ', map {"$_ = ?"} @$criteria : "$criteria = ?"); | ||
| 81 | } | ||
| 82 | |||
| 83 | sub delete { | ||
| 84 | my ($self, $params, $options) = @_; | ||
| 85 | |||
| 86 | $options->{criteria} ||= $self->{key}; | ||
| 87 | my $sql = $options->{sql} || $self->_delete_sql($options); | ||
| 88 | |||
| 89 | if ($options->{debug}) { | ||
| 90 | warn Dumper { sql => $sql, params => $params, options => $options }; | ||
| 91 | } | ||
| 92 | |||
| 93 | my $return = $self->{dbh}->do($sql, undef, ref ($options->{criteria}) eq 'ARRAY' ? map {$params->{$_}} @{$options->{criteria}} : $params->{$options->{criteria}}); | ||
| 94 | $return ? $self->message(defined $options->{message} ? $options->{message} : $self->{delete_message}) : $self->error($self->{dbh}->errstr()); | ||
| 95 | return $return; | ||
| 96 | } | ||
| 97 | |||
| 98 | sub _record_sql { | ||
| 99 | my ($self, $options) = @_; | ||
| 100 | |||
| 101 | my @cols = $options && $options->{cols} ? @{$options->{cols}} : @{$self->{cols}}; | ||
| 102 | foreach my $c(!$self->{key} ? () : ref $self->{key} eq 'ARRAY' ? @{$self->{key}} : ($self->{key})) { | ||
| 103 | push @cols, $c unless(grep {$_ eq $c} @cols) | ||
| 104 | } | ||
| 105 | my $criteria = $options->{criteria}; | ||
| 106 | |||
| 107 | my $return = "select ".(join ',', @cols)." from $self->{table} "; | ||
| 108 | if ($criteria) { | ||
| 109 | $return .= " where ".(ref $criteria eq 'ARRAY' ? join ' and ', map {"$_ = ?"} @$criteria : "$criteria = ?"); | ||
| 110 | } | ||
| 111 | if($options->{order_by}) { | ||
| 112 | if($options->{order_by} =~ /^(.+)(\+|\-)$/) { | ||
| 113 | $return .= " order by $1 ".($2 eq '-' ? 'desc' : 'asc') | ||
| 114 | } else { | ||
| 115 | $return .= " order by $options->{order_by}" | ||
| 116 | } | ||
| 117 | } | ||
| 118 | $return .= " limit $options->{limit}" if $options->{limit}; | ||
| 119 | return $return; | ||
| 120 | } | ||
| 121 | |||
| 122 | sub record { | ||
| 123 | my ($self, $params, $options) = @_; | ||
| 124 | |||
| 125 | $options->{cols} ||= $self->{cols}; | ||
| 126 | $options->{criteria} ||= $self->{key}; | ||
| 127 | my $sql = $options->{sql} || $self->_record_sql($options); | ||
| 128 | |||
| 129 | $self->{dbh}->selectrow_hashref($sql, undef, ref ($options->{criteria}) eq 'ARRAY' ? map {$params->{$_}} @{$options->{criteria}} : $params->{$options->{criteria}}); | ||
| 130 | } | ||
| 131 | |||
| 132 | sub records { | ||
| 133 | my ($self, $params, $options) = @_; | ||
| 134 | |||
| 135 | $options->{cols} ||= $self->{cols}; | ||
| 136 | my $sql = $options->{sql} || $self->_record_sql($options); | ||
| 137 | |||
| 138 | my $return = $self->{dbh}->selectall_arrayref($sql, $self->fixup, exists $options->{criteria} ? ref ($options->{criteria}) eq 'ARRAY' ? map {$params->{$_}} @{$options->{criteria}} : $params->{$options->{criteria}} : ()); | ||
| 139 | |||
| 140 | return @$return ? $return : undef; | ||
| 141 | } | ||
| 142 | |||
| 143 | sub count { | ||
| 144 | my ($self, $params, $options) = @_; | ||
| 145 | $options->{sql} = "select count(*) from $self->{table}"; | ||
| 146 | my @crit = !$options->{criteria} ? () : ref $options->{criteria} eq 'ARRAY' ? @{$options->{criteria}} : ($options->{criteria}); | ||
| 147 | $options->{sql} .= " where ".join(' and ', map {"$_ = ?"} @crit) if(@crit); | ||
| 148 | $self->{dbh}->selectrow_array($options->{sql}, undef, map {$params->{$_}} @crit) | ||
| 149 | } | ||
| 150 | |||
| 151 | 1; |