From: Matt S Trout Date: Sun, 14 Apr 2019 18:01:25 +0000 (+0000) Subject: introduce "top level" concept, move -values to Clauses to use it X-Git-Tag: v2.000000~3^2~242 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=434c40f14ac84178bd80c054d439f57e7bdbde30;p=dbsrgits%2FSQL-Abstract.git introduce "top level" concept, move -values to Clauses to use it --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index dce3b5c..1d699bb 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -209,7 +209,6 @@ sub new { ident => '_expand_ident', value => '_expand_value', func => '_expand_func', - values => '_expand_values', }; $opt{expand_op} = { @@ -225,7 +224,7 @@ sub new { }; $opt{render} = { - (map +($_, "_render_$_"), qw(op func bind ident literal row values)), + (map +($_, "_render_$_"), qw(op func bind ident literal row)), %{$opt{render}||{}} }; @@ -272,7 +271,7 @@ sub insert { my @parts = ([ $self->_sqlcase('insert into').' '.$table ]); push @parts, [ $self->render_aqt($f_aqt) ] if $f_aqt; - push @parts, [ $self->render_aqt({ -values => $v_aqt }) ]; + push @parts, [ $self->_sqlcase('values') ], [ $self->render_aqt($v_aqt) ]; if ($options->{returning}) { push @parts, [ $self->_insert_returning($options) ]; @@ -1049,17 +1048,6 @@ sub _expand_bind { return { -bind => $bind }; } -sub _expand_values { - my ($self, undef, $values) = @_; - return { -values => [ - map +( - ref($_) eq 'HASH' - ? $self->expand_expr($_) - : +{ -row => [ map $self->expand_expr($_), @$_ ] } - ), ref($values) eq 'ARRAY' ? @$values : $values - ] }; -} - sub _recurse_where { my ($self, $where, $logic) = @_; @@ -1095,16 +1083,6 @@ sub _render_row { return "($sql)", @bind; } -sub _render_values { - my ($self, $values) = @_; - my ($sql, @bind) = $self->_join_parts( - ', ', - map [ $self->render_aqt($_) ], - ref($values) eq 'ARRAY' ? @$values : $values - ); - return $self->_sqlcase('values ').$sql, @bind; -} - sub _render_func { my ($self, $rest) = @_; my ($func, @args) = @$rest; diff --git a/lib/SQL/Abstract/Clauses.pm b/lib/SQL/Abstract/Clauses.pm index 86f74b9..4900dfa 100644 --- a/lib/SQL/Abstract/Clauses.pm +++ b/lib/SQL/Abstract/Clauses.pm @@ -67,8 +67,10 @@ sub register_defaults { ($self->_sqlcase('insert into ').$sql, @bind); }; $self->{render_clause}{'insert.from'} = sub { - return $_[0]->render_aqt($_[1]); + return $_[0]->render_aqt($_[1], 1); }; + $self->{expand}{values} = '_expand_values'; + $self->{render}{values} = '_render_values'; return $self; } @@ -149,7 +151,7 @@ sub _render_statement { if (my $rdr = $self->{render_clause}{"${type}.${clause}"}) { $self->$rdr($clause_expr); } else { - my ($clause_sql, @bind) = $self->render_aqt($clause_expr); + my ($clause_sql, @bind) = $self->render_aqt($clause_expr, 1); my $sql = join ' ', $self->_sqlcase(join ' ', split '_', $clause), $clause_sql; @@ -159,13 +161,31 @@ sub _render_statement { next unless defined($sql) and length($sql); push @parts, \@part; } - return $self->_join_parts(' ', @parts); + my ($sql, @bind) = $self->_join_parts(' ', @parts); + return ( + (our $Render_Top_Level ? $sql : '('.$sql.')'), + @bind + ); +} + +sub render_aqt { + my ($self, $aqt, $top_level) = @_; + local our $Render_Top_Level = $top_level; + return $self->next::method($aqt); +} + +sub render_statement { + my ($self, $expr, $default_scalar_to) = @_; + my ($sql, @bind) = $self->render_aqt( + $self->expand_expr($expr, $default_scalar_to), 1 + ); + return (wantarray ? ($sql, @bind) : $sql); } sub select { my ($self, @args) = @_; - return $self->render_expr({ -select => $_[1] }) if ref($_[1]) eq 'HASH'; + return $self->render_statement({ -select => $_[1] }) if ref($_[1]) eq 'HASH'; my %clauses; @clauses{qw(from select where order_by)} = @args; @@ -176,38 +196,38 @@ sub select { $clauses{select} = { -literal => [ $clauses{select}||'*' ] } unless ref($clauses{select}); - return $self->render_expr({ -select => \%clauses }); + return $self->render_statement({ -select => \%clauses }); } sub update { my ($self, $table, $set, $where, $options) = @_; - return $self->render_expr({ -update => $_[1] }) if ref($_[1]) eq 'HASH'; + 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_expr({ -update => \%clauses }); + return $self->render_statement({ -update => \%clauses }); } sub delete { my ($self, $table, $where, $options) = @_; - return $self->render_expr({ -delete => $_[1] }) if ref($_[1]) eq 'HASH'; + return $self->render_statement({ -delete => $_[1] }) if ref($_[1]) eq 'HASH'; my %clauses = (target => $table, where => $where, %{$options||{}}); - return $self->render_expr({ -delete => \%clauses }); + return $self->render_statement({ -delete => \%clauses }); } sub insert { my ($self, $table, $data, $options) = @_; - return $self->render_expr({ -insert => $_[1] }) if ref($_[1]) eq 'HASH'; + return $self->render_statement({ -insert => $_[1] }) if ref($_[1]) eq 'HASH'; my %clauses = (target => $table, values => $data, %{$options||{}}); - return $self->render_expr({ -insert => \%clauses }); + return $self->render_statement({ -insert => \%clauses }); } sub _expand_insert_clause_target { @@ -224,4 +244,30 @@ sub _expand_insert_clause_values { return (from => { -values => $v_aqt }, ($f_aqt ? (fields => $f_aqt) : ())); } +sub _expand_values { + my ($self, undef, $values) = @_; + return { -values => [ + map +( + ref($_) eq 'HASH' + ? $self->expand_expr($_) + : +{ -row => [ map $self->expand_expr($_), @$_ ] } + ), ref($values) eq 'ARRAY' ? @$values : $values + ] }; +} + +sub _render_values { + my ($self, $values) = @_; + my ($v_sql, @bind) = $self->_join_parts( + ', ', + map [ $self->render_aqt($_) ], + ref($values) eq 'ARRAY' ? @$values : $values + ); + my $sql = $self->_sqlcase('values').' '.$v_sql; + return ( + (our $Render_Top_Level ? $sql : '('.$sql.')'), + @bind + ); +} + + 1; diff --git a/lib/SQL/Abstract/ExtraClauses.pm b/lib/SQL/Abstract/ExtraClauses.pm index e668de8..47f8607 100644 --- a/lib/SQL/Abstract/ExtraClauses.pm +++ b/lib/SQL/Abstract/ExtraClauses.pm @@ -139,9 +139,7 @@ sub _render_as { my ($thing, $as, @cols) = @$args; return $self->_join_parts( ' ', - [ $self->render_aqt( - map +($_->{-ident} ? $_ : { -row => [ $_ ] }), $thing - ) ], + [ $self->render_aqt($thing) ], [ $self->_sqlcase('as') ], (@cols ? [ $self->_join_parts('',