2 use MooseX::Method::Signatures;
10 use Moose::Util::TypeConstraints;
11 use MooseX::Types -declare => [qw/NameSeparator/];
12 use MooseX::Types::Moose qw/ArrayRef Str/;
13 use MooseX::AttributeHelpers;
15 use namespace::clean -except => ['meta'];
17 subtype NameSeparator,
19 #where { @$_ == 1 ||| @$_ == 2 },
20 #message { "Name separator must be one or two elements" };
22 coerce NameSeparator, from Str, via { [ $_ ] };
24 our $VERSION = '2.000000';
26 our $AST_VERSION = '1';
28 # Operator precedence for bracketing
41 has name_separator => (
44 default => sub { ['.'] },
49 has list_separator => (
58 default => sub { [ ] },
59 metaclass => 'Collection::Array',
66 method generate (Object|ClassName $self: ArrayRef $ast) {
67 $self = $self->new unless blessed($self);
70 s/^-/_/ or croak "Unknown type tag '$_'";
71 my $meth = $self->can($_) || \&_generic_func;
72 return $meth->($self, $ast);
75 method _select(ArrayRef $ast) {
79 method _name(ArrayRef $ast) {
80 my (undef, @names) = @$ast;
82 my $sep = $self->name_separator;
85 join( $sep->[1] . $sep->[0], @names ) .
89 return join($sep->[0], @names);
92 method _list(ArrayRef $ast) {
93 my (undef, @items) = @$ast;
96 $self->list_separator,
97 map { $self->generate($_) } @items);
100 method _alias(ArrayRef $ast) {
101 my (undef, $alias, $as) = @$ast;
103 return $self->generate($alias) . " AS $as";
107 method _value(ArrayRef $ast) {
108 my ($undef, $value) = @$ast;
110 $self->add_bind($value);
114 method _where(ArrayRef $ast) {
115 my (undef, @clauses) = @$ast;
118 return 'WHERE ' . $self->_recurse_where(\@clauses);
121 method _recurse_where($clauses) {
125 my $prio = $PRIO{and};
126 my $first = $clauses->[0];
128 if (!ref $first && $first =~ /^-(and|or)$/) {
135 foreach (@$clauses) {
138 unless (substr($op, 0, 1) eq '-') {
139 # A simple comparison op (==, >, etc.)
141 push @output, $self->_binop(@$_);
143 } elsif ($op =~ /^-(and|or)$/) {
144 my $sub_prio = $PRIO{$1};
146 if ($sub_prio >= $prio) {
147 push @output, $self->_recurse_where($_);
149 push @output, '(' . $self->_recurse_where($_) . ')';
152 push @output, $self->generate($_);
156 return wantarray ? @output : join(" $OP ", @output);
159 method _binop($op, $lhs, $rhs) {
160 join (' ', $self->generate($lhs),
161 $OP_MAP{$op} || croak("Unknown binary operator $op"),
162 $self->generate($rhs)
166 method _generic_func(ArrayRef $ast) {