From: Ash Berlin Date: Thu, 26 Mar 2009 17:57:25 +0000 (+0000) Subject: Remove HashAST and ArrayAST types X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=bad761ba581f9b27669725e3f8c08eee198d094c;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Remove HashAST and ArrayAST types --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index 95178a4..848c01a 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -8,7 +8,7 @@ class SQL::Abstract { use Moose::Util::TypeConstraints; use MooseX::Types::Moose qw/ArrayRef Str Int HashRef CodeRef/; use MooseX::AttributeHelpers; - use SQL::Abstract::Types qw/NameSeparator QuoteChars AST HashAST ArrayAST/; + use SQL::Abstract::Types qw/NameSeparator QuoteChars AST/; use Devel::PartialDump qw/dump/; clean; @@ -121,7 +121,7 @@ class SQL::Abstract { } # Main entry point - method generate(ClassName $class: HashAST $ast) { + method generate(ClassName $class: AST $ast) { my $ver = $ast->{-ast_version}; croak "SQL::Abstract AST version not specified" unless defined $ver; @@ -138,14 +138,7 @@ class SQL::Abstract { method dispatch (AST $ast) { - - # I want multi methods! - my $tag; - if (is_ArrayAST($ast)) { - confess "FIX: " . dump($ast); - } else { - $tag = "_" . $ast->{-type}; - } + my $tag = "_" . $ast->{-type}; my $meth = $self->can($tag) || croak "Unknown tag '$tag'"; return $meth->($self, $ast); diff --git a/lib/SQL/Abstract/AST/v1.pm b/lib/SQL/Abstract/AST/v1.pm index ae2e05c..821245c 100644 --- a/lib/SQL/Abstract/AST/v1.pm +++ b/lib/SQL/Abstract/AST/v1.pm @@ -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; @@ -158,7 +158,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 +179,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 +190,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 +208,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'; @@ -225,7 +225,7 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { } - method _binop(HashAST $ast) { + method _binop(AST $ast) { my ($lhs, $rhs) = @{$ast->{args}}; my $op = $ast->{op}; @@ -235,7 +235,7 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { ); } - method _in(HashAST $ast) { + method _in(AST $ast) { my ($field,@values) = @{$ast->{args}}; diff --git a/lib/SQL/Abstract/Compat.pm b/lib/SQL/Abstract/Compat.pm index aa2bba6..1c13897 100644 --- a/lib/SQL/Abstract/Compat.pm +++ b/lib/SQL/Abstract/Compat.pm @@ -3,10 +3,13 @@ use MooseX::Declare; class SQL::Abstract::Compat { use Moose::Util::TypeConstraints; - use MooseX::Types -declare => [qw/LogicEnum/]; + use MooseX::Types::Moose qw/Str ScalarRef ArrayRef HashRef/; + use MooseX::Types -declare => [qw/LogicEnum WhereType/]; enum LogicEnum, qw(OR AND); + subtype WhereType, as Str; + clean; has logic => ( @@ -15,15 +18,26 @@ class SQL::Abstract::Compat { default => 'AND' ); + + method select(Str|ArrayRef|ScalarRef $from, ArrayRef|Str $fields, Str|ScalarRef|ArrayRef|HashRef $where?, Str|ScalarRef|ArrayRef|HashRef $order?) { return ("", ); } - method where(Str|ScalarRef|ArrayRef|HashRef $where?, + method where(Str|ScalarRef|ArrayRef|HashRef $where, Str|ScalarRef|ArrayRef|HashRef $order?) { + + my $ast = { + -type => 'expr', + }; } + + method recurse_where(LogicEsnum $where) { + + } + } =head1 NAME diff --git a/lib/SQL/Abstract/Types.pm b/lib/SQL/Abstract/Types.pm index 62eecba..9c49bc9 100644 --- a/lib/SQL/Abstract/Types.pm +++ b/lib/SQL/Abstract/Types.pm @@ -1,19 +1,13 @@ use MooseX::Declare; class SQL::Abstract::Types { use Moose::Util::TypeConstraints; - use MooseX::Types -declare => [qw/NameSeparator QuoteChars AST ArrayAST HashAST/]; + use MooseX::Types -declare => [qw/NameSeparator QuoteChars AST/]; use MooseX::Types::Moose qw/ArrayRef Str Int Ref HashRef/; - subtype ArrayAST, as ArrayRef, - where { is_Str($_->[0]) && substr($_->[0],0,1) eq '-' }, - message { "First key of arrayref must be a string starting with '-'"; }; - - subtype HashAST, as HashRef, + subtype AST, as HashRef, where { exists $_->{-type} && is_Str($_->{-type}) }, message { "No '-type' key, or it is not a string" }; - subtype AST, as ArrayAST|HashAST; - subtype NameSeparator, as Str, where { length($_) == 1 }; diff --git a/t/002_types.t b/t/002_types.t index e4b7486..e0d9d59 100644 --- a/t/002_types.t +++ b/t/002_types.t @@ -1,20 +1,13 @@ use strict; use warnings; -use Test::More tests => 7; +use Test::More tests => 3; use MooseX::Types::Moose qw/ArrayRef Str Int Ref HashRef/; use SQL::Abstract::Types ':all'; -is(ArrayAST->validate( [ -foo => 'bar' ] ), undef, "is_ArrayAST with valid" ); -ok(!is_ArrayAST( [ foo => 'bar' ] ), "is_ArrayAST with invalid" ); - - -is(HashAST->validate( { -type => 'select', select => [] } ), undef, "is_HashAST with valid" ); -ok(!is_HashAST( { foo => 'bar' } ), "is_HashAST with invalid" ); - +is(AST->validate( { -type => 'select', select => [] } ), undef, "is_AST with valid" ); +ok(!is_AST( { foo => 'bar' } ), "is_AST with invalid" ); is(AST->validate( { -type => 'select', select => [] } ), undef, "is_AST with valid hash" ); -is(AST->validate( [ -name => 1, 2 ] ), undef, "is_AST with valid array" ); -is(is_AST([ -name => qw/me id/]), 1);