From: Matt S Trout Date: Sat, 6 Apr 2019 02:07:58 +0000 (+0000) Subject: add update to clauses X-Git-Tag: v2.000000~3^2~269 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d5f4b9d398778b37c793563e0f655128b93189cd;p=dbsrgits%2FSQL-Abstract.git add update to clauses --- diff --git a/lib/SQL/Abstract/Clauses.pm b/lib/SQL/Abstract/Clauses.pm index b2d682d..c640109 100644 --- a/lib/SQL/Abstract/Clauses.pm +++ b/lib/SQL/Abstract/Clauses.pm @@ -5,6 +5,8 @@ use warnings; use mro 'c3'; use base 'SQL::Abstract'; +BEGIN { *puke = \&SQL::Abstract::puke } + sub new { shift->next::method(@_)->register_defaults } @@ -22,6 +24,17 @@ sub register_defaults { }; $self->{expand_clause}{'select.where'} = 'expand_expr'; $self->{expand_clause}{'select.order_by'} = '_expand_order_by'; + $self->{clauses_of}{update} = [ qw(update set where returning) ]; + $self->{expand}{update} = sub { shift->_expand_statement(@_) }; + $self->{render}{update} = sub { shift->_render_statement(update => @_) }; + $self->{expand_clause}{'update.update'} = sub { + $_[0]->expand_expr($_[1], -ident) + }; + $self->{expand_clause}{'update.set'} = '_expand_update_set_values'; + $self->{expand_clause}{'update.where'} = 'expand_expr'; + $self->{expand_clause}{'update.returning'} = sub { + shift->_expand_maybe_list_expr(@_, -ident); + }; return $self; } @@ -71,4 +84,15 @@ sub select { return wantarray ? ($sql, @bind) : $sql; } +sub update { + my ($self, $table, $set, $where, $options) = @_; + my %clauses; + @clauses{qw(update set where)} = ($table, $set, $where); + puke "Unsupported data type specified to \$sql->update" + unless ref($clauses{set}) eq 'HASH'; + @clauses{keys %$options} = values %$options; + my ($sql, @bind) = $self->render_expr({ -update => \%clauses }); + return wantarray ? ($sql, @bind) : $sql; +} + 1;