X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FAbstract.pm;h=11f568d431fc8711241c79a67bba71d9877f76ea;hb=1c0c0f41f6218aef223eb22829576c845beae088;hp=df94d9686ff23130ff763cb373518e7005909e60;hpb=b07681b111aa75ea79cf1e70d00586117deae0a5;p=scpubgit%2FQ-Branch.git diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index df94d96..11f568d 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -155,7 +155,8 @@ sub new { $opt{sqlfalse} ||= '0=1'; # special operators - $opt{user_special_ops} = [ @{$opt{special_ops} ||= []} ]; + $opt{special_ops} ||= []; + # regexes are applied in order, thus push after user-defines push @{$opt{special_ops}}, @BUILTIN_SPECIAL_OPS; @@ -181,6 +182,8 @@ sub new { qw(op func value bind ident literal) }; + $opt{expand_unary} = {}; + return bless \%opt, $class; } @@ -379,6 +382,7 @@ sub _expand_update_set_values { $self->_expand_maybe_list_expr( [ map { my ($k, $set) = @$_; + $set = { -bind => $_ } unless defined $set; +{ -op => [ '=', { -ident => $k }, $set ] }; } map { @@ -429,6 +433,7 @@ sub select { sub _select_fields { my ($self, $fields) = @_; + return $fields unless ref($fields); return $self->_render_expr( $self->_expand_maybe_list_expr($fields, undef, '-ident') ); @@ -494,6 +499,7 @@ sub where { sub _expand_expr { my ($self, $expr, $logic, $default_scalar_to) = @_; local our $Default_Scalar_To = $default_scalar_to if $default_scalar_to; + our $Expand_Depth ||= 0; local $Expand_Depth = $Expand_Depth + 1; return undef unless defined($expr); if (ref($expr) eq 'HASH') { if (keys %$expr > 1) { @@ -504,14 +510,19 @@ sub _expand_expr { sort keys %$expr ] }; } - return unless %$expr; + return { -literal => [ '' ] } unless keys %$expr; return $self->_expand_expr_hashpair(%$expr, $logic); } if (ref($expr) eq 'ARRAY') { my $logic = lc($logic || $self->{logic}); $logic eq 'and' or $logic eq 'or' or puke "unknown logic: $logic"; - my @expr = @$expr; + #my @expr = @$expr; + my @expr = grep { + (ref($_) eq 'ARRAY' and @$_) + or (ref($_) eq 'HASH' and %$_) + or 1 + } @$expr; my @res; @@ -548,6 +559,8 @@ sub _expand_expr { die "notreached"; } +my $Nest_Warned = 0; + sub _expand_expr_hashpair { my ($self, $k, $v, $logic) = @_; unless (defined($k) and length($k)) { @@ -564,6 +577,17 @@ sub _expand_expr_hashpair { . "You probably wanted ...-and => [ $k => COND1, $k => COND2 ... ]"; } if ($k eq '-nest') { + # DBIx::Class requires a nest warning to be emitted once but the private + # method it overrode to do so no longer exists + if (ref($self) =~ /^DBIx::Class::SQLMaker/) { + unless ($Nest_Warned) { + belch( + "-nest in search conditions is deprecated, you most probably wanted:\n" + .q|{..., -and => [ \%cond0, \@cond1, \'cond2', \[ 'cond3', [ col => bind ] ], etc. ], ... }| + ); + $Nest_Warned = 1; + } + } return $self->_expand_expr($v); } if ($k eq '-bool') { @@ -595,24 +619,37 @@ sub _expand_expr_hashpair { $op =~ s/^-// if length($op) > 1; # top level special ops are illegal in general - puke "Illegal use of top-level '-$op'" - if List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}; + # note that, arguably, if it makes no sense at top level, it also + # makes no sense on the other side of an = sign or similar but DBIC + # gets disappointingly upset if I disallow it + if ( + (our $Expand_Depth) == 1 + and List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}} + ) { + puke "Illegal use of top-level '-$op'" + } + if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) { + return { -op => [ $op, $v ] }; + } } if ($k eq '-value' and my $m = our $Cur_Col_Meta) { return +{ -bind => [ $m, $v ] }; } + if (my $custom = $self->{expand_unary}{$k}) { + return $self->$custom($v); + } if ($self->{node_types}{$k}) { return { $k => $v }; } - if (my $custom = $self->{custom_expansions}{($k =~ /^-(.*)$/)[0]}) { - return $self->$custom($v); - } if ( ref($v) eq 'HASH' and keys %$v == 1 and (keys %$v)[0] =~ /^-/ ) { my ($func) = $k =~ /^-(.*)$/; + if (List::Util::first { $func =~ $_->{regex} } @{$self->{special_ops}}) { + return +{ -op => [ $func, $self->_expand_expr($v) ] }; + } return +{ -func => [ $func, $self->_expand_expr($v) ] }; } if (!ref($v) or is_literal_value($v)) { @@ -630,11 +667,12 @@ sub _expand_expr_hashpair { return $self->_expand_expr_hashpair($k => { $self->{cmp} => undef }); } if (!ref($v) or Scalar::Util::blessed($v)) { + my $d = our $Default_Scalar_To; return +{ -op => [ $self->{cmp}, { -ident => $k }, - { -bind => [ $k, $v ] } + ($d ? { $d => $v } : { -bind => [ $k, $v ] }) ] }; } @@ -701,8 +739,8 @@ sub _expand_expr_hashpair { ] }; } if ($vk eq 'ident') { - if (! defined $vv or ref $vv) { - puke "-$vk requires a single plain scalar argument (a quotable identifier)"; + if (! defined $vv or (ref($vv) and ref($vv) eq 'ARRAY')) { + puke "-$vk requires a single plain scalar argument (a quotable identifier) or an arrayref of identifier parts"; } return +{ -op => [ $self->{cmp}, @@ -738,9 +776,16 @@ sub _expand_expr_hashpair { ] }; } } - if (my $us = List::Util::first { $vk =~ $_->{regex} } @{$self->{user_special_ops}}) { + if (my $us = List::Util::first { $vk =~ $_->{regex} } @{$self->{special_ops}}) { return { -op => [ $vk, { -ident => $k }, $vv ] }; } + if (my $us = List::Util::first { $vk =~ $_->{regex} } @{$self->{unary_ops}}) { + return { -op => [ + $self->{cmp}, + { -ident => $k }, + { -op => [ $vk, $vv ] } + ] }; + } if (ref($vv) eq 'ARRAY') { my ($logic, @values) = ( (defined($vv->[0]) and $vv->[0] =~ /^-(and|or)$/i) @@ -821,9 +866,7 @@ sub _expand_expr_hashpair { my ($sql, @bind) = @$literal; if ($self->{bindtype} eq 'columns') { for (@bind) { - if (!defined $_ || ref($_) ne 'ARRAY' || @$_ != 2) { - puke "bindtype 'columns' selected, you need to pass: [column_name => bind_value]" - } + $self->_assert_bindval_matches_bindtype($_); } } return +{ -literal => [ $self->_quote($k).' '.$sql, @bind ] }; @@ -846,7 +889,12 @@ sub _recurse_where { #print STDERR Data::Dumper::Concise::Dumper([ $where, $logic ]); - my $where_exp = $self->_expand_expr($where, $logic); + # Special case: top level simple string treated as literal + + my $where_exp = (ref($where) + ? $self->_expand_expr($where, $logic) + : { -literal => [ $where ] }); +#::Dwarn([ EXPANDED => $where_exp ]); #print STDERR Data::Dumper::Concise::Dumper([ EXP => $where_exp ]); @@ -856,7 +904,6 @@ sub _recurse_where { # my ($sql, @bind) = $self->$method($where_exp, $logic); my ($sql, @bind) = defined($where_exp) ? $self->_render_expr($where_exp) : (undef); - # DBIx::Class used to call _recurse_where in scalar context # something else might too... if (wantarray) { @@ -939,11 +986,15 @@ sub _render_op { if (my $h = $special{$op}) { return $self->$h(\@args); } - if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{user_special_ops}}) { + my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{special_ops}}; + if ($us and @args > 1) { puke "Special op '${op}' requires first value to be identifier" unless my ($k) = map $_->{-ident}, grep ref($_) eq 'HASH', $args[0]; return $self->${\($us->{handler})}($k, $op, $args[1]); } + if (my $us = List::Util::first { $op =~ $_->{regex} } @{$self->{unary_ops}}) { + return $self->${\($us->{handler})}($op, $args[0]); + } my $final_op = $op =~ /^(?:is|not)_/ ? join(' ', split '_', $op) : $op; if (@args == 1 and $op !~ /^(and|or)$/) { my ($expr_sql, @bind) = $self->_render_expr($args[0]); @@ -953,10 +1004,15 @@ sub _render_op { ? "${expr_sql} ${op_sql}" : "${op_sql} ${expr_sql}" ); - return (($op eq 'not' ? '('.$final_sql.')' : $final_sql), @bind); + return (($op eq 'not' || $us ? '('.$final_sql.')' : $final_sql), @bind); + #} elsif (@args == 0) { + # return ''; } else { - my @parts = map [ $self->_render_expr($_) ], @args; - my ($final_sql) = map +($op =~ /^(and|or)$/ ? "(${_})" : $_), join( + my @parts = grep length($_->[0]), map [ $self->_render_expr($_) ], @args; + return '' unless @parts; + my $is_andor = !!($op =~ /^(and|or)$/); + return @{$parts[0]} if $is_andor and @parts == 1; + my ($final_sql) = map +($is_andor ? "( ${_} )" : $_), join( ($final_op eq ',' ? '' : ' ').$self->_sqlcase($final_op).' ', map $_->[0], @parts ); @@ -1026,25 +1082,41 @@ sub _open_outer_paren { # ORDER BY #====================================================================== -sub _order_by { +sub _expand_order_by { my ($self, $arg) = @_; - return '' unless defined($arg) and not (ref($arg) eq 'ARRAY' and !@$arg); + return unless defined($arg) and not (ref($arg) eq 'ARRAY' and !@$arg); my $expander = sub { my ($self, $dir, $expr) = @_; + my @to_expand = ref($expr) eq 'ARRAY' ? @$expr : $expr; + foreach my $arg (@to_expand) { + if ( + ref($arg) eq 'HASH' + and keys %$arg > 1 + and grep /^-(asc|desc)$/, keys %$arg + ) { + puke "ordering direction hash passed to order by must have exactly one key (-asc or -desc)"; + } + } my @exp = map +(defined($dir) ? { -op => [ $dir => $_ ] } : $_), map $self->_expand_expr($_, undef, -ident), - ref($expr) eq 'ARRAY' ? @$expr : $expr; + map ref($_) eq 'ARRAY' ? @$_ : $_, @to_expand; return (@exp > 1 ? { -op => [ ',', @exp ] } : $exp[0]); }; - local $self->{custom_expansions} = { - asc => sub { shift->$expander(asc => @_) }, - desc => sub { shift->$expander(desc => @_) }, - }; + local @{$self->{expand_unary}}{qw(-asc -desc)} = ( + sub { shift->$expander(asc => @_) }, + sub { shift->$expander(desc => @_) }, + ); + + return $self->$expander(undef, $arg); +} + +sub _order_by { + my ($self, $arg) = @_; - my $expanded = $self->$expander(undef, $arg); + return '' unless defined(my $expanded = $self->_expand_order_by($arg)); my ($sql, @bind) = $self->_render_expr($expanded); @@ -1053,6 +1125,28 @@ sub _order_by { return wantarray ? ($final_sql, @bind) : $final_sql; } +# _order_by no longer needs to call this so doesn't but DBIC uses it. + +sub _order_by_chunks { + my ($self, $arg) = @_; + + return () unless defined(my $expanded = $self->_expand_order_by($arg)); + + return $self->_chunkify_order_by($expanded); +} + +sub _chunkify_order_by { + my ($self, $expanded) = @_; + for ($expanded) { + if (ref() eq 'HASH' and my $op = $_->{-op}) { + if ($op->[0] eq ',') { + return map $self->_chunkify_order_by($_), @{$op}[1..$#$op]; + } + } + return [ $self->_render_expr($_) ]; + } +} + #====================================================================== # DATASOURCE (FOR NOW, JUST PLAIN TABLE OR LIST OF TABLES) #======================================================================