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/;
19 method generate(WhereType $ast) returns (AST) {
20 return $self->recurse_where($ast);
23 method recurse_where(WhereType $ast, LogicEnum $logic?) returns (AST) {
24 return $self->recurse_where_hash($logic || 'AND', $ast) if is_HashRef($ast);
25 return $self->recurse_where_array($logic || 'OR', $ast) if is_ArrayRef($ast);
26 croak "Unknown where clause type " . dump($ast);
29 method recurse_where_hash(LogicEnum $logic, HashRef $ast) returns (AST) {
37 while (my ($key,$value) = each %$ast) {
38 if ($key =~ /^-(or|and)$/) {
39 my $val = $self->recurse_where($value, uc $1);
40 if ($val->{op} eq $ret->{op}) {
41 push @args, @{$val->{args}};
49 push @args, $self->field($key, $value);
52 return $args[0] if @args == 1;
57 method recurse_where_array(LogicEnum $logic, ArrayRef $ast) returns (AST) {
66 while (my $key = shift @nodes) {
67 if ($key =~ /^-(or|and)$/) {
68 my $value = shift @nodes
69 or confess "missing value after $key at " . dump($ast);
71 my $val = $self->recurse_where($value, uc $1);
72 if ($val->{op} eq $ret->{op}) {
73 push @args, @{$val->{args}};
81 push @args, $self->recurse_where($key);
84 return $args[0] if @args == 1;
89 method field(Str $key, $value) returns (AST) {
94 { -type => 'name', args => [$key] }
98 if (is_HashRef($value)) {
99 my ($op, @rest) = keys %$value;
100 confess "Don't know how to handle " . dump($value) . " (too many keys)"
103 # TODO: Validate the op?
104 if ($op =~ /^-([a-z_]+)$/i) {
107 if (is_ArrayRef($value->{$op})) {
108 push @{$ret->{args}}, $self->value($_)
109 for @{$value->{$op}};
117 push @{$ret->{args}}, $self->value($value->{$op});
120 elsif (is_ArrayRef($value)) {
121 # Return an or clause, sort of.
130 { -type => 'name', args => [$key] },
138 push @{$ret->{args}}, $self->value($value);
144 method value($value) returns (AST) {
145 return { -type => 'value', value => $value }
148 confess "Don't know how to handle terminal value " . dump($value);
158 SQL::Abstract::AST::Compat - v1.xx AST -> v2 AST visitor
162 The purpose of this module is to take the where clause arguments from version
163 1.x of SQL::Abstract, and turn it into a proper, explicit AST, suitable for use
164 in the rest of the code.
166 Please note that this module does not have the same interface as other
171 Ash Berlin C<< <ash@cpan.org> >>