Skip to content

Commit

Permalink
修正EQL查询中对于参数的类型推断
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Aug 14, 2024
1 parent 0c4e223 commit ff842f4
Show file tree
Hide file tree
Showing 14 changed files with 82 additions and 7 deletions.
7 changes: 7 additions & 0 deletions nop-commons/src/main/java/io/nop/commons/type/StdSqlType.java
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,11 @@ public static StdSqlType fromStdName(String stdName) {
public static StdSqlType fromStdDataTYpe(StdDataType dataType) {
return stdTypeMap.get(dataType);
}

public static StdSqlType fromJavaClass(Class<?> clazz) {
StdDataType dataType = StdDataType.fromJavaClass(clazz);
if (dataType == null)
return null;
return StdSqlType.fromStdDataTYpe(dataType);
}
}
7 changes: 7 additions & 0 deletions nop-commons/src/main/java/io/nop/commons/util/MathHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;

import static io.nop.api.core.convert.ConvertHelper.toBigDecimal;
import static io.nop.api.core.convert.ConvertHelper.toBigInteger;
Expand Down Expand Up @@ -168,6 +169,12 @@ public static Number getPrimitiveDefaultValue(Class<?> forClass) {

private static IRandom s_randomImpl = DefaultThreadLocalRandom.INSTANCE;

private static AtomicLong s_seq = new AtomicLong();

public static long nextSeq() {
return s_seq.incrementAndGet();
}

/**
* 缺省返回ThreadLocalRandom
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
*/
package io.nop.orm.eql.ast;

import io.nop.commons.type.StdSqlType;
import io.nop.orm.eql.ast._gen._SqlBitValueLiteral;

public class SqlBitValueLiteral extends _SqlBitValueLiteral {

@Override
public StdSqlType getSqlType() {
return StdSqlType.TINYINT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
*/
package io.nop.orm.eql.ast;

import io.nop.commons.type.StdSqlType;
import io.nop.orm.eql.ast._gen._SqlBooleanLiteral;

public class SqlBooleanLiteral extends _SqlBooleanLiteral {

@Override
public StdSqlType getSqlType() {
return StdSqlType.BOOLEAN;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
*/
package io.nop.orm.eql.ast;

import io.nop.commons.type.StdSqlType;
import io.nop.orm.eql.ast._gen._SqlDateTimeLiteral;

public class SqlDateTimeLiteral extends _SqlDateTimeLiteral {

@Override
public StdSqlType getSqlType() {
return StdSqlType.DATETIME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
*/
package io.nop.orm.eql.ast;

import io.nop.commons.type.StdSqlType;
import io.nop.orm.eql.ast._gen._SqlHexadecimalLiteral;

public class SqlHexadecimalLiteral extends _SqlHexadecimalLiteral {

@Override
public StdSqlType getSqlType() {
return StdSqlType.BIGINT;
}
}
2 changes: 2 additions & 0 deletions nop-orm-eql/src/main/java/io/nop/orm/eql/ast/SqlLiteral.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
*/
package io.nop.orm.eql.ast;

import io.nop.commons.type.StdSqlType;
import io.nop.orm.eql.ast._gen._SqlLiteral;

public abstract class SqlLiteral extends _SqlLiteral {
public abstract StdSqlType getSqlType();
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@
*/
package io.nop.orm.eql.ast;

import io.nop.commons.type.StdSqlType;
import io.nop.orm.eql.ast._gen._SqlNullLiteral;

public class SqlNullLiteral extends _SqlNullLiteral {

@Override
public StdSqlType getSqlType() {
return StdSqlType.ANY;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package io.nop.orm.eql.ast;

import io.nop.commons.type.StdSqlType;
import io.nop.commons.util.StringHelper;
import io.nop.orm.eql.ast._gen._SqlNumberLiteral;

Expand All @@ -15,4 +16,11 @@ public class SqlNumberLiteral extends _SqlNumberLiteral {
public Number getNumberValue() {
return StringHelper.parseNumber(getValue());
}

@Override
public StdSqlType getSqlType() {
Number num = getNumberValue();
StdSqlType sqlType = StdSqlType.fromJavaClass(num.getClass());
return sqlType == null ? StdSqlType.DECIMAL : sqlType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
package io.nop.orm.eql.ast;

import io.nop.api.core.exceptions.NopException;
import io.nop.commons.type.StdSqlType;
import io.nop.orm.eql.ast._gen._SqlStringLiteral;

import static io.nop.core.CoreErrors.ARG_AST_NODE;
Expand All @@ -20,4 +21,9 @@ protected void checkMandatory(String propName, Object value) {
throw new NopException(ERR_LANG_AST_NODE_PROP_NOT_ALLOW_EMPTY).loc(getLocation())
.param(ARG_AST_NODE, getASTType()).param(ARG_PROP_NAME, propName);
}

@Override
public StdSqlType getSqlType() {
return StdSqlType.VARCHAR;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import io.nop.orm.eql.ast.SqlBinaryExpr;
import io.nop.orm.eql.ast.SqlDateTimeLiteral;
import io.nop.orm.eql.ast.SqlExpr;
import io.nop.orm.eql.ast.SqlLiteral;
import io.nop.orm.eql.ast.SqlRegularFunction;
import io.nop.orm.eql.meta.ISqlExprMeta;
import io.nop.orm.eql.meta.SingleColumnExprMeta;
Expand Down Expand Up @@ -80,6 +81,10 @@ private StdSqlType resolveType(SqlExpr expr) {
break;
}
}
if(type == StdSqlType.OTHER){
if(expr instanceof SqlLiteral)
return ((SqlLiteral) expr).getSqlType();
}
if (type == null)
type = StdSqlType.ANY;
return type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.nop.orm.eql.ast.EqlASTVisitor;
import io.nop.orm.eql.ast.SqlAssignment;
import io.nop.orm.eql.ast.SqlBinaryExpr;
import io.nop.orm.eql.ast.SqlExpr;
import io.nop.orm.eql.ast.SqlParameterMarker;
import io.nop.orm.eql.meta.ISqlExprMeta;
import io.nop.orm.eql.meta.SingleColumnExprMeta;
Expand Down Expand Up @@ -64,15 +65,26 @@ public void visitSqlParameterMarker(SqlParameterMarker node) {
case SqlBinaryExpr: {
SqlBinaryExpr binaryExpr = (SqlBinaryExpr) parent;
StdSqlType sqlType;
SqlExpr otherExpr;
if (binaryExpr.getLeft() == node) {
otherExpr = binaryExpr.getRight();
resolvedMeta = binaryExpr.getRight().getResolvedExprMeta();
sqlType = binaryExpr.getOperator().getLeftSqlType();
} else {
otherExpr = binaryExpr.getLeft();
resolvedMeta = binaryExpr.getLeft().getResolvedExprMeta();
sqlType = binaryExpr.getOperator().getRightSqlType();
}

// 比较算符要求相同的类型
if (resolvedMeta == null && binaryExpr.getOperator().isCompareOp()) {
resolvedMeta = new ExprTypeResolver(dialect).resolveExprMeta(otherExpr);
}

if (resolvedMeta == null) {
IDataParameterBinder binder = dialect.getDataParameterBinder(sqlType.getStdDataType(), sqlType);
if (binder == null)
binder = DataParameterBinders.ANY;
resolvedMeta = new SingleColumnExprMeta("?", binder, ExprOrmDataType.fromSqlType(sqlType));
}
break;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.nop.orm.support;

import io.nop.api.core.util.Guard;
import io.nop.commons.util.CollectionHelper;
import io.nop.commons.util.StringHelper;
import io.nop.orm.model.IEntityModel;
Expand All @@ -23,7 +22,7 @@ public OrmMappingTableMeta(IEntityModel mappingTable) {
this.refProp1 = CollectionHelper.first(rels);
this.refProp2 = CollectionHelper.last(rels);

Guard.checkArgument(rels.size() == 2, "mappingTable must contains two to-one relations");
// Guard.checkArgument(rels.size() == 2, "mappingTable must contains two to-one relations");
}

/**
Expand Down
9 changes: 9 additions & 0 deletions nop-orm/src/test/java/io/nop/orm/impl/TestOrmTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,15 @@ public void testSupports() {
}
}

@Test
public void testParamValueType() {
txn().runInTransaction(null, TransactionPropagation.SUPPORTS, txn -> {
SQL sql = SQL.begin().sql("select o,2 from io.nop.app.SimsClass o where 1=? and date(o.createdTime) > ?", 3,"2002-01-03").end();
orm().findAll(sql);
return null;
});
}

@Test
public void testEntityId() {
SimsClass entity = (SimsClass) orm().newEntity(SimsClass.class.getName());
Expand Down

0 comments on commit ff842f4

Please sign in to comment.