From: Ash Berlin Date: Sun, 29 Mar 2009 17:33:06 +0000 (+0100) Subject: Start testing/writing API compatability layer X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0a18aa4f2fd71db7f80a6e53b11d5759a2c2f762;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Start testing/writing API compatability layer --- diff --git a/lib/SQL/Abstract/AST/Compat.pm b/lib/SQL/Abstract/AST/Compat.pm index 6ca78fd..4093bc2 100644 --- a/lib/SQL/Abstract/AST/Compat.pm +++ b/lib/SQL/Abstract/AST/Compat.pm @@ -16,8 +16,35 @@ class SQL::Abstract::AST::Compat { default => 'AND' ); - method generate(WhereType $ast) returns (AST) { - return $self->recurse_where($ast); + sub mk_name { + shift; + return { -type => 'name', args => [ @_ ] }; + } + + method select(Str|ArrayRef|ScalarRef $from, ArrayRef|Str $fields, + WhereType $where?, + WhereType $order?) + { + my $ast = { + -type => 'select', + columns => [ + map { + $self->mk_name($_) + } ( is_Str($fields) ? $fields : @$fields ) + ], + tablespec => $self->tablespec($from) + }; + + + $ast->{where} = $self->recurse_where($where) + if defined $where; + + return $ast; + } + + method tablespec(Str|ArrayRef|ScalarRef $from) { + return $self->mk_name($from) + if is_Str($from); } method recurse_where(WhereType $ast, LogicEnum $logic?) returns (AST) { diff --git a/lib/SQL/Abstract/Compat.pm b/lib/SQL/Abstract/Compat.pm index 1c13897..2fd21c6 100644 --- a/lib/SQL/Abstract/Compat.pm +++ b/lib/SQL/Abstract/Compat.pm @@ -4,12 +4,12 @@ class SQL::Abstract::Compat { use Moose::Util::TypeConstraints; 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; + use SQL::Abstract::Types::Compat ':all'; + use SQL::Abstract::AST::Compat; + use SQL::Abstract::AST::v1; + use Data::Dump qw/pp/; + class_type 'SQL::Abstract'; clean; has logic => ( @@ -18,26 +18,48 @@ class SQL::Abstract::Compat { default => 'AND' ); + has visitor => ( + is => 'rw', + isa => 'SQL::Abstract', + clearer => 'clear_visitor', + lazy => 1, + builder => '_build_visitor', + ); method select(Str|ArrayRef|ScalarRef $from, ArrayRef|Str $fields, - Str|ScalarRef|ArrayRef|HashRef $where?, - Str|ScalarRef|ArrayRef|HashRef $order?) { - return ("", ); - } + WhereType $where?, + WhereType $order?) + { + + my $ast = $self->_new_compat_ast->select($from,$fields,$where,$order); + pp($ast); - method where(Str|ScalarRef|ArrayRef|HashRef $where, - Str|ScalarRef|ArrayRef|HashRef $order?) { + return ($self->visitor->dispatch($ast), $self->visitor->binds); + } - my $ast = { - -type => 'expr', - }; + method where(WhereType $where, + WhereType $order?) + { + my $ret = ""; + + if ($where) { + my $ast = $self->_new_compat_ast->generate($where); + $ret .= "WHERE " . $self->visitor->_expr($ast); + } + + return $ret; } - method recurse_where(LogicEsnum $where) { - + #TODO: Handle logic and similar args later + method _new_compat_ast() { + return SQL::Abstract::AST::Compat->new; } + method _build_visitor() { + return SQL::Abstract->create(1); + } + } =head1 NAME diff --git a/t/compat/00new.t b/t/compat/00new.t new file mode 100644 index 0000000..4d28b95 --- /dev/null +++ b/t/compat/00new.t @@ -0,0 +1,31 @@ +use strict; +use warnings; +use Test::More; + +use SQL::Abstract::Test import => ['is_same_sql_bind']; + +#LDNOTE: renamed all "bind" into "where" because that's what they are + +my @handle_tests = ( + #2 + { + args => {}, + stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? )' + } +); + +plan tests => (1 + scalar(@handle_tests)); + +use_ok('SQL::Abstract::Compat'); + +for (@handle_tests) { + my $sql = SQL::Abstract::Compat->new($_->{args}); + my $where = $_->{where} || { a => 4, b => 0}; + my($stmt, @bind) = $sql->select('test', '*', $where); + + + # LDNOTE: this original test suite from NWIGER did no comparisons + # on @bind values, just checking if @bind is nonempty. + # So here we just fake a [1] bind value for the comparison. + is_same_sql_bind($stmt, [@bind ? 1 : 0], $_->{stmt}, [1]); +}