Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[Squerl] formats insert statements with sub-queries

  • Loading branch information...
commit 9f3de5f299c66d778aa184acfaba4a8eb8da9b51 1 parent 98852e3
@masak authored
Showing with 17 additions and 4 deletions.
  1. +10 −4 lib/Squerl.pm
  2. +7 −0 t/squerl/02-dataset.t
View
14 lib/Squerl.pm
@@ -95,7 +95,10 @@ class Squerl::Dataset does Positional {
method select_sql() {
# RAKUDO: Real string interpolation
- "SELECT * FROM {%!opts<from>}";
+ "SELECT * FROM {%!opts<from>}"
+ ~ (%!opts.exists('filter')
+ ?? " WHERE ({%!opts<filter>.fmt('%s = %s')})"
+ !! '');
}
method delete_sql() {
@@ -115,7 +118,7 @@ class Squerl::Dataset does Positional {
@columns.push(.key);
@values.push(.value);
}
- when Num|Str {
+ when Num|Str|Squerl::Dataset {
@values.push($_);
}
when .^can('values') {
@@ -133,8 +136,11 @@ class Squerl::Dataset does Positional {
}
my $columns = @columns ?? "({join $COMMA_SEPARATOR, @columns}) "
!! '';
- my $values = @values ?? 'VALUES ' ~ self.literal_array(@values)
- !! 'DEFAULT VALUES';
+ my $values = @values
+ ?? (@values[0] ~~ Squerl::Dataset
+ ?? @values[0].select_sql()
+ !! 'VALUES ' ~ self.literal_array(@values))
+ !! 'DEFAULT VALUES';
# RAKUDO: Real string interpolation
"INSERT INTO {%!opts<from>} $columns$values";
}
View
7 t/squerl/02-dataset.t
@@ -233,4 +233,11 @@ role R2 { method values { {} } }
'format an insert statement with an arbitrary value';
}
+{
+ my $sub = Squerl::Dataset.new('').from('something').filter('x' => 2);
+ is $dataset.insert_sql($sub),
+ 'INSERT INTO test SELECT * FROM something WHERE (x = 2)',
+ 'format an insert statement with sub-query';
+}
+
done_testing;
Please sign in to comment.
Something went wrong with that request. Please try again.