From: Matt S Trout Date: Thu, 11 Oct 2012 15:27:46 +0000 (+0100) Subject: separate to_sql mechanism X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d605d949f67550fb33e7c52fc73e236fa8d4ab94;p=dbsrgits%2FData-Query.git separate to_sql mechanism --- diff --git a/t/example.t b/t/example.t index 4ccc2a6..4458a8b 100644 --- a/t/example.t +++ b/t/example.t @@ -1,42 +1,27 @@ use strictures; -use Data::Query::ExprDeclare; -use Data::Query::Renderer::SQL::Naive; -use Moo::Role (); use Devel::Dwarn; -sub make_renderer { - my $class = do { - if ($_[0]) { - Moo::Role->create_class_with_roles( - 'Data::Query::Renderer::SQL::Naive', - "Data::Query::Renderer::SQL::Slice::$_[0]" - ) - } else { - 'Data::Query::Renderer::SQL::Naive' - } - }; - $class->new; -} - -my $renderer = make_renderer; +BEGIN { require 't/example.to_sql' }; -sub to_sql { - my ($sql, @bindp) = @{$renderer->render($_[0])}; - ($sql, map $_->{value}, @bindp); -} +make_renderer; DwarnL to_sql(SELECT { $_->foo, "bar" }); DwarnL to_sql(SELECT { $_->foo, "bar" } FROM { $_->baz }); DwarnL to_sql(SELECT { $_->foo } FROM { $_->baz, AS('quux') }); DwarnL to_sql( - SELECT { $_->cd->name } FROM { $_->cds, AS('cd') } JOIN { $_->artists } + SELECT { $_->cd->name } FROM { $_->cds, AS('cd') } ); -DwarnL to_sql( + +my $w_join = SELECT { $_->cd->name } FROM { $_->cds, AS 'cd' } JOIN { $_->artists, AS 'artist' } - ON { $_->cd->artistid eq $_->artist->id } -); + ON { $_->cd->artistid eq $_->artist->id }; + +DwarnL to_sql($w_join); + +Dwarn to_sql(FROM { $w_join } WHERE { $_->cd->year > 2000 }); + DwarnL to_sql( SELECT { $_->artist->name } FROM { $_->artists, AS 'artist' } @@ -77,15 +62,15 @@ my $lo = sub render_orders { - $renderer = make_renderer('LimitOffset'); + make_renderer('LimitOffset'); DwarnL to_sql($lo); - $renderer = make_renderer('RowNum'); + make_renderer('RowNum'); DwarnL to_sql($lo); - $renderer = make_renderer('RowNumberOver'); + make_renderer('RowNumberOver'); DwarnL to_sql($lo); @@ -103,3 +88,10 @@ $lo = LIMIT { 10 } OFFSET { 20 }; render_orders(); + +my @cols = qw(name year genre); + +DwarnL to_sql( + SELECT { my $cd = $_->cd; map $cd->$_, @cols } + FROM { $_->cds, AS 'cd' } +); diff --git a/t/example.to_sql b/t/example.to_sql new file mode 100644 index 0000000..a20124f --- /dev/null +++ b/t/example.to_sql @@ -0,0 +1,27 @@ +use strictures; +use Data::Query::ExprDeclare; +use Data::Query::Renderer::SQL::Naive; +use Moo::Role (); + +my $renderer; + +sub make_renderer { + my $class = do { + if ($_[0]) { + Moo::Role->create_class_with_roles( + 'Data::Query::Renderer::SQL::Naive', + "Data::Query::Renderer::SQL::Slice::$_[0]" + ) + } else { + 'Data::Query::Renderer::SQL::Naive' + } + }; + $renderer = $class->new; +} + +sub to_sql { + my ($sql, @bindp) = @{$renderer->render($_[0])}; + ($sql, map $_->{value}, @bindp); +} + +1;