Fucntion support
[dbsrgits/SQL-Abstract-2.0-ish.git] / lib / SQL / Abstract / AST / v1.pm
index 2596c63..2c00f26 100644 (file)
@@ -8,7 +8,7 @@ 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;
@@ -31,7 +31,7 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
     };
   }
 
-  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
@@ -54,14 +54,24 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
     push @output, FROM => $self->dispatch($ast->{tablespec})
       if exists $ast->{tablespec};
 
-    for (qw//) {
+    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->{$_};
-        $sub_ast->{-type} = "$_" if is_HashRef($sub_ast);
-        confess "$_ option is not an AST"
-          unless is_AST($sub_ast);
 
-        push @output, $self->dispatch($sub_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);
       }
     }
 
@@ -69,39 +79,42 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
   }
 
   method _join(HashRef $ast) {
-    my ($from, $to) = @{ $ast->{args} };
+
+    # TODO: Validate join type
+    my $type = $ast->{join_type} || "";
   
-    my $output = $self->dispatch($from)
-               . ' JOIN ' 
-               . $self->dispatch($to);
+    my @output = $self->dispatch($ast->{lhs});
 
-    $output .= exists $ast->{on}
-             ? ' ON (' . $self->_expr( $ast->{on} )
-             : ' USING (' .$self->dispatch($ast->{using} || croak "No 'on' or 'join' clause passed to -join");
+    push @output, uc $type if $type;
+    push @output, "JOIN", $self->dispatch($ast->{rhs});
 
-    $output .= ")";
-    return $output;
+    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(" ", @output);
       
   }
 
-  method _order_by(AST $ast) {
-    my @clauses = @{$ast->{order_by}};
-  
-    my @output;
-   
-    for (@clauses) {
-      if (is_ArrayRef($_) && $_->[0] =~ /^-(asc|desc)$/) {
-        my $o = $1;
-        push @output, $self->dispatch($_->[1]) . " " . uc($o);
-        next;
-      }
-      push @output, $self->dispatch($_);
-    }
+  method _ordering(AST $ast) {
+    my $output = $self->_expr($ast->{expr});
+
+    $output .= " " . uc $1
+      if $ast->{direction} && 
+         ( $ast->{direction} =~ /^(asc|desc)$/i 
+           || confess "Unknown ordering direction " . dump($ast)
+         );
 
-    return "ORDER BY " . join(", ", @output);
+    return $output;
   }
 
-  method _name(HashAST $ast) {
+  method _name(AST $ast) {
     my @names = @{$ast->{args}};
 
     my $sep = $self->name_separator;
@@ -117,18 +130,28 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
     my $post;
     $post = pop @names if $names[-1] eq '*';
 
-    my $ret = 
-      $quote->[0] . 
-      join( $join, @names ) . 
-      $quote->[-1];
+    my $ret;
+    $ret = $quote->[0] . 
+           join( $join, @names ) . 
+           $quote->[-1]
+      if @names;
+
+    $ret = $ret 
+         ? $ret . $sep . $post
+         : $post
+      if defined $post;
+
 
-    $ret .= $sep . $post if defined $post;
     return $ret;
   }
 
 
   method _list(AST $ast) {
-    my @items = @{$ast->{args}};
+    return "" unless $ast->{args};
+
+    my @items = is_ArrayRef($ast->{args})
+              ? @{$ast->{args}}
+              : $ast->{args};
 
     return join(
       $self->list_separator,
@@ -143,14 +166,28 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
 
   }
 
-  method _value(HashAST $ast) {
+  method _value(AST $ast) {
 
     $self->add_bind($ast->{value});
     return "?";
   }
 
+  # 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});
+  }
+
+  method __order_by($args) {
+    return "ORDER BY " . $self->_list({-type => 'list', args => $args});
+  }
+
+
   # Perhaps badly named. handles 'and' and 'or' clauses
-  method _recurse_where(HashAST $ast) {
+  method _recurse_where(AST $ast) {
 
     my $op = $ast->{op};
 
@@ -161,7 +198,7 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
 
     my @output;
     foreach ( @{$ast->{args}} ) {
-      croak "invalid component in where clause: $_" unless is_HashAST($_);
+      croak "invalid component in where clause: $_" unless is_AST($_);
 
       if ($_->{-type} eq 'expr' && $_->{op} =~ /^(and|or)$/) {
         my $sub_prio = $SQL::Abstract::PRIO{$1}; 
@@ -179,7 +216,7 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
     return join(" $OP ", @output);
   }
 
-  method _expr(HashAST $ast) {
+  method _expr(AST $ast) {
     my $op = $ast->{-type};
 
     $op = $ast->{op} if $op eq 'expr';
@@ -192,11 +229,16 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
     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 in an expression with " . dump($ast);
+    # This is an attempt to do some form of validation on function names. This
+    # might end up being a bad thing.
+    croak "'$op' is not a valid operator in an expression with " . dump($ast)
+      if $op =~ /\W/;
+
+    return $self->_generic_function_op($ast);
    
   }
 
-  method _binop(HashAST $ast) {
+  method _binop(AST $ast) {
     my ($lhs, $rhs) = @{$ast->{args}};
     my $op = $ast->{op};
 
@@ -206,7 +248,13 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
     );
   }
 
-  method _in(HashAST $ast) {
+  method _generic_function_op(AST $ast) {
+    my $op = $ast->{op};
+
+    return "$op(" . $self->_list($ast) . ")";
+  }
+
+  method _in(AST $ast) {
   
     my ($field,@values) = @{$ast->{args}};
 
@@ -221,9 +269,6 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
            ")";
   }
 
-  method _generic_func(ArrayRef $ast) {
-  }
-
   # 'constants' that are portable across DBs
   method _false($ast?) { "0 = 1" }
   method _true($ast?) { "1 = 1" }