2fd21c6c2e11e27645f04983361f30a43232318e
[dbsrgits/SQL-Abstract-2.0-ish.git] / lib / SQL / Abstract / Compat.pm
1 use MooseX::Declare;
2
3 class SQL::Abstract::Compat {
4
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::AST::Compat;
9   use SQL::Abstract::AST::v1;
10   use Data::Dump qw/pp/;
11
12   class_type 'SQL::Abstract';
13   clean;
14
15   has logic => (
16     is => 'rw',
17     isa => LogicEnum,
18     default => 'AND'
19   );
20
21   has visitor => (
22     is => 'rw',
23     isa => 'SQL::Abstract',
24     clearer => 'clear_visitor',
25     lazy => 1,
26     builder => '_build_visitor',
27   );
28
29
30   method select(Str|ArrayRef|ScalarRef $from, ArrayRef|Str $fields,
31                 WhereType $where?,
32                 WhereType $order?)
33   {
34
35     my $ast = $self->_new_compat_ast->select($from,$fields,$where,$order);
36     pp($ast);
37
38     return ($self->visitor->dispatch($ast), $self->visitor->binds);
39   }
40
41   method where(WhereType $where,
42                WhereType $order?)
43   {
44     my $ret = "";
45  
46     if ($where) {
47       my $ast = $self->_new_compat_ast->generate($where);
48       $ret .= "WHERE " . $self->visitor->_expr($ast);
49     }
50
51     return $ret;
52   }
53
54   #TODO: Handle logic and similar args later
55   method _new_compat_ast() {
56     return SQL::Abstract::AST::Compat->new;
57   }
58
59   method _build_visitor() {
60     return SQL::Abstract->create(1);
61   } 
62
63 }
64
65 =head1 NAME
66
67 SQL::Abstract::Compant - compatibility layer for SQL::Abstrct v 1.xx
68
69 =head1 DESCRIPTION
70
71 This class attempts to maintain the original behaviour of version 1 of
72 SQL::Abstract. It does this by internally converting to an AST and then using
73 the standard AST visitor.
74
75 If so desired, you can get hold of this transformed AST somehow. This is aimed
76 at libraries such as L<DBIx::Class> that use SQL::Abstract-style arrays or
77 hashes as part of their public interface.
78
79 =head1 AUTHOR
80
81 Ash Berlin C<< <ash@cpan.org> >>
82
83 =cut
84
85 1;