From 22391a9c54b63520d717eaf23cd74976cd22dc69 Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Fri, 10 Jul 2020 17:30:10 +0200 Subject: [PATCH] Migrate benchmarks to sqlite3/ package --- extras/benchmarks/lib/src/moor/database.dart | 2 +- .../benchmarks/lib/src/moor/database.g.dart | 84 +++++++++++++------ .../lib/src/sqlite/bind_string.dart | 8 +- extras/benchmarks/pubspec.yaml | 4 +- moor/pubspec.yaml | 5 +- 5 files changed, 65 insertions(+), 38 deletions(-) diff --git a/extras/benchmarks/lib/src/moor/database.dart b/extras/benchmarks/lib/src/moor/database.dart index 9638cfc0..966397a9 100644 --- a/extras/benchmarks/lib/src/moor/database.dart +++ b/extras/benchmarks/lib/src/moor/database.dart @@ -1,7 +1,7 @@ import 'dart:io'; +import 'package:moor/ffi.dart'; import 'package:moor/moor.dart'; -import 'package:moor_ffi/moor_ffi.dart'; import 'package:path/path.dart' as p; import 'package:uuid/uuid.dart'; diff --git a/extras/benchmarks/lib/src/moor/database.g.dart b/extras/benchmarks/lib/src/moor/database.g.dart index 30d59cca..60bd295f 100644 --- a/extras/benchmarks/lib/src/moor/database.g.dart +++ b/extras/benchmarks/lib/src/moor/database.g.dart @@ -21,6 +21,26 @@ class KeyValue extends DataClass implements Insertable { stringType.mapFromDatabaseResponse(data['${effectivePrefix}value']), ); } + @override + Map toColumns(bool nullToAbsent) { + final map = {}; + if (!nullToAbsent || key != null) { + map['key'] = Variable(key); + } + if (!nullToAbsent || value != null) { + map['value'] = Variable(value); + } + return map; + } + + KeyValuesCompanion toCompanion(bool nullToAbsent) { + return KeyValuesCompanion( + key: key == null && nullToAbsent ? const Value.absent() : Value(key), + value: + value == null && nullToAbsent ? const Value.absent() : Value(value), + ); + } + factory KeyValue.fromJson(Map json, {ValueSerializer serializer}) { serializer ??= moorRuntimeOptions.defaultSerializer; @@ -38,15 +58,6 @@ class KeyValue extends DataClass implements Insertable { }; } - @override - KeyValuesCompanion createCompanion(bool nullToAbsent) { - return KeyValuesCompanion( - key: key == null && nullToAbsent ? const Value.absent() : Value(key), - value: - value == null && nullToAbsent ? const Value.absent() : Value(value), - ); - } - KeyValue copyWith({String key, String value}) => KeyValue( key: key ?? this.key, value: value ?? this.value, @@ -80,12 +91,43 @@ class KeyValuesCompanion extends UpdateCompanion { @required String value, }) : key = Value(key), value = Value(value); + static Insertable custom({ + Expression key, + Expression value, + }) { + return RawValuesInsertable({ + if (key != null) 'key': key, + if (value != null) 'value': value, + }); + } + KeyValuesCompanion copyWith({Value key, Value value}) { return KeyValuesCompanion( key: key ?? this.key, value: value ?? this.value, ); } + + @override + Map toColumns(bool nullToAbsent) { + final map = {}; + if (key.present) { + map['key'] = Variable(key.value); + } + if (value.present) { + map['value'] = Variable(value.value); + } + return map; + } + + @override + String toString() { + return (StringBuffer('KeyValuesCompanion(') + ..write('key: $key, ') + ..write('value: $value') + ..write(')')) + .toString(); + } } class $KeyValuesTable extends KeyValues @@ -126,17 +168,19 @@ class $KeyValuesTable extends KeyValues @override final String actualTableName = 'key_values'; @override - VerificationContext validateIntegrity(KeyValuesCompanion d, + VerificationContext validateIntegrity(Insertable instance, {bool isInserting = false}) { final context = VerificationContext(); - if (d.key.present) { - context.handle(_keyMeta, key.isAcceptableValue(d.key.value, _keyMeta)); + final data = instance.toColumns(true); + if (data.containsKey('key')) { + context.handle( + _keyMeta, key.isAcceptableOrUnknown(data['key'], _keyMeta)); } else if (isInserting) { context.missing(_keyMeta); } - if (d.value.present) { + if (data.containsKey('value')) { context.handle( - _valueMeta, value.isAcceptableValue(d.value.value, _valueMeta)); + _valueMeta, value.isAcceptableOrUnknown(data['value'], _valueMeta)); } else if (isInserting) { context.missing(_valueMeta); } @@ -151,18 +195,6 @@ class $KeyValuesTable extends KeyValues return KeyValue.fromData(data, _db, prefix: effectivePrefix); } - @override - Map entityToSql(KeyValuesCompanion d) { - final map = {}; - if (d.key.present) { - map['key'] = Variable(d.key.value); - } - if (d.value.present) { - map['value'] = Variable(d.value.value); - } - return map; - } - @override $KeyValuesTable createAlias(String alias) { return $KeyValuesTable(_db, alias); diff --git a/extras/benchmarks/lib/src/sqlite/bind_string.dart b/extras/benchmarks/lib/src/sqlite/bind_string.dart index 814b0d5a..84870055 100644 --- a/extras/benchmarks/lib/src/sqlite/bind_string.dart +++ b/extras/benchmarks/lib/src/sqlite/bind_string.dart @@ -1,5 +1,5 @@ import 'package:benchmarks/benchmarks.dart'; -import 'package:moor_ffi/database.dart'; +import 'package:sqlite3/sqlite3.dart'; class SelectStringBenchmark extends BenchmarkBase { SelectStringBenchmark(ScoreEmitter emitter) @@ -10,7 +10,7 @@ class SelectStringBenchmark extends BenchmarkBase { @override void setup() { - database = Database.memory(); + database = sqlite3.openInMemory(); statement = database.prepare('SELECT ?;'); } @@ -29,7 +29,7 @@ class SelectStringBenchmark extends BenchmarkBase { @override void teardown() { - statement.close(); - database.close(); + statement.dispose(); + database.dispose(); } } diff --git a/extras/benchmarks/pubspec.yaml b/extras/benchmarks/pubspec.yaml index 514d636c..568209e3 100644 --- a/extras/benchmarks/pubspec.yaml +++ b/extras/benchmarks/pubspec.yaml @@ -3,7 +3,7 @@ description: Runs simple and complex benchmarks to measure performance of moor a dependencies: moor: - moor_ffi: + sqlite3: ^0.1.3 benchmark_harness: ^1.0.5 intl: ^0.16.0 uuid: ^2.0.0 @@ -16,8 +16,6 @@ dev_dependencies: dependency_overrides: moor: path: ../../moor - moor_ffi: - path: ../../moor_ffi moor_generator: path: ../../moor_generator sqlparser: diff --git a/moor/pubspec.yaml b/moor/pubspec.yaml index 8980efa4..8158df88 100644 --- a/moor/pubspec.yaml +++ b/moor/pubspec.yaml @@ -14,10 +14,7 @@ dependencies: collection: ^1.0.0 synchronized: ^2.1.0 pedantic: ^1.0.0 - sqlite3: - git: - url: https://github.com/simolus3/sqlite3.dart.git - path: sqlite3 + sqlite3: ^0.1.3 dev_dependencies: moor_generator: ^3.2.0