(fix)(headless) fix sql parsing error containning with as (#1494)

This commit is contained in:
jipeli
2024-07-31 11:34:08 +08:00
committed by GitHub
parent 4a5bb9e457
commit 27a70de1be

View File

@@ -776,24 +776,25 @@ public class SqlSelectHelper {
private static void getFieldsWithSubQuery(PlainSelect plainSelect, Map<String, Set<String>> fields) { private static void getFieldsWithSubQuery(PlainSelect plainSelect, Map<String, Set<String>> fields) {
if (plainSelect.getFromItem() instanceof Table) { if (plainSelect.getFromItem() instanceof Table) {
boolean isWith = false; List<String> withAlias = new ArrayList<>();
if (!CollectionUtils.isEmpty(plainSelect.getWithItemsList())) { if (!CollectionUtils.isEmpty(plainSelect.getWithItemsList())) {
for (WithItem withItem : plainSelect.getWithItemsList()) { for (WithItem withItem : plainSelect.getWithItemsList()) {
if (Objects.nonNull(withItem.getSelect())) { if (Objects.nonNull(withItem.getSelect())) {
getFieldsWithSubQuery(withItem.getSelect().getPlainSelect(), fields); getFieldsWithSubQuery(withItem.getSelect().getPlainSelect(), fields);
isWith = true; withAlias.add(withItem.getAlias().getName());
} }
} }
} }
if (!isWith) {
Table table = (Table) plainSelect.getFromItem(); Table table = (Table) plainSelect.getFromItem();
String tableName = table.getFullyQualifiedName();
if (!withAlias.contains(tableName)) {
if (!fields.containsKey(table.getFullyQualifiedName())) { if (!fields.containsKey(table.getFullyQualifiedName())) {
fields.put(table.getFullyQualifiedName(), new HashSet<>()); fields.put(tableName, new HashSet<>());
} }
List<String> sqlFields = getFieldsByPlainSelect(plainSelect).stream().map(f -> f.replaceAll("`", "")) List<String> sqlFields = getFieldsByPlainSelect(plainSelect).stream().map(f -> f.replaceAll("`", ""))
.collect( .collect(
Collectors.toList()); Collectors.toList());
fields.get(table.getFullyQualifiedName()).addAll(sqlFields); fields.get(tableName).addAll(sqlFields);
} }
} }
if (plainSelect.getFromItem() instanceof ParenthesedSelect) { if (plainSelect.getFromItem() instanceof ParenthesedSelect) {