Fix aliases for views

This commit is contained in:
Simon Binder 2022-11-14 22:35:49 +01:00
parent 75fe463a9f
commit b7690e84d9
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
4 changed files with 39 additions and 6 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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 (

View File

@ -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)));