From b442fe9d34ba31af7c78bc76f316dd29c3f72a4c Mon Sep 17 00:00:00 2001 From: yohom <382146139@qq.com> Date: Tue, 23 Apr 2019 15:05:18 +0800 Subject: [PATCH] bugfix: Remove `_orReplace` flag in `InsertStatement`, use `orReplace` parameter. --- moor/lib/src/runtime/statements/insert.dart | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/moor/lib/src/runtime/statements/insert.dart b/moor/lib/src/runtime/statements/insert.dart index 261ee2de..21dfd01e 100644 --- a/moor/lib/src/runtime/statements/insert.dart +++ b/moor/lib/src/runtime/statements/insert.dart @@ -3,6 +3,7 @@ import 'dart:async'; import 'package:meta/meta.dart'; import 'package:moor/moor.dart'; import 'package:moor/src/runtime/components/component.dart'; + import 'update.dart'; class InsertStatement { @@ -11,8 +12,6 @@ class InsertStatement { @protected final TableInfo table; - bool _orReplace = false; - InsertStatement(this.database, this.table); /// Inserts a row constructed from the fields in [entity]. @@ -25,9 +24,9 @@ class InsertStatement { /// /// If the table contains an auto-increment column, the generated value will /// be returned. - Future insert(DataClass entity) async { + Future insert(DataClass entity, {bool orReplace = false}) async { _validateIntegrity(entity); - final ctx = _createContext(entity, _orReplace); + final ctx = _createContext(entity, orReplace); return await database.executor.doWhenOpened((e) async { final id = await database.executor.runInsert(ctx.sql, ctx.boundVariables); @@ -43,7 +42,7 @@ class InsertStatement { final ctx = GenerationContext(database); ctx.buffer ..write('INSERT ') - ..write(_orReplace ? 'OR REPLACE ' : '') + ..write(replace ? 'OR REPLACE ' : '') ..write('INTO ') ..write(table.$tableName) ..write(' (') @@ -84,7 +83,7 @@ class InsertStatement { /// When a row with the same primary or unique key already exists in the /// database, the insert will fail. Use [orReplace] to replace rows that /// already exist. - Future insertAll(List rows, {bool orReplace}) async { + Future insertAll(List rows, {bool orReplace = false}) async { final statements = >{}; // Not every insert has the same sql, as fields which are set to null are @@ -117,7 +116,6 @@ class InsertStatement { /// /// However, if no such row exists, a new row will be written instead. Future insertOrReplace(DataClass entity) async { - _orReplace = true; - await insert(entity); + return await insert(entity, orReplace: true); } }