X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FSQL%2FAbstract.pm;h=e21f4cae3128a19189b50e98a0d18357c8b17cc7;hb=HEAD;hp=ad9b32e77a8a7893144be93ca2cce8f7b376ee2f;hpb=4ee32f4102e019f9d8acaa3aaaa7ce721789f73b;p=dbsrgits%2FSQL-Abstract-2.0-ish.git diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index ad9b32e..e21f4ca 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -1,18 +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 QuoteChars AST ArrayAST/; - - clean; + use SQL::Abstract::Types qw/NameSeparator QuoteChars AST/; + use Devel::PartialDump qw/dump/; our $VERSION = '2.000000'; @@ -25,23 +22,31 @@ class SQL::Abstract { ); our %BINOP_MAP = ( + + '+' => '+', + '-' => '-', + '/' => '/', + '*' => '*', + '>' => '>', + '>=' => '>=', '<' => '<', + '<=' => '<=', '==' => '=', '!=' => '!=', # LIKE is always "field LIKE " - '-like' => 'IN', - '-not_like' => 'NOT LIKE', + 'like' => 'LIKE', + 'not_like' => 'NOT LIKE', ); - has where_dispatch_table => ( + has expr_dispatch_table => ( is => 'ro', lazy => 1, - builder => '_build_where_dispatch_table', + builder => '_build_expr_dispatch_table', isa => HashRef[CodeRef], metaclass => 'Collection::ImmutableHash', provides => { - get => 'lookup_where_dispatch' + get => 'lookup_expr_dispatch' } ); @@ -61,7 +66,7 @@ class SQL::Abstract { # 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,11 +79,10 @@ class SQL::Abstract { required => 1 ); - has name_separator => ( + has ident_separator => ( is => 'rw', isa => NameSeparator, default => '.', - coerece => 1, required => 1, ); @@ -92,9 +96,9 @@ class SQL::Abstract { has quote_chars => ( is => 'rw', isa => QuoteChars, - coerece => 1, predicate => 'is_quoting', clearer => 'disable_quoting', + coerce => 1, ); has binds => ( @@ -110,6 +114,7 @@ class SQL::Abstract { ); # TODO: once MXMS supports %args, use that here + # TODO: improve this so you can pass other args method create(ClassName $class: Int $ver) { croak "AST version $ver is greater than supported version of $AST_VERSION" if $ver > $AST_VERSION; @@ -122,10 +127,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); @@ -138,16 +142,29 @@ 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); } }; + +__END__ + +=head1 NAME + +SQL::Abstract - AST based re-implementation of SQL::Abstract + +=head1 LICENSE + +=head1 AUTHORS + +Ash Berlin C<< >> + +=head1 LICENSE + +This program is free software; you can redistribute it and/or modify it under +the same terms as Perl itself. +