mirror of
https://github.com/johrpan/musicus_mobile.git
synced 2025-10-26 10:47:25 +01:00
Move database to seperate package
This commit is contained in:
parent
939840d95f
commit
7e25880cc5
28 changed files with 98 additions and 24 deletions
1
database/lib/musicus_database.dart
Normal file
1
database/lib/musicus_database.dart
Normal file
|
|
@ -0,0 +1 @@
|
|||
export 'src/database.dart';
|
||||
102
database/lib/src/database.dart
Normal file
102
database/lib/src/database.dart
Normal file
|
|
@ -0,0 +1,102 @@
|
|||
import 'dart:math';
|
||||
|
||||
import 'package:moor/moor.dart';
|
||||
|
||||
part 'database.g.dart';
|
||||
|
||||
final _random = Random(DateTime.now().millisecondsSinceEpoch);
|
||||
int generateId() => _random.nextInt(0xFFFFFFFF);
|
||||
|
||||
class WorkModel {
|
||||
final Work work;
|
||||
final List<int> instrumentIds;
|
||||
|
||||
WorkModel({
|
||||
@required this.work,
|
||||
@required this.instrumentIds,
|
||||
});
|
||||
}
|
||||
|
||||
class PerformanceModel {
|
||||
final Person person;
|
||||
final Ensemble ensemble;
|
||||
final Instrument role;
|
||||
|
||||
PerformanceModel({
|
||||
this.person,
|
||||
this.ensemble,
|
||||
this.role,
|
||||
});
|
||||
}
|
||||
|
||||
@UseMoor(
|
||||
include: {
|
||||
'database.moor',
|
||||
},
|
||||
)
|
||||
class Database extends _$Database {
|
||||
Database.connect(DatabaseConnection connection) : super.connect(connection);
|
||||
|
||||
@override
|
||||
int get schemaVersion => 1;
|
||||
|
||||
@override
|
||||
MigrationStrategy get migration => MigrationStrategy(
|
||||
beforeOpen: (details) async {
|
||||
await customStatement('PRAGMA foreign_keys = ON');
|
||||
},
|
||||
);
|
||||
|
||||
Future<void> updatePerson(Person person) async {
|
||||
await into(persons).insert(person, orReplace: true);
|
||||
}
|
||||
|
||||
Future<void> updateInstrument(Instrument instrument) async {
|
||||
await into(instruments).insert(instrument, orReplace: true);
|
||||
}
|
||||
|
||||
Future<void> updateWork(WorkModel model, List<WorkModel> 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<void> 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> updateEnsemble(Ensemble ensemble) async {
|
||||
await into(ensembles).insert(ensemble, orReplace: true);
|
||||
}
|
||||
|
||||
Future<void> updateRecording(
|
||||
Recording recording, List<PerformanceModel> models) async {
|
||||
await transaction(() async {
|
||||
await (delete(performances)
|
||||
..where((p) => p.recording.equals(recording.id)))
|
||||
.go();
|
||||
await into(recordings).insert(recording, orReplace: true);
|
||||
for (final model in models) {
|
||||
await into(performances).insert(Performance(
|
||||
recording: recording.id,
|
||||
person: model.person?.id,
|
||||
ensemble: model.ensemble?.id,
|
||||
role: model.role?.id,
|
||||
));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
91
database/lib/src/database.moor
Normal file
91
database/lib/src/database.moor
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
CREATE TABLE persons (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
first_name TEXT NOT NULL,
|
||||
last_name TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- This represents real instruments as well as other roles that can be played
|
||||
-- in a recording.
|
||||
CREATE TABLE instruments (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE works (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
composer INTEGER REFERENCES persons(id),
|
||||
title TEXT NOT NULL,
|
||||
part_of INTEGER REFERENCES works(id) ON DELETE CASCADE,
|
||||
part_index INTEGER
|
||||
);
|
||||
|
||||
CREATE TABLE instrumentations (
|
||||
work INTEGER NOT NULL REFERENCES works(id) ON DELETE CASCADE,
|
||||
instrument INTEGER NOT NULL REFERENCES instruments(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE ensembles (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE recordings (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
work INTEGER REFERENCES works(id),
|
||||
comment TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE performances (
|
||||
recording INTEGER NOT NULL REFERENCES recordings(id) ON DELETE CASCADE,
|
||||
person INTEGER REFERENCES persons(id) ON DELETE CASCADE,
|
||||
ensemble INTEGER REFERENCES ensembles(id) ON DELETE CASCADE,
|
||||
role INTEGER REFERENCES instruments(id)
|
||||
);
|
||||
|
||||
allPersons:
|
||||
SELECT * FROM persons ORDER BY last_name;
|
||||
|
||||
personById:
|
||||
SELECT * FROM persons WHERE id = :id LIMIT 1;
|
||||
|
||||
allInstruments:
|
||||
SELECT * FROM instruments ORDER BY name;
|
||||
|
||||
instrumentById:
|
||||
SELECT * FROM instruments WHERE id = :id LIMIT 1;
|
||||
|
||||
workById:
|
||||
SELECT * FROM works WHERE id = :id LIMIT 1;
|
||||
|
||||
workParts:
|
||||
SELECT * FROM works WHERE part_of = :id ORDER BY part_index;
|
||||
|
||||
-- TODO: Maybe optimize.
|
||||
worksByComposer:
|
||||
SELECT DISTINCT A.* FROM works A LEFT JOIN works B ON A.id = B.part_of
|
||||
WHERE A.part_of IS NULL AND A.composer = :id OR B.composer = :id;
|
||||
|
||||
composersByWork:
|
||||
SELECT DISTINCT persons.* FROM persons
|
||||
JOIN works ON works.composer = persons.id
|
||||
WHERE works.id = :id OR works.part_of = :id;
|
||||
|
||||
instrumentsByWork:
|
||||
SELECT instruments.* FROM instrumentations
|
||||
JOIN instruments ON instrumentations.instrument=instruments.id
|
||||
WHERE instrumentations.work = :workId;
|
||||
|
||||
allEnsembles:
|
||||
SELECT * FROM ensembles ORDER BY name;
|
||||
|
||||
ensembleById:
|
||||
SELECT * FROM ensembles WHERE id = :id LIMIT 1;
|
||||
|
||||
recordingById:
|
||||
SELECT * FROM recordings WHERE id = :id;
|
||||
|
||||
recordingsByWork:
|
||||
SELECT * FROM recordings WHERE work = :id;
|
||||
|
||||
performancesByRecording:
|
||||
SELECT * FROM performances WHERE recording = :id;
|
||||
Loading…
Add table
Add a link
Reference in a new issue