Fucntion support
[dbsrgits/SQL-Abstract-2.0-ish.git] / lib / SQL / Abstract / AST / v1.pm
index ae2e05c..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
@@ -114,7 +114,7 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
     return $output;
   }
 
-  method _name(HashAST $ast) {
+  method _name(AST $ast) {
     my @names = @{$ast->{args}};
 
     my $sep = $self->name_separator;
@@ -130,17 +130,25 @@ 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) {
+    return "" unless $ast->{args};
+
     my @items = is_ArrayRef($ast->{args})
               ? @{$ast->{args}}
               : $ast->{args};
@@ -158,7 +166,7 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
 
   }
 
-  method _value(HashAST $ast) {
+  method _value(AST $ast) {
 
     $self->add_bind($ast->{value});
     return "?";
@@ -179,7 +187,7 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract {
 
 
   # Perhaps badly named. handles 'and' and 'or' clauses
-  method _recurse_where(HashAST $ast) {
+  method _recurse_where(AST $ast) {
 
     my $op = $ast->{op};
 
@@ -190,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}; 
@@ -208,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';
@@ -221,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};
 
@@ -235,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}};
 
@@ -250,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" }