diff --git a/extras/integration_tests/tests/lib/database/database.dart b/extras/integration_tests/tests/lib/database/database.dart index bb969f2b..36c58746 100644 --- a/extras/integration_tests/tests/lib/database/database.dart +++ b/extras/integration_tests/tests/lib/database/database.dart @@ -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 updateSettings(int userId, Preferences c) async { diff --git a/moor/CHANGELOG.md b/moor/CHANGELOG.md index db977703..b5af2400 100644 --- a/moor/CHANGELOG.md +++ b/moor/CHANGELOG.md @@ -6,7 +6,8 @@ - top-level `year`, `month`, `day`, `hour`, `minute`, `second` methods. Use the extension member on `Expression` 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 diff --git a/moor/lib/src/runtime/query_builder/statements/insert.dart b/moor/lib/src/runtime/query_builder/statements/insert.dart index 2e883425..a59d69aa 100644 --- a/moor/lib/src/runtime/query_builder/statements/insert.dart +++ b/moor/lib/src/runtime/query_builder/statements/insert.dart @@ -30,15 +30,9 @@ class InsertStatement { /// fails. Future insert( Insertable 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 { }); } - /// 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 insertAll( - List> 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 = >{}; - - // 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 { return ctx; } - InsertMode _resolveMode(InsertMode mode, bool orReplace) { - return mode ?? - (orReplace == true ? InsertMode.insertOrReplace : InsertMode.insert); - } - void _validateIntegrity(Insertable d) { if (d == null) { throw InvalidDataException( diff --git a/moor/test/insert_test.dart b/moor/test/insert_test.dart index d2be92a6..784f2b58 100644 --- a/moor/test/insert_test.dart +++ b/moor/test/insert_test.dart @@ -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'),