Fix behavoiur of * in names
[dbsrgits/SQL-Abstract-2.0-ish.git] / lib / SQL / Abstract / AST / v1.pm
index 7d269e4..34d340e 100644 (file)
@@ -8,124 +8,197 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
   use Moose::Util::TypeConstraints;
   use MooseX::Types::Moose qw/ArrayRef Str Int Ref HashRef/;
   use MooseX::AttributeHelpers;
-  use SQL::Abstract::Types qw/AST ArrayAST HashAST/;
+  use SQL::Abstract::Types qw/AST/;
+  use Devel::PartialDump qw/dump/;
 
   clean;
 
   # set things that are valid in where clauses
-  override _build_where_dispatch_table {
+  override _build_expr_dispatch_table {
     return { 
       %{super()},
-      -in => $self->can('_in'),
-      -not_in => $self->can('_in'),
-      map { +"-$_" => $self->can("_$_") } qw/
+      in => $self->can('_in'),
+      not_in => $self->can('_in'),
+      and => $self->can('_recurse_where'),
+      or => $self->can('_recurse_where'),
+      map { +"$_" => $self->can("_$_") } qw/
         value
         name
         true
         false
+        expr
       /
     };
   }
 
-  method _select(HashAST $ast) {
+  method _select(AST $ast) {
+    # Default to requiring columns and from.
+    # DB specific ones (i.e. mysql/Pg) can not require the FROM part with a bit
+    # of refactoring
     
-  }
+    for (qw/columns tablespec/) {
+      confess "'$_' is required in select AST with " . dump ($ast)
+        unless exists $ast->{$_};
+    }
+   
+    # Check that columns is a -list
+    confess "'columns' should be an array ref, not " . dump($ast->{columns})
+      unless is_ArrayRef($ast->{columns});
 
-  method _where(ArrayAST $ast) {
-    my (undef, @clauses) = @$ast;
-  
-    return 'WHERE ' . $self->_recurse_where(\@clauses);
-  }
+    my $cols = $self->_list({-type => 'list', args => $ast->{columns} });
+
+    my @output = (
+      SELECT => $cols
+    );
 
-  method _order_by(ArrayAST $ast) {
-    my (undef, @clauses) = @$ast;
+    push @output, FROM => $self->dispatch($ast->{tablespec})
+      if exists $ast->{tablespec};
 
-    my @output;
-   
-    for (@clauses) {
-      if ($_->[0] =~ /^-(asc|desc)$/) {
-        my $o = $1;
-        push @output, $self->dispatch($_->[1]) . " " . uc($o);
-        next;
+    if (exists $ast->{where}) {
+      my $sub_ast = $ast->{where};
+
+      confess "$_ option is not an AST: " . dump($sub_ast)
+        unless is_AST($sub_ast);
+
+      push @output, "WHERE", $self->_expr($sub_ast);
+    }
+
+    for (qw/group_by having order_by/) {
+      if (exists $ast->{$_}) {
+        my $sub_ast = $ast->{$_};
+
+        confess "$_ option is not an AST or an ArrayRef: " . dump($sub_ast)
+          unless is_AST($sub_ast) || is_ArrayRef($sub_ast);;
+
+        my $meth = "__$_";
+        push @output, $self->$meth($sub_ast);
       }
-      push @output, $self->dispatch($_);
     }
 
-    return "ORDER BY " . join(", ", @output);
+    return join(' ', @output);
   }
 
-  method _name(ArrayAST $ast) {
-    my (undef, @names) = @$ast;
+  method _join(HashRef $ast) {
 
-    my $sep = $self->name_separator;
+    # TODO: Validate join type
+    my $type = $ast->{join_type} || "";
+  
+    my @output = $self->dispatch($ast->{lhs});
+
+    push @output, uc $type if $type;
+    push @output, "JOIN", $self->dispatch($ast->{rhs});
 
-    return $sep->[0] . 
-           join( $sep->[1] . $sep->[0], @names ) . 
-           $sep->[1]
-              if (@$sep > 1);
+    push @output, 
+        exists $ast->{on}
+      ? ('ON', '(' . $self->_expr( $ast->{on} ) . ')' )
+      : ('USING', '(' .$self->dispatch($ast->{using} 
+                        || croak "No 'on' or 'uinsg' clause passed to join cluase: " .
+                                 dump($ast) 
+                        ) .
+                  ')' );
 
-    return join($sep->[0], @names);
+    return join(" ", @output);
+      
   }
 
-  method _join(HashAST $ast) {
-  
-    my $output = 'JOIN ' . $self->dispatch($ast->{tablespec});
+  method _ordering(AST $ast) {
+    my $output = $self->_expr($ast->{expr});
 
-    $output .= exists $ast->{on}
-             ? ' ON (' . $self->_recurse_where( $ast->{on} )
-             : ' USING (' .$self->dispatch($ast->{using} || croak "No 'on' or 'join' clause passed to -join");
+    $output .= " " . uc $1
+      if $ast->{direction} && 
+         ( $ast->{direction} =~ /^(asc|desc)$/i 
+           || confess "Unknown ordering direction " . dump($ast)
+         );
 
-    $output .= ")";
     return $output;
-      
   }
 
-  method _list(ArrayAST $ast) {
-    my (undef, @items) = @$ast;
+  method _name(AST $ast) {
+    my @names = @{$ast->{args}};
+
+    my $sep = $self->name_separator;
+    my $quote = $self->is_quoting 
+              ? $self->quote_chars
+              : [ '' ];
+
+    my $join = $quote->[-1] . $sep . $quote->[0];
+
+    # We dont want to quote * in [qw/me */]: `me`.* is the desired output there
+    # This means you can't have a field called `*`. I am willing to accept this
+    # situation, cos thats a really stupid thing to want.
+    my $post;
+    $post = pop @names if $names[-1] eq '*';
+
+    my $ret;
+    $ret = $quote->[0] . 
+           join( $join, @names ) . 
+           $quote->[-1]
+      if @names;
+
+    $ret = $ret 
+         ? $ret . $sep . $post
+         : $post
+      if defined $post;
+
+
+    return $ret;
+  }
+
+
+  method _list(AST $ast) {
+    my @items = is_ArrayRef($ast->{args})
+              ? @{$ast->{args}}
+              : $ast->{args};
 
     return join(
       $self->list_separator,
       map { $self->dispatch($_) } @items);
   }
 
-  method _alias(ArrayAST $ast) {
-    my (undef, $alias, $as) = @$ast;
-
-    return $self->dispatch($alias) . " AS $as";
+  # TODO: I think i want to parameterized AST type to get better validation
+  method _alias(AST $ast) {
+    
+    # TODO: Maybe we want qq{ AS "$as"} here
+    return $self->dispatch($ast->{ident}) . " AS " . $ast->{as};
 
   }
 
-  method _value(ArrayAST $ast) {
-    my ($undef, $value) = @$ast;
+  method _value(AST $ast) {
 
-    $self->add_bind($value);
+    $self->add_bind($ast->{value});
     return "?";
   }
 
-  method _recurse_where(ArrayRef $clauses) {
+  # Not dispatchable to.
+  method __having($args) {
+    return "HAVING " . $self->_list({-type => 'list', args => $args});
+  }
+
+  method __group_by($args) {
+    return "GROUP BY " . $self->_list({-type => 'list', args => $args});
+  }
 
-    my $OP = 'AND';
-    my $prio = $SQL::Abstract::PRIO{and};
-    my $first = $clauses->[0];
+  method __order_by($args) {
+    return "ORDER BY " . $self->_list({-type => 'list', args => $args});
+  }
 
-    if (!ref $first) {
-      if ($first =~ /^-(and|or)$/) {
-        $OP = uc($1);
-        $prio = $SQL::Abstract::PRIO{$1};
-        shift @$clauses;
-      } else {
-        $clauses = [ $clauses ];
-      }
-    }
 
-    my $dispatch_table = $self->where_dispatch_table;
+  # Perhaps badly named. handles 'and' and 'or' clauses
+  method _recurse_where(AST $ast) {
+
+    my $op = $ast->{op};
+
+    my $OP = uc $op;
+    my $prio = $SQL::Abstract::PRIO{$op};
+
+    my $dispatch_table = $self->expr_dispatch_table;
 
     my @output;
-    foreach (@$clauses) {
-      croak "invalid component in where clause: $_" unless is_ArrayRef($_);
-      my $op = $_->[0];
+    foreach ( @{$ast->{args}} ) {
+      croak "invalid component in where clause: $_" unless is_AST($_);
 
-      if ($op =~ /^-(and|or)$/) {
+      if ($_->{-type} eq 'expr' && $_->{op} =~ /^(and|or)$/) {
         my $sub_prio = $SQL::Abstract::PRIO{$1}; 
 
         if ($sub_prio <= $prio) {
@@ -134,46 +207,50 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
           push @output, '(' . $self->_recurse_where($_) . ')';
         }
       } else {
-        push @output, $self->_where_component($_);
+        push @output, $self->_expr($_);
       }
     }
 
     return join(" $OP ", @output);
   }
 
-  method _where_component(ArrayRef $ast) {
-    my $op = $ast->[0];
+  method _expr(AST $ast) {
+    my $op = $ast->{-type};
+
+    $op = $ast->{op} if $op eq 'expr';
 
-    if (my $code = $self->lookup_where_dispatch($op)) { 
+    if (my $code = $self->lookup_expr_dispatch($op)) { 
       
       return $code->($self, $ast);
 
     }
-    croak "'$op' is not a valid clause in a where AST"
-      if $op =~ /^-/;
+    croak "'$op' is not a valid AST type in an expression with " . dump($ast)
+      if $ast->{-type} ne 'expr';
 
-    croak "'$op' is not a valid operator";
+    croak "'$op' is not a valid operator in an expression with " . dump($ast);
    
   }
 
+  method _binop(AST $ast) {
+    my ($lhs, $rhs) = @{$ast->{args}};
+    my $op = $ast->{op};
 
-  method _binop(ArrayRef $ast) {
-    my ($op, $lhs, $rhs) = @$ast;
-
-    join (' ', $self->_where_component($lhs), 
+    join (' ', $self->_expr($lhs), 
                $self->binop_mapping($op) || croak("Unknown binary operator $op"),
-               $self->_where_component($rhs)
+               $self->_expr($rhs)
     );
   }
 
-  method _in(ArrayAST $ast) {
-    my ($tag, $field, @values) = @$ast;
+  method _in(AST $ast) {
+  
+    my ($field,@values) = @{$ast->{args}};
+
+    my $not = ($ast->{op} =~ /^not_/) ? " NOT" : "";
 
-    my $not = $tag =~ /^-not/ ? " NOT" : "";
+    return $self->_false unless @values;
 
-    return $self->_false if @values == 0;
-    return $self->_where_component($field) .
-           $not. 
+    return $self->_expr($field) .
+           $not . 
            " IN (" .
            join(", ", map { $self->dispatch($_) } @values ) .
            ")";