Skip to content

Commit

Permalink
[fix]修正eql中left join on语法支持
Browse files Browse the repository at this point in the history
  • Loading branch information
entropy-cloud committed Jan 3, 2024
1 parent 8b2a937 commit 446bc99
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
6 changes: 6 additions & 0 deletions nop-dao/src/main/java/io/nop/dao/api/IEntityDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
import io.nop.dao.exceptions.DaoException;
import io.nop.dao.exceptions.UnknownEntityException;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -269,6 +271,10 @@ default void forEachEntity(QueryBean query, Consumer<T> consumer) {

void batchLoadProps(Collection<T> entities, Collection<String> propNames);

default void batchLoadPropsForEntity(T entity, String... propNames) {
batchLoadProps(Collections.singleton(entity), Arrays.asList(propNames));
}

/**
* 将一级缓存中的修改刷新到数据库中
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,16 @@ void visitTableSource(SqlTableScope tableScope, SqlTableSource table) {
tableScope.addTable(source.getAliasName(), source);
} else if (table instanceof SqlJoinTableSource) {
SqlJoinTableSource source = (SqlJoinTableSource) table;
boolean hasCondition = source.getCondition() != null;
visitJoinLeft(tableScope, source);
visitJoinRight(tableScope, source);

if (hasCondition) {
SqlTableScope oldScope = currentScope;
currentScope = tableScope;
visitJoinCondition(source);
currentScope = oldScope;
}
}
}

Expand Down Expand Up @@ -481,6 +489,8 @@ void visitJoinRight(SqlTableScope tableScope, SqlJoinTableSource source) {
SqlSingleTableSource table = (SqlSingleTableSource) right;
table.setAlias(makeTableAlias(table.getAlias()));

tableScope.addTable(table.getAliasName(), table);

SqlTableName tableName = table.getTableName();
String fullName = tableName.getFullName();
ISqlTableMeta entityMeta = context.resolveEntityTableMeta(fullName);
Expand Down Expand Up @@ -737,7 +747,7 @@ SqlExpr makePropExpr(SqlTableSource source, IEntityPropModel propModel, Object v
col.setResolvedExprMeta(source.getResolvedTableMeta().getFieldExprMeta(propModel.getName()));
SqlQualifiedName name = new SqlQualifiedName();
name.setName(source.getAliasName());
col.setOwner(new SqlQualifiedName());
col.setOwner(name);
return col;
} else {
if (value instanceof Number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,13 +563,16 @@ public void visitSqlSingleTableSource(SqlSingleTableSource node) {
public void visitSqlJoinTableSource(SqlJoinTableSource node) {
visit(node.getLeft());
print(" ");
incIndent();
println();
print(node.getJoinType().getText());
print(" ");
visit(node.getRight());
if (node.getCondition() != null) {
print(" on ");
visit(node.getCondition());
}
decIndent();
}

@Override
Expand Down Expand Up @@ -674,9 +677,10 @@ public void visitSqlBinaryExpr(SqlBinaryExpr node) {
public void visitSqlIsNullExpr(SqlIsNullExpr node) {
printLeft(node.getExpr(), SqlOperator.IS);
if (node.getNot()) {
print(" not");
print(" is not null ");
}else {
print(" is null ");
}
print(" is null ");
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions nop-orm/src/test/java/io/nop/orm/loader/TestEqlQuery.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,4 +242,17 @@ public void testComplexQuery() {
orm().findFirst(new SQL(sql));
});
}

@Test
public void testLeftJoinOn() {
String sql = "select o from io.nop.app.SimsClass o left join io.nop.app.SimsCollege c1 on o.collegeId = c1.collegeId";
List<SimsClass> list = orm().findAll(new SQL(sql));
assertEquals(1, list.size());


String sql2 = "select o from SimsClass o left join SimsCollege c1 on o.collegeId = c1.collegeId " +
" left join SimsCollege c2 on c1.collegeId = c2.collegeId where c1.collegeId is not null or c2.collegeId is not null";
List<SimsClass> list2 = orm().findAll(new SQL(sql2));
assertEquals(1, list2.size());
}
}
4 changes: 2 additions & 2 deletions nop-orm/src/test/resources/_vfs/nop/test/orm/app.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<entities>
<entity name="io.nop.app.SimsClassFee" x:override="remove"/>

<entity name="io.nop.app.SimsClass">
<entity name="io.nop.app.SimsClass" registerShortName="true">
<columns>
<column name="collegeId" propId="100" lazy="true"/>
<column name="jsonExt" code="JSON_EXT" propId="101" tagSet="json" stdSqlType="JSON"
Expand Down Expand Up @@ -49,7 +49,7 @@
</relations>
</entity>

<entity name="io.nop.app.SimsCollege">
<entity name="io.nop.app.SimsCollege" registerShortName="true">
<relations>
<to-many name="ext" refEntityName="io.nop.app.SimsExtField" keyProp="fieldName" notGenCode="true">
<join>
Expand Down

0 comments on commit 446bc99

Please sign in to comment.