Permalink
Browse files

[Squerl] multi-table dataset dies on .update_sql

  • Loading branch information...
1 parent ae25379 commit 6ccdc96b082c7d159b5129832be953b551f4db73 @masak committed Sep 24, 2009
Showing with 39 additions and 2 deletions.
  1. +17 −2 lib/Squerl.pm
  2. +22 −0 t/squerl/02-dataset.t
View
19 lib/Squerl.pm
@@ -1,5 +1,17 @@
use SQLite3;
+class Squerl::InvalidOperation is Exception {
+ has $.message;
+
+ multi method new($message) {
+ self.bless(*, :$message);
+ }
+
+ method Str() {
+ "{self.WHAT}: $!message";
+ }
+}
+
class Squerl::Dataset does Positional {
has $.db;
has %.opts;
@@ -36,8 +48,8 @@ class Squerl::Dataset does Positional {
:opts(%new-opts));
}
- method from($value) {
- self.clone(:from($value));
+ method from(*@tables) {
+ self.clone(:from(@tables.elems > 1 ?? @tables !! @tables[0]));
}
method filter($value) {
@@ -186,6 +198,9 @@ class Squerl::Dataset does Positional {
return self.static_sql(%!opts<sql>)
if %!opts.exists('sql');
+ die ~Squerl::InvalidOperation.new('Joined datasets cannot be modified')
+ if %!opts<from> ~~ Array && %!opts<from>.elems > 1;
+
my $values = join $COMMA_SEPARATOR, map {
"{.key} = {self.literal(.value)}"
}, %nameds.pairs;
View
22 t/squerl/02-dataset.t
@@ -266,4 +266,26 @@ role R2 { method values { {} } }
is $ds.truncate_sql(), $sql, ':sql option works for .truncate_sql';
}
+sub throws_exception(&block, $expected-type, $message = '') {
+ try {
+ &block();
+ }
+ if $! {
+ my $got-type = ~$!;
+ ok $got-type.substr(0, $expected-type.chars) eq $expected-type,
+ $message;
+ }
+ else {
+ is 'ran without failure', $expected-type, $message;
+ }
+}
+
+$dataset = Squerl::Dataset.new(undef).from('t1', 't2');
+
+{
+ throws_exception { $dataset.update_sql( a => 1 ) },
+ 'Squerl::InvalidOperation',
+ 'multi-table dataset dies on .update_sql';
+}
+
done_testing;

0 comments on commit 6ccdc96

Please sign in to comment.