From: Matt S Trout Date: Tue, 18 Sep 2018 02:13:37 +0000 (+0000) Subject: set values in update via expand/render X-Git-Tag: v1.90_01~421 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=89690da26325b14721e2bbebdf12406b868b3194;p=dbsrgits%2FSQL-Abstract.git set values in update via expand/render --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index e7a0827..71021e3 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -364,59 +364,32 @@ sub update { 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; - my $label = $self->_quote($k); - - $self->_SWITCH_refkind($v, { - ARRAYREF => sub { - if ($self->{array_datatypes}) { # array datatype - push @set, "$label = ?"; - push @all_bind, $self->_bindtype($k, $v); - } - else { # literal SQL with bind - my ($sql, @bind) = @$v; - $self->_assert_bindval_matches_bindtype(@bind); - push @set, "$label = $sql"; - push @all_bind, @bind; - } - }, - ARRAYREFREF => sub { # literal SQL with bind - my ($sql, @bind) = @${$v}; - $self->_assert_bindval_matches_bindtype(@bind); - push @set, "$label = $sql"; - push @all_bind, @bind; - }, - SCALARREF => sub { # literal SQL without bind - push @set, "$label = $$v"; - }, - HASHREF => sub { - my ($op, $arg, @rest) = %$v; - - puke 'Operator calls in update must be in the form { -op => $arg }' - if (@rest or not $op =~ /^\-(.+)/); - - local our $Cur_Col_Meta = $k; - my ($sql, @bind) = $self->_render_expr( - $self->_expand_expr_hashpair($op, $arg) - ); - - push @set, "$label = $sql"; - push @all_bind, @bind; - }, - SCALAR_or_UNDEF => sub { - push @set, "$label = ?"; - push @all_bind, $self->_bindtype($k, $v); - }, - }); - } - - # generate sql - my $sql = join ', ', @set; + return $self->_render_expr( + $self->_expand_update_set_values($data), + ); +} - return ($sql, @all_bind); +sub _expand_update_set_values { + my ($self, $data) = @_; + $self->_expand_maybe_list_expr( [ + map { + my ($k, $set) = @$_; + +{ -op => [ '=', { -ident => $k }, $set ] }; + } + map { + my $k = $_; + my $v = $data->{$k}; + (ref($v) eq 'ARRAY' + ? ($self->{array_datatypes} + ? [ $k, +{ -bind => [ $k, $v ] } ] + : [ $k, +{ -literal => $v } ]) + : do { + local our $Cur_Col_Meta = $k; + [ $k, $self->_expand_expr($v) ] + } + ); + } sort keys %$data + ] ); } # So that subclasses can override UPDATE ... RETURNING separately from diff --git a/t/01generate.t b/t/01generate.t index 3e55f15..f328981 100644 --- a/t/01generate.t +++ b/t/01generate.t @@ -860,7 +860,7 @@ for my $t (@tests) { \@bind, $quoted ? $t->{stmt_q}: $t->{stmt}, $t->{bind} - ); + ) || diag dumper({ args => $t->{args}, result => $stmt });; } } }