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 => (
59 default => sub { [ ] },
60 metaclass => 'Collection::Array',
63 clear => '_clear_binds',
67 method generate (Object|ClassName $self: ArrayRef $ast) {
68 my $class_meth = !blessed($self);
69 $self = $self->new if $class_meth;
72 s/^-/_/g or croak "Unknown type tag '$_'";
73 my $meth = $self->can($_) || \&_generic_func;
75 ? ($meth->($self, $ast), $self->binds)
76 : $meth->($self, $ast);
79 method _select(ArrayRef $ast) {
83 method _where(ArrayRef $ast) {
84 my (undef, @clauses) = @$ast;
86 return 'WHERE ' . $self->_recurse_where(\@clauses);
89 method _order_by(ArrayRef $ast) {
90 my (undef, @clauses) = @$ast;
95 if ($_->[0] =~ /^-(asc|desc)$/) {
97 push @output, $self->generate($_->[1]) . " " . uc($o);
100 push @output, $self->generate($_);
103 return "ORDER BY " . join(", ", @output);
106 method _name(ArrayRef $ast) {
107 my (undef, @names) = @$ast;
109 my $sep = $self->name_separator;
112 join( $sep->[1] . $sep->[0], @names ) .
116 return join($sep->[0], @names);
119 method _list(ArrayRef $ast) {
120 my (undef, @items) = @$ast;
123 $self->list_separator,
124 map { $self->generate($_) } @items);
127 method _alias(ArrayRef $ast) {
128 my (undef, $alias, $as) = @$ast;
130 return $self->generate($alias) . " AS $as";
134 method _value(ArrayRef $ast) {
135 my ($undef, $value) = @$ast;
137 $self->add_bind($value);
141 method _recurse_where($clauses) {
144 my $prio = $PRIO{and};
145 my $first = $clauses->[0];
147 if (!ref $first && $first =~ /^-(and|or)$/) {
154 foreach (@$clauses) {
155 croak "invalid component in where clause" unless ArrayRef->check($_);
158 unless (substr($op, 0, 1) eq '-') {
159 # A simple comparison op (==, >, etc.)
161 push @output, $self->_binop(@$_);
163 } elsif ($op =~ /^-(and|or)$/) {
164 my $sub_prio = $PRIO{$1};
166 if ($sub_prio <= $prio) {
167 push @output, $self->_recurse_where($_);
169 push @output, '(' . $self->_recurse_where($_) . ')';
172 push @output, $self->generate($_);
176 return join(" $OP ", @output);
179 method _binop($op, $lhs, $rhs) {
180 join (' ', $self->generate($lhs),
181 $OP_MAP{$op} || croak("Unknown binary operator $op"),
182 $self->generate($rhs)
186 method _generic_func(ArrayRef $ast) {