From a26f84ceaf41a9c9ed76f029fa892014d6d8189f Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Tue, 19 Feb 2019 10:42:30 +0100 Subject: [PATCH] Turns out LIMIT doesn't work for update and delete :O --- sally/lib/src/runtime/statements/query.dart | 7 ------- sally/lib/src/runtime/statements/select.dart | 8 ++++++++ sally/test/delete_test.dart | 5 ++--- sally/test/update_test.dart | 7 +++---- sally_flutter/README.md | 2 +- sally_flutter/example/lib/database.dart | 2 +- 6 files changed, 15 insertions(+), 16 deletions(-) diff --git a/sally/lib/src/runtime/statements/query.dart b/sally/lib/src/runtime/statements/query.dart index 92b09a0c..51e4c035 100644 --- a/sally/lib/src/runtime/statements/query.dart +++ b/sally/lib/src/runtime/statements/query.dart @@ -37,13 +37,6 @@ abstract class Query { } } - /// Limits the amount of rows returned by capping them at [limit]. If [offset] - /// is provided as well, the first [offset] rows will be skipped and not - /// included in the result. - void limit(int limit, {int offset}) { - limitExpr = Limit(limit, offset); - } - /// Constructs the query that can then be sent to the database executor. @protected GenerationContext constructQuery() { diff --git a/sally/lib/src/runtime/statements/select.dart b/sally/lib/src/runtime/statements/select.dart index 839b5e56..31379646 100644 --- a/sally/lib/src/runtime/statements/select.dart +++ b/sally/lib/src/runtime/statements/select.dart @@ -1,4 +1,5 @@ import 'package:sally/src/runtime/components/component.dart'; +import 'package:sally/src/runtime/components/limit.dart'; import 'package:sally/src/runtime/executor/executor.dart'; import 'package:sally/src/runtime/statements/query.dart'; import 'package:sally/src/runtime/structure/table_info.dart'; @@ -22,6 +23,13 @@ class SelectStatement extends Query { return results.map(table.map).toList(); } + /// Limits the amount of rows returned by capping them at [limit]. If [offset] + /// is provided as well, the first [offset] rows will be skipped and not + /// included in the result. + void limit(int limit, {int offset}) { + limitExpr = Limit(limit, offset); + } + /// Creates an auto-updating stream that emits new items whenever this table /// changes. Stream> watch() { diff --git a/sally/test/delete_test.dart b/sally/test/delete_test.dart index 92afe422..bc16fbd0 100644 --- a/sally/test/delete_test.dart +++ b/sally/test/delete_test.dart @@ -24,12 +24,11 @@ void main() { test('for complex components', () async { await (db.delete(db.users) - ..where((u) => or(not(u.isAwesome), u.id.isSmallerThanValue(100))) - ..limit(10, offset: 100)) + ..where((u) => or(not(u.isAwesome), u.id.isSmallerThanValue(100)))) .go(); verify(executor.runDelete( - 'DELETE FROM users WHERE (NOT (is_awesome = 1)) OR (id < ?) LIMIT 10, 100;', + 'DELETE FROM users WHERE (NOT (is_awesome = 1)) OR (id < ?);', [100])); }); }); diff --git a/sally/test/update_test.dart b/sally/test/update_test.dart index a5a8bcef..61b2a647 100644 --- a/sally/test/update_test.dart +++ b/sally/test/update_test.dart @@ -25,14 +25,13 @@ void main() { 'UPDATE todos SET title = ? category = ?;', ['Updated title', 3])); }); - test('with WHERE and LIMIT clauses', () async { + test('with a WHERE clause', () async { await (db.update(db.todosTable) - ..where((t) => t.id.isSmallerThanValue(50)) - ..limit(10)) + ..where((t) => t.id.isSmallerThanValue(50))) .write(TodoEntry(title: 'Changed title')); verify(executor.runUpdate( - 'UPDATE todos SET title = ? WHERE id < ? LIMIT 10;', + 'UPDATE todos SET title = ? WHERE id < ?;', ['Changed title', 50])); }); }); diff --git a/sally_flutter/README.md b/sally_flutter/README.md index 49644584..8660c26b 100644 --- a/sally_flutter/README.md +++ b/sally_flutter/README.md @@ -154,7 +154,7 @@ Future feelingLazy() { return (delete(todos)..limit(10)).go(); } ``` -__⚠️ Caution:__ If you don't explicitly add a `where` or `limit` clause on updates or deletes, +__⚠️ Caution:__ If you don't explicitly add a `where` clause on updates or deletes, the statement will affect all rows in the table! ### Inserts diff --git a/sally_flutter/example/lib/database.dart b/sally_flutter/example/lib/database.dart index aef17e24..ad5e7b47 100644 --- a/sally_flutter/example/lib/database.dart +++ b/sally_flutter/example/lib/database.dart @@ -63,7 +63,7 @@ class Database extends _$Database { (select(todos)..where((t) => isNull(t.category))).watch(); Future test() { - (delete(todos)..limit(10)).go(); + } Future addTodoEntry(TodoEntry entry) {