Fixed ORM analysis warnings

This commit is contained in:
thomashii@dukefirehawk.com 2022-08-27 16:58:45 +08:00
parent 729a7740db
commit cec05d8c19
4 changed files with 40 additions and 41 deletions

View file

@ -106,12 +106,12 @@ class NumericSqlExpressionBuilder<T extends num>
}
void isIn(Iterable<T> values) {
_raw = 'IN (' + values.join(', ') + ')';
_raw = 'IN (${values.join(', ')})';
_hasValue = true;
}
void isNotIn(Iterable<T> values) {
_raw = 'NOT IN (' + values.join(', ') + ')';
_raw = 'NOT IN (${values.join(', ')})';
_hasValue = true;
}
}
@ -173,12 +173,12 @@ class EnumSqlExpressionBuilder<T> extends SqlExpressionBuilder<T> {
void isNotBetween(T lower, T upper) => throw _unsupported();
void isIn(Iterable<T> values) {
_raw = 'IN (' + values.map(_getValue).join(', ') + ')';
_raw = 'IN (${values.map(_getValue).join(', ')})';
_hasValue = true;
}
void isNotIn(Iterable<T> 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<String> {
/// ```
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> {
}
String _in(Iterable<String> 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<String> values) {
@ -283,7 +281,7 @@ class StringSqlExpressionBuilder extends SqlExpressionBuilder<String> {
}
void isNotIn(Iterable<String> values) {
_raw = 'NOT ' + _in(values);
_raw = 'NOT ${_in(values)}';
_hasValue = true;
}
}
@ -374,9 +372,9 @@ class DateTimeSqlExpressionBuilder extends SqlExpressionBuilder<DateTime> {
_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<DateTime> {
}
void isIn(Iterable<DateTime> 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<DateTime> 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';
}

View file

@ -222,7 +222,8 @@ class MigrationGenerator extends GeneratorForAnnotation<Orm> {
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 =

View file

@ -58,7 +58,7 @@ FieldElement? findPrimaryFieldInList(
Future<OrmBuildContext?> buildOrmContext(
Map<String, OrmBuildContext> cache,
ClassElement clazz,
InterfaceElement clazz,
ConstantReader annotation,
BuildStep buildStep,
Resolver resolver,
@ -73,7 +73,7 @@ Future<OrmBuildContext?> buildOrmContext(
.firstAnnotationOf(clazz)) !=
null) {
if (clazz.supertype != null) {
clazz = clazz.supertype!.element;
clazz = clazz.supertype!.element2;
}
}
@ -171,7 +171,7 @@ Future<OrmBuildContext?> 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<OrmBuildContext?> 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<OrmBuildContext?> 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<OrmBuildContext?> 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<OrmBuildContext?> 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;
}

View file

@ -266,14 +266,15 @@ class OrmGenerator extends GeneratorForAnnotation<Orm> {
// .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<Orm> {
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<Orm> {
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<Orm> {
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<Orm> {
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)) {