Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

insert_sql accepts arbitrary values

  • Loading branch information...
commit 98852e3c907077f34e7abf49548908dfca841c07 1 parent 1dcc613
@masak authored
Showing with 13 additions and 4 deletions.
  1. +8 −4 lib/Squerl.pm
  2. +5 −0 t/squerl/02-dataset.t
View
12 lib/Squerl.pm
@@ -115,6 +115,9 @@ class Squerl::Dataset does Positional {
@columns.push(.key);
@values.push(.value);
}
+ when Num|Str {
+ @values.push($_);
+ }
when .^can('values') {
for .values.pairs {
die "Expected a Pair, got a {.WHAT}"
@@ -128,11 +131,12 @@ class Squerl::Dataset does Positional {
@columns.push(.key);
@values.push(.value);
}
- my $values = [~] '(', (join $COMMA_SEPARATOR, @columns), ')',
- ' VALUES ', self.literal_array(@values);
+ my $columns = @columns ?? "({join $COMMA_SEPARATOR, @columns}) "
+ !! '';
+ my $values = @values ?? 'VALUES ' ~ self.literal_array(@values)
+ !! 'DEFAULT VALUES';
# RAKUDO: Real string interpolation
- @values ?? "INSERT INTO {%!opts<from>} $values"
- !! "INSERT INTO {%!opts<from>} DEFAULT VALUES";
+ "INSERT INTO {%!opts<from>} $columns$values";
}
submethod literal_array(@values) {
View
5 t/squerl/02-dataset.t
@@ -228,4 +228,9 @@ role R2 { method values { {} } }
'format an insert statement with an object that .can("values") II';
}
+{
+ is $dataset.insert_sql(123), 'INSERT INTO test VALUES (123)',
+ 'format an insert statement with an arbitrary value';
+}
+
done_testing;
Please sign in to comment.
Something went wrong with that request. Please try again.