diff --git a/moor/lib/src/runtime/statements/insert.dart b/moor/lib/src/runtime/statements/insert.dart index 261ee2de..cc995660 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); @@ -36,46 +35,6 @@ class InsertStatement { }); } - GenerationContext _createContext(DataClass entry, bool replace) { - final map = table.entityToSql(entry) - ..removeWhere((_, value) => value == null); - - final ctx = GenerationContext(database); - ctx.buffer - ..write('INSERT ') - ..write(_orReplace ? 'OR REPLACE ' : '') - ..write('INTO ') - ..write(table.$tableName) - ..write(' (') - ..write(map.keys.join(', ')) - ..write(') ') - ..write('VALUES ('); - - var first = true; - for (var variable in map.values) { - if (!first) { - ctx.buffer.write(', '); - } - first = false; - - variable.writeInto(ctx); - } - - ctx.buffer.write(')'); - return ctx; - } - - void _validateIntegrity(DataClass d) { - if (d == null) { - throw InvalidDataException( - 'Cannot writee null row into ${table.$tableName}'); - } - if (!table.validateIntegrity(d, true)) { - throw InvalidDataException( - 'Invalid data: $d cannot be written into ${table.$tableName}'); - } - } - /// Inserts all [rows] into the table. /// /// All fields in a row that don't have a default value or auto-increment @@ -84,7 +43,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 +76,46 @@ 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); + } + + GenerationContext _createContext(DataClass entry, bool replace) { + final map = table.entityToSql(entry) + ..removeWhere((_, value) => value == null); + + final ctx = GenerationContext(database); + ctx.buffer + ..write('INSERT ') + ..write(replace ? 'OR REPLACE ' : '') + ..write('INTO ') + ..write(table.$tableName) + ..write(' (') + ..write(map.keys.join(', ')) + ..write(') ') + ..write('VALUES ('); + + var first = true; + for (var variable in map.values) { + if (!first) { + ctx.buffer.write(', '); + } + first = false; + + variable.writeInto(ctx); + } + + ctx.buffer.write(')'); + return ctx; + } + + void _validateIntegrity(DataClass d) { + if (d == null) { + throw InvalidDataException( + 'Cannot writee null row into ${table.$tableName}'); + } + if (!table.validateIntegrity(d, true)) { + throw InvalidDataException( + 'Invalid data: $d cannot be written into ${table.$tableName}'); + } } } diff --git a/moor_generator/pubspec.yaml b/moor_generator/pubspec.yaml index 43a638fb..c725ada0 100644 --- a/moor_generator/pubspec.yaml +++ b/moor_generator/pubspec.yaml @@ -18,6 +18,7 @@ dependencies: build_runner: '>=1.1.0 <1.4.0' build_config: ^0.3.1 moor: ^1.3.0 + meta: '>= 1.0.0 <2.0.0' dev_dependencies: test: ^1.6.0