From d6513d7f4012fa6f6d8ee358b689a717105864c5 Mon Sep 17 00:00:00 2001 From: zhouhao Date: Thu, 3 Aug 2023 15:33:58 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DnativeSQL=E8=BF=9B?= =?UTF-8?q?=E8=A1=8C=E5=B5=8C=E5=A5=97=E6=9F=A5=E8=AF=A2=E9=A9=BC=E5=B3=B0?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E5=8F=AF=E8=83=BD=E9=94=99=E8=AF=AF=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/crud/query/QueryAnalyzerImpl.java | 10 ++++++++-- .../web/crud/query/DefaultQueryHelperTest.java | 7 +++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/query/QueryAnalyzerImpl.java b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/query/QueryAnalyzerImpl.java index 285e722c4..486773619 100644 --- a/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/query/QueryAnalyzerImpl.java +++ b/hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/query/QueryAnalyzerImpl.java @@ -113,6 +113,11 @@ private Map getColumnMappings() { Column col = new Column(column.getName(), column.getAlias(), select.table.alias, column.metadata); columnMappings.put(entry.getKey(), col); columnMappings.put(select.table.alias + "." + entry.getKey(), col); + + if (!(column instanceof ExpressionColumn) && column.metadata != null) { + columnMappings.put(column.metadata.getName(), col); + columnMappings.put(select.table.alias + "." + column.metadata.getName(), col); + } } for (Column column : select.getColumnList()) { @@ -191,7 +196,7 @@ private static SelectBody parse(String sql) { } private String parsePlainName(String name) { - if (name == null || name.length() == 0) { + if (name == null || name.isEmpty()) { return null; } char firstChar = name.charAt(0); @@ -524,10 +529,11 @@ public SqlFragments createTermFragments(QueryAnalyzerImpl impl, Term term) { if (col.metadata == null) { metadata = table.metadata; } + String colName = col.metadata != null ? col.metadata.getName() : col.name; return metadata .findFeature(createFeatureId(term.getTermType())) .map(feature -> feature.createFragments( - table.alias + "." + dialect.quote(col.name, col.metadata != null), col.metadata, term)) + table.alias + "." + dialect.quote(colName, col.metadata != null), col.metadata, term)) .orElse(EmptySqlFragments.INSTANCE); } } diff --git a/hsweb-commons/hsweb-commons-crud/src/test/java/org/hswebframework/web/crud/query/DefaultQueryHelperTest.java b/hsweb-commons/hsweb-commons-crud/src/test/java/org/hswebframework/web/crud/query/DefaultQueryHelperTest.java index 3413a4844..e956eb44f 100644 --- a/hsweb-commons/hsweb-commons-crud/src/test/java/org/hswebframework/web/crud/query/DefaultQueryHelperTest.java +++ b/hsweb-commons/hsweb-commons-crud/src/test/java/org/hswebframework/web/crud/query/DefaultQueryHelperTest.java @@ -65,14 +65,17 @@ public void testInner() { .insert("s_test") .value("id", "inner-test") .value("name", "inner") + .value("testName","inner") .value("age", 31) .execute() .sync(); - helper.select("select age,count(1) c from ( select name,age from s_test ) a group by age ", 0) + helper.select("select age,count(1) c from ( select *,'1' as x from s_test ) a group by age ", 0) .where(dsl -> dsl - .is("a.name", "inner") + .is("x", "1") + .is("name", "inner") + .is("a.testName", "inner") .is("age", 31)) .fetchPaged(0, 10) .doOnNext(v -> System.out.println(JSON.toJSONString(v, SerializerFeature.PrettyFormat)))