From cec05d8c19e5fb1f5724dcf62eb292110d56b1f4 Mon Sep 17 00:00:00 2001 From: "thomashii@dukefirehawk.com" Date: Sat, 27 Aug 2022 16:58:45 +0800 Subject: [PATCH] Fixed ORM analysis warnings --- packages/orm/angel_orm/lib/src/builder.dart | 42 +++++++++---------- .../lib/src/migration_generator.dart | 3 +- .../lib/src/orm_build_context.dart | 23 +++++----- .../lib/src/orm_generator.dart | 13 +++--- 4 files changed, 40 insertions(+), 41 deletions(-) diff --git a/packages/orm/angel_orm/lib/src/builder.dart b/packages/orm/angel_orm/lib/src/builder.dart index 4810987a..b8eea6a4 100644 --- a/packages/orm/angel_orm/lib/src/builder.dart +++ b/packages/orm/angel_orm/lib/src/builder.dart @@ -106,12 +106,12 @@ class NumericSqlExpressionBuilder } void isIn(Iterable values) { - _raw = 'IN (' + values.join(', ') + ')'; + _raw = 'IN (${values.join(', ')})'; _hasValue = true; } void isNotIn(Iterable values) { - _raw = 'NOT IN (' + values.join(', ') + ')'; + _raw = 'NOT IN (${values.join(', ')})'; _hasValue = true; } } @@ -173,12 +173,12 @@ class EnumSqlExpressionBuilder extends SqlExpressionBuilder { void isNotBetween(T lower, T upper) => throw _unsupported(); void isIn(Iterable values) { - _raw = 'IN (' + values.map(_getValue).join(', ') + ')'; + _raw = 'IN (${values.map(_getValue).join(', ')})'; _hasValue = true; } void isNotIn(Iterable values) { - _raw = 'NOT IN (' + values.map(_getValue).join(', ') + ')'; + _raw = 'NOT IN (${values.map(_getValue).join(', ')})'; _hasValue = true; } } @@ -237,7 +237,7 @@ class StringSqlExpressionBuilder extends SqlExpressionBuilder { /// ``` void like(String pattern, {String Function(String)? sanitize}) { sanitize ??= (s) => pattern; - _raw = 'LIKE \'' + sanitize('@$substitution') + '\''; + _raw = 'LIKE \'${sanitize('@$substitution')}\''; //query.substitutionValues[substitution] = pattern; _hasValue = true; _value = null; @@ -268,13 +268,11 @@ class StringSqlExpressionBuilder extends SqlExpressionBuilder { } String _in(Iterable values) { - return 'IN (' + - values.map((v) { - var name = query.reserveName('${columnName}_in_value'); - query.substitutionValues[name] = v; - return '@$name'; - }).join(', ') + - ')'; + return 'IN (${values.map((v) { + var name = query.reserveName('${columnName}_in_value'); + query.substitutionValues[name] = v; + return '@$name'; + }).join(', ')})'; } void isIn(Iterable values) { @@ -283,7 +281,7 @@ class StringSqlExpressionBuilder extends SqlExpressionBuilder { } void isNotIn(Iterable values) { - _raw = 'NOT ' + _in(values); + _raw = 'NOT ${_in(values)}'; _hasValue = true; } } @@ -374,9 +372,9 @@ class DateTimeSqlExpressionBuilder extends SqlExpressionBuilder { _minute?.hasValue == true || _second?.hasValue == true; - bool _change(String _op, DateTime dt, bool time) { + bool _change(String op, DateTime dt, bool time) { var dateString = time ? dateYmdHms.format(dt) : dateYmd.format(dt); - _raw = '$columnName $_op \'$dateString\''; + _raw = '$columnName $op \'$dateString\''; return true; } @@ -409,15 +407,13 @@ class DateTimeSqlExpressionBuilder extends SqlExpressionBuilder { } void isIn(Iterable values) { - _raw = '$columnName IN (' + - values.map(dateYmdHms.format).map((s) => '$s').join(', ') + - ')'; + _raw = + '$columnName IN (${values.map(dateYmdHms.format).map((s) => s).join(', ')})'; } void isNotIn(Iterable values) { - _raw = '$columnName NOT IN (' + - values.map(dateYmdHms.format).map((s) => '$s').join(', ') + - ')'; + _raw = + '$columnName NOT IN (${values.map(dateYmdHms.format).map((s) => s).join(', ')})'; } void isBetween(DateTime lower, DateTime upper) { @@ -624,7 +620,7 @@ class JsonSqlExpressionBuilderProperty { var r = builder._raw; if (r != null) { - builder._raw = r + '$nameString IS NOT NULL'; + builder._raw = '$r$nameString IS NOT NULL'; } else { builder._raw = '$nameString IS NOT NULL'; } @@ -637,7 +633,7 @@ class JsonSqlExpressionBuilderProperty { var r = builder._raw; if (r != null) { - builder._raw = r + '$nameString IS NULL'; + builder._raw = '$r$nameString IS NULL'; } else { builder._raw = '$nameString IS NULL'; } diff --git a/packages/orm/angel_orm_generator/lib/src/migration_generator.dart b/packages/orm/angel_orm_generator/lib/src/migration_generator.dart index 57b7a755..c7cb35df 100644 --- a/packages/orm/angel_orm_generator/lib/src/migration_generator.dart +++ b/packages/orm/angel_orm_generator/lib/src/migration_generator.dart @@ -222,7 +222,8 @@ class MigrationGenerator extends GeneratorForAnnotation { ConstantReader(defaultValue).read('value').stringValue; defaultExpr = refer('RawSql').constInstance([literalString(value)]); - } else if (type is InterfaceType && type.element.isEnum) { + } else if (type is InterfaceType && + type.element2 is EnumElement) { // Default to enum index. try { var index = diff --git a/packages/orm/angel_orm_generator/lib/src/orm_build_context.dart b/packages/orm/angel_orm_generator/lib/src/orm_build_context.dart index f5457e4f..3220b420 100644 --- a/packages/orm/angel_orm_generator/lib/src/orm_build_context.dart +++ b/packages/orm/angel_orm_generator/lib/src/orm_build_context.dart @@ -58,7 +58,7 @@ FieldElement? findPrimaryFieldInList( Future buildOrmContext( Map cache, - ClassElement clazz, + InterfaceElement clazz, ConstantReader annotation, BuildStep buildStep, Resolver resolver, @@ -73,7 +73,7 @@ Future buildOrmContext( .firstAnnotationOf(clazz)) != null) { if (clazz.supertype != null) { - clazz = clazz.supertype!.element; + clazz = clazz.supertype!.element2; } } @@ -171,7 +171,7 @@ Future buildOrmContext( } var modelType = firstModelAncestor(refType) ?? refType; - var modelTypeElement = modelType.element; + var modelTypeElement = modelType.element2; if (modelTypeElement != null) { foreign = await buildOrmContext( @@ -187,7 +187,7 @@ Future buildOrmContext( if (through != null && through is InterfaceType) { throughContext = await buildOrmContext( cache, - through.element, + through.element2, ConstantReader(const TypeChecker.fromRuntime(Serializable) .firstAnnotationOf(modelTypeElement)), buildStep, @@ -221,15 +221,15 @@ Future buildOrmContext( var rcc = ReCase(field.name); String keyName(OrmBuildContext ctx, String missing) { - var _keyName = + var localKeyName = findPrimaryFieldInList(ctx, ctx.buildContext.fields)?.name; // print( // 'Keyname for ${buildCtx.originalClassName}.${field.name} maybe = $_keyName??'); - if (_keyName == null) { + if (localKeyName == null) { throw '${ctx.buildContext.originalClassName} has no defined primary key, ' 'so the relation on field ${buildCtx.originalClassName}.${field.name} must define a $missing.'; } else { - return _keyName; + return localKeyName; } } @@ -252,8 +252,9 @@ Future buildOrmContext( // Unfortunately, the analyzer library provides little to nothing // in the way of reading enums from source, so here's a hack. var joinTypeType = (joinTypeRdr.type as InterfaceType); - var enumFields = - joinTypeType.element.fields.where((f) => f.isEnumConstant).toList(); + var enumFields = joinTypeType.element2.fields + .where((f) => f.isEnumConstant) + .toList(); for (var i = 0; i < enumFields.length; i++) { if (enumFields[i].computeConstantValue() == joinTypeRdr) { @@ -293,7 +294,7 @@ Future buildOrmContext( if (foreign != null) { if (isSpecialId(foreign, foreignField)) { // Use integer - type = field.type.element?.library?.typeProvider.intType + type = field.type.element2?.library?.typeProvider.intType as DartType; //type = field.type.element?.context.typeProvider.intType; @@ -362,7 +363,7 @@ ColumnType inferColumnType(DartType type) { if (const TypeChecker.fromRuntime(List).isAssignableFromType(type)) { return ColumnType.jsonb; } - if (type is InterfaceType && type.element.isEnum) { + if (type is InterfaceType && type.element2 is EnumElement) { return ColumnType.int; } diff --git a/packages/orm/angel_orm_generator/lib/src/orm_generator.dart b/packages/orm/angel_orm_generator/lib/src/orm_generator.dart index 7cbbbce9..39630b6f 100644 --- a/packages/orm/angel_orm_generator/lib/src/orm_generator.dart +++ b/packages/orm/angel_orm_generator/lib/src/orm_generator.dart @@ -266,14 +266,15 @@ class OrmGenerator extends GeneratorForAnnotation { // .property('tryParse') // .call([expr.property('toString').call([])]); expr = refer('mapToDouble').call([expr]); - } else if (fType is InterfaceType && fType.element.isEnum) { + } else if (fType is InterfaceType && + fType.element2 is EnumElement) { var isNull = expr.equalTo(literalNull); expr = isNull.conditional(literalNull, type.property('values').index(expr.asA(refer('int')))); } else if (fType.isDartCoreBool) { // Generated Code: mapToBool(row[i]) expr = refer('mapToBool').call([expr]); - } else if (fType.element?.displayName == 'DateTime') { + } else if (fType.element2?.displayName == 'DateTime') { // Generated Code: mapToDateTime(row[i]) if (fType.nullabilitySuffix == NullabilitySuffix.question) { expr = refer('mapToNullableDateTime').call([expr]); @@ -295,7 +296,7 @@ class OrmGenerator extends GeneratorForAnnotation { defaultRef = CodeExpression(Code('0.0')); } else if (fType.isDartCoreInt || fType.isDartCoreNum) { defaultRef = CodeExpression(Code('0')); - } else if (fType.element?.displayName == 'DateTime') { + } else if (fType.element2?.displayName == 'DateTime') { defaultRef = CodeExpression( Code('DateTime.parse("1970-01-01 00:00:00")')); } @@ -738,7 +739,7 @@ class OrmGenerator extends GeneratorForAnnotation { builderType = TypeReference((b) => b ..symbol = 'NumericSqlExpressionBuilder' ..types.add(refer(typeName))); - } else if (type is InterfaceType && type.element.isEnum) { + } else if (type is InterfaceType && type.element2 is EnumElement) { builderType = TypeReference((b) => b ..symbol = 'EnumSqlExpressionBuilder' ..types.add(convertTypeReference(type))); @@ -872,7 +873,7 @@ class OrmGenerator extends GeneratorForAnnotation { clazz.methods.add(Method((b) { var value = refer('values').index(literalString(name!)); - if (fType is InterfaceType && fType.element.isEnum) { + if (fType is InterfaceType && fType.element2 is EnumElement) { var asInt = value.asA(refer('int')); var t = convertTypeReference(fType); value = t.property('values').index(asInt); @@ -900,7 +901,7 @@ class OrmGenerator extends GeneratorForAnnotation { clazz.methods.add(Method((b) { Expression value = refer('value'); - if (fType is InterfaceType && fType.element.isEnum) { + if (fType is InterfaceType && fType.element2 is EnumElement) { value = CodeExpression(Code('value?.index')); } else if (const TypeChecker.fromRuntime(List) .isAssignableFromType(fType)) {