Various fixes for modular code gen

This commit is contained in:
Alexander Wilde 2022-12-05 00:12:44 +00:00
parent dafa07a88a
commit ec01ad275a
9 changed files with 43 additions and 33 deletions

View File

@ -1624,7 +1624,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
],
readsFrom: {
config,
}).asyncMap((QueryRow row) => config.mapFromRowWithAlias(row, const {
}).asyncMap((row) => config.mapFromRowWithAlias(row, const {
'ck': 'config_key',
'cf': 'config_value',
'cs1': 'sync_state',
@ -1691,7 +1691,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
readsFrom: {
config,
...generatedpred.watchedTables,
}).map((QueryRow row) => row.read<String>('config_key'));
}).map((row) => row.read<String>('config_key'));
}
Selectable<JsonResult> tableValued() {
@ -1700,7 +1700,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
variables: [],
readsFrom: {
config,
}).map((QueryRow row) {
}).map((row) {
return JsonResult(
row: row,
key: row.read<String>('key'),
@ -1713,7 +1713,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
return customSelect(
'SELECT \'one\' AS "key", NULLIF(\'two\', \'another\') AS value',
variables: [],
readsFrom: {}).map((QueryRow row) {
readsFrom: {}).map((row) {
return JsonResult(
row: row,
key: row.read<String>('key'),
@ -1739,7 +1739,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
withDefaults,
withConstraints,
...generatedpredicate.watchedTables,
}).asyncMap((QueryRow row) async {
}).asyncMap((row) async {
return MultipleResult(
row: row,
a: row.readNullable<String>('a'),
@ -1773,7 +1773,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
readsFrom: {
config,
...generatedexpr.watchedTables,
}).map((QueryRow row) {
}).map((row) {
return ReadRowIdResult(
row: row,
rowid: row.read<int>('rowid'),
@ -1809,7 +1809,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
return customSelect(
'WITH RECURSIVE cnt (x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM cnt LIMIT 1000000) SELECT x FROM cnt',
variables: [],
readsFrom: {}).map((QueryRow row) => row.read<int>('x'));
readsFrom: {}).map((row) => row.read<int>('x'));
}
Selectable<int?> nullableQuery() {
@ -1817,7 +1817,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
variables: [],
readsFrom: {
config,
}).map((QueryRow row) => row.readNullable<int>('_c0'));
}).map((row) => row.readNullable<int>('_c0'));
}
Future<List<Config>> addConfig({required Insertable<Config> value}) {
@ -1841,7 +1841,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase {
readsFrom: {
withConstraints,
withDefaults,
}).asyncMap((QueryRow row) async {
}).asyncMap((row) async {
return NestedResult(
row: row,
defaults: await withDefaults.mapFromRow(row, tablePrefix: 'nested_0'),

View File

@ -1669,7 +1669,7 @@ abstract class _$TodoDb extends GeneratedDatabase {
readsFrom: {
categories,
todosTable,
}).map((QueryRow row) {
}).map((row) {
return AllTodosWithCategoryResult(
row: row,
id: row.read<int>('id'),
@ -1728,7 +1728,7 @@ abstract class _$TodoDb extends GeneratedDatabase {
variables: [],
readsFrom: {
tableWithoutPK,
}).map((QueryRow row) => $TableWithoutPKTable.$convertercustom
}).map((row) => $TableWithoutPKTable.$convertercustom
.fromSql(row.read<String>('custom')));
}

View File

@ -113,11 +113,13 @@ class DatabaseWriter {
code: createIndex(scope, entity),
);
} else if (entity is DriftView) {
final viewClassName = dbScope.dartCode(dbScope.entityInfoType(entity));
writeMemoizedGetter(
buffer: dbScope.leaf().buffer,
getterName: entity.dbGetterName,
returnType: entity.entityInfoName,
code: '${entity.entityInfoName}(this)',
returnType: viewClassName,
code: '$viewClassName(this)',
);
}
}

View File

@ -42,7 +42,11 @@ class ModularAccessorWriter {
referencedElements.addAll(queryElement.references);
}
QueryWriter(scope.child()).write(query.value);
final value = query.value;
if (value is SqlSelectQuery) {
referencedElements.addAll(value.readsFromTables);
}
QueryWriter(scope.child()).write(value);
}
final restOfClass = scope.leaf();

View File

@ -79,7 +79,7 @@ class QueryWriter {
if (resultSet.singleColumn) {
final column = resultSet.columns.single;
_buffer.write('(QueryRow row) => '
_buffer.write('(row) => '
'${readingCode(column, scope.generationOptions, options)}');
} else if (resultSet.matchingTable != null) {
// note that, even if the result set has a matching table, we can't just
@ -92,7 +92,7 @@ class QueryWriter {
_buffer.write('${table.dbGetterName}.mapFromRow');
} else {
_buffer
..write('(QueryRow row) => ')
..write('(row) => ')
..write('${table.dbGetterName}.mapFromRowWithAlias(row, const {');
for (final alias in match.aliasToColumn.entries) {
@ -106,7 +106,7 @@ class QueryWriter {
_buffer.write('})');
}
} else {
_buffer.write('(QueryRow row) ');
_buffer.write('(row) ');
if (query.needsAsyncMapping) {
_buffer.write('async ');
}
@ -292,7 +292,8 @@ class QueryWriter {
final scopedType = scopedTypeName(element);
final args = element.availableResultSets
.map((e) => '${e.argumentType} ${e.name}')
.map((e) =>
'${_emitter.dartCode(scope.entityInfoType(e.entity))} ${e.name}')
.join(', ');
root.leaf().write('typedef $scopedType = $type Function($args);');
@ -421,9 +422,9 @@ class QueryWriter {
void _writeUpdateKind(UpdatingQuery update) {
if (update.isOnlyDelete) {
_buffer.write(', updateKind: UpdateKind.delete');
_buffer.write(', updateKind: ${_emitter.drift('UpdateKind.delete')}');
} else if (update.isOnlyUpdate) {
_buffer.write(', updateKind: UpdateKind.update');
_buffer.write(', updateKind: ${_emitter.drift('UpdateKind.update')}');
}
}
}

View File

@ -76,11 +76,11 @@ abstract class TableOrViewWriter {
final literalEntries = [
for (final entry in constraints.entries)
'SqlDialect.${entry.key.name}: ${asDartLiteral(entry.value)},',
'${emitter.drift('SqlDialect.${entry.key.name}')}: ${asDartLiteral(entry.value)},',
];
additionalParams['defaultConstraints'] =
'GeneratedColumn.constraintsDependsOnDialect({${literalEntries.join('\n')}})';
'${emitter.drift('GeneratedColumn.constraintsDependsOnDialect')}({${literalEntries.join('\n')}})';
} else {
// Constraints are the same regardless of dialect, only generate one set
// of them
@ -88,7 +88,7 @@ abstract class TableOrViewWriter {
final constraint = asDartLiteral(constraints.values.first);
additionalParams['defaultConstraints'] =
'GeneratedColumn.constraintIsAlways($constraint)';
'${emitter.drift('GeneratedColumn.constraintIsAlways')}($constraint)';
}
}

View File

@ -30,18 +30,21 @@ class ViewWriter extends TableOrViewWriter {
void _writeViewInfoClass() {
emitter = scope.leaf();
buffer.write('class ${view.entityInfoName} extends ViewInfo');
buffer.write(
'class ${view.entityInfoName} extends ${emitter.drift('ViewInfo')}');
if (scope.generationOptions.writeDataClasses) {
final viewClassName = emitter.dartCode(emitter.entityInfoType(view));
emitter
..write('<${view.entityInfoName}, ')
..write('<$viewClassName, ')
..writeDart(emitter.rowType(view))
..write('>');
} else {
buffer.write('<${view.entityInfoName}, Never>');
}
buffer.writeln(' implements HasResultSet {');
buffer.writeln(' implements ${emitter.drift('HasResultSet')} {');
final dbClassName = databaseWriter?.dbClassName ?? 'GeneratedDatabase';
final dbClassName =
databaseWriter?.dbClassName ?? emitter.drift('GeneratedDatabase');
buffer
..writeln('final String? _alias;')
..writeln('@override final $dbClassName attachedDatabase;')
@ -118,7 +121,7 @@ class ViewWriter extends TableOrViewWriter {
}
void _writeQuery() {
buffer.write('@override\nQuery? get query => ');
buffer.write('@override\n${emitter.drift('Query?')} get query => ');
final source = view.source;
if (source is DartViewSource) {

View File

@ -472,4 +472,4 @@ class UsersDrift extends i3.ModularAccessor {
i1.Users get users => this.resultSet<i1.Users>('users');
}
typedef FindUsers$predicate = i0.Expression<bool> Function(Users users);
typedef FindUsers$predicate = i0.Expression<bool> Function(i1.Users users);

View File

@ -562,7 +562,7 @@ abstract class _$Database extends GeneratedDatabase {
],
readsFrom: {
friendships,
}).map((QueryRow row) => row.read<int>('_c0'));
}).map((row) => row.read<int>('_c0'));
}
Selectable<FriendshipsOfResult> friendshipsOf(int user) {
@ -574,7 +574,7 @@ abstract class _$Database extends GeneratedDatabase {
readsFrom: {
friendships,
users,
}).asyncMap((QueryRow row) async {
}).asyncMap((row) async {
return FriendshipsOfResult(
reallyGoodFriends: row.read<bool>('really_good_friends'),
user: await users.mapFromRow(row, tablePrefix: 'nested_0'),
@ -587,7 +587,7 @@ abstract class _$Database extends GeneratedDatabase {
variables: [],
readsFrom: {
users,
}).map((QueryRow row) => row.read<int>('_c0'));
}).map((row) => row.read<int>('_c0'));
}
Selectable<Preferences?> settingsFor(int user) {
@ -597,7 +597,7 @@ abstract class _$Database extends GeneratedDatabase {
],
readsFrom: {
users,
}).map((QueryRow row) => $UsersTable.$converterpreferences
}).map((row) => $UsersTable.$converterpreferences
.fromSql(row.readNullable<String>('preferences')));
}