Support generation for generic row classes

This commit is contained in:
Simon Binder 2021-07-26 20:04:27 +02:00
parent 61b6dd403d
commit 3b8c0050b4
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
8 changed files with 46 additions and 9 deletions

View File

@ -1,6 +1,8 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:moor_generator/moor_generator.dart';
import 'package:moor_generator/writer.dart';
/// Some schema entity found.
///
@ -32,8 +34,14 @@ abstract class MoorEntityWithResultSet extends MoorSchemaEntity {
List<MoorColumn> get columns;
/// The name of the Dart row class for this result set.
@Deprecated('Use dartTypeCode instead')
String get dartTypeName;
/// The type name of the Dart row class for this result set.
///
/// This may contain generics.
String dartTypeCode([GenerationOptions options = const GenerationOptions()]);
/// The name of the Dart class storing additional properties like type
/// converters.
String get entityInfoName;
@ -63,4 +71,17 @@ class ExistingRowClass {
ExistingRowClass(this.targetClass, this.constructor, this.mapping,
{this.typeInstantiation = const []});
String dartType([GenerationOptions options = const GenerationOptions()]) {
if (typeInstantiation.isEmpty) {
return targetClass.name;
} else {
return targetClass
.instantiate(
typeArguments: typeInstantiation,
nullabilitySuffix: NullabilitySuffix.none,
)
.getDisplayString(withNullability: options.nnbd);
}
}
}

View File

@ -140,7 +140,7 @@ abstract class SqlQuery {
}
if (resultSet.matchingTable != null) {
return resultSet.matchingTable!.table.dartTypeName;
return resultSet.matchingTable!.table.dartTypeCode(options);
}
if (resultSet.singleColumn) {
@ -592,7 +592,7 @@ class InsertableDartPlaceholderType extends DartPlaceholderType {
if (table == null) {
return 'Insertable';
} else {
return 'Insertable<${table!.dartTypeName}>';
return 'Insertable<${table!.dartTypeCode(options)}>';
}
}
}

View File

@ -2,6 +2,7 @@ import 'package:analyzer/dart/element/element.dart';
import 'package:moor/moor.dart' show UpdateKind;
import 'package:moor_generator/src/analyzer/options.dart';
import 'package:moor_generator/src/model/used_type_converter.dart';
import 'package:moor_generator/writer.dart';
import 'package:recase/recase.dart';
import 'package:sqlparser/sqlparser.dart';
@ -70,6 +71,11 @@ class MoorTable extends MoorEntityWithResultSet {
return name;
}
@override
String dartTypeCode([GenerationOptions options = const GenerationOptions()]) {
return existingRowClass?.dartType(options) ?? dartTypeName;
}
String getNameForCompanionClass(MoorOptions options) {
final baseName =
options.useDataClassNameForCompanions ? dartTypeName : _baseName;

View File

@ -5,6 +5,7 @@ import 'package:moor_generator/src/utils/names.dart';
import 'package:recase/recase.dart';
import 'package:sqlparser/sqlparser.dart';
import '../../writer.dart';
import 'base_entity.dart';
import 'declarations/declaration.dart';
import 'model.dart';
@ -45,6 +46,11 @@ class MoorView extends MoorEntityWithResultSet {
this.existingRowClass,
});
@override
String dartTypeCode([GenerationOptions options = const GenerationOptions()]) {
return existingRowClass?.dartType(options) ?? dartTypeName;
}
/// Obtains all tables transitively referenced by the declaration of this
/// view.
///

View File

@ -39,7 +39,7 @@ class ResultSetWriter {
}
for (final nested in resultSet.nestedResults) {
var typeName = nested.table.dartTypeName;
var typeName = nested.table.dartTypeCode(scope.generationOptions);
final fieldName = nested.dartFieldName;
if (nested.isNullable) {

View File

@ -132,7 +132,7 @@ abstract class TableOrViewWriter {
return;
}
final dataClassName = tableOrView.dartTypeName;
final dataClassName = tableOrView.dartTypeCode(scope.generationOptions);
buffer.write('@override\n$dataClassName map(Map<String, dynamic> data, '
'{${scope.nullableType('String')} tablePrefix}) {\n');
@ -248,7 +248,7 @@ class TableWriter extends TableOrViewWriter {
}
} else {
// Regular generation, write full table class
final dataClass = table.dartTypeName;
final dataClass = table.dartTypeCode(scope.generationOptions);
final tableDslName = table.fromClass?.name ?? 'Table';
// class UsersTable extends Users implements TableInfo<Users, User> {
@ -318,9 +318,10 @@ class TableWriter extends TableOrViewWriter {
void _writeValidityCheckMethod() {
if (_skipVerification) return;
final innerType = table.dartTypeCode(scope.generationOptions);
buffer
..write('@override\nVerificationContext validateIntegrity'
'(Insertable<${table.dartTypeName}> instance, '
'(Insertable<$innerType> instance, '
'{bool isInserting = false}) {\n')
..write('final context = VerificationContext();\n')
..write('final data = instance.toColumns(true);\n');

View File

@ -15,7 +15,8 @@ class UpdateCompanionWriter {
void write() {
_buffer.write('class ${table.getNameForCompanionClass(scope.options)} '
'extends UpdateCompanion<${table.dartTypeName}> {\n');
'extends '
'UpdateCompanion<${table.dartTypeCode(scope.generationOptions)}> {\n');
_writeFields();
_writeConstructor();
@ -106,8 +107,9 @@ class UpdateCompanionWriter {
? 'createCustom'
: 'custom';
final dartTypeName = table.dartTypeCode(scope.generationOptions);
_buffer
..write('static Insertable<${table.dartTypeName}> $constructorName')
..write('static Insertable<$dartTypeName> $constructorName')
..write('({');
for (final column in table.columns) {

View File

@ -31,7 +31,8 @@ class ViewWriter extends TableOrViewWriter {
buffer.write('class ${view.entityInfoName} extends View');
if (scope.generationOptions.writeDataClasses) {
buffer.write('<${view.entityInfoName}, ${view.dartTypeName}>');
buffer.write('<${view.entityInfoName}, '
'${view.dartTypeCode(scope.generationOptions)}>');
} else {
buffer.write('<${view.entityInfoName}, Never>');
}