From: Matt S Trout Date: Fri, 29 Jul 2011 20:51:02 +0000 (+0000) Subject: handle crack fueled inner function thing X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3a507101;p=dbsrgits%2FSQL-Abstract.git handle crack fueled inner function thing --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index 0033758..eb6104c 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -512,7 +512,7 @@ sub _where_to_dq { or (ref($where) eq 'REF' and ref($$where) eq 'ARRAY') ) { return $self->_literal_to_dq($$where); - } elsif (!ref($where)) { + } elsif (!ref($where) or Scalar::Util::blessed($where)) { return $self->_value_to_dq($where); } die "Can't handle $where"; @@ -643,12 +643,26 @@ sub _where_hashpair_to_dq { args => [ ref($v) ? $self->_where_to_dq($v) : $self->_ident_to_dq($v) ] }; } else { + my @args = do { + if (ref($v) eq 'HASH' and keys(%$v) == 1 and (keys %$v)[0] =~ /-(.*)/) { + my ($inner) = values %$v; + +{ + type => DQ_OPERATOR, + operator => { 'SQL.Naive' => uc($1) }, + args => [ + (map $self->_where_to_dq($_), + (ref($inner) eq 'ARRAY' ? @$inner : $inner)) + ] + }; + } else { + (map $self->_where_to_dq($_), (ref($v) eq 'ARRAY' ? @$v : $v)) + } + }; return +{ type => DQ_OPERATOR, operator => { 'SQL.Naive' => 'apply' }, args => [ - $self->_ident_to_dq($op), - (map $self->_where_to_dq($_), (ref($v) eq 'ARRAY' ? @$v : $v)) + $self->_ident_to_dq($op), @args ], }; } diff --git a/t/02where.t b/t/02where.t index 351d86c..02f5baf 100644 --- a/t/02where.t +++ b/t/02where.t @@ -347,7 +347,7 @@ my @handle_tests = ( }, { where => { timestamp => { '>=' => { -to_date => '2009-12-21 00:00:00' } } }, - stmt => " WHERE ( timestamp >= TO_DATE ? )", + stmt => " WHERE ( timestamp >= TO_DATE(?) )", bind => ['2009-12-21 00:00:00'], }, @@ -400,9 +400,14 @@ my @handle_tests = ( stmt => " WHERE ( (NOT ( c AND (NOT ( (NOT a = ?) AND (NOT b) )) )) ) ", bind => [ 1 ], }, + { + where => { foo => { '>=', [] } }, + stmt => " WHERE 0=1", + bind => [ ], + }, ); -plan tests => ( @handle_tests * 2 ) + 1; +plan tests => ( @handle_tests * 2 ); for my $case (@handle_tests) { local $Data::Dumper::Terse = 1; @@ -417,8 +422,3 @@ for my $case (@handle_tests) { fail "Died: $e: Search term:\n" . Dumper $case->{where}; } } - -ok(exception { - my $sql = SQL::Abstract->new; - $sql->where({ foo => { '>=' => [] }},); -});