database: Add pagination and searching

This also unifies the method names.
This commit is contained in:
Elias Projahn 2020-05-01 14:31:47 +02:00
parent 1c64d56346
commit da47a01a25
2 changed files with 113 additions and 10 deletions

View file

@ -15,6 +15,8 @@ int generateId() => _random.nextInt(0xFFFFFFFF);
}, },
) )
class Database extends _$Database { class Database extends _$Database {
static const pageSize = 25;
Database(QueryExecutor queryExecutor) : super(queryExecutor); Database(QueryExecutor queryExecutor) : super(queryExecutor);
Database.connect(DatabaseConnection connection) : super.connect(connection); Database.connect(DatabaseConnection connection) : super.connect(connection);
@ -29,10 +31,50 @@ class Database extends _$Database {
}, },
); );
/// Get all available persons.
///
/// This will return a list of [pageSize] persons. You can get another page
/// using the [page] parameter. If a non empty [search] string is provided,
/// the persons will get filtered based on that string.
Future<List<Person>> getPersons([int page = 0, String search]) async {
assert(page != null);
final offset = page * pageSize;
List<Person> result;
if (search == null || search.isEmpty) {
result = await allPersons(pageSize, offset).get();
} else {
result = await searchPersons('$search%', pageSize, offset).get();
}
return result;
}
Future<void> updatePerson(Person person) async { Future<void> updatePerson(Person person) async {
await into(persons).insert(person, orReplace: true); await into(persons).insert(person, orReplace: true);
} }
/// Get all available instruments.
///
/// This will return a list of [pageSize] instruments. You can get another
/// page using the [page] parameter. If a non empty [search] string is
/// provided, the instruments will get filtered based on that string.
Future<List<Instrument>> getInstruments([int page = 0, String search]) async {
assert(page != null);
final offset = page * pageSize;
List<Instrument> result;
if (search == null || search.isEmpty) {
result = await allInstruments(pageSize, offset).get();
} else {
result = await searchInstruments('$search%', pageSize, offset).get();
}
return result;
}
Future<void> updateInstrument(Instrument instrument) async { Future<void> updateInstrument(Instrument instrument) async {
await into(instruments).insert(instrument, orReplace: true); await into(instruments).insert(instrument, orReplace: true);
} }
@ -70,8 +112,24 @@ class Database extends _$Database {
} }
/// Get information on all works written by the person with ID [personId]. /// Get information on all works written by the person with ID [personId].
Future<List<WorkInfo>> getWorks(int personId) async { ///
final works = await worksByComposer(personId).get(); /// This will return a list of [pageSize] results. You can get another page
/// using the [page] parameter. If a non empty [search] string is provided,
/// the works will be filtered using that string.
Future<List<WorkInfo>> getWorks(int personId,
[int page = 0, String search]) async {
assert(page != null);
final offset = page * pageSize;
List<Work> works;
if (search == null || search.isEmpty) {
works = await worksByComposer(personId, pageSize, offset).get();
} else {
works =
await searchWorksByComposer(personId, '$search%', pageSize, offset)
.get();
}
final List<WorkInfo> result = []; final List<WorkInfo> result = [];
for (final work in works) { for (final work in works) {
@ -123,6 +181,26 @@ class Database extends _$Database {
}); });
} }
/// Get all available ensembles.
///
/// This will return a list of [pageSize] ensembles. You can get another page
/// using the [page] parameter. If a non empty [search] string is provided,
/// the ensembles will get filtered based on that string.
Future<List<Ensemble>> getEnsembles([int page = 0, String search]) async {
assert(page != null);
final offset = page * pageSize;
List<Ensemble> result;
if (search == null || search.isEmpty) {
result = await allEnsembles(pageSize, offset).get();
} else {
result = await searchEnsembles('$search%', pageSize, offset).get();
}
return result;
}
Future<void> updateEnsemble(Ensemble ensemble) async { Future<void> updateEnsemble(Ensemble ensemble) async {
await into(ensembles).insert(ensemble, orReplace: true); await into(ensembles).insert(ensemble, orReplace: true);
} }
@ -195,8 +273,14 @@ class Database extends _$Database {
} }
/// Get information on all recordings of the work with ID [workId]. /// Get information on all recordings of the work with ID [workId].
Future<List<RecordingInfo>> getRecordings(int workId) async { ///
final recordings = await recordingsByWork(workId).get(); /// This will return a list of [pageSize] recordings. You can get the other
/// pages using the [page] parameter.
Future<List<RecordingInfo>> getRecordings(int workId, [int page = 0]) async {
assert(page != null);
final offset = page * pageSize;
final recordings = await recordingsByWork(workId, pageSize, offset).get();
final List<RecordingInfo> result = []; final List<RecordingInfo> result = [];
for (final recording in recordings) { for (final recording in recordings) {

View file

@ -43,13 +43,22 @@ CREATE TABLE performances (
); );
allPersons: allPersons:
SELECT * FROM persons ORDER BY last_name; SELECT * FROM persons ORDER BY last_name, first_name
LIMIT :limit OFFSET :offset;
searchPersons:
SELECT * FROM persons WHERE last_name LIKE :search
ORDER BY last_name, first_name LIMIT :limit OFFSET :offset;
personById: personById:
SELECT * FROM persons WHERE id = :id LIMIT 1; SELECT * FROM persons WHERE id = :id LIMIT 1;
allInstruments: allInstruments:
SELECT * FROM instruments ORDER BY name; SELECT * FROM instruments ORDER BY name LIMIT :limit OFFSET :offset;
searchInstruments:
SELECT * FROM instruments WHERE name LIKE :search ORDER BY name
LIMIT :limit OFFSET :offset;
instrumentById: instrumentById:
SELECT * FROM instruments WHERE id = :id LIMIT 1; SELECT * FROM instruments WHERE id = :id LIMIT 1;
@ -60,10 +69,15 @@ SELECT * FROM works WHERE id = :id LIMIT 1;
workParts: workParts:
SELECT * FROM works WHERE part_of = :id ORDER BY part_index; SELECT * FROM works WHERE part_of = :id ORDER BY part_index;
-- TODO: Maybe optimize.
worksByComposer: worksByComposer:
SELECT DISTINCT A.* FROM works A LEFT JOIN works B ON A.id = B.part_of 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; WHERE (A.part_of IS NULL AND A.composer = :id) OR B.composer = :id
ORDER BY A.title LIMIT :limit OFFSET :offset;
searchWorksByComposer:
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)
AND A.title LIKE :search ORDER BY A.title LIMIT :limit OFFSET :offset;
composersByWork: composersByWork:
SELECT DISTINCT persons.* FROM persons SELECT DISTINCT persons.* FROM persons
@ -76,7 +90,11 @@ SELECT instruments.* FROM instrumentations
WHERE instrumentations.work = :workId; WHERE instrumentations.work = :workId;
allEnsembles: allEnsembles:
SELECT * FROM ensembles ORDER BY name; SELECT * FROM ensembles ORDER BY name LIMIT :limit OFFSET :offset;
searchEnsembles:
SELECT * FROM ensembles WHERE name LIKE :search ORDER BY name
LIMIT :limit OFFSET :offset;
ensembleById: ensembleById:
SELECT * FROM ensembles WHERE id = :id LIMIT 1; SELECT * FROM ensembles WHERE id = :id LIMIT 1;
@ -85,7 +103,8 @@ recordingById:
SELECT * FROM recordings WHERE id = :id; SELECT * FROM recordings WHERE id = :id;
recordingsByWork: recordingsByWork:
SELECT * FROM recordings WHERE work = :id; SELECT * FROM recordings WHERE work = :id ORDER BY id
LIMIT :limit OFFSET :offset;
performancesByRecording: performancesByRecording:
SELECT * FROM performances WHERE recording = :id; SELECT * FROM performances WHERE recording = :id;