-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Feature: Add support for @DatabaseView annotations This adds initial support for @DatabaseView annotations known from room. One test was added, further testing is needed. Implements #130. fix linting error * Clean up Uint8List/Blob<->View interop * Add unit tests * Add integration tests for view * Fix issues from review - Do not allow queries from views to be streamed - throw error if a user tries to do that and test that error - remove Stream<> queries from DAO in integration test - getStreamEntities will only return entities again - Create new Superclass for EntityProcessor and ViewProcessor named QueryableProcessor to deduplicate common functions - Code cleanup - clean up integration tests - improve code layout and comment wording in annotations, DatabaseProcessor, QueryMethodProcessor - fix toString and hashCode methods in Database (value object) - improve error message wording in DatabaseViewError * Adapt to upstream changes,split integrations tests * Fix from review part 2 (tests & documentation) - Clean up small details in code, according to the review - Improve tests - Simplify integration tests - split off and merge common QueryableProcessor tests into a separate file - move createClassElement function, which is now used in three test files to testutils - Add documentation to README.md
- Loading branch information
Showing
33 changed files
with
1,221 additions
and
411 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import 'package:floor/floor.dart'; | ||
|
||
import '../model/name.dart'; | ||
|
||
@dao | ||
abstract class NameDao { | ||
@Query('SELECT * FROM names ORDER BY name ASC') | ||
Future<List<Name>> findAllNames(); | ||
|
||
@Query('SELECT * FROM names WHERE name = :name') | ||
Future<Name> findExactName(String name); | ||
|
||
@Query('SELECT * FROM names WHERE name LIKE :suffix ORDER BY name ASC') | ||
Future<List<Name>> findNamesLike(String suffix); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import 'package:floor/floor.dart'; | ||
|
||
@DatabaseView( | ||
'SELECT custom_name as name FROM person UNION SELECT name from dog', | ||
viewName: 'names') | ||
class Name { | ||
final String name; | ||
|
||
Name(this.name); | ||
|
||
@override | ||
bool operator ==(Object other) => | ||
identical(this, other) || | ||
other is Name && runtimeType == other.runtimeType && name == other.name; | ||
|
||
@override | ||
int get hashCode => name.hashCode; | ||
|
||
@override | ||
String toString() { | ||
return 'Name{name: $name}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:floor/floor.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:path/path.dart' hide equals; | ||
import 'package:sqflite/sqflite.dart' as sqflite; | ||
import 'package:sqflite_ffi_test/sqflite_ffi_test.dart'; | ||
|
||
import '../dao/dog_dao.dart'; | ||
import '../dao/name_dao.dart'; | ||
import '../dao/person_dao.dart'; | ||
import '../model/dog.dart'; | ||
import '../model/name.dart'; | ||
import '../model/person.dart'; | ||
|
||
part 'view_test.g.dart'; | ||
|
||
@Database(version: 1, entities: [Person, Dog], views: [Name]) | ||
abstract class ViewTestDatabase extends FloorDatabase { | ||
PersonDao get personDao; | ||
|
||
DogDao get dogDao; | ||
|
||
NameDao get nameDao; | ||
} | ||
|
||
void main() { | ||
TestWidgetsFlutterBinding.ensureInitialized(); | ||
sqfliteFfiTestInit(); | ||
|
||
group('database tests', () { | ||
ViewTestDatabase database; | ||
PersonDao personDao; | ||
DogDao dogDao; | ||
NameDao nameDao; | ||
|
||
setUp(() async { | ||
database = await $FloorViewTestDatabase.inMemoryDatabaseBuilder().build(); | ||
|
||
personDao = database.personDao; | ||
dogDao = database.dogDao; | ||
nameDao = database.nameDao; | ||
}); | ||
|
||
tearDown(() async { | ||
await database.close(); | ||
}); | ||
|
||
group('Query Views', () { | ||
test('query view with exact value', () async { | ||
final person = Person(1, 'Frank'); | ||
await personDao.insertPerson(person); | ||
|
||
final actual = await nameDao.findExactName('Frank'); | ||
|
||
final expected = Name('Frank'); | ||
expect(actual, equals(expected)); | ||
}); | ||
|
||
test('query view with LIKE', () async { | ||
final persons = [Person(1, 'Leo'), Person(2, 'Frank')]; | ||
await personDao.insertPersons(persons); | ||
|
||
final dog = Dog(1, 'Romeo', 'Rome', 1); | ||
await dogDao.insertDog(dog); | ||
|
||
final actual = await nameDao.findNamesLike('%eo'); | ||
|
||
final expected = [Name('Leo'), Name('Romeo')]; | ||
expect(actual, equals(expected)); | ||
}); | ||
|
||
test('query view with all values', () async { | ||
final persons = [Person(1, 'Leo'), Person(2, 'Frank')]; | ||
await personDao.insertPersons(persons); | ||
|
||
final dog = Dog(1, 'Romeo', 'Rome', 1); | ||
await dogDao.insertDog(dog); | ||
|
||
final actual = await nameDao.findAllNames(); | ||
|
||
final expected = [Name('Frank'), Name('Leo'), Name('Romeo')]; | ||
expect(actual, equals(expected)); | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/// Marks a class as a database view (a fixed select statement). | ||
class DatabaseView { | ||
/// The table name of the SQLite view. | ||
final String viewName; | ||
|
||
/// The SELECT query on which the view is based on. | ||
final String query; | ||
|
||
/// Marks a class as a database view (a fixed select statement). | ||
const DatabaseView( | ||
this.query, { | ||
this.viewName, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.