Skip to content

Commit d6513d7

Browse files
committed
fix: 修复nativeSQL进行嵌套查询驼峰字段可能错误问题
1 parent 04963ff commit d6513d7

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

hsweb-commons/hsweb-commons-crud/src/main/java/org/hswebframework/web/crud/query/QueryAnalyzerImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@ private Map<String, Column> getColumnMappings() {
113113
Column col = new Column(column.getName(), column.getAlias(), select.table.alias, column.metadata);
114114
columnMappings.put(entry.getKey(), col);
115115
columnMappings.put(select.table.alias + "." + entry.getKey(), col);
116+
117+
if (!(column instanceof ExpressionColumn) && column.metadata != null) {
118+
columnMappings.put(column.metadata.getName(), col);
119+
columnMappings.put(select.table.alias + "." + column.metadata.getName(), col);
120+
}
116121
}
117122

118123
for (Column column : select.getColumnList()) {
@@ -191,7 +196,7 @@ private static SelectBody parse(String sql) {
191196
}
192197

193198
private String parsePlainName(String name) {
194-
if (name == null || name.length() == 0) {
199+
if (name == null || name.isEmpty()) {
195200
return null;
196201
}
197202
char firstChar = name.charAt(0);
@@ -524,10 +529,11 @@ public SqlFragments createTermFragments(QueryAnalyzerImpl impl, Term term) {
524529
if (col.metadata == null) {
525530
metadata = table.metadata;
526531
}
532+
String colName = col.metadata != null ? col.metadata.getName() : col.name;
527533
return metadata
528534
.findFeature(createFeatureId(term.getTermType()))
529535
.map(feature -> feature.createFragments(
530-
table.alias + "." + dialect.quote(col.name, col.metadata != null), col.metadata, term))
536+
table.alias + "." + dialect.quote(colName, col.metadata != null), col.metadata, term))
531537
.orElse(EmptySqlFragments.INSTANCE);
532538
}
533539
}

hsweb-commons/hsweb-commons-crud/src/test/java/org/hswebframework/web/crud/query/DefaultQueryHelperTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,17 @@ public void testInner() {
6565
.insert("s_test")
6666
.value("id", "inner-test")
6767
.value("name", "inner")
68+
.value("testName","inner")
6869
.value("age", 31)
6970
.execute()
7071
.sync();
7172

7273

73-
helper.select("select age,count(1) c from ( select name,age from s_test ) a group by age ", 0)
74+
helper.select("select age,count(1) c from ( select *,'1' as x from s_test ) a group by age ", 0)
7475
.where(dsl -> dsl
75-
.is("a.name", "inner")
76+
.is("x", "1")
77+
.is("name", "inner")
78+
.is("a.testName", "inner")
7679
.is("age", 31))
7780
.fetchPaged(0, 10)
7881
.doOnNext(v -> System.out.println(JSON.toJSONString(v, SerializerFeature.PrettyFormat)))

0 commit comments

Comments
 (0)