Turns out LIMIT doesn't work for update and delete :O

This commit is contained in:
Simon Binder 2019-02-19 10:42:30 +01:00
parent 6ca6275a13
commit a26f84ceaf
6 changed files with 15 additions and 16 deletions

View File

@ -37,13 +37,6 @@ abstract class Query<Table, DataClass> {
}
}
/// 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() {

View File

@ -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<T, D> extends Query<T, D> {
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<List<D>> watch() {

View File

@ -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]));
});
});

View File

@ -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]));
});
});

View File

@ -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

View File

@ -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) {