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' }
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);
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' }
+);
--- /dev/null
+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;