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
33 # LIKE is always "field LIKE <value>"
35 '-not_like' => 'NOT LIKE',
38 has expr_dispatch_table => (
41 builder => '_build_expr_dispatch_table',
42 isa => HashRef[CodeRef],
43 metaclass => 'Collection::ImmutableHash',
45 get => 'lookup_expr_dispatch'
52 builder => '_build_binops',
54 metaclass => 'Collection::ImmutableHash',
56 exists => 'is_valid_binop',
57 get => 'binop_mapping',
58 keys => 'binary_operators'
62 # List of default binary operators (for in where clauses)
63 sub _build_binops { return {%BINOP_MAP} };
65 method _build_expr_dispatch_table {
66 my $binop = $self->can('_binop') or croak "InternalError: $self can't do _binop!";
68 map { $_ => $binop } $self->binary_operators
78 has name_separator => (
85 has list_separator => (
95 predicate => 'is_quoting',
96 clearer => 'disable_quoting',
103 clearer => '_clear_binds',
105 default => sub { [ ] },
106 metaclass => 'Collection::Array',
112 # TODO: once MXMS supports %args, use that here
113 method create(ClassName $class: Int $ver) {
114 croak "AST version $ver is greater than supported version of $AST_VERSION"
115 if $ver > $AST_VERSION;
117 my $name = "${class}::AST::v$ver";
118 Class::MOP::load_class($name);
120 return $name->new(ast_version => $ver);
124 method generate(ClassName $class: AST $ast) {
125 my $ver = $ast->{-ast_version};
126 croak "SQL::Abstract AST version not specified"
129 # TODO: once MXMS supports %args, use that here
130 my $self = $class->create($ver);
132 return ($self->dispatch($ast), $self->binds);
136 $self->_clear_binds();
139 method dispatch (AST $ast) {
141 my $tag = "_" . $ast->{-type};
143 my $meth = $self->can($tag) || croak "Unknown tag '$tag'";
144 return $meth->($self, $ast);