mirror of https://github.com/AMT-Cheif/drift.git
Fix aliases for views
This commit is contained in:
parent
75fe463a9f
commit
b7690e84d9
|
@ -71,7 +71,7 @@ abstract class DatabaseConnectionUser {
|
|||
/// innerJoin(destination, routes.startPoint.equalsExp(destination.id)),
|
||||
/// ]);
|
||||
/// ```
|
||||
T alias<T extends Table, D>(TableInfo<T, D> table, String alias) {
|
||||
T alias<T, D>(ResultSetImplementation<T, D> table, String alias) {
|
||||
return table.createAlias(alias).asDslTable;
|
||||
}
|
||||
|
||||
|
|
|
@ -1761,10 +1761,20 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
|
|||
});
|
||||
}
|
||||
|
||||
Selectable<MyViewData> readView() {
|
||||
return customSelect('SELECT * FROM my_view', variables: [], readsFrom: {
|
||||
config,
|
||||
}).asyncMap(myView.mapFromRow);
|
||||
Selectable<MyViewData> readView({ReadView$where? where}) {
|
||||
var $arrayStartIndex = 1;
|
||||
final generatedwhere = $write(
|
||||
where?.call(this.myView) ?? const CustomExpression('(TRUE)'),
|
||||
startIndex: $arrayStartIndex);
|
||||
$arrayStartIndex += generatedwhere.amountOfVariables;
|
||||
return customSelect('SELECT * FROM my_view WHERE ${generatedwhere.sql}',
|
||||
variables: [
|
||||
...generatedwhere.introducedVariables
|
||||
],
|
||||
readsFrom: {
|
||||
config,
|
||||
...generatedwhere.watchedTables,
|
||||
}).asyncMap(myView.mapFromRow);
|
||||
}
|
||||
|
||||
Selectable<int> cfeTest() {
|
||||
|
@ -1966,6 +1976,7 @@ class ReadRowIdResult extends CustomResultSet {
|
|||
}
|
||||
|
||||
typedef ReadRowId$expr = Expression<int> Function(ConfigTable config);
|
||||
typedef ReadView$where = Expression<bool> Function(MyView my_view);
|
||||
|
||||
class NestedResult extends CustomResultSet {
|
||||
final WithDefault defaults;
|
||||
|
|
|
@ -80,7 +80,7 @@ searchEmails(REQUIRED :term AS TEXT OR NULL): SELECT * FROM email WHERE email MA
|
|||
|
||||
readRowId: SELECT oid, * FROM config WHERE _rowid_ = $expr;
|
||||
|
||||
readView: SELECT * FROM my_view;
|
||||
readView($where = TRUE): SELECT * FROM my_view WHERE $where;
|
||||
|
||||
cfeTest: WITH RECURSIVE
|
||||
cnt(x) AS (
|
||||
|
|
|
@ -56,6 +56,28 @@ void main() {
|
|||
return expectLater(db.readView().get(), completion(isEmpty));
|
||||
});
|
||||
|
||||
test('can be selected from with predicates', () async {
|
||||
await db.update(db.config).write(
|
||||
const ConfigCompanion(
|
||||
configKey: Value('k'),
|
||||
syncState: Value(SyncType.synchronized),
|
||||
),
|
||||
);
|
||||
|
||||
var rows = await db
|
||||
.readView(where: (v) => v.configKey.length.isBiggerOrEqualValue(3))
|
||||
.get();
|
||||
expect(rows, isEmpty);
|
||||
|
||||
await db
|
||||
.update(db.config)
|
||||
.write(const ConfigCompanion(configKey: Value('key')));
|
||||
rows = await db
|
||||
.readView(where: (v) => v.configKey.length.isBiggerOrEqualValue(3))
|
||||
.get();
|
||||
expect(rows, isNotEmpty);
|
||||
});
|
||||
|
||||
test('can be selected from dart', () async {
|
||||
await db.update(db.config).write(
|
||||
const ConfigCompanion(syncState: Value(SyncType.synchronized)));
|
||||
|
|
Loading…
Reference in New Issue