3 class SQL::Abstract::AST::Compat {
5 use MooseX::Types::Moose qw/ArrayRef HashRef Str ScalarRef/;
6 use SQL::Abstract::Types qw/AST/;
7 use SQL::Abstract::Types::Compat ':all';
8 use Devel::PartialDump qw/dump/;
21 return { -type => 'name', args => [ @_ ] };
24 method select(Str|ArrayRef|ScalarRef $from, ArrayRef|Str $fields,
33 } ( is_Str($fields) ? $fields : @$fields )
35 tablespec => $self->tablespec($from)
39 $ast->{where} = $self->recurse_where($where)
45 method tablespec(Str|ArrayRef|ScalarRef $from) {
46 return $self->mk_name($from)
50 method recurse_where(WhereType $ast, LogicEnum $logic?) returns (AST) {
51 return $self->recurse_where_hash($logic || 'AND', $ast) if is_HashRef($ast);
52 return $self->recurse_where_array($logic || 'OR', $ast) if is_ArrayRef($ast);
53 croak "Unknown where clause type " . dump($ast);
56 method recurse_where_hash(LogicEnum $logic, HashRef $ast) returns (AST) {
64 while (my ($key,$value) = each %$ast) {
65 if ($key =~ /^-(or|and)$/) {
66 my $val = $self->recurse_where($value, uc $1);
67 if ($val->{op} eq $ret->{op}) {
68 push @args, @{$val->{args}};
76 push @args, $self->field($key, $value);
79 return $args[0] if @args == 1;
84 method recurse_where_array(LogicEnum $logic, ArrayRef $ast) returns (AST) {
93 while (my $key = shift @nodes) {
94 if ($key =~ /^-(or|and)$/) {
95 my $value = shift @nodes
96 or confess "missing value after $key at " . dump($ast);
98 my $val = $self->recurse_where($value, uc $1);
99 if ($val->{op} eq $ret->{op}) {
100 push @args, @{$val->{args}};
108 push @args, $self->recurse_where($key);
111 return $args[0] if @args == 1;
116 method field(Str $key, $value) returns (AST) {
121 { -type => 'name', args => [$key] }
125 if (is_HashRef($value)) {
126 my ($op, @rest) = keys %$value;
127 confess "Don't know how to handle " . dump($value) . " (too many keys)"
130 # TODO: Validate the op?
131 if ($op =~ /^-([a-z_]+)$/i) {
134 if (is_ArrayRef($value->{$op})) {
135 push @{$ret->{args}}, $self->value($_)
136 for @{$value->{$op}};
144 push @{$ret->{args}}, $self->value($value->{$op});
147 elsif (is_ArrayRef($value)) {
148 # Return an or clause, sort of.
157 { -type => 'name', args => [$key] },
165 push @{$ret->{args}}, $self->value($value);
171 method value($value) returns (AST) {
172 return { -type => 'value', value => $value }
175 confess "Don't know how to handle terminal value " . dump($value);
185 SQL::Abstract::AST::Compat - v1.xx AST -> v2 AST visitor
189 The purpose of this module is to take the where clause arguments from version
190 1.x of SQL::Abstract, and turn it into a proper, explicit AST, suitable for use
191 in the rest of the code.
193 Please note that this module does not have the same interface as other
198 Ash Berlin C<< <ash@cpan.org> >>