From: Matt S Trout Date: Thu, 11 Oct 2012 12:45:05 +0000 (+0100) Subject: demonstrate limit dialects X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a609e537d08fbb3707dd02adf0df21cae43edcc6;p=dbsrgits%2FData-Query.git demonstrate limit dialects --- diff --git a/t/example.t b/t/example.t index f7999d6..4ccc2a6 100644 --- a/t/example.t +++ b/t/example.t @@ -4,65 +4,102 @@ use Data::Query::Renderer::SQL::Naive; use Moo::Role (); use Devel::Dwarn; -my $renderer = Data::Query::Renderer::SQL::Naive->new; +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; -sub render_expr { +sub to_sql { my ($sql, @bindp) = @{$renderer->render($_[0])}; ($sql, map $_->{value}, @bindp); } -DwarnL render_expr(SELECT { $_->foo, "bar" }); -DwarnL render_expr(SELECT { $_->foo, "bar" } FROM { $_->baz }); -DwarnL render_expr(SELECT { $_->foo } FROM { $_->baz, AS('quux') }); -DwarnL render_expr( +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 } ); -DwarnL render_expr( +DwarnL to_sql( SELECT { $_->cd->name } FROM { $_->cds, AS 'cd' } JOIN { $_->artists, AS 'artist' } ON { $_->cd->artistid eq $_->artist->id } ); -DwarnL render_expr( +DwarnL to_sql( SELECT { $_->artist->name } FROM { $_->artists, AS 'artist' } LEFT JOIN { $_->cds, AS 'cd' } ON { $_->cd->artistid eq $_->artist->id } ); -DwarnL render_expr( +DwarnL to_sql( SELECT { $_->artist->name } WHERE { $_->artist->age > 25 } ); -DwarnL render_expr( +DwarnL to_sql( SELECT { $_->cd->name } FROM { $_->cds, AS 'cd' } JOIN { $_->artists, AS 'artist' } ON { $_->cd->artistid eq $_->artist->id } WHERE { $_->artist->age > 25 } ); -DwarnL render_expr(ORDER BY { $_->foo, $_->bar, $_->baz } DESC); +DwarnL to_sql(ORDER BY { $_->foo, $_->bar, $_->baz } DESC); my $basic = SELECT { $_->cd->name } FROM { $_->cds, AS 'cd' }; -DwarnL render_expr( +DwarnL to_sql( FROM { $basic } WHERE { $_->year > 2000 } ); -DwarnL render_expr( +DwarnL to_sql( FROM { $basic } ORDER BY { $_->year } ); -DwarnL render_expr( +DwarnL to_sql( SELECT { $_->cd->name } FROM { $_->cds, AS 'cd' } ORDER BY { $_->year } ); -$renderer = Moo::Role->create_class_with_roles( - 'Data::Query::Renderer::SQL::Naive', - 'Data::Query::Renderer::SQL::Slice::LimitOffset' -)->new; - -DwarnL render_expr( +my $lo = SELECT { $_->cd->name } FROM { $_->cds, AS 'cd' } ORDER BY { $_->year } - LIMIT { 10 } OFFSET { 10 } -); + LIMIT { 10 } OFFSET { 10 }; + +sub render_orders { + + $renderer = make_renderer('LimitOffset'); + + DwarnL to_sql($lo); + + $renderer = make_renderer('RowNum'); + + DwarnL to_sql($lo); + + $renderer = make_renderer('RowNumberOver'); + + DwarnL to_sql($lo); + +} + +render_orders(); + +$lo = + SELECT { $_->cd->name, $_->artist->name } + FROM { $_->cds, AS 'cd' } + JOIN { $_->artists, AS 'artist' } + ON { $_->cd->artistid eq $_->artist->id } + WHERE { $_->artist->age > 25 } + ORDER BY { $_->artist->age, $_->cd->id } + LIMIT { 10 } OFFSET { 20 }; + +render_orders();