import 'dart:math'; import 'package:moor_flutter/moor_flutter.dart'; part 'database.g.dart'; final _random = Random(DateTime.now().millisecondsSinceEpoch); int generateId() => _random.nextInt(0xFFFFFFFF); class WorkModel { final Work work; final List instrumentIds; WorkModel({ @required this.work, @required this.instrumentIds, }); } @UseMoor( include: { 'database.moor', }, ) class Database extends _$Database { Database(String fileName) : super(FlutterQueryExecutor.inDatabaseFolder(path: fileName)); @override int get schemaVersion => 1; @override MigrationStrategy get migration => MigrationStrategy( beforeOpen: (details) async { await customStatement('PRAGMA foreign_keys = ON'); } ); // TODO: Remove this once https://github.com/simolus3/moor/issues/453 is fixed. Selectable worksByComposer(int id) { return customSelectQuery( 'SELECT DISTINCT A.* FROM works A, works B ON A.id = B.part_of WHERE A.composer = :id OR B.composer = :id', variables: [Variable.withInt(id)], readsFrom: {works}).map(_rowToWork); } Future updatePerson(Person person) async { await into(persons).insert(person, orReplace: true); } Future updateInstrument(Instrument instrument) async { await into(instruments).insert(instrument, orReplace: true); } Future updateWork(WorkModel model, List parts) async { await transaction(() async { final workId = model.work.id; await (delete(works)..where((w) => w.id.equals(workId))).go(); await (delete(works)..where((w) => w.partOf.equals(workId))).go(); Future insertWork(WorkModel model) async { await into(works).insert(model.work); await batch((b) => b.insertAll( instrumentations, model.instrumentIds .map((id) => Instrumentation(work: model.work.id, instrument: id)) .toList())); } await insertWork(model); for (final part in parts) { await insertWork(part); } }); } }