diff --git a/database/lib/src/database.dart b/database/lib/src/database.dart index c00491c..193ba38 100644 --- a/database/lib/src/database.dart +++ b/database/lib/src/database.dart @@ -15,6 +15,8 @@ int generateId() => _random.nextInt(0xFFFFFFFF); }, ) class Database extends _$Database { + static const pageSize = 25; + Database(QueryExecutor queryExecutor) : super(queryExecutor); 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> getPersons([int page = 0, String search]) async { + assert(page != null); + + final offset = page * pageSize; + List result; + + if (search == null || search.isEmpty) { + result = await allPersons(pageSize, offset).get(); + } else { + result = await searchPersons('$search%', pageSize, offset).get(); + } + + return result; + } + Future updatePerson(Person person) async { 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> getInstruments([int page = 0, String search]) async { + assert(page != null); + + final offset = page * pageSize; + List result; + + if (search == null || search.isEmpty) { + result = await allInstruments(pageSize, offset).get(); + } else { + result = await searchInstruments('$search%', pageSize, offset).get(); + } + + return result; + } + Future updateInstrument(Instrument instrument) async { 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]. - Future> 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> getWorks(int personId, + [int page = 0, String search]) async { + assert(page != null); + + final offset = page * pageSize; + List works; + + if (search == null || search.isEmpty) { + works = await worksByComposer(personId, pageSize, offset).get(); + } else { + works = + await searchWorksByComposer(personId, '$search%', pageSize, offset) + .get(); + } final List result = []; 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> getEnsembles([int page = 0, String search]) async { + assert(page != null); + + final offset = page * pageSize; + List result; + + if (search == null || search.isEmpty) { + result = await allEnsembles(pageSize, offset).get(); + } else { + result = await searchEnsembles('$search%', pageSize, offset).get(); + } + + return result; + } + Future updateEnsemble(Ensemble ensemble) async { 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]. - Future> 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> getRecordings(int workId, [int page = 0]) async { + assert(page != null); + + final offset = page * pageSize; + final recordings = await recordingsByWork(workId, pageSize, offset).get(); final List result = []; for (final recording in recordings) { diff --git a/database/lib/src/database.moor b/database/lib/src/database.moor index 90e6b43..91a6478 100644 --- a/database/lib/src/database.moor +++ b/database/lib/src/database.moor @@ -43,13 +43,22 @@ CREATE TABLE performances ( ); 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: SELECT * FROM persons WHERE id = :id LIMIT 1; 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: SELECT * FROM instruments WHERE id = :id LIMIT 1; @@ -60,10 +69,15 @@ 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; + 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: SELECT DISTINCT persons.* FROM persons @@ -76,7 +90,11 @@ SELECT instruments.* FROM instrumentations WHERE instrumentations.work = :workId; 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: SELECT * FROM ensembles WHERE id = :id LIMIT 1; @@ -85,7 +103,8 @@ recordingById: SELECT * FROM recordings WHERE id = :id; recordingsByWork: -SELECT * FROM recordings WHERE work = :id; +SELECT * FROM recordings WHERE work = :id ORDER BY id + LIMIT :limit OFFSET :offset; performancesByRecording: SELECT * FROM performances WHERE recording = :id; \ No newline at end of file