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) {
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 => (
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
--- /dev/null
+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]);
+}