From: Matt S Trout Date: Thu, 11 Oct 2012 12:07:51 +0000 (+0100) Subject: select+join+where X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4e0c6139c29c4d618adafef7d11c125dc111d821;p=dbsrgits%2FData-Query.git select+join+where --- diff --git a/lib/Data/Query/ExprDeclare.pm b/lib/Data/Query/ExprDeclare.pm index 0a1bcaa..971e3cc 100644 --- a/lib/Data/Query/ExprDeclare.pm +++ b/lib/Data/Query/ExprDeclare.pm @@ -8,7 +8,7 @@ use Safe::Isa; use base qw(Exporter); -our @EXPORT = qw(expr SELECT AS FROM BY JOIN ON LEFT); +our @EXPORT = qw(expr SELECT AS FROM BY JOIN ON LEFT WHERE ORDER GROUP); sub expr (&) { _run_expr($_[0])->{expr}; @@ -22,7 +22,13 @@ sub _run_expr { } sub _value { - ref($_[0]) ? $_[0]->{expr} : perl_scalar_value($_[0]) + if ($_[0]->$_isa('Data::Query::ExprBuilder')) { + $_[0]->{expr}; + } elsif (ref($_[0])) { + $_[0] + } else { + perl_scalar_value($_[0]); + } } sub AS { @@ -56,11 +62,13 @@ sub FROM (&;@) { _value($from[0]); } }; - while ($_[0] and is_Join($_[0])) { + while (is_Join($_[0])) { $from_dq = { %{+shift}, left => $from_dq }; } + if (is_Where($_[0])) { + $from_dq = Where(shift->{where}, $from_dq); + } return $from_dq; - die "Huh?" } sub LEFT { diff --git a/lib/Data/Query/Renderer/SQL/Naive.pm b/lib/Data/Query/Renderer/SQL/Naive.pm index 492323b..64e5bc2 100644 --- a/lib/Data/Query/Renderer/SQL/Naive.pm +++ b/lib/Data/Query/Renderer/SQL/Naive.pm @@ -313,7 +313,7 @@ sub _render_alias { } return [ $self->_maybe_parenthesise($dq->{from}), - $as || ' ', + $as || '', $self->_render_identifier({ elements => [ $dq->{to} ] }) ]; } diff --git a/t/example.t b/t/example.t index f59dd98..25d6466 100644 --- a/t/example.t +++ b/t/example.t @@ -28,3 +28,13 @@ DwarnL render_expr( LEFT JOIN { $_->cds, AS 'cd' } ON { $_->cd->artistid eq $_->artist->id } ); +DwarnL render_expr( + SELECT { $_->artist->name } WHERE { $_->artist->age > 25 } +); +DwarnL render_expr( + SELECT { $_->cd->name } + FROM { $_->cds, AS 'cd' } + JOIN { $_->artists, AS 'artist' } + ON { $_->cd->artistid eq $_->artist->id } + WHERE { $_->artist->age > 25 } +);