Remove insertAll and orReplace

This commit is contained in:
Simon Binder 2020-02-17 18:09:29 +01:00
parent 11af414551
commit 8c4c6cd8cd
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 4 additions and 117 deletions

View File

@ -153,7 +153,7 @@ class Database extends _$Database {
reallyGoodFriends: friendsValue,
);
await into(friendships).insert(companion, orReplace: true);
await into(friendships).insert(companion, mode: InsertMode.insertOrReplace);
}
Future<void> updateSettings(int userId, Preferences c) async {

View File

@ -6,7 +6,8 @@
- top-level `year`, `month`, `day`, `hour`, `minute`, `second` methods.
Use the extension member on `Expression<DateTime>` instead.
- __Breaking__: Remove the second type variable on `Expression` and subclasses.
- __Breaking__: Remove `InsertStatement.insertAll` (use batches instead) and `insert(orReplace: true)`
(use `mode: InsertMode.orReplace` instead).
- Experimentally support IndexedDB to store sqlite data on the web
## 2.4.0

View File

@ -30,15 +30,9 @@ class InsertStatement<D extends DataClass> {
/// fails.
Future<int> insert(
Insertable<D> entity, {
@Deprecated('Use mode: InsertMode.replace instead') bool orReplace = false,
InsertMode mode,
}) async {
assert(
mode == null || (orReplace != true),
'If the mode parameter is set on insertAll, orReplace must be null or '
'false',
);
final ctx = createContext(entity, _resolveMode(mode, orReplace));
final ctx = createContext(entity, mode ?? InsertMode.insert);
return await database.executor.doWhenOpened((e) async {
final id = await database.executor.runInsert(ctx.sql, ctx.boundVariables);
@ -47,46 +41,6 @@ class InsertStatement<D extends DataClass> {
});
}
/// Inserts all [rows] into the table.
///
/// All fields in a row that don't have a default value or auto-increment
/// must be set and non-null. Otherwise, an [InvalidDataException] will be
/// thrown.
/// By default, an exception will be thrown if another row with the same
/// primary key already exists. This behavior can be overridden with [mode],
/// for instance by using [InsertMode.replace] or [InsertMode.insertOrIgnore].
@Deprecated('Call batch() on a generated database, then use Batch.insertAll')
Future<void> insertAll(
List<Insertable<D>> rows, {
@Deprecated('Use mode: InsertMode.replace instead') bool orReplace = false,
InsertMode mode,
}) async {
assert(
mode == null || (orReplace != true),
'If the mode parameter is set on insertAll, orReplace must be null or '
'false',
);
final statements = <String, List<GenerationContext>>{};
// Not every insert has the same sql, as fields which are set to null are
// not included. So, we have a map for sql -> list of variables which we can
// then turn into prepared statements
for (final row in rows) {
final ctx = createContext(row, _resolveMode(mode, orReplace));
statements.putIfAbsent(ctx.sql, () => []).add(ctx);
}
final batchedStatements = statements.entries.map((e) {
final vars = e.value.map((context) => context.boundVariables).toList();
return BatchedStatement(e.key, vars);
}).toList(growable: false);
await database.executor.doWhenOpened((e) async {
await e.runBatched(batchedStatements);
});
database.markTablesUpdated({table});
}
/// Creates a [GenerationContext] which contains the sql necessary to run an
/// insert statement fro the [entry] with the [mode].
///
@ -147,11 +101,6 @@ class InsertStatement<D extends DataClass> {
return ctx;
}
InsertMode _resolveMode(InsertMode mode, bool orReplace) {
return mode ??
(orReplace == true ? InsertMode.insertOrReplace : InsertMode.insert);
}
void _validateIntegrity(Insertable<D> d) {
if (d == null) {
throw InvalidDataException(

View File

@ -51,75 +51,12 @@ void main() {
[113, 'Done']));
});
test('generates insert or replace statements with legacy parameter',
() async {
await db.into(db.todosTable).insert(
TodoEntry(
id: 113,
content: 'Done',
),
orReplace: true);
verify(executor.runInsert(
'INSERT OR REPLACE INTO todos (id, content) VALUES (?, ?)',
[113, 'Done']));
});
test('generates DEFAULT VALUES statement when otherwise empty', () async {
await db.into(db.pureDefaults).insert(const PureDefaultsCompanion());
verify(executor.runInsert('INSERT INTO pure_defaults DEFAULT VALUES', []));
});
test('runs bulk inserts', () async {
// ignore: deprecated_member_use_from_same_package
await db.into(db.todosTable).insertAll(const [
TodosTableCompanion(content: Value('a')),
TodosTableCompanion(title: Value('title'), content: Value('b')),
TodosTableCompanion(title: Value('title'), content: Value('c')),
]);
const insertSimple = 'INSERT INTO todos (content) VALUES (?)';
const insertTitle = 'INSERT INTO todos (title, content) VALUES (?, ?)';
verify(executor.runBatched([
BatchedStatement(insertSimple, [
['a']
]),
BatchedStatement(insertTitle, [
['title', 'b'],
['title', 'c']
]),
]));
verify(streamQueries.handleTableUpdates({db.todosTable}));
});
test('runs bulk inserts with OR REPLACE', () async {
// ignore: deprecated_member_use_from_same_package
await db.into(db.todosTable).insertAll(const [
TodosTableCompanion(content: Value('a')),
TodosTableCompanion(title: Value('title'), content: Value('b')),
TodosTableCompanion(title: Value('title'), content: Value('c')),
], orReplace: true);
const insertSimple = 'INSERT OR REPLACE INTO todos (content) VALUES (?)';
const insertTitle =
'INSERT OR REPLACE INTO todos (title, content) VALUES (?, ?)';
verify(executor.runBatched([
BatchedStatement(insertSimple, [
['a']
]),
BatchedStatement(insertTitle, [
['title', 'b'],
['title', 'c']
]),
]));
verify(streamQueries.handleTableUpdates({db.todosTable}));
});
test('notifies stream queries on inserts', () async {
await db.into(db.users).insert(UsersCompanion(
name: const Value('User McUserface'),