mirror of
https://github.com/johrpan/musicus_mobile.git
synced 2025-10-26 10:47:25 +01:00
Add basic database for works and composers
This includes tables for persons, instruments and works as well as some queries and functions to work with them. The generated code should be ignored by Git for now. I also added a section to README.md explaining how to handle code generation.
This commit is contained in:
parent
2be7238a96
commit
437e30933a
4 changed files with 102 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -19,6 +19,7 @@
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|
||||||
# Flutter/Dart/Pub related
|
# Flutter/Dart/Pub related
|
||||||
|
**/*.g.dart
|
||||||
**/doc/api/
|
**/doc/api/
|
||||||
.dart_tool/
|
.dart_tool/
|
||||||
pubspec.lock
|
pubspec.lock
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,13 @@ The classical music player and organizer.
|
||||||
|
|
||||||
https://musicus.org
|
https://musicus.org
|
||||||
|
|
||||||
|
## Hacking
|
||||||
|
|
||||||
|
You can use the following command to automatically update generated code while
|
||||||
|
working on Musicus:
|
||||||
|
|
||||||
|
`flutter pub run build_runner watch`
|
||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
Musicus is free and open source software: you can redistribute it and/or modify
|
Musicus is free and open source software: you can redistribute it and/or modify
|
||||||
|
|
|
||||||
40
lib/database.dart
Normal file
40
lib/database.dart
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
import 'package:moor_flutter/moor_flutter.dart';
|
||||||
|
|
||||||
|
part 'database.g.dart';
|
||||||
|
|
||||||
|
@UseMoor(
|
||||||
|
include: {
|
||||||
|
'database.moor',
|
||||||
|
},
|
||||||
|
)
|
||||||
|
class Database extends _$Database {
|
||||||
|
Database(String fileName)
|
||||||
|
: super(FlutterQueryExecutor.inDatabaseFolder(path: fileName));
|
||||||
|
|
||||||
|
@override
|
||||||
|
int get schemaVersion => 0;
|
||||||
|
|
||||||
|
Future<void> updatePerson(Person person) async {
|
||||||
|
await into(persons).insert(person);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> updateInstrument(Instrument instrument) async {
|
||||||
|
await into(instruments).insert(instrument);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> updateWork(Work work, List<int> instrumentIds) async {
|
||||||
|
await transaction(() async {
|
||||||
|
await into(works).insert(work);
|
||||||
|
|
||||||
|
await (delete(instrumentations)..where((i) => i.work.equals(work.id)))
|
||||||
|
.go();
|
||||||
|
|
||||||
|
await into(instrumentations).insertAll(instrumentIds
|
||||||
|
.map((id) => Instrumentation(
|
||||||
|
work: work.id,
|
||||||
|
instrument: id,
|
||||||
|
))
|
||||||
|
.toList());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
54
lib/database.moor
Normal file
54
lib/database.moor
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
CREATE TABLE persons (
|
||||||
|
id INTEGER NOT NULL PRIMARY KEY,
|
||||||
|
first_name TEXT NOT NULL,
|
||||||
|
last_name TEXT NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
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
|
||||||
|
);
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
worksByComposer:
|
||||||
|
WITH RECURSIVE work_parts(id, part_of) AS (
|
||||||
|
SELECT id, part_of FROM works WHERE composer = :composerId
|
||||||
|
UNION ALL
|
||||||
|
SELECT works.id, works.part_of FROM works
|
||||||
|
JOIN work_parts ON works.id = work_parts.part_of
|
||||||
|
)
|
||||||
|
SELECT works.* FROM works
|
||||||
|
JOIN work_parts ON works.id = work_parts.id
|
||||||
|
WHERE work_parts.part_of IS NULL;
|
||||||
|
|
||||||
|
instrumentsByWork:
|
||||||
|
SELECT instruments.* FROM instrumentations
|
||||||
|
JOIN instruments ON instrumentations.instrument=instruments.id
|
||||||
|
WHERE instrumentations.work = :workId;
|
||||||
Loading…
Add table
Add a link
Reference in a new issue