Start porting more back compat changes
[dbsrgits/SQL-Abstract-2.0-ish.git] / lib / SQL / Abstract.pm
CommitLineData
a0185af2 1use MooseX::Declare;
a0185af2 2
a0185af2 3class SQL::Abstract {
4
5 use Carp qw/croak/;
6 use Data::Dump qw/pp/;
7
8 use Moose::Util::TypeConstraints;
0c371882 9 use MooseX::Types::Moose qw/ArrayRef Str Int HashRef CodeRef/;
4769c837 10 use MooseX::AttributeHelpers;
bad761ba 11 use SQL::Abstract::Types qw/NameSeparator QuoteChars AST/;
747f7c21 12 use Devel::PartialDump qw/dump/;
4769c837 13
c314b35d 14 clean;
a0185af2 15
a0185af2 16 our $VERSION = '2.000000';
17
18 our $AST_VERSION = '1';
19
3e63a4d5 20 # Operator precedence for bracketing
21 our %PRIO = (
22 and => 10,
23 or => 50
24 );
25
0bf8a8c4 26 our %BINOP_MAP = (
3e63a4d5 27 '>' => '>',
64f3d05e 28 '>=' => '>=',
3e63a4d5 29 '<' => '<',
64f3d05e 30 '<=' => '<=',
3e63a4d5 31 '==' => '=',
32 '!=' => '!=',
0bf8a8c4 33 # LIKE is always "field LIKE <value>"
db861e66 34 'like' => 'LIKE',
35 'not_like' => 'NOT LIKE',
3e63a4d5 36 );
37
ef0d6124 38 has expr_dispatch_table => (
0bf8a8c4 39 is => 'ro',
eb22ecd3 40 lazy => 1,
ef0d6124 41 builder => '_build_expr_dispatch_table',
0c371882 42 isa => HashRef[CodeRef],
43 metaclass => 'Collection::ImmutableHash',
44 provides => {
ef0d6124 45 get => 'lookup_expr_dispatch'
0c371882 46 }
0bf8a8c4 47 );
48
49 has binop_map => (
50 is => 'ro',
eb22ecd3 51 lazy => 1,
52 builder => '_build_binops',
0bf8a8c4 53 isa => HashRef,
54 metaclass => 'Collection::ImmutableHash',
55 provides => {
56 exists => 'is_valid_binop',
57 get => 'binop_mapping',
58 keys => 'binary_operators'
59 }
60 );
61
eb22ecd3 62 # List of default binary operators (for in where clauses)
63 sub _build_binops { return {%BINOP_MAP} };
0bf8a8c4 64
ef0d6124 65 method _build_expr_dispatch_table {
0c371882 66 my $binop = $self->can('_binop') or croak "InternalError: $self can't do _binop!";
0bf8a8c4 67 return {
68 map { $_ => $binop } $self->binary_operators
69 }
70 }
71
c314b35d 72 has ast_version => (
73 is => 'ro',
74 isa => Int,
75 required => 1
76 );
77
a0185af2 78 has name_separator => (
79 is => 'rw',
80 isa => NameSeparator,
4ee32f41 81 default => '.',
a0185af2 82 required => 1,
83 );
84
85 has list_separator => (
86 is => 'rw',
87 isa => Str,
88 default => ', ',
89 required => 1,
90 );
91
4ee32f41 92 has quote_chars => (
93 is => 'rw',
94 isa => QuoteChars,
4ee32f41 95 predicate => 'is_quoting',
96 clearer => 'disable_quoting',
8eda2119 97 coerce => 1,
4ee32f41 98 );
99
4769c837 100 has binds => (
101 isa => ArrayRef,
5bf8c024 102 is => 'ro',
0bf8a8c4 103 clearer => '_clear_binds',
104 lazy => 1,
4769c837 105 default => sub { [ ] },
106 metaclass => 'Collection::Array',
107 provides => {
108 push => 'add_bind',
4769c837 109 }
110 );
111
14774be0 112 # TODO: once MXMS supports %args, use that here
e76b9ff7 113 # TODO: improve this so you can pass other args
14774be0 114 method create(ClassName $class: Int $ver) {
115 croak "AST version $ver is greater than supported version of $AST_VERSION"
116 if $ver > $AST_VERSION;
117
118 my $name = "${class}::AST::v$ver";
119 Class::MOP::load_class($name);
120
121 return $name->new(ast_version => $ver);
c314b35d 122 }
123
124 # Main entry point
bad761ba 125 method generate(ClassName $class: AST $ast) {
a464be15 126 my $ver = $ast->{-ast_version};
c314b35d 127 croak "SQL::Abstract AST version not specified"
a464be15 128 unless defined $ver;
c314b35d 129
14774be0 130 # TODO: once MXMS supports %args, use that here
131 my $self = $class->create($ver);
c314b35d 132
133 return ($self->dispatch($ast), $self->binds);
134 }
135
0bf8a8c4 136 method reset() {
137 $self->_clear_binds();
138 }
139
cbcfedc1 140 method dispatch (AST $ast) {
f7dc4536 141
bad761ba 142 my $tag = "_" . $ast->{-type};
0bf8a8c4 143
cbcfedc1 144 my $meth = $self->can($tag) || croak "Unknown tag '$tag'";
0bf8a8c4 145 return $meth->($self, $ast);
146 }
4769c837 147
a0185af2 148};