Permalink
Browse files

[Squerl] can format an update statement

  • Loading branch information...
1 parent 4aa6988 commit 5cddf99a3ee7d5765bcf4129473ab7dcdc752b62 @masak committed Sep 23, 2009
Showing with 31 additions and 15 deletions.
  1. +26 −15 lib/Squerl.pm
  2. +5 −0 t/squerl/02-dataset.t
View
@@ -78,7 +78,27 @@ class Squerl::Dataset does Positional {
# [perl #69204]
$name.="$!identifier_input_method";
}
- $!quote_identifiers ?? qq["$name"] !! $name;
+ $!quote_identifiers ?? quoted_identifier($name) !! $name;
+ }
+
+ submethod literal_array(@values) {
+ "({join $COMMA_SEPARATOR, map { self.literal($^value) }, @values})";
+ }
+
+ sub literal_integer($value) {
+ ~$value
+ }
+
+ sub literal_number($value) {
+ ~$value
+ }
+
+ sub literal_string($value) {
+ "'{$value.subst('\\', '\\\\', :g).subst("'", "''", :g)}'"
+ }
+
+ sub quoted_identifier($name) {
+ qq["{$name.subst(q["], q[""], :g)}"]
}
method output_identifier($name is copy) {
@@ -146,20 +166,11 @@ class Squerl::Dataset does Positional {
"INSERT INTO {%!opts<from>} $columns$values";
}
- submethod literal_array(@values) {
- "({join $COMMA_SEPARATOR, map { self.literal($^value) }, @values})";
- }
-
- sub literal_integer($value) {
- ~$value
- }
-
- sub literal_number($value) {
- ~$value
- }
-
- sub literal_string($value) {
- "'{$value.subst('\\', '\\\\', :g).subst("'", "''", :g)}'"
+ method update_sql(*%nameds) {
+ my $values = join $COMMA_SEPARATOR, map {
+ "{.key} = {self.literal(.value)}"
+ }, %nameds.pairs;
+ "UPDATE {%!opts<from>} SET $values";
}
}
View
@@ -246,4 +246,9 @@ role R2 { method values { {} } }
'format an insert statement with array';
}
+{
+ is $dataset.update_sql(:name<abc>), q[UPDATE test SET name = 'abc'],
+ 'format an update statement';
+}
+
done_testing;

0 comments on commit 5cddf99

Please sign in to comment.