Make LIKE op work
[dbsrgits/SQL-Abstract-2.0-ish.git] / lib / SQL / Abstract.pm
index e95b8b8..194792e 100644 (file)
@@ -1,16 +1,15 @@
 use MooseX::Declare;
 
-
 class SQL::Abstract {
 
   use Carp qw/croak/;
   use Data::Dump qw/pp/;
 
   use Moose::Util::TypeConstraints;
-  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 SQL::Abstract::Types qw/NameSeparator QuoteChars AST/;
+  use Devel::PartialDump qw/dump/;
 
   clean;
 
@@ -26,27 +25,31 @@ class SQL::Abstract {
 
   our %BINOP_MAP = (
     '>' => '>',
+    '>=' => '>=',
     '<' => '<',
+    '<=' => '<=',
     '==' => '=',
     '!=' => '!=',
     # LIKE is always "field LIKE <value>"
-    '-like' => 'IN',
-    '-not_like' => 'NOT LIKE',
+    'like' => 'LIKE',
+    'not_like' => 'NOT LIKE',
   );
 
-  has where_dispatch_table => (
+  has expr_dispatch_table => (
     is => 'ro',
-    lazy_build => 1,
+    lazy => 1,
+    builder => '_build_expr_dispatch_table',
     isa => HashRef[CodeRef],
     metaclass => 'Collection::ImmutableHash',
     provides => {
-      get => 'lookup_where_dispatch'
+      get => 'lookup_expr_dispatch'
     }
   );
 
   has binop_map => (
     is => 'ro',
-    lazy_build => 1,
+    lazy => 1,
+    builder => '_build_binops',
     isa => HashRef,
     metaclass => 'Collection::ImmutableHash',
     provides => {
@@ -56,9 +59,10 @@ class SQL::Abstract {
     }
   );
 
-  sub _build_binop_map { return {%BINOP_MAP} };
+  # List of default binary operators (for in where clauses)
+  sub _build_binops { return {%BINOP_MAP} };
 
-  method _build_where_dispatch_table {
+  method _build_expr_dispatch_table {
     my $binop = $self->can('_binop') or croak "InternalError: $self can't do _binop!";
     return {
       map { $_ => $binop } $self->binary_operators
@@ -74,8 +78,7 @@ class SQL::Abstract {
   has name_separator => ( 
     is => 'rw', 
     isa => NameSeparator,
-    default => sub { ['.'] },
-    coerece => 1,
+    default => '.',
     required => 1,
   );
 
@@ -86,6 +89,14 @@ class SQL::Abstract {
     required => 1,
   );
 
+  has quote_chars => (
+    is => 'rw', 
+    isa => QuoteChars,
+    predicate => 'is_quoting',
+    clearer => 'disable_quoting', 
+    coerce => 1,
+  );
+
   has binds => (
     isa => ArrayRef,
     is => 'ro',
@@ -111,10 +122,9 @@ class SQL::Abstract {
 
   # Main entry point
   method generate(ClassName $class: AST $ast) {
+    my $ver = $ast->{-ast_version};
     croak "SQL::Abstract AST version not specified"
-      unless ($ast->[0] eq '-ast_version');
-
-    my (undef, $ver) = splice(@$ast, 0, 2);
+      unless defined $ver;
 
     # TODO: once MXMS supports %args, use that here
     my $self = $class->create($ver);
@@ -127,13 +137,8 @@ class SQL::Abstract {
   }
 
   method dispatch (AST $ast) {
-    # I want multi methods!
-    my $tag;
-    if (is_ArrayAST($ast)) {
-      ($tag = $ast->[0]) =~ s/^-/_/;
-    } else {
-      $tag = "_" . $ast->{-type};
-    }
+
+    my $tag = "_" . $ast->{-type};
     
     my $meth = $self->can($tag) || croak "Unknown tag '$tag'";
     return $meth->($self, $ast);