DQ_OPERATOR => 'Operator',
DQ_VALUE => 'Value',
DQ_SELECT => 'Select',
+ DQ_ALIAS => 'Alias',
+ DQ_LITERAL => 'Literal',
+ DQ_JOIN => 'Join',
))
};
\@select,
# if present this may be a bare FROM, a FROM+WHERE, or a FROM+WHERE+GROUP
# since we're the SELECT and therefore always come first, we don't care.
- $dq->{from} ? @{$self->_render($dq->{from})} : ()
+ ($dq->{from}
+ ? ($self->_format_keyword('FROM'), @{$self->_render($dq->{from})})
+ : ()
+ )
+ ];
+}
+
+sub _render_alias {
+ my ($self, $dq) = @_;
+ # FROM foo foo -> FROM foo
+ # FROM foo.bar bar -> FROM foo.bar
+ if ($dq->{alias}{type} eq DQ_IDENTIFIER) {
+ if ($dq->{alias}{elements}[-1] eq $dq->{as}) {
+ return $self->_render($dq->{alias});
+ }
+ }
+ return [
+ $self->_render($dq->{alias}),
+ ' ',
+ $self->_render_identifier({ elements => [ $dq->{as} ] })
+ ];
+}
+
+sub _render_literal {
+ my ($self, $dq) = @_;
+ unless ($dq->{subtype} eq 'SQL') {
+ die "Can't render non-SQL literal";
+ }
+ return [
+ $dq->{literal},
];
}
use strictures 1;
use Data::Query::ExprBuilder::Identifier;
-use Data::Query::Constants qw(DQ_SELECT);
+use Data::Query::Constants qw(
+ DQ_SELECT DQ_IDENTIFIER DQ_OPERATOR DQ_VALUE DQ_ALIAS
+);
use Data::Query::ExprHelpers qw(perl_scalar_value identifier);
sub expr (&) {
- _mk_expr($_[0]);
+ _run_expr($_[0])->{expr};
}
-sub _mk_expr {
+sub _run_expr {
local $_ = Data::Query::ExprBuilder::Identifier->new({
expr => identifier()
});
- $_[0]->()->{expr};
+ $_[0]->();
}
sub AS {
ref()
? $_
: { expr => perl_scalar_value($_) }
- ), do {
- local $_ = Data::Query::ExprBuilder::Identifier->new({
- expr => identifier()
- });
- $_[0]->();
- };
+ ), _run_expr(shift);
my @final;
while (@select) {
my $e = shift @select;
type => DQ_SELECT,
select => \@final
},
+ @_ ? (from => $_[0]->{expr}) : ()
};
}
+sub BY (&;@) { @_ }
+
+sub FROM (&;@) {
+ my @from = _run_expr(shift);
+ if (@from == 2 and ref($from[1]) eq 'LIES::AS') {
+ return +{
+ expr => {
+ type => DQ_ALIAS,
+ source => $from[0],
+ alias => identifier(${$from[1]}),
+ }
+ };
+ } elsif (@from == 1) {
+ return { expr => $from[0] };
+ }
+ die "Huh?"
+}
+
1;
sub expr_is (&;@) {
my $sub = shift;
- is_deeply(_mk_expr($sub), @_);
+ is_deeply(_run_expr($sub)->{expr}, @_);
}
expr_is { $_->foo }
sub expr_sql_is (&;@) {
my $sub = shift;
@_
- ? is_deeply($rend->render(_mk_expr($sub)), @_)
- : ::Dwarn($rend->render(_mk_expr($sub)));
+ ? is_deeply($rend->render(_run_expr($sub)->{expr}), @_)
+ : ::Dwarn($rend->render(_run_expr($sub)->{expr}));
}
expr_sql_is { $_->foo }