| 1 | package DBomb::Query::Update; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | DBomb::Query::Update - An SQL UPDATE wrapper | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | our $VERSION = '$Revision: 1.4 $'; | ||
| 12 | |||
| 13 | use Carp qw(croak); | ||
| 14 | use Carp::Assert; | ||
| 15 | use DBomb; | ||
| 16 | use DBomb::Conf; | ||
| 17 | |||
| 18 | use Class::MethodMaker | ||
| 19 | 'new_with_init' => 'new', | ||
| 20 | 'get_set' => [ qw(_column_names), # [ column_name, ... ] | ||
| 21 | qw(_values), # [ value, ... ] | ||
| 22 | qw(_table_name), | ||
| 23 | qw(dbh), | ||
| 24 | qw(sth), | ||
| 25 | qw(where_obj), # expr | ||
| 26 | ], | ||
| 27 | ; | ||
| 28 | |||
| 29 | ## init() | ||
| 30 | ## init($dbh,[$_column_names]) | ||
| 31 | sub init | ||
| 32 | { | ||
| 33 | my $self = shift; | ||
| 34 | $self->_column_names([]); | ||
| 35 | $self->_values([]); | ||
| 36 | $self->where_obj(new DBomb::Query::Expr()); | ||
| 37 | |||
| 38 | for(@_){ | ||
| 39 | if ( UNIVERSAL::isa($_,'DBI::db')) { $self->dbh($_); } | ||
| 40 | elsif ( UNIVERSAL::isa($_,'ARRAY')) { $self->_column_names($_); } | ||
| 41 | else{ | ||
| 42 | croak "unrecognized argument to Update->new"; | ||
| 43 | } | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | ## set the table | ||
| 48 | ## table($_table_name) | ||
| 49 | sub table | ||
| 50 | { | ||
| 51 | my $self = shift; | ||
| 52 | assert(UNIVERSAL::isa($self,__PACKAGE__)); | ||
| 53 | |||
| 54 | while(my $a = shift){ | ||
| 55 | if ( UNIVERSAL::isa($a,'DBI::db')) { $self->dbh($a); } | ||
| 56 | else { $self->_table_name($a) } | ||
| 57 | } | ||
| 58 | |||
| 59 | return $self; | ||
| 60 | } | ||
| 61 | |||
| 62 | ## set({ name => value}) | ||
| 63 | ## set( name => value) | ||
| 64 | sub set | ||
| 65 | { | ||
| 66 | my $self = shift; | ||
| 67 | |||
| 68 | if (@_ == 1){ | ||
| 69 | my $hash = shift; | ||
| 70 | assert(UNIVERSAL::isa($hash,'HASH'), "Update::set requires a hashref (or name value)"); | ||
| 71 | while (my ($column_name, $value) = each %$hash){ | ||
| 72 | $self->set($column_name, $value); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | else{ | ||
| 76 | assert(@_ == 2, "Update::set requires a name and a value (or hashref)"); | ||
| 77 | my ($column_name, $value) = @_; | ||
| 78 | |||
| 79 | ## for expression objects, extract the bind_value | ||
| 80 | if (UNIVERSAL::isa($value,'DBomb::Query::Expr')){ | ||
| 81 | |||
| 82 | } | ||
| 83 | |||
| 84 | |||
| 85 | push @{$self->_column_names}, $column_name; | ||
| 86 | push @{$self->_values}, $value; | ||
| 87 | } | ||
| 88 | |||
| 89 | return $self; | ||
| 90 | } | ||
| 91 | |||
| 92 | ## Same as prepare->execute | ||
| 93 | ## update() | ||
| 94 | ## update(@bind_values) | ||
| 95 | ## update($dbh,@bind_values) | ||
| 96 | sub update | ||
| 97 | { | ||
| 98 | my $self = shift; | ||
| 99 | my @bv; | ||
| 100 | |||
| 101 | for (@_){ | ||
| 102 | if (UNIVERSAL::isa($_,'DBI::db')){ $self->dbh($_) } | ||
| 103 | else { push @bv, $_ } | ||
| 104 | } | ||
| 105 | assert(defined($self->dbh), 'update requires a dbh'); | ||
| 106 | |||
| 107 | if (not $self->sth){ $self->prepare } | ||
| 108 | return $self->execute(@bv); | ||
| 109 | } | ||
| 110 | |||
| 111 | ## execute() | ||
| 112 | ## execute(@bind_values) | ||
| 113 | ## execute($dbh,@bind_values) | ||
| 114 | sub execute | ||
| 115 | { | ||
| 116 | my $self = shift; | ||
| 117 | my @bv; | ||
| 118 | |||
| 119 | for (@_){ | ||
| 120 | if (UNIVERSAL::isa($_,'DBI::db')){ $self->dbh($_) } | ||
| 121 | else { push @bv, $_ } | ||
| 122 | } | ||
| 123 | assert(defined($self->dbh), 'update requires a dbh'); | ||
| 124 | |||
| 125 | if (not $self->sth){ $self->prepare } | ||
| 126 | return $self->sth->execute((@{$self->bind_values},@bv)); | ||
| 127 | } | ||
| 128 | |||
| 129 | |||
| 130 | ## prepare() | ||
| 131 | ## prepare($dbh) | ||
| 132 | sub prepare | ||
| 133 | { | ||
| 134 | my $self = shift; | ||
| 135 | |||
| 136 | for (@_){ | ||
| 137 | $self->dbh($_) if UNIVERSAL::isa($_, 'DBI::db'); | ||
| 138 | } | ||
| 139 | assert(defined($self->dbh), 'prepare requires a dbh'); | ||
| 140 | assert($self->_table_name, 'update requires a table_name'); | ||
| 141 | assert(@{$self->_column_names} > 0, 'update requires at least one column'); | ||
| 142 | |||
| 143 | |||
| 144 | if ($DBomb::Conf::prepare_cached){ | ||
| 145 | $self->sth($self->dbh->prepare_cached(scalar $self->sql)); | ||
| 146 | } | ||
| 147 | else{ | ||
| 148 | $self->sth($self->dbh->prepare(scalar $self->sql)); | ||
| 149 | } | ||
| 150 | return $self; | ||
| 151 | } | ||
| 152 | |||
| 153 | ## where(EXPR, @bind_values) | ||
| 154 | sub where | ||
| 155 | { | ||
| 156 | my $self = shift; | ||
| 157 | $self->where_obj->append( new DBomb::Query::Expr(@_)); | ||
| 158 | return $self; | ||
| 159 | } | ||
| 160 | |||
| 161 | sub sql | ||
| 162 | { | ||
| 163 | my $self = shift; | ||
| 164 | $self->dbh(shift) if @_; | ||
| 165 | assert($self->dbh, 'Update::sql requires a dbh'); | ||
| 166 | |||
| 167 | my $sql = "UPDATE "; | ||
| 168 | |||
| 169 | $sql .= $self->_table_name if $self->_table_name; | ||
| 170 | |||
| 171 | ## grab the sql from any expression objects in the _values list | ||
| 172 | $sql .= " SET "; | ||
| 173 | my ($names,$values) = ($self->_column_names,$self->_values); | ||
| 174 | $sql .= join ", ", map { | ||
| 175 | UNIVERSAL::isa($values->[$_],'DBomb::Query::Expr') | ||
| 176 | ? ("$names->[$_] = " . $values->[$_]->sql($self->dbh)) | ||
| 177 | : ("$names->[$_] = ?") | ||
| 178 | } (0..$#$names); | ||
| 179 | |||
| 180 | if ($self->where_obj->is_not_empty){ | ||
| 181 | $sql .= " WHERE "; | ||
| 182 | $sql .= $self->where_obj->sql($self->dbh); | ||
| 183 | } | ||
| 184 | return $sql; | ||
| 185 | } | ||
| 186 | |||
| 187 | sub bind_values | ||
| 188 | { | ||
| 189 | my $self = shift; | ||
| 190 | my $bv = []; | ||
| 191 | assert(@_ == 0, 'bind_values takes no parameters'); | ||
| 192 | |||
| 193 | ## allow Expr and Value objects as well as scalars | ||
| 194 | for (@{$self->_values}, @{$self->where_obj->bind_values}){ | ||
| 195 | if (UNIVERSAL::isa($_,'DBomb::Value')) { push @$bv, $_->value } | ||
| 196 | elsif (UNIVERSAL::isa($_,'DBomb::Query::Expr')) { push @$bv, @{$_->bind_values} } | ||
| 197 | else { push @$bv, $_ } | ||
| 198 | } | ||
| 199 | return $bv; | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 203 | 1; | ||
| 204 | __END__ |