Replace lazy_build with lazt + builder
[dbsrgits/SQL-Abstract-2.0-ish.git] / lib / SQL / Abstract.pm
index 7c89615..1963975 100644 (file)
@@ -1,5 +1,4 @@
 use MooseX::Declare;
-use MooseX::Method::Signatures;
 
 
 class SQL::Abstract {
@@ -8,23 +7,73 @@ class SQL::Abstract {
   use Data::Dump qw/pp/;
 
   use Moose::Util::TypeConstraints;
-  use MooseX::Types -declare => ['NameSeparator'];
-  use MooseX::Types::Moose qw/ArrayRef Str/;
+  use MooseX::Types -declare => [qw/NameSeparator/];
+  use MooseX::Types::Moose qw/ArrayRef Str Int HashRef CodeRef/;
   use MooseX::AttributeHelpers;
+  use SQL::Abstract::Types qw/NameSeparator AST ArrayAST/;
 
-  use namespace::clean -except => ['meta'];
-
-  subtype NameSeparator,
-    as ArrayRef[Str];
-    #where { @$_ == 1 ||| @$_ == 2 },
-    #message { "Name separator must be one or two elements" };
-
-  coerce NameSeparator, from Str, via { [ $_ ] };
+  clean;
 
   our $VERSION = '2.000000';
 
   our $AST_VERSION = '1';
 
+  # Operator precedence for bracketing
+  our %PRIO = (
+    and => 10,
+    or  => 50
+  );
+
+  our %BINOP_MAP = (
+    '>' => '>',
+    '<' => '<',
+    '==' => '=',
+    '!=' => '!=',
+    # LIKE is always "field LIKE <value>"
+    '-like' => 'IN',
+    '-not_like' => 'NOT LIKE',
+  );
+
+  has where_dispatch_table => (
+    is => 'ro',
+    lazy => 1,
+    builder => '_build_where_dispatch_table',
+    isa => HashRef[CodeRef],
+    metaclass => 'Collection::ImmutableHash',
+    provides => {
+      get => 'lookup_where_dispatch'
+    }
+  );
+
+  has binop_map => (
+    is => 'ro',
+    lazy => 1,
+    builder => '_build_binops',
+    isa => HashRef,
+    metaclass => 'Collection::ImmutableHash',
+    provides => {
+      exists => 'is_valid_binop',
+      get => 'binop_mapping',
+      keys => 'binary_operators'
+    }
+  );
+
+  # List of default binary operators (for in where clauses)
+  sub _build_binops { return {%BINOP_MAP} };
+
+  method _build_where_dispatch_table {
+    my $binop = $self->can('_binop') or croak "InternalError: $self can't do _binop!";
+    return {
+      map { $_ => $binop } $self->binary_operators
+    }
+  }
+
+  has ast_version => (
+    is => 'ro',
+    isa => Int,
+    required => 1
+  );
+
   has name_separator => ( 
     is => 'rw', 
     isa => NameSeparator,
@@ -42,86 +91,55 @@ class SQL::Abstract {
 
   has binds => (
     isa => ArrayRef,
+    is => 'ro',
+    clearer => '_clear_binds',
+    lazy => 1,
     default => sub { [ ] },
     metaclass => 'Collection::Array',
     provides => {
       push => 'add_bind',
-      get => 'binds'
     }
   );
 
-  method generate (ArrayRef $ast) {
-    $self = new $self unless blessed($self);
+  # TODO: once MXMS supports %args, use that here
+  method create(ClassName $class: Int $ver) {
+    croak "AST version $ver is greater than supported version of $AST_VERSION"
+      if $ver > $AST_VERSION;
 
-    local $_ = $ast->[0];
-    s/^-/_/ or croak "Unknown type tag '$_'";
-    my $meth = $self->can($_) || \&_generic_func;
-    return $meth->($self, $ast);
-  }
-
-  method _select(ArrayRef $ast) {
-    
-  }
-
-  method _name(ArrayRef $ast) {
-    my (undef, @names) = @$ast;
+    my $name = "${class}::AST::v$ver";
+    Class::MOP::load_class($name);
 
-    my $sep = $self->name_separator;
-
-    return $sep->[0] . 
-           join( $sep->[1] . $sep->[0], @names ) . 
-           $sep->[1]
-              if (@$sep > 1);
-
-    return join($sep->[0], @names);
+    return $name->new(ast_version => $ver);
   }
 
-  method _list(ArrayRef $ast) {
-    my (undef, @items) = @$ast;
+  # Main entry point
+  method generate(ClassName $class: AST $ast) {
+    croak "SQL::Abstract AST version not specified"
+      unless ($ast->[0] eq '-ast_version');
 
-    return join(
-      $self->list_separator,
-      map { $self->generate($_) } @items);
-  }
+    my (undef, $ver) = splice(@$ast, 0, 2);
 
-  method _alias(ArrayRef $ast) {
-    my (undef, $alias, $as) = @$ast;
-
-    return $self->generate($alias) . " AS $as";
+    # TODO: once MXMS supports %args, use that here
+    my $self = $class->create($ver);
 
+    return ($self->dispatch($ast), $self->binds);
   }
 
-  method _value(ArrayRef $ast) {
-    my ($undef, $value) = @$ast;
-
-    $self->add_bind($value);
-    return "?";
+  method reset() {
+    $self->_clear_binds();
   }
 
-  method _where(ArrayRef $ast) {
-    my (undef, @clauses) = @$ast;
-
-    my @output;
-
-    foreach (@clauses) {
-      my $op = $_->[0];
-
-      unless (substr($op, 0, 1) eq '-') {
-        # A simple comparison op (==, >, etc.)
-        croak "Binary operator $op expects 2 children, got " . $#$_
-          if @{$_} > 3;
-
-        push @output, $self->generate($_->[1]), 
-                      $op,
-                      $self->generate($_->[2]);
-      }
+  method dispatch (AST $ast) {
+    # I want multi methods!
+    my $tag;
+    if (is_ArrayAST($ast)) {
+      ($tag = $ast->[0]) =~ s/^-/_/;
+    } else {
+      $tag = "_" . $ast->{-type};
     }
-
-    return join(' ', 'WHERE', @output);
-  }
-
-  method _generic_func(ArrayRef $ast) {
+    
+    my $meth = $self->can($tag) || croak "Unknown tag '$tag'";
+    return $meth->($self, $ast);
   }
 
-
 };