hashpair expansion - defaulting to $self->{cmp}
[dbsrgits/SQL-Abstract.git] / lib / SQL / Abstract.pm
index af8c256..6be4bbb 100644 (file)
@@ -27,7 +27,7 @@ BEGIN {
 # GLOBALS
 #======================================================================
 
-our $VERSION  = '1.86';
+our $VERSION  = '1.87';
 
 # This would confuse some packagers
 $VERSION = eval $VERSION if $VERSION =~ /_/; # numify for warning-free dev releases
@@ -535,14 +535,40 @@ sub where {
   return wantarray ? ($sql, @bind) : $sql;
 }
 
+sub _expand_expr {
+  my ($self, $expr, $logic) = @_;
+  if (ref($expr) eq 'HASH') {
+    if (keys %$expr > 1) {
+      $logic ||= 'and';
+      return +{ "-${logic}" => [
+        map $self->_expand_expr_hashpair($_ => $expr->{$_}, $logic),
+          sort keys %$expr
+      ] };
+    }
+    return $self->_expand_expr_hashpair(%$expr, $logic);
+  }
+  return $expr;
+}
+
+sub _expand_expr_hashpair {
+  my ($self, $k, $v, $logic) = @_;
+  if (!ref($v)) {
+    if ($k !~ /^-/) {
+      return +{ $k => { $self->{cmp} => $v } };
+    }
+  }
+  return { $k => $v };
+}
 
 sub _recurse_where {
   my ($self, $where, $logic) = @_;
 
+  my $where_exp = $self->_expand_expr($where, $logic);
+
   # dispatch on appropriate method according to refkind of $where
-  my $method = $self->_METHOD_FOR_refkind("_where", $where);
+  my $method = $self->_METHOD_FOR_refkind("_where", $where_exp);
 
-  my ($sql, @bind) =  $self->$method($where, $logic);
+  my ($sql, @bind) =  $self->$method($where_exp, $logic);
 
   # DBIx::Class used to call _recurse_where in scalar context
   # something else might too...
@@ -844,7 +870,7 @@ sub _where_op_VALUE {
   # special-case NULL
   if (! defined $rhs) {
     return defined $lhs
-      ? $self->_convert($self->_quote($lhs)) . ' IS NULL'
+      ? $self->_where_hashpair_HASHREF($lhs, { -is => undef })
       : undef
     ;
   }
@@ -1089,19 +1115,14 @@ sub _where_hashpair_ARRAYREFREF {
 sub _where_hashpair_SCALAR {
   my ($self, $k, $v) = @_;
   $self->_debug("NOREF($k) means simple key=val: $k $self->{cmp} $v");
-  my $sql = join ' ', $self->_convert($self->_quote($k)),
-                      $self->_sqlcase($self->{cmp}),
-                      $self->_convert('?');
-  my @bind =  $self->_bindtype($k, $v);
-  return ($sql, @bind);
+  return ($self->_where_hashpair_HASHREF($k, { $self->{cmp} => $v }));
 }
 
 
 sub _where_hashpair_UNDEF {
   my ($self, $k, $v) = @_;
   $self->_debug("UNDEF($k) means IS NULL");
-  my $sql = $self->_quote($k) . $self->_sqlcase(' is null');
-  return ($sql);
+  return $self->_where_hashpair_HASHREF($k, { -is => undef });
 }
 
 #======================================================================