Deprecate old insertOrReplace method in favor of parameter

This commit is contained in:
Simon Binder 2019-04-23 14:04:45 +02:00
parent 7ed9b02cf2
commit 51ae079c33
No known key found for this signature in database
GPG Key ID: B807FDF954BA00CF
3 changed files with 36 additions and 7 deletions

View File

@ -18,9 +18,11 @@ class InsertStatement<DataClass> {
/// ///
/// All fields in the entity that don't have a default value or auto-increment /// All fields in the entity that don't have a default value or auto-increment
/// must be set and non-null. Otherwise, an [InvalidDataException] will be /// must be set and non-null. Otherwise, an [InvalidDataException] will be
/// thrown. An insert will also fail if another row with the same primary key /// thrown.
/// or unique constraints already exists. If you want to override data in that ///
/// case, use [insertOrReplace] instead. /// If [orReplace] is true and a row with the same primary key already exists,
/// the columns of that row will be updated and now new row will be written.
/// Otherwise, an exception will be thrown.
/// ///
/// If the table contains an auto-increment column, the generated value will /// If the table contains an auto-increment column, the generated value will
/// be returned. /// be returned.
@ -75,6 +77,7 @@ class InsertStatement<DataClass> {
/// includes setting columns with null values back to null. /// includes setting columns with null values back to null.
/// ///
/// However, if no such row exists, a new row will be written instead. /// However, if no such row exists, a new row will be written instead.
@Deprecated('Use insert with orReplace: true instead')
Future<void> insertOrReplace(DataClass entity) async { Future<void> insertOrReplace(DataClass entity) async {
return await insert(entity, orReplace: true); return await insert(entity, orReplace: true);
} }

View File

@ -86,8 +86,8 @@ class UpdateStatement<T extends Table, D> extends Query<T, D>
/// See also: /// See also:
/// - [write], which doesn't apply a [where] statement itself and ignores /// - [write], which doesn't apply a [where] statement itself and ignores
/// null values in the entity. /// null values in the entity.
/// - [InsertStatement.insertOrReplace], which behaves similar to this method /// - [InsertStatement.insert] with the `orReplace` parameter, which behaves
/// but creates a new row if none exists. /// similar to this method but creates a new row if none exists.
Future<bool> replace(D entity) async { Future<bool> replace(D entity) async {
// We set isInserting to true here although we're in an update. This is // We set isInserting to true here although we're in an update. This is
// because all the fields from the entity will be written (as opposed to a // because all the fields from the entity will be written (as opposed to a

View File

@ -25,10 +25,12 @@ void main() {
}); });
test('generates insert or replace statements', () async { test('generates insert or replace statements', () async {
await db.into(db.todosTable).insertOrReplace(TodoEntry( await db.into(db.todosTable).insert(
TodoEntry(
id: 113, id: 113,
content: 'Done', content: 'Done',
)); ),
orReplace: true);
verify(executor.runInsert( verify(executor.runInsert(
'INSERT OR REPLACE INTO todos (id, content) VALUES (?, ?)', 'INSERT OR REPLACE INTO todos (id, content) VALUES (?, ?)',
@ -58,6 +60,30 @@ void main() {
verify(streamQueries.handleTableUpdates({db.todosTable})); verify(streamQueries.handleTableUpdates({db.todosTable}));
}); });
test('runs bulk inserts with OR REPLACE', () async {
await db.into(db.todosTable).insertAll([
TodoEntry(content: 'a'),
TodoEntry(title: 'title', content: 'b'),
TodoEntry(title: 'title', content: 'c'),
], orReplace: true);
final insertSimple = 'INSERT OR REPLACE INTO todos (content) VALUES (?)';
final 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 { test('notifies stream queries on inserts', () async {
await db.into(db.users).insert(User( await db.into(db.users).insert(User(
name: 'User McUserface', name: 'User McUserface',