diff --git a/drift/test/generated/custom_tables.g.dart b/drift/test/generated/custom_tables.g.dart index 0a302840..a55c5f2f 100644 --- a/drift/test/generated/custom_tables.g.dart +++ b/drift/test/generated/custom_tables.g.dart @@ -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('config_key')); + }).map((row) => row.read('config_key')); } Selectable 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('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('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('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('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('x')); + readsFrom: {}).map((row) => row.read('x')); } Selectable nullableQuery() { @@ -1817,7 +1817,7 @@ abstract class _$CustomTablesDb extends GeneratedDatabase { variables: [], readsFrom: { config, - }).map((QueryRow row) => row.readNullable('_c0')); + }).map((row) => row.readNullable('_c0')); } Future> addConfig({required Insertable 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'), diff --git a/drift/test/generated/todos.g.dart b/drift/test/generated/todos.g.dart index 13745e9d..ff401091 100644 --- a/drift/test/generated/todos.g.dart +++ b/drift/test/generated/todos.g.dart @@ -1669,7 +1669,7 @@ abstract class _$TodoDb extends GeneratedDatabase { readsFrom: { categories, todosTable, - }).map((QueryRow row) { + }).map((row) { return AllTodosWithCategoryResult( row: row, id: row.read('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('custom'))); } diff --git a/drift_dev/lib/src/writer/database_writer.dart b/drift_dev/lib/src/writer/database_writer.dart index cbfc492a..b0a065e4 100644 --- a/drift_dev/lib/src/writer/database_writer.dart +++ b/drift_dev/lib/src/writer/database_writer.dart @@ -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)', ); } } diff --git a/drift_dev/lib/src/writer/modules.dart b/drift_dev/lib/src/writer/modules.dart index 75812796..6e19af71 100644 --- a/drift_dev/lib/src/writer/modules.dart +++ b/drift_dev/lib/src/writer/modules.dart @@ -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(); diff --git a/drift_dev/lib/src/writer/queries/query_writer.dart b/drift_dev/lib/src/writer/queries/query_writer.dart index e1d07ead..266240d8 100644 --- a/drift_dev/lib/src/writer/queries/query_writer.dart +++ b/drift_dev/lib/src/writer/queries/query_writer.dart @@ -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')}'); } } } diff --git a/drift_dev/lib/src/writer/tables/table_writer.dart b/drift_dev/lib/src/writer/tables/table_writer.dart index b825547e..86276a48 100644 --- a/drift_dev/lib/src/writer/tables/table_writer.dart +++ b/drift_dev/lib/src/writer/tables/table_writer.dart @@ -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)'; } } diff --git a/drift_dev/lib/src/writer/tables/view_writer.dart b/drift_dev/lib/src/writer/tables/view_writer.dart index bb69ccec..1e2b9cfa 100644 --- a/drift_dev/lib/src/writer/tables/view_writer.dart +++ b/drift_dev/lib/src/writer/tables/view_writer.dart @@ -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) { diff --git a/examples/modular/lib/src/users.drift.dart b/examples/modular/lib/src/users.drift.dart index f3fce9e1..434a143d 100644 --- a/examples/modular/lib/src/users.drift.dart +++ b/examples/modular/lib/src/users.drift.dart @@ -472,4 +472,4 @@ class UsersDrift extends i3.ModularAccessor { i1.Users get users => this.resultSet('users'); } -typedef FindUsers$predicate = i0.Expression Function(Users users); +typedef FindUsers$predicate = i0.Expression Function(i1.Users users); diff --git a/extras/integration_tests/drift_testcases/lib/database/database.g.dart b/extras/integration_tests/drift_testcases/lib/database/database.g.dart index 86bbee6d..6eb5fbec 100644 --- a/extras/integration_tests/drift_testcases/lib/database/database.g.dart +++ b/extras/integration_tests/drift_testcases/lib/database/database.g.dart @@ -562,7 +562,7 @@ abstract class _$Database extends GeneratedDatabase { ], readsFrom: { friendships, - }).map((QueryRow row) => row.read('_c0')); + }).map((row) => row.read('_c0')); } Selectable 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('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('_c0')); + }).map((row) => row.read('_c0')); } Selectable 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('preferences'))); }