Use replace parameter rather than flag for inserts

This commit is contained in:
Simon Binder 2019-04-23 13:55:57 +02:00 committed by GitHub
commit 7ed9b02cf2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 46 additions and 47 deletions

View File

@ -3,6 +3,7 @@ import 'dart:async';
import 'package:meta/meta.dart'; import 'package:meta/meta.dart';
import 'package:moor/moor.dart'; import 'package:moor/moor.dart';
import 'package:moor/src/runtime/components/component.dart'; import 'package:moor/src/runtime/components/component.dart';
import 'update.dart'; import 'update.dart';
class InsertStatement<DataClass> { class InsertStatement<DataClass> {
@ -11,8 +12,6 @@ class InsertStatement<DataClass> {
@protected @protected
final TableInfo<Table, DataClass> table; final TableInfo<Table, DataClass> table;
bool _orReplace = false;
InsertStatement(this.database, this.table); InsertStatement(this.database, this.table);
/// Inserts a row constructed from the fields in [entity]. /// Inserts a row constructed from the fields in [entity].
@ -25,9 +24,9 @@ class InsertStatement<DataClass> {
/// ///
/// 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.
Future<int> insert(DataClass entity) async { Future<int> insert(DataClass entity, {bool orReplace = false}) async {
_validateIntegrity(entity); _validateIntegrity(entity);
final ctx = _createContext(entity, _orReplace); final ctx = _createContext(entity, orReplace);
return await database.executor.doWhenOpened((e) async { return await database.executor.doWhenOpened((e) async {
final id = await database.executor.runInsert(ctx.sql, ctx.boundVariables); final id = await database.executor.runInsert(ctx.sql, ctx.boundVariables);
@ -36,46 +35,6 @@ class InsertStatement<DataClass> {
}); });
} }
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. /// Inserts all [rows] into the table.
/// ///
/// All fields in a row that don't have a default value or auto-increment /// All fields in a row that don't have a default value or auto-increment
@ -84,7 +43,7 @@ class InsertStatement<DataClass> {
/// When a row with the same primary or unique key already exists in the /// 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 /// database, the insert will fail. Use [orReplace] to replace rows that
/// already exist. /// already exist.
Future<void> insertAll(List<DataClass> rows, {bool orReplace}) async { Future<void> insertAll(List<DataClass> rows, {bool orReplace = false}) async {
final statements = <String, List<GenerationContext>>{}; final statements = <String, List<GenerationContext>>{};
// Not every insert has the same sql, as fields which are set to null are // Not every insert has the same sql, as fields which are set to null are
@ -117,7 +76,46 @@ class InsertStatement<DataClass> {
/// ///
/// 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.
Future<void> insertOrReplace(DataClass entity) async { Future<void> insertOrReplace(DataClass entity) async {
_orReplace = true; return await insert(entity, orReplace: true);
await insert(entity); }
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}');
}
} }
} }

View File

@ -18,6 +18,7 @@ dependencies:
build_runner: '>=1.1.0 <1.4.0' build_runner: '>=1.1.0 <1.4.0'
build_config: ^0.3.1 build_config: ^0.3.1
moor: ^1.3.0 moor: ^1.3.0
meta: '>= 1.0.0 <2.0.0'
dev_dependencies: dev_dependencies:
test: ^1.6.0 test: ^1.6.0