From: Sebastian Riedel Date: Fri, 26 Jan 2018 19:01:20 +0000 (+0100) Subject: add reusable _update_set_values method, consistent with _insert_values X-Git-Tag: v1.85~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9ade906e82820a00f2246603d2aa584a6359ae9d;p=dbsrgits%2FSQL-Abstract.git add reusable _update_set_values method, consistent with _insert_values --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index 0c33b4b..f31fb53 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -368,10 +368,32 @@ sub update { my $options = shift; # first build the 'SET' part of the sql statement - my (@set, @all_bind); puke "Unsupported data type specified to \$sql->update" unless ref $data eq 'HASH'; + my ($sql, @all_bind) = $self->_update_set_values($data); + $sql = $self->_sqlcase('update') . " $table " . $self->_sqlcase('set ') + . $sql; + + if ($where) { + my($where_sql, @where_bind) = $self->where($where); + $sql .= $where_sql; + push @all_bind, @where_bind; + } + + if ($options->{returning}) { + my ($returning_sql, @returning_bind) = $self->_update_returning($options); + $sql .= $returning_sql; + push @all_bind, @returning_bind; + } + + return wantarray ? ($sql, @all_bind) : $sql; +} + +sub _update_set_values { + my ($self, $data) = @_; + + my (@set, @all_bind); for my $k (sort keys %$data) { my $v = $data->{$k}; my $r = ref $v; @@ -419,22 +441,9 @@ sub update { } # generate sql - my $sql = $self->_sqlcase('update') . " $table " . $self->_sqlcase('set ') - . join ', ', @set; - - if ($where) { - my($where_sql, @where_bind) = $self->where($where); - $sql .= $where_sql; - push @all_bind, @where_bind; - } + my $sql = join ', ', @set; - if ($options->{returning}) { - my ($returning_sql, @returning_bind) = $self->_update_returning($options); - $sql .= $returning_sql; - push @all_bind, @returning_bind; - } - - return wantarray ? ($sql, @all_bind) : $sql; + return ($sql, @all_bind); } # So that subclasses can override UPDATE ... RETURNING separately from