8 use Moose::Util::TypeConstraints;
9 use MooseX::Types::Moose qw/ArrayRef Str Int HashRef CodeRef/;
10 use MooseX::AttributeHelpers;
11 use SQL::Abstract::Types qw/NameSeparator QuoteChars AST/;
12 use Devel::PartialDump qw/dump/;
16 our $VERSION = '2.000000';
18 our $AST_VERSION = '1';
20 # Operator precedence for bracketing
39 # LIKE is always "field LIKE <value>"
41 'not_like' => 'NOT LIKE',
44 has expr_dispatch_table => (
47 builder => '_build_expr_dispatch_table',
48 isa => HashRef[CodeRef],
49 metaclass => 'Collection::ImmutableHash',
51 get => 'lookup_expr_dispatch'
58 builder => '_build_binops',
60 metaclass => 'Collection::ImmutableHash',
62 exists => 'is_valid_binop',
63 get => 'binop_mapping',
64 keys => 'binary_operators'
68 # List of default binary operators (for in where clauses)
69 sub _build_binops { return {%BINOP_MAP} };
71 method _build_expr_dispatch_table {
72 my $binop = $self->can('_binop') or croak "InternalError: $self can't do _binop!";
74 map { $_ => $binop } $self->binary_operators
84 has ident_separator => (
91 has list_separator => (
101 predicate => 'is_quoting',
102 clearer => 'disable_quoting',
109 clearer => '_clear_binds',
111 default => sub { [ ] },
112 metaclass => 'Collection::Array',
118 # TODO: once MXMS supports %args, use that here
119 # TODO: improve this so you can pass other args
120 method create(ClassName $class: Int $ver) {
121 croak "AST version $ver is greater than supported version of $AST_VERSION"
122 if $ver > $AST_VERSION;
124 my $name = "${class}::AST::v$ver";
125 Class::MOP::load_class($name);
127 return $name->new(ast_version => $ver);
131 method generate(ClassName $class: AST $ast) {
132 my $ver = $ast->{-ast_version};
133 croak "SQL::Abstract AST version not specified"
136 # TODO: once MXMS supports %args, use that here
137 my $self = $class->create($ver);
139 return ($self->dispatch($ast), $self->binds);
143 $self->_clear_binds();
146 method dispatch (AST $ast) {
148 my $tag = "_" . $ast->{-type};
150 my $meth = $self->can($tag) || croak "Unknown tag '$tag'";
151 return $meth->($self, $ast);