mirror of https://github.com/AMT-Cheif/drift.git
Migrate to analyzer version 0.40
This commit is contained in:
parent
bbde479479
commit
e4d985728a
|
@ -18,7 +18,7 @@ class CategoriesDrawer extends StatelessWidget {
|
||||||
.subtitle1
|
.subtitle1
|
||||||
.copyWith(color: Colors.white),
|
.copyWith(color: Colors.white),
|
||||||
),
|
),
|
||||||
decoration: BoxDecoration(color: Colors.orange),
|
decoration: const BoxDecoration(color: Colors.orange),
|
||||||
),
|
),
|
||||||
Flexible(
|
Flexible(
|
||||||
child: StreamBuilder<List<CategoryWithActiveInfo>>(
|
child: StreamBuilder<List<CategoryWithActiveInfo>>(
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import 'dart:isolate';
|
import 'dart:isolate';
|
||||||
|
|
||||||
import 'package:analyzer_plugin/starter.dart';
|
import 'package:analyzer_plugin_fork/starter.dart';
|
||||||
import 'package:moor_generator/src/backends/plugin/plugin.dart';
|
import 'package:moor_generator/src/backends/plugin/plugin.dart';
|
||||||
|
|
||||||
void start(List<String> args, SendPort sendPort) {
|
void start(List<String> args, SendPort sendPort) {
|
||||||
|
|
|
@ -252,12 +252,11 @@ class ColumnParser {
|
||||||
final annotations = getter.metadata;
|
final annotations = getter.metadata;
|
||||||
final object = annotations.singleWhere((e) {
|
final object = annotations.singleWhere((e) {
|
||||||
final value = e.computeConstantValue();
|
final value = e.computeConstantValue();
|
||||||
return isFromMoor(value.type) &&
|
return isFromMoor(value.type) && value.type.element.name == 'JsonKey';
|
||||||
value.type.getDisplayString() == 'JsonKey';
|
|
||||||
}, orElse: () => null);
|
}, orElse: () => null);
|
||||||
|
|
||||||
if (object == null) return null;
|
if (object == null) return null;
|
||||||
|
|
||||||
return object.constantValue.getField('key').toStringValue();
|
return object.computeConstantValue().getField('key').toStringValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,14 +42,16 @@ class TableParser {
|
||||||
|
|
||||||
String _readDartTypeName(ClassElement element) {
|
String _readDartTypeName(ClassElement element) {
|
||||||
final nameAnnotation = element.metadata.singleWhere(
|
final nameAnnotation = element.metadata.singleWhere(
|
||||||
(e) =>
|
(e) => e.computeConstantValue().type.element.name == 'DataClassName',
|
||||||
e.computeConstantValue().type.getDisplayString() == 'DataClassName',
|
|
||||||
orElse: () => null);
|
orElse: () => null);
|
||||||
|
|
||||||
if (nameAnnotation == null) {
|
if (nameAnnotation == null) {
|
||||||
return dataClassNameForClassName(element.name);
|
return dataClassNameForClassName(element.name);
|
||||||
} else {
|
} else {
|
||||||
return nameAnnotation.constantValue.getField('name').toStringValue();
|
return nameAnnotation
|
||||||
|
.computeConstantValue()
|
||||||
|
.getField('name')
|
||||||
|
.toStringValue();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -108,7 +108,7 @@ class QueryHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
columns.add(ResultColumn(column.name, moorType, type?.nullable ?? true,
|
columns.add(ResultColumn(column.name, moorType, type?.nullable ?? true,
|
||||||
converter: converter));
|
typeConverter: converter));
|
||||||
|
|
||||||
final table = _tableOfColumn(column);
|
final table = _tableOfColumn(column);
|
||||||
candidatesForSingleTable.removeWhere((t) => t != table);
|
candidatesForSingleTable.removeWhere((t) => t != table);
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import 'package:build/build.dart';
|
import 'package:build/build.dart';
|
||||||
import 'package:moor_generator/src/backends/build/moor_builder.dart';
|
import 'package:moor_generator/src/backends/build/moor_builder.dart';
|
||||||
|
import 'package:moor_generator/src/utils/type_utils.dart';
|
||||||
import 'package:moor_generator/writer.dart';
|
import 'package:moor_generator/writer.dart';
|
||||||
import 'package:source_gen/source_gen.dart';
|
import 'package:source_gen/source_gen.dart';
|
||||||
|
|
||||||
|
@ -18,8 +19,9 @@ class DaoGenerator extends Generator implements BaseGenerator {
|
||||||
|
|
||||||
final daoName = element.displayName;
|
final daoName = element.displayName;
|
||||||
|
|
||||||
|
final dbTypeName = dao.dbClass.codeString(writer.generationOptions);
|
||||||
classScope.leaf().write('mixin _\$${daoName}Mixin on '
|
classScope.leaf().write('mixin _\$${daoName}Mixin on '
|
||||||
'DatabaseAccessor<${dao.dbClass.getDisplayString()}> {\n');
|
'DatabaseAccessor<$dbTypeName> {\n');
|
||||||
|
|
||||||
for (final table in dao.tables) {
|
for (final table in dao.tables) {
|
||||||
final infoType = table.tableInfoName;
|
final infoType = table.tableInfoName;
|
||||||
|
|
|
@ -4,8 +4,8 @@ import 'package:analyzer/src/context/context_root.dart'; // ignore: implementati
|
||||||
// ignore: implementation_imports
|
// ignore: implementation_imports
|
||||||
import 'package:analyzer/src/dart/analysis/driver.dart'
|
import 'package:analyzer/src/dart/analysis/driver.dart'
|
||||||
show AnalysisDriverScheduler;
|
show AnalysisDriverScheduler;
|
||||||
import 'package:analyzer_plugin/plugin/plugin.dart';
|
import 'package:analyzer_plugin_fork/plugin/plugin.dart';
|
||||||
import 'package:analyzer_plugin/protocol/protocol_generated.dart' as proto;
|
import 'package:analyzer_plugin_fork/protocol/protocol_generated.dart' as proto;
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:moor_generator/src/analyzer/options.dart';
|
import 'package:moor_generator/src/analyzer/options.dart';
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import 'package:analyzer_plugin/protocol/protocol_generated.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_generated.dart';
|
||||||
import 'package:logging/logging.dart';
|
import 'package:logging/logging.dart';
|
||||||
import 'package:moor_generator/src/backends/plugin/plugin.dart';
|
import 'package:moor_generator/src/backends/plugin/plugin.dart';
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
import 'package:analyzer/file_system/file_system.dart';
|
import 'package:analyzer/file_system/file_system.dart';
|
||||||
import 'package:analyzer/file_system/physical_file_system.dart';
|
import 'package:analyzer/file_system/physical_file_system.dart';
|
||||||
import 'package:analyzer_plugin/plugin/completion_mixin.dart';
|
import 'package:analyzer_plugin_fork/plugin/completion_mixin.dart';
|
||||||
import 'package:analyzer_plugin/plugin/folding_mixin.dart';
|
import 'package:analyzer_plugin_fork/plugin/folding_mixin.dart';
|
||||||
import 'package:analyzer_plugin/plugin/navigation_mixin.dart';
|
import 'package:analyzer_plugin_fork/plugin/navigation_mixin.dart';
|
||||||
import 'package:analyzer_plugin/plugin/outline_mixin.dart';
|
import 'package:analyzer_plugin_fork/plugin/outline_mixin.dart';
|
||||||
import 'package:analyzer_plugin/protocol/protocol.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol.dart';
|
||||||
import 'package:analyzer_plugin/protocol/protocol_generated.dart' as plugin;
|
import 'package:analyzer_plugin_fork/protocol/protocol_generated.dart'
|
||||||
import 'package:analyzer_plugin/utilities/completion/completion_core.dart';
|
as plugin;
|
||||||
import 'package:analyzer_plugin/utilities/folding/folding.dart';
|
import 'package:analyzer_plugin_fork/utilities/completion/completion_core.dart';
|
||||||
import 'package:analyzer_plugin/utilities/navigation/navigation.dart';
|
import 'package:analyzer_plugin_fork/utilities/folding/folding.dart';
|
||||||
import 'package:analyzer_plugin/utilities/outline/outline.dart';
|
import 'package:analyzer_plugin_fork/utilities/navigation/navigation.dart';
|
||||||
|
import 'package:analyzer_plugin_fork/utilities/outline/outline.dart';
|
||||||
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
|
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
|
||||||
import 'package:moor_generator/src/backends/common/base_plugin.dart';
|
import 'package:moor_generator/src/backends/common/base_plugin.dart';
|
||||||
import 'package:moor_generator/src/backends/common/driver.dart';
|
import 'package:moor_generator/src/backends/common/driver.dart';
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_common.dart';
|
||||||
import 'package:analyzer_plugin/utilities/completion/completion_core.dart';
|
import 'package:analyzer_plugin_fork/utilities/completion/completion_core.dart';
|
||||||
import 'package:moor_generator/src/backends/plugin/services/requests.dart';
|
import 'package:moor_generator/src/backends/plugin/services/requests.dart';
|
||||||
|
|
||||||
class MoorCompletingContributor implements CompletionContributor {
|
class MoorCompletingContributor implements CompletionContributor {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_common.dart';
|
||||||
import 'package:analyzer_plugin/protocol/protocol_generated.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_generated.dart';
|
||||||
import 'package:moor_generator/src/analyzer/errors.dart';
|
import 'package:moor_generator/src/analyzer/errors.dart';
|
||||||
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
|
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
|
||||||
import 'package:moor_generator/src/backends/plugin/plugin.dart';
|
import 'package:moor_generator/src/backends/plugin/plugin.dart';
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_common.dart';
|
||||||
import 'package:analyzer_plugin/utilities/folding/folding.dart';
|
import 'package:analyzer_plugin_fork/utilities/folding/folding.dart';
|
||||||
import 'package:moor_generator/src/backends/plugin/services/requests.dart';
|
import 'package:moor_generator/src/backends/plugin/services/requests.dart';
|
||||||
import 'package:sqlparser/sqlparser.dart';
|
import 'package:sqlparser/sqlparser.dart';
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_common.dart';
|
||||||
import 'package:analyzer_plugin/utilities/navigation/navigation.dart';
|
import 'package:analyzer_plugin_fork/utilities/navigation/navigation.dart';
|
||||||
import 'package:moor_generator/moor_generator.dart';
|
import 'package:moor_generator/moor_generator.dart';
|
||||||
import 'package:moor_generator/src/backends/plugin/services/requests.dart';
|
import 'package:moor_generator/src/backends/plugin/services/requests.dart';
|
||||||
import 'package:moor_generator/src/backends/plugin/utils/ast_to_location.dart';
|
import 'package:moor_generator/src/backends/plugin/utils/ast_to_location.dart';
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_common.dart';
|
||||||
import 'package:analyzer_plugin/utilities/outline/outline.dart';
|
import 'package:analyzer_plugin_fork/utilities/outline/outline.dart';
|
||||||
import 'package:moor_generator/src/backends/plugin/services/requests.dart';
|
import 'package:moor_generator/src/backends/plugin/services/requests.dart';
|
||||||
import 'package:moor_generator/src/backends/plugin/utils/ast_to_location.dart';
|
import 'package:moor_generator/src/backends/plugin/utils/ast_to_location.dart';
|
||||||
import 'package:sqlparser/sqlparser.dart';
|
import 'package:sqlparser/sqlparser.dart';
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import 'package:analyzer/file_system/file_system.dart';
|
import 'package:analyzer/file_system/file_system.dart';
|
||||||
import 'package:analyzer_plugin/utilities/assist/assist.dart';
|
import 'package:analyzer_plugin_fork/utilities/assist/assist.dart';
|
||||||
import 'package:analyzer_plugin/utilities/completion/completion_core.dart';
|
import 'package:analyzer_plugin_fork/utilities/completion/completion_core.dart';
|
||||||
import 'package:analyzer_plugin/utilities/folding/folding.dart';
|
import 'package:analyzer_plugin_fork/utilities/folding/folding.dart';
|
||||||
import 'package:analyzer_plugin/utilities/highlights/highlights.dart';
|
import 'package:analyzer_plugin_fork/utilities/highlights/highlights.dart';
|
||||||
import 'package:analyzer_plugin/utilities/navigation/navigation.dart';
|
import 'package:analyzer_plugin_fork/utilities/navigation/navigation.dart';
|
||||||
import 'package:analyzer_plugin/utilities/outline/outline.dart';
|
import 'package:analyzer_plugin_fork/utilities/outline/outline.dart';
|
||||||
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
|
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
|
||||||
import 'package:moor_generator/src/analyzer/runner/results.dart';
|
import 'package:moor_generator/src/analyzer/runner/results.dart';
|
||||||
import 'package:source_span/source_span.dart';
|
import 'package:source_span/source_span.dart';
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_common.dart';
|
||||||
import 'package:moor_generator/moor_generator.dart';
|
import 'package:moor_generator/moor_generator.dart';
|
||||||
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
|
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
|
||||||
import 'package:source_gen/source_gen.dart' show spanForElement;
|
import 'package:source_gen/source_gen.dart' show spanForElement;
|
||||||
|
|
|
@ -2,7 +2,7 @@ import 'dart:io';
|
||||||
|
|
||||||
import 'package:analyzer/file_system/file_system.dart';
|
import 'package:analyzer/file_system/file_system.dart';
|
||||||
import 'package:analyzer/file_system/memory_file_system.dart';
|
import 'package:analyzer/file_system/memory_file_system.dart';
|
||||||
import 'package:analyzer_plugin/protocol/protocol_generated.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_generated.dart';
|
||||||
import 'package:cli_util/cli_util.dart';
|
import 'package:cli_util/cli_util.dart';
|
||||||
import 'package:moor_generator/src/analyzer/options.dart';
|
import 'package:moor_generator/src/analyzer/options.dart';
|
||||||
import 'package:moor_generator/src/backends/common/base_plugin.dart';
|
import 'package:moor_generator/src/backends/common/base_plugin.dart';
|
||||||
|
|
|
@ -2,8 +2,8 @@ import 'dart:async';
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:analyzer_plugin/channel/channel.dart';
|
import 'package:analyzer_plugin_fork/channel/channel.dart';
|
||||||
import 'package:analyzer_plugin/protocol/protocol.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol.dart';
|
||||||
import 'package:moor_generator/src/backends/plugin/plugin.dart';
|
import 'package:moor_generator/src/backends/plugin/plugin.dart';
|
||||||
|
|
||||||
import '../cli.dart';
|
import '../cli.dart';
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import 'package:moor_generator/src/analyzer/options.dart';
|
import 'package:moor_generator/src/analyzer/options.dart';
|
||||||
|
|
||||||
import 'declarations/declaration.dart';
|
import 'declarations/declaration.dart';
|
||||||
|
import 'types.dart';
|
||||||
import 'used_type_converter.dart';
|
import 'used_type_converter.dart';
|
||||||
|
|
||||||
/// The column types in sql.
|
/// The column types in sql.
|
||||||
|
@ -37,48 +38,8 @@ class ColumnName {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const Map<ColumnType, String> dartTypeNames = {
|
|
||||||
ColumnType.boolean: 'bool',
|
|
||||||
ColumnType.text: 'String',
|
|
||||||
ColumnType.integer: 'int',
|
|
||||||
ColumnType.datetime: 'DateTime',
|
|
||||||
ColumnType.blob: 'Uint8List',
|
|
||||||
ColumnType.real: 'double',
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Maps to the method name of a "QueryRow" from moor to extract a column type
|
|
||||||
/// of a result row.
|
|
||||||
const Map<ColumnType, String> readFromMethods = {
|
|
||||||
ColumnType.boolean: 'readBool',
|
|
||||||
ColumnType.text: 'readString',
|
|
||||||
ColumnType.integer: 'readInt',
|
|
||||||
ColumnType.datetime: 'readDateTime',
|
|
||||||
ColumnType.blob: 'readBlob',
|
|
||||||
ColumnType.real: 'readDouble',
|
|
||||||
};
|
|
||||||
|
|
||||||
/// Maps from a column type to code that can be used to create a variable of the
|
|
||||||
/// respective type.
|
|
||||||
const Map<ColumnType, String> createVariable = {
|
|
||||||
ColumnType.boolean: 'Variable.withBool',
|
|
||||||
ColumnType.text: 'Variable.withString',
|
|
||||||
ColumnType.integer: 'Variable.withInt',
|
|
||||||
ColumnType.datetime: 'Variable.withDateTime',
|
|
||||||
ColumnType.blob: 'Variable.withBlob',
|
|
||||||
ColumnType.real: 'Variable.withReal',
|
|
||||||
};
|
|
||||||
|
|
||||||
const Map<ColumnType, String> sqlTypes = {
|
|
||||||
ColumnType.boolean: 'BoolType',
|
|
||||||
ColumnType.text: 'StringType',
|
|
||||||
ColumnType.integer: 'IntType',
|
|
||||||
ColumnType.datetime: 'DateTimeType',
|
|
||||||
ColumnType.blob: 'BlobType',
|
|
||||||
ColumnType.real: 'RealType',
|
|
||||||
};
|
|
||||||
|
|
||||||
/// A column, as specified by a getter in a table.
|
/// A column, as specified by a getter in a table.
|
||||||
class MoorColumn implements HasDeclaration {
|
class MoorColumn implements HasDeclaration, HasType {
|
||||||
/// The getter name of this column in the table class. It will also be used
|
/// The getter name of this column in the table class. It will also be used
|
||||||
/// as getter name in the TableInfo class (as it needs to override the field)
|
/// as getter name in the TableInfo class (as it needs to override the field)
|
||||||
/// and in the generated data class that will be generated for each table.
|
/// and in the generated data class that will be generated for each table.
|
||||||
|
@ -93,6 +54,7 @@ class MoorColumn implements HasDeclaration {
|
||||||
bool get declaredInMoorFile => declaration?.isDefinedInMoorFile ?? false;
|
bool get declaredInMoorFile => declaration?.isDefinedInMoorFile ?? false;
|
||||||
|
|
||||||
/// The sql type of this column
|
/// The sql type of this column
|
||||||
|
@override
|
||||||
final ColumnType type;
|
final ColumnType type;
|
||||||
|
|
||||||
/// The name of this column, as chosen by the user
|
/// The name of this column, as chosen by the user
|
||||||
|
@ -110,6 +72,7 @@ class MoorColumn implements HasDeclaration {
|
||||||
|
|
||||||
/// Whether the user has explicitly declared this column to be nullable, the
|
/// Whether the user has explicitly declared this column to be nullable, the
|
||||||
/// default is false
|
/// default is false
|
||||||
|
@override
|
||||||
final bool nullable;
|
final bool nullable;
|
||||||
|
|
||||||
/// Whether this column has auto increment.
|
/// Whether this column has auto increment.
|
||||||
|
@ -130,22 +93,9 @@ class MoorColumn implements HasDeclaration {
|
||||||
final String clientDefaultCode;
|
final String clientDefaultCode;
|
||||||
|
|
||||||
/// The [UsedTypeConverter], if one has been set on this column.
|
/// The [UsedTypeConverter], if one has been set on this column.
|
||||||
|
@override
|
||||||
final UsedTypeConverter typeConverter;
|
final UsedTypeConverter typeConverter;
|
||||||
|
|
||||||
/// The dart type that matches the values of this column. For instance, if a
|
|
||||||
/// table has declared an `IntColumn`, the matching dart type name would be
|
|
||||||
/// [int].
|
|
||||||
String get dartTypeName {
|
|
||||||
if (typeConverter != null) {
|
|
||||||
return typeConverter.mappedType?.getDisplayString();
|
|
||||||
}
|
|
||||||
return variableTypeName;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// the Dart type of this column that can be handled by moors type mapping.
|
|
||||||
/// Basically the same as [dartTypeName], minus custom types.
|
|
||||||
String get variableTypeName => dartTypeNames[type];
|
|
||||||
|
|
||||||
/// The column type from the dsl library. For instance, if a table has
|
/// The column type from the dsl library. For instance, if a table has
|
||||||
/// declared an `IntColumn`, the matching dsl column name would also be an
|
/// declared an `IntColumn`, the matching dsl column name would also be an
|
||||||
/// `IntColumn`.
|
/// `IntColumn`.
|
||||||
|
@ -170,10 +120,6 @@ class MoorColumn implements HasDeclaration {
|
||||||
ColumnType.real: 'GeneratedRealColumn',
|
ColumnType.real: 'GeneratedRealColumn',
|
||||||
}[type];
|
}[type];
|
||||||
|
|
||||||
/// The class inside the moor library that represents the same sql type as
|
|
||||||
/// this column.
|
|
||||||
String get sqlTypeName => sqlTypes[type];
|
|
||||||
|
|
||||||
MoorColumn({
|
MoorColumn({
|
||||||
this.type,
|
this.type,
|
||||||
this.dartGetterName,
|
this.dartGetterName,
|
||||||
|
|
|
@ -8,4 +8,5 @@ export 'special_queries.dart';
|
||||||
export 'sql_query.dart';
|
export 'sql_query.dart';
|
||||||
export 'table.dart';
|
export 'table.dart';
|
||||||
export 'trigger.dart';
|
export 'trigger.dart';
|
||||||
|
export 'types.dart';
|
||||||
export 'used_type_converter.dart';
|
export 'used_type_converter.dart';
|
||||||
|
|
|
@ -3,11 +3,13 @@ import 'package:meta/meta.dart';
|
||||||
import 'package:moor/moor.dart' show $mrjf, $mrjc, UpdateKind;
|
import 'package:moor/moor.dart' show $mrjf, $mrjc, UpdateKind;
|
||||||
import 'package:moor_generator/src/analyzer/runner/results.dart';
|
import 'package:moor_generator/src/analyzer/runner/results.dart';
|
||||||
import 'package:moor_generator/src/utils/hash.dart';
|
import 'package:moor_generator/src/utils/hash.dart';
|
||||||
|
import 'package:moor_generator/src/writer/writer.dart';
|
||||||
import 'package:recase/recase.dart';
|
import 'package:recase/recase.dart';
|
||||||
import 'package:sqlparser/sqlparser.dart';
|
import 'package:sqlparser/sqlparser.dart';
|
||||||
|
|
||||||
import 'column.dart';
|
import 'column.dart';
|
||||||
import 'table.dart';
|
import 'table.dart';
|
||||||
|
import 'types.dart';
|
||||||
import 'used_type_converter.dart';
|
import 'used_type_converter.dart';
|
||||||
|
|
||||||
final _illegalChars = RegExp(r'[^0-9a-zA-Z_]');
|
final _illegalChars = RegExp(r'[^0-9a-zA-Z_]');
|
||||||
|
@ -112,22 +114,6 @@ class SqlSelectQuery extends SqlQuery {
|
||||||
/// The name of the result class, as requested by the user.
|
/// The name of the result class, as requested by the user.
|
||||||
final String /*?*/ requestedResultClass;
|
final String /*?*/ requestedResultClass;
|
||||||
|
|
||||||
String get resultClassName {
|
|
||||||
if (resultSet.matchingTable != null) {
|
|
||||||
return resultSet.matchingTable.table.dartTypeName;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (resultSet.singleColumn) {
|
|
||||||
return resultSet.columns.single.dartType;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (resultSet.resultClassName != null) {
|
|
||||||
return resultSet.resultClassName;
|
|
||||||
}
|
|
||||||
|
|
||||||
return '${ReCase(name).pascalCase}Result';
|
|
||||||
}
|
|
||||||
|
|
||||||
SqlSelectQuery(
|
SqlSelectQuery(
|
||||||
String name,
|
String name,
|
||||||
AnalysisContext fromContext,
|
AnalysisContext fromContext,
|
||||||
|
@ -151,6 +137,30 @@ class SqlSelectQuery extends SqlQuery {
|
||||||
null,
|
null,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String get resultClassName {
|
||||||
|
if (resultSet.matchingTable != null || resultSet.singleColumn) {
|
||||||
|
throw UnsupportedError('This result set does not introduce a class, '
|
||||||
|
'either because it has a matching table or because it only returns '
|
||||||
|
'one column.');
|
||||||
|
}
|
||||||
|
|
||||||
|
return resultSet.resultClassName ?? '${ReCase(name).pascalCase}Result';
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The Dart type representing a row of this result set.
|
||||||
|
String resultTypeCode(
|
||||||
|
[GenerationOptions options = const GenerationOptions()]) {
|
||||||
|
if (resultSet.matchingTable != null) {
|
||||||
|
return resultSet.matchingTable.table.dartTypeName;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (resultSet.singleColumn) {
|
||||||
|
return resultSet.columns.single.dartTypeCode(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
return resultClassName;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class UpdatingQuery extends SqlQuery {
|
class UpdatingQuery extends SqlQuery {
|
||||||
|
@ -297,29 +307,25 @@ class MatchingMoorTable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ResultColumn {
|
class ResultColumn implements HasType {
|
||||||
final String name;
|
final String name;
|
||||||
|
@override
|
||||||
final ColumnType type;
|
final ColumnType type;
|
||||||
|
@override
|
||||||
final bool nullable;
|
final bool nullable;
|
||||||
|
|
||||||
final UsedTypeConverter converter;
|
@override
|
||||||
|
final UsedTypeConverter typeConverter;
|
||||||
|
|
||||||
ResultColumn(this.name, this.type, this.nullable, {this.converter});
|
ResultColumn(this.name, this.type, this.nullable, {this.typeConverter});
|
||||||
|
|
||||||
/// The dart type that can store a result of this column.
|
|
||||||
String get dartType {
|
|
||||||
if (converter != null) {
|
|
||||||
return converter.mappedType.getDisplayString();
|
|
||||||
} else {
|
|
||||||
return dartTypeNames[type];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Hash-code that matching [compatibleTo], so that two compatible columns
|
/// Hash-code that matching [compatibleTo], so that two compatible columns
|
||||||
/// will have the same [compatibilityHashCode].
|
/// will have the same [compatibilityHashCode].
|
||||||
int get compatibilityHashCode {
|
int get compatibilityHashCode {
|
||||||
return $mrjf($mrjc(name.hashCode,
|
return $mrjf($mrjc(
|
||||||
$mrjc(type.hashCode, $mrjc(nullable.hashCode, converter.hashCode))));
|
name.hashCode,
|
||||||
|
$mrjc(
|
||||||
|
type.hashCode, $mrjc(nullable.hashCode, typeConverter.hashCode))));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks whether this column is compatible to the [other], meaning that they
|
/// Checks whether this column is compatible to the [other], meaning that they
|
||||||
|
@ -328,7 +334,7 @@ class ResultColumn {
|
||||||
return other.name == name &&
|
return other.name == name &&
|
||||||
other.type == type &&
|
other.type == type &&
|
||||||
other.nullable == nullable &&
|
other.nullable == nullable &&
|
||||||
other.converter == converter;
|
other.typeConverter == typeConverter;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -439,7 +445,8 @@ class FoundVariable extends FoundElement {
|
||||||
String get parameterType {
|
String get parameterType {
|
||||||
String innerType;
|
String innerType;
|
||||||
if (converter != null) {
|
if (converter != null) {
|
||||||
innerType = converter.mappedType.getDisplayString();
|
// todo: Respect nullability here
|
||||||
|
innerType = converter.mappedType.getDisplayString(withNullability: false);
|
||||||
} else {
|
} else {
|
||||||
innerType = dartTypeNames[type] ?? 'dynamic';
|
innerType = dartTypeNames[type] ?? 'dynamic';
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
import 'package:moor_generator/src/model/model.dart';
|
||||||
|
import 'package:moor_generator/src/utils/type_utils.dart';
|
||||||
|
import 'package:moor_generator/writer.dart';
|
||||||
|
|
||||||
|
/// Something that has a type.
|
||||||
|
///
|
||||||
|
/// This includes table and result-set column and variables.
|
||||||
|
abstract class HasType {
|
||||||
|
/// Whether the type is nullable in Dart.
|
||||||
|
bool get nullable;
|
||||||
|
|
||||||
|
/// The associated sql type.
|
||||||
|
ColumnType get type;
|
||||||
|
|
||||||
|
/// The applied type converter, or null.
|
||||||
|
UsedTypeConverter get typeConverter;
|
||||||
|
}
|
||||||
|
|
||||||
|
extension OperationOnTypes on HasType {
|
||||||
|
/// the Dart type of this column that can be handled by moors type mapping.
|
||||||
|
/// Basically the same as [dartTypeCode], minus custom types.
|
||||||
|
// todo: Respect nullability
|
||||||
|
String get variableTypeName => dartTypeNames[type];
|
||||||
|
|
||||||
|
/// The class inside the moor library that represents the same sql type as
|
||||||
|
/// this column.
|
||||||
|
String get sqlTypeName => sqlTypes[type];
|
||||||
|
|
||||||
|
/// The dart type that matches the values of this column. For instance, if a
|
||||||
|
/// table has declared an `IntColumn`, the matching dart type name would be
|
||||||
|
/// [int].
|
||||||
|
String dartTypeCode([GenerationOptions options = const GenerationOptions()]) {
|
||||||
|
if (typeConverter != null) {
|
||||||
|
return typeConverter.mappedType?.codeString(options);
|
||||||
|
}
|
||||||
|
|
||||||
|
final hasSuffix = nullable && options.nnbd;
|
||||||
|
return hasSuffix ? '$variableTypeName?' : variableTypeName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const Map<ColumnType, String> dartTypeNames = {
|
||||||
|
ColumnType.boolean: 'bool',
|
||||||
|
ColumnType.text: 'String',
|
||||||
|
ColumnType.integer: 'int',
|
||||||
|
ColumnType.datetime: 'DateTime',
|
||||||
|
ColumnType.blob: 'Uint8List',
|
||||||
|
ColumnType.real: 'double',
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Maps to the method name of a "QueryRow" from moor to extract a column type
|
||||||
|
/// of a result row.
|
||||||
|
const Map<ColumnType, String> readFromMethods = {
|
||||||
|
ColumnType.boolean: 'readBool',
|
||||||
|
ColumnType.text: 'readString',
|
||||||
|
ColumnType.integer: 'readInt',
|
||||||
|
ColumnType.datetime: 'readDateTime',
|
||||||
|
ColumnType.blob: 'readBlob',
|
||||||
|
ColumnType.real: 'readDouble',
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Maps from a column type to code that can be used to create a variable of the
|
||||||
|
/// respective type.
|
||||||
|
const Map<ColumnType, String> createVariable = {
|
||||||
|
ColumnType.boolean: 'Variable.withBool',
|
||||||
|
ColumnType.text: 'Variable.withString',
|
||||||
|
ColumnType.integer: 'Variable.withInt',
|
||||||
|
ColumnType.datetime: 'Variable.withDateTime',
|
||||||
|
ColumnType.blob: 'Variable.withBlob',
|
||||||
|
ColumnType.real: 'Variable.withReal',
|
||||||
|
};
|
||||||
|
|
||||||
|
const Map<ColumnType, String> sqlTypes = {
|
||||||
|
ColumnType.boolean: 'BoolType',
|
||||||
|
ColumnType.text: 'StringType',
|
||||||
|
ColumnType.integer: 'IntType',
|
||||||
|
ColumnType.datetime: 'DateTimeType',
|
||||||
|
ColumnType.blob: 'BlobType',
|
||||||
|
ColumnType.real: 'RealType',
|
||||||
|
};
|
|
@ -2,8 +2,11 @@ import 'package:analyzer/dart/element/element.dart';
|
||||||
import 'package:analyzer/dart/element/type.dart';
|
import 'package:analyzer/dart/element/type.dart';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:moor_generator/src/model/table.dart';
|
import 'package:moor_generator/src/model/table.dart';
|
||||||
|
import 'package:moor_generator/src/utils/type_utils.dart';
|
||||||
|
import 'package:moor_generator/src/writer/writer.dart';
|
||||||
|
|
||||||
import 'column.dart';
|
import 'column.dart';
|
||||||
|
import 'types.dart';
|
||||||
|
|
||||||
class UsedTypeConverter {
|
class UsedTypeConverter {
|
||||||
/// Index of this converter in the table in which it has been created.
|
/// Index of this converter in the table in which it has been created.
|
||||||
|
@ -23,12 +26,6 @@ class UsedTypeConverter {
|
||||||
/// The type that will be written to the database.
|
/// The type that will be written to the database.
|
||||||
final ColumnType sqlType;
|
final ColumnType sqlType;
|
||||||
|
|
||||||
/// A suitable typename to store an instance of the type converter used here.
|
|
||||||
String get displayNameOfConverter {
|
|
||||||
final sqlDartType = dartTypeNames[sqlType];
|
|
||||||
return 'TypeConverter<${mappedType.getDisplayString()}, $sqlDartType>';
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Type converters are stored as static fields in the table that created
|
/// Type converters are stored as static fields in the table that created
|
||||||
/// them. This will be the field name for this converter.
|
/// them. This will be the field name for this converter.
|
||||||
String get fieldName => '\$converter$index';
|
String get fieldName => '\$converter$index';
|
||||||
|
@ -57,6 +54,12 @@ class UsedTypeConverter {
|
||||||
sqlType: ColumnType.integer,
|
sqlType: ColumnType.integer,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A suitable typename to store an instance of the type converter used here.
|
||||||
|
String converterNameInCode(GenerationOptions options) {
|
||||||
|
final sqlDartType = dartTypeNames[sqlType];
|
||||||
|
return 'TypeConverter<${mappedType.codeString(options)}, $sqlDartType>';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class InvalidTypeForEnumConverterException implements Exception {
|
class InvalidTypeForEnumConverterException implements Exception {
|
||||||
|
@ -66,13 +69,13 @@ class InvalidTypeForEnumConverterException implements Exception {
|
||||||
InvalidTypeForEnumConverterException(this.reason, this.invalidType);
|
InvalidTypeForEnumConverterException(this.reason, this.invalidType);
|
||||||
|
|
||||||
String get errorDescription {
|
String get errorDescription {
|
||||||
return "Can't use the type ${invalidType.getDisplayString()} as an enum "
|
return "Can't use the type ${invalidType.userVisibleName} as an enum "
|
||||||
'type: $reason';
|
'type: $reason';
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
String toString() {
|
String toString() {
|
||||||
return 'Invalid type for enum converter: '
|
return 'Invalid type for enum converter: '
|
||||||
'${invalidType.getDisplayString()}. Reason: $reason';
|
'${invalidType.userVisibleName}. Reason: $reason';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_common.dart';
|
||||||
import 'package:analyzer_plugin/protocol/protocol_generated.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_generated.dart';
|
||||||
import 'package:analyzer_plugin/utilities/assist/assist.dart';
|
import 'package:analyzer_plugin_fork/utilities/assist/assist.dart';
|
||||||
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
|
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
|
||||||
import 'package:sqlparser/sqlparser.dart';
|
import 'package:sqlparser/sqlparser.dart';
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_common.dart';
|
||||||
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
|
import 'package:moor_generator/src/analyzer/runner/file_graph.dart';
|
||||||
import 'package:sqlparser/sqlparser.dart';
|
import 'package:sqlparser/sqlparser.dart';
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_common.dart';
|
||||||
import 'package:analyzer_plugin/protocol/protocol_generated.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_generated.dart';
|
||||||
import 'package:moor_generator/src/analyzer/session.dart';
|
import 'package:moor_generator/src/analyzer/session.dart';
|
||||||
import 'package:moor_generator/src/services/ide/assists/assist_service.dart';
|
import 'package:moor_generator/src/services/ide/assists/assist_service.dart';
|
||||||
|
|
||||||
|
|
|
@ -101,7 +101,8 @@ class SchemaWriter {
|
||||||
if (column.typeConverter != null)
|
if (column.typeConverter != null)
|
||||||
'type_converter': {
|
'type_converter': {
|
||||||
'dart_expr': column.typeConverter.expression,
|
'dart_expr': column.typeConverter.expression,
|
||||||
'dart_type_name': column.typeConverter.mappedType.getDisplayString(),
|
'dart_type_name': column.typeConverter.mappedType
|
||||||
|
.getDisplayString(withNullability: false),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
|
import 'package:analyzer/dart/element/nullability_suffix.dart';
|
||||||
import 'package:analyzer/dart/element/type.dart';
|
import 'package:analyzer/dart/element/type.dart';
|
||||||
|
import 'package:moor_generator/src/writer/writer.dart';
|
||||||
|
|
||||||
bool isFromMoor(DartType type) {
|
bool isFromMoor(DartType type) {
|
||||||
return type.element?.library?.location?.components?.first?.contains('moor') ??
|
return type.element?.library?.location?.components?.first?.contains('moor') ??
|
||||||
|
@ -6,9 +8,23 @@ bool isFromMoor(DartType type) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isColumn(DartType type) {
|
bool isColumn(DartType type) {
|
||||||
final name = type.getDisplayString();
|
final name = type.element?.name ?? '';
|
||||||
|
|
||||||
return isFromMoor(type) &&
|
return isFromMoor(type) &&
|
||||||
name.contains('Column') &&
|
name.contains('Column') &&
|
||||||
!name.contains('Builder');
|
!name.contains('Builder');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension TypeUtils on DartType {
|
||||||
|
String get userVisibleName => getDisplayString(withNullability: true);
|
||||||
|
|
||||||
|
/// How this type should look like in generated code.
|
||||||
|
String codeString([GenerationOptions options = const GenerationOptions()]) {
|
||||||
|
if (nullabilitySuffix == NullabilitySuffix.star) {
|
||||||
|
// We can't actually use the legacy star in code, so don't show it.
|
||||||
|
return getDisplayString(withNullability: false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return getDisplayString(withNullability: options.nnbd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ import 'package:moor/src/runtime/executor/stream_queries.dart';
|
||||||
import 'package:moor_generator/moor_generator.dart';
|
import 'package:moor_generator/moor_generator.dart';
|
||||||
import 'package:moor_generator/src/services/find_stream_update_rules.dart';
|
import 'package:moor_generator/src/services/find_stream_update_rules.dart';
|
||||||
import 'package:moor_generator/src/utils/string_escaper.dart';
|
import 'package:moor_generator/src/utils/string_escaper.dart';
|
||||||
|
import 'package:moor_generator/src/utils/type_utils.dart';
|
||||||
import 'package:moor_generator/writer.dart';
|
import 'package:moor_generator/writer.dart';
|
||||||
import 'package:recase/recase.dart';
|
import 'package:recase/recase.dart';
|
||||||
|
|
||||||
|
@ -86,7 +87,7 @@ class DatabaseWriter {
|
||||||
|
|
||||||
// Write fields to access an dao. We use a lazy getter for that.
|
// Write fields to access an dao. We use a lazy getter for that.
|
||||||
for (final dao in db.daos) {
|
for (final dao in db.daos) {
|
||||||
final typeName = dao.getDisplayString();
|
final typeName = dao.codeString(scope.generationOptions);
|
||||||
final getterName = ReCase(typeName).camelCase;
|
final getterName = ReCase(typeName).camelCase;
|
||||||
final databaseImplName = db.fromClass.name;
|
final databaseImplName = db.fromClass.name;
|
||||||
|
|
||||||
|
|
|
@ -143,8 +143,8 @@ class QueryWriter {
|
||||||
final dartLiteral = asDartLiteral(column.name);
|
final dartLiteral = asDartLiteral(column.name);
|
||||||
var code = 'row.$readMethod($dartLiteral)';
|
var code = 'row.$readMethod($dartLiteral)';
|
||||||
|
|
||||||
if (column.converter != null) {
|
if (column.typeConverter != null) {
|
||||||
final converter = column.converter;
|
final converter = column.typeConverter;
|
||||||
code = '${_converter(converter)}.mapToDart($code)';
|
code = '${_converter(converter)}.mapToDart($code)';
|
||||||
}
|
}
|
||||||
return code;
|
return code;
|
||||||
|
@ -161,7 +161,8 @@ class QueryWriter {
|
||||||
/// Writes a method returning a `Selectable<T>`, where `T` is the return type
|
/// Writes a method returning a `Selectable<T>`, where `T` is the return type
|
||||||
/// of the custom query.
|
/// of the custom query.
|
||||||
void _writeSelectStatementCreator() {
|
void _writeSelectStatementCreator() {
|
||||||
final returnType = 'Selectable<${_select.resultClassName}>';
|
final returnType =
|
||||||
|
'Selectable<${_select.resultTypeCode(scope.generationOptions)}>';
|
||||||
final methodName = _nameOfCreationMethod();
|
final methodName = _nameOfCreationMethod();
|
||||||
|
|
||||||
_buffer.write('$returnType $methodName(');
|
_buffer.write('$returnType $methodName(');
|
||||||
|
@ -180,7 +181,9 @@ class QueryWriter {
|
||||||
}
|
}
|
||||||
|
|
||||||
void _writeOneTimeReader() {
|
void _writeOneTimeReader() {
|
||||||
_buffer.write('Future<List<${_select.resultClassName}>> ${query.name}(');
|
final returnType =
|
||||||
|
'Future<List<${_select.resultTypeCode(scope.generationOptions)}>>';
|
||||||
|
_buffer.write('$returnType ${query.name}(');
|
||||||
_writeParameters();
|
_writeParameters();
|
||||||
_buffer..write(') {\n')..write('return ${_nameOfCreationMethod()}(');
|
_buffer..write(') {\n')..write('return ${_nameOfCreationMethod()}(');
|
||||||
_writeUseParameters();
|
_writeUseParameters();
|
||||||
|
@ -199,7 +202,9 @@ class QueryWriter {
|
||||||
methodName = 'watch$upperQueryName';
|
methodName = 'watch$upperQueryName';
|
||||||
}
|
}
|
||||||
|
|
||||||
_buffer.write('Stream<List<${_select.resultClassName}>> $methodName(');
|
final returnType =
|
||||||
|
'Stream<List<${_select.resultTypeCode(scope.generationOptions)}>>';
|
||||||
|
_buffer.write('$returnType $methodName(');
|
||||||
_writeParameters();
|
_writeParameters();
|
||||||
_buffer..write(') {\n')..write('return ${_nameOfCreationMethod()}(');
|
_buffer..write(') {\n')..write('return ${_nameOfCreationMethod()}(');
|
||||||
_writeUseParameters();
|
_writeUseParameters();
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import 'package:moor_generator/src/model/sql_query.dart';
|
import 'package:moor_generator/src/model/sql_query.dart';
|
||||||
|
import 'package:moor_generator/src/model/types.dart';
|
||||||
import 'package:moor_generator/src/writer/utils/override_toString.dart';
|
import 'package:moor_generator/src/writer/utils/override_toString.dart';
|
||||||
import 'package:moor_generator/writer.dart';
|
import 'package:moor_generator/writer.dart';
|
||||||
|
|
||||||
|
@ -28,7 +29,7 @@ class ResultSetWriter {
|
||||||
// write fields
|
// write fields
|
||||||
for (final column in resultSet.columns) {
|
for (final column in resultSet.columns) {
|
||||||
final name = resultSet.dartNameFor(column);
|
final name = resultSet.dartNameFor(column);
|
||||||
final runtimeType = column.dartType;
|
final runtimeType = column.dartTypeCode(scope.generationOptions);
|
||||||
|
|
||||||
into.write('$modifier $runtimeType $name\n;');
|
into.write('$modifier $runtimeType $name\n;');
|
||||||
|
|
||||||
|
|
|
@ -21,8 +21,8 @@ class DataClassWriter {
|
||||||
// write individual fields
|
// write individual fields
|
||||||
for (final column in table.columns) {
|
for (final column in table.columns) {
|
||||||
final modifier = scope.options.fieldModifier;
|
final modifier = scope.options.fieldModifier;
|
||||||
_buffer.write(
|
_buffer.write('$modifier ${column.dartTypeCode(scope.generationOptions)} '
|
||||||
'$modifier ${column.dartTypeName} ${column.dartGetterName}; \n');
|
'${column.dartGetterName}; \n');
|
||||||
}
|
}
|
||||||
|
|
||||||
// write constructor with named optional fields
|
// write constructor with named optional fields
|
||||||
|
@ -123,7 +123,7 @@ class DataClassWriter {
|
||||||
for (final column in table.columns) {
|
for (final column in table.columns) {
|
||||||
final getter = column.dartGetterName;
|
final getter = column.dartGetterName;
|
||||||
final jsonKey = column.getJsonKey(scope.options);
|
final jsonKey = column.getJsonKey(scope.options);
|
||||||
final type = column.dartTypeName;
|
final type = column.dartTypeCode(scope.generationOptions);
|
||||||
|
|
||||||
_buffer.write("$getter: serializer.fromJson<$type>(json['$jsonKey']),");
|
_buffer.write("$getter: serializer.fromJson<$type>(json['$jsonKey']),");
|
||||||
}
|
}
|
||||||
|
@ -151,9 +151,9 @@ class DataClassWriter {
|
||||||
final getter = column.dartGetterName;
|
final getter = column.dartGetterName;
|
||||||
final needsThis = getter == 'serializer';
|
final needsThis = getter == 'serializer';
|
||||||
final value = needsThis ? 'this.$getter' : getter;
|
final value = needsThis ? 'this.$getter' : getter;
|
||||||
|
final dartType = column.dartTypeCode(scope.generationOptions);
|
||||||
|
|
||||||
_buffer
|
_buffer.write("'$name': serializer.toJson<$dartType>($value),");
|
||||||
.write("'$name': serializer.toJson<${column.dartTypeName}>($value),");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_buffer.write('};}');
|
_buffer.write('};}');
|
||||||
|
@ -168,12 +168,13 @@ class DataClassWriter {
|
||||||
final column = table.columns[i];
|
final column = table.columns[i];
|
||||||
final last = i == table.columns.length - 1;
|
final last = i == table.columns.length - 1;
|
||||||
|
|
||||||
|
final typeName = column.dartTypeCode(scope.generationOptions);
|
||||||
if (wrapNullableInValue && column.nullable) {
|
if (wrapNullableInValue && column.nullable) {
|
||||||
_buffer
|
_buffer
|
||||||
..write('Value<${column.dartTypeName}> ${column.dartGetterName} ')
|
..write('Value<$typeName> ${column.dartGetterName} ')
|
||||||
..write('= const Value.absent()');
|
..write('= const Value.absent()');
|
||||||
} else {
|
} else {
|
||||||
_buffer.write('${column.dartTypeName} ${column.dartGetterName}');
|
_buffer.write('$typeName ${column.dartGetterName}');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!last) {
|
if (!last) {
|
||||||
|
|
|
@ -94,7 +94,7 @@ class TableWriter {
|
||||||
|
|
||||||
void _writeConvertersAsStaticFields() {
|
void _writeConvertersAsStaticFields() {
|
||||||
for (final converter in table.converters) {
|
for (final converter in table.converters) {
|
||||||
final typeName = converter.displayNameOfConverter;
|
final typeName = converter.converterNameInCode(scope.generationOptions);
|
||||||
final code = converter.expression;
|
final code = converter.expression;
|
||||||
_buffer.write('static $typeName ${converter.fieldName} = $code;');
|
_buffer.write('static $typeName ${converter.fieldName} = $code;');
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,8 +32,8 @@ class UpdateCompanionWriter {
|
||||||
void _writeFields() {
|
void _writeFields() {
|
||||||
for (final column in table.columns) {
|
for (final column in table.columns) {
|
||||||
final modifier = scope.options.fieldModifier;
|
final modifier = scope.options.fieldModifier;
|
||||||
_buffer.write('$modifier Value<${column.dartTypeName}>'
|
final type = column.dartTypeCode(scope.generationOptions);
|
||||||
' ${column.dartGetterName};\n');
|
_buffer.write('$modifier Value<$type> ${column.dartGetterName};\n');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,8 +72,9 @@ class UpdateCompanionWriter {
|
||||||
|
|
||||||
if (table.isColumnRequiredForInsert(column)) {
|
if (table.isColumnRequiredForInsert(column)) {
|
||||||
requiredColumns.add(column);
|
requiredColumns.add(column);
|
||||||
|
final typeName = column.dartTypeCode(scope.generationOptions);
|
||||||
|
|
||||||
_buffer.write('@required ${column.dartTypeName} $param,');
|
_buffer.write('@required $typeName $param,');
|
||||||
} else {
|
} else {
|
||||||
_buffer.write('this.$param = const Value.absent(),');
|
_buffer.write('this.$param = const Value.absent(),');
|
||||||
}
|
}
|
||||||
|
@ -138,7 +139,9 @@ class UpdateCompanionWriter {
|
||||||
_buffer.write(', ');
|
_buffer.write(', ');
|
||||||
}
|
}
|
||||||
first = false;
|
first = false;
|
||||||
_buffer.write('Value<${column.dartTypeName}> ${column.dartGetterName}');
|
|
||||||
|
final typeName = column.dartTypeCode(scope.generationOptions);
|
||||||
|
_buffer.write('Value<$typeName> ${column.dartGetterName}');
|
||||||
}
|
}
|
||||||
|
|
||||||
_buffer
|
_buffer
|
||||||
|
|
|
@ -98,9 +98,16 @@ class Scope extends _Node {
|
||||||
|
|
||||||
/// Options that are specific to code-generation.
|
/// Options that are specific to code-generation.
|
||||||
class GenerationOptions {
|
class GenerationOptions {
|
||||||
|
/// Whether we're generating code to verify schema migrations.
|
||||||
|
///
|
||||||
|
/// When non-null, we're generating from a schema snapshot instead of from
|
||||||
|
/// source.
|
||||||
final int forSchema;
|
final int forSchema;
|
||||||
|
|
||||||
const GenerationOptions({this.forSchema});
|
/// Whether to generate Dart code that supports non-nullable types.
|
||||||
|
final bool nnbd;
|
||||||
|
|
||||||
|
const GenerationOptions({this.forSchema, this.nnbd = false});
|
||||||
|
|
||||||
/// Whether, instead of generating the full database code, we're only
|
/// Whether, instead of generating the full database code, we're only
|
||||||
/// generating a subset needed for schema verification.
|
/// generating a subset needed for schema verification.
|
||||||
|
|
|
@ -26,8 +26,9 @@ dependencies:
|
||||||
sqlparser: ^0.11.0-dev
|
sqlparser: ^0.11.0-dev
|
||||||
|
|
||||||
# Dart analysis
|
# Dart analysis
|
||||||
analyzer: '^0.39.11'
|
analyzer: '^0.40.0'
|
||||||
analyzer_plugin: '>=0.2.4 <0.4.0'
|
analyzer_plugin_fork:
|
||||||
|
path: /home/simon/programming/analyzer_plugin_fork
|
||||||
source_span: ^1.5.5
|
source_span: ^1.5.5
|
||||||
|
|
||||||
# Build system
|
# Build system
|
||||||
|
|
|
@ -48,7 +48,7 @@ class ProductsDao extends BaseProductsDao with _$ProductDaoMixin {
|
||||||
expect(file.errors.errors, isEmpty);
|
expect(file.errors.errors, isEmpty);
|
||||||
|
|
||||||
final dao = (file.currentResult as ParsedDartFile).declaredDaos.single;
|
final dao = (file.currentResult as ParsedDartFile).declaredDaos.single;
|
||||||
expect(dao.dbClass.getDisplayString(), 'MyDatabase');
|
expect(dao.dbClass.element.name, 'MyDatabase');
|
||||||
|
|
||||||
state.close();
|
state.close();
|
||||||
});
|
});
|
||||||
|
|
|
@ -39,7 +39,7 @@ void main() {
|
||||||
contains('EnumIndexConverter<Fruits>'),
|
contains('EnumIndexConverter<Fruits>'),
|
||||||
)
|
)
|
||||||
.having(
|
.having(
|
||||||
(e) => e.mappedType.getDisplayString(),
|
(e) => e.mappedType.getDisplayString(withNullability: false),
|
||||||
'mappedType',
|
'mappedType',
|
||||||
'Fruits',
|
'Fruits',
|
||||||
),
|
),
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
import 'package:build/build.dart';
|
import 'package:build/build.dart';
|
||||||
|
import 'package:moor_generator/src/model/types.dart';
|
||||||
import 'package:moor_generator/src/analyzer/runner/results.dart';
|
import 'package:moor_generator/src/analyzer/runner/results.dart';
|
||||||
import 'package:moor_generator/src/analyzer/runner/steps.dart';
|
import 'package:moor_generator/src/analyzer/runner/steps.dart';
|
||||||
import 'package:moor_generator/src/analyzer/session.dart';
|
import 'package:moor_generator/src/analyzer/session.dart';
|
||||||
|
@ -41,7 +42,7 @@ usersWithLongName: SELECT * FROM users WHERE LENGTH(name) > 25;
|
||||||
['id', 'name', 'field', 'another', 'different_json']);
|
['id', 'name', 'field', 'another', 'different_json']);
|
||||||
expect(table.columns.map((c) => c.dartGetterName),
|
expect(table.columns.map((c) => c.dartGetterName),
|
||||||
['id', 'name', 'field', 'another', 'differentJson']);
|
['id', 'name', 'field', 'another', 'differentJson']);
|
||||||
expect(table.columns.map((c) => c.dartTypeName),
|
expect(table.columns.map((c) => c.dartTypeCode()),
|
||||||
['int', 'String', 'bool', 'DateTime', 'int']);
|
['int', 'String', 'bool', 'DateTime', 'int']);
|
||||||
expect(table.columns.map((c) => c.getJsonKey()),
|
expect(table.columns.map((c) => c.getJsonKey()),
|
||||||
['id', 'name', 'field', 'another', 'myJsonKey']);
|
['id', 'name', 'field', 'another', 'myJsonKey']);
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_common.dart';
|
||||||
import 'package:build/build.dart';
|
import 'package:build/build.dart';
|
||||||
import 'package:meta/meta.dart';
|
import 'package:meta/meta.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import 'package:analyzer_plugin/protocol/protocol_common.dart';
|
import 'package:analyzer_plugin_fork/protocol/protocol_common.dart';
|
||||||
import 'package:build/build.dart';
|
import 'package:build/build.dart';
|
||||||
import 'package:moor_generator/src/services/ide/moor_ide.dart';
|
import 'package:moor_generator/src/services/ide/moor_ide.dart';
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
Loading…
Reference in New Issue