From: Matt S Trout Date: Fri, 19 Apr 2019 05:03:02 +0000 (+0000) Subject: finish porting to join_query_parts as arrayref X-Git-Tag: v2.000000~3^2~188 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=68a92d22660cd54b5648520eeee452b66703ec7a;p=dbsrgits%2FSQL-Abstract.git finish porting to join_query_parts as arrayref --- diff --git a/lib/SQL/Abstract/Clauses.pm b/lib/SQL/Abstract/Clauses.pm index c790b38..f60ddad 100644 --- a/lib/SQL/Abstract/Clauses.pm +++ b/lib/SQL/Abstract/Clauses.pm @@ -185,7 +185,7 @@ sub select { my $stmt = do { if (ref(my $sel = $args[0]) eq 'HASH') { - $self->render_statement({ -select => $sel }); + $sel } else { my %clauses; @clauses{qw(from select where order_by)} = @args; @@ -206,32 +206,50 @@ sub select { sub update { my ($self, $table, $set, $where, $options) = @_; - return $self->render_statement({ -update => $_[1] }) if ref($_[1]) eq 'HASH'; - - my %clauses; - @clauses{qw(target set where)} = ($table, $set, $where); - puke "Unsupported data type specified to \$sql->update" - unless ref($clauses{set}) eq 'HASH'; - @clauses{keys %$options} = values %$options; - return $self->render_statement({ -update => \%clauses }); + my $stmt = do { + if (ref($table) eq 'HASH') { + $table + } else { + my %clauses; + @clauses{qw(target set where)} = ($table, $set, $where); + puke "Unsupported data type specified to \$sql->update" + unless ref($clauses{set}) eq 'HASH'; + @clauses{keys %$options} = values %$options; + \%clauses; + } + }; + my $rendered = $self->render_statement({ -update => $stmt }); + return wantarray ? @$rendered : $rendered->[0]; } sub delete { my ($self, $table, $where, $options) = @_; - return $self->render_statement({ -delete => $_[1] }) if ref($_[1]) eq 'HASH'; - - my %clauses = (target => $table, where => $where, %{$options||{}}); - return $self->render_statement({ -delete => \%clauses }); + my $stmt = do { + if (ref($table) eq 'HASH') { + $table; + } else { + my %clauses = (target => $table, where => $where, %{$options||{}}); + \%clauses; + } + }; + my $rendered = $self->render_statement({ -delete => $stmt }); + return wantarray ? @$rendered : $rendered->[0]; } sub insert { my ($self, $table, $data, $options) = @_; - return $self->render_statement({ -insert => $_[1] }) if ref($_[1]) eq 'HASH'; - - my %clauses = (target => $table, values => $data, %{$options||{}}); - return $self->render_statement({ -insert => \%clauses }); + my $stmt = do { + if (ref($table) eq 'HASH') { + $table; + } else { + my %clauses = (target => $table, values => $data, %{$options||{}}); + \%clauses; + } + }; + my $rendered = $self->render_statement({ -insert => $stmt }); + return wantarray ? @$rendered : $rendered->[0]; } sub _expand_insert_clause_target { @@ -272,14 +290,12 @@ sub _expand_values { sub _render_values { my ($self, undef, $values) = @_; - my $inner = [ - $self->join_query_parts(' ', - $self->format_keyword('values'), - $self->join_query_parts(', ', - ref($values) eq 'ARRAY' ? @$values : $values - ), + my $inner = $self->join_query_parts(' ', + $self->format_keyword('values'), + $self->join_query_parts(', ', + ref($values) eq 'ARRAY' ? @$values : $values ), - ]; + ); return $self->join_query_parts('', (our $Render_Top_Level ? $inner : ('(', $inner, ')')) ); diff --git a/lib/SQL/Abstract/ExtraClauses.pm b/lib/SQL/Abstract/ExtraClauses.pm index 9eb04a8..6d2fcfd 100644 --- a/lib/SQL/Abstract/ExtraClauses.pm +++ b/lib/SQL/Abstract/ExtraClauses.pm @@ -87,7 +87,7 @@ sub register_defaults { $self->clause_renderer('select.setop' => sub { my ($self, undef, $setop) = @_; - @{ $self->render_aqt($setop) }; + $self->render_aqt($setop); }); $self->renderer($_ => sub { @@ -140,16 +140,16 @@ sub register_defaults { $self->clause_expander('select.with_recursive', $with_expander); $self->clause_renderer('select.with' => sub { my ($self, undef, $with) = @_; - my $q_part = [ $self->join_query_parts(', ', + my $q_part = $self->join_query_parts(', ', map { my ($alias, $query) = @$_; - [ $self->join_query_parts(' ', + $self->join_query_parts(' ', $self->_render_alias($alias), $self->format_keyword('as'), $query, - ) ] + ) } @{$with->{queries}} - ) ]; + ); return $self->join_query_parts(' ', $self->format_keyword(join '_', 'with', ($with->{type}||'')), $q_part, @@ -259,12 +259,12 @@ sub _render_alias { return (@cols ? $self->join_query_parts('', $as, - [ '(' ], - [ $self->join_query_parts( - ', ', - @cols - ) ], - [ ')' ], + '(', + $self->join_query_parts( + ', ', + @cols + ), + ')', ) : $self->render_aqt($as) );