3 class SQL::Abstract::Compat {
5 use Moose::Util::TypeConstraints;
6 use MooseX::Types::Moose qw/Str ScalarRef ArrayRef HashRef/;
7 use SQL::Abstract::Types::Compat ':all';
8 use SQL::Abstract::Types qw/AST/;
9 use SQL::Abstract::AST::v1;
10 use Data::Dump qw/pp/;
11 use Devel::PartialDump qw/dump/;
14 class_type 'SQL::Abstract';
27 isa => 'SQL::Abstract',
28 clearer => 'clear_visitor',
30 builder => '_build_visitor',
44 method select(Str|ArrayRef|ScalarRef $from, ArrayRef|Str $fields,
53 } ( is_Str($fields) ? $fields : @$fields )
55 tablespec => $self->tablespec($from)
59 $ast->{where} = $self->recurse_where($where)
62 return ($self->visitor->dispatch($ast), $self->visitor->binds);
65 method where(WhereType $where,
71 my $ast = $self->recurse_where($where);
72 $ret .= "WHERE " . $self->visitor->_expr($ast);
78 method _build_visitor() {
79 return SQL::Abstract->create(1);
84 return { -type => 'name', args => [ @_ ] };
87 method tablespec(Str|ArrayRef|ScalarRef $from) {
88 return $self->mk_name($from)
92 method recurse_where(WhereType $ast, LogicEnum $logic?) returns (AST) {
93 return $self->recurse_where_hash($logic || 'AND', $ast) if is_HashRef($ast);
94 return $self->recurse_where_array($logic || 'OR', $ast) if is_ArrayRef($ast);
95 croak "Unknown where clause type " . dump($ast);
98 method recurse_where_hash(LogicEnum $logic, HashRef $ast) returns (AST) {
106 while (my ($key,$value) = each %$ast) {
107 if ($key =~ /^-(or|and)$/) {
108 my $val = $self->recurse_where($value, uc $1);
109 if ($val->{op} eq $ret->{op}) {
110 push @args, @{$val->{args}};
118 push @args, $self->field($key, $value);
121 return $args[0] if @args == 1;
126 method recurse_where_array(LogicEnum $logic, ArrayRef $ast) returns (AST) {
135 while (my $key = shift @nodes) {
136 if ($key =~ /^-(or|and)$/) {
137 my $value = shift @nodes
138 or confess "missing value after $key at " . dump($ast);
140 my $val = $self->recurse_where($value, uc $1);
141 if ($val->{op} eq $ret->{op}) {
142 push @args, @{$val->{args}};
150 push @args, $self->recurse_where($key);
153 return $args[0] if @args == 1;
158 method field(Str $key, $value) returns (AST) {
159 my $op = $CMP_MAP{$self->cmp} || $self->cmp;
164 { -type => 'name', args => [$key] }
168 if (is_HashRef($value)) {
169 my ($op, @rest) = keys %$value;
170 confess "Don't know how to handle " . dump($value) . " (too many keys)"
173 # TODO: Validate the op?
174 if ($op =~ /^-([a-z_]+)$/i) {
177 if (is_ArrayRef($value->{$op})) {
178 push @{$ret->{args}}, $self->value($_)
179 for @{$value->{$op}};
187 push @{$ret->{args}}, $self->value($value->{$op});
190 elsif (is_ArrayRef($value)) {
191 # Return an or clause, sort of.
200 { -type => 'name', args => [$key] },
208 push @{$ret->{args}}, $self->value($value);
214 method value($value) returns (AST) {
215 return { -type => 'value', value => $value }
218 confess "Don't know how to handle terminal value " . dump($value);
226 SQL::Abstract::Compant - compatibility layer for SQL::Abstrct v 1.xx
230 This class attempts to maintain the original behaviour of version 1 of
231 SQL::Abstract. It does this by internally converting to an AST and then using
232 the standard AST visitor.
234 If so desired, you can get hold of this transformed AST somehow. This is aimed
235 at libraries such as L<DBIx::Class> that use SQL::Abstract-style arrays or
236 hashes as part of their public interface.
240 Ash Berlin C<< <ash@cpan.org> >>