forked from github/dataease
fix: 支持解析多重小括号的sql
This commit is contained in:
parent
a298652d65
commit
0180110e93
@ -101,34 +101,47 @@ public class SqlparserUtils {
|
|||||||
private static void handleFromItems(PlainSelect plainSelect, String dsType) throws Exception {
|
private static void handleFromItems(PlainSelect plainSelect, String dsType) throws Exception {
|
||||||
FromItem fromItem = plainSelect.getFromItem();
|
FromItem fromItem = plainSelect.getFromItem();
|
||||||
if (fromItem instanceof ParenthesedSelect) {
|
if (fromItem instanceof ParenthesedSelect) {
|
||||||
if (((ParenthesedSelect) fromItem).getSelect() instanceof SetOperationList) {
|
handleParenthesedSelect(fromItem, dsType);
|
||||||
StringBuilder result = new StringBuilder();
|
plainSelect.setFromItem(fromItem);
|
||||||
SetOperationList setOperationList = (SetOperationList) ((ParenthesedSelect) fromItem).getSelect().getSelectBody();
|
} else {
|
||||||
for (int i = 0; i < setOperationList.getSelects().size(); i++) {
|
if (fromItem instanceof ParenthesedFromItem) {
|
||||||
result.append(handlePlainSelect((PlainSelect) setOperationList.getSelects().get(i), null, dsType));
|
fromItem = ((ParenthesedFromItem) fromItem).getFromItem();
|
||||||
if (i < setOperationList.getSelects().size() - 1) {
|
while (fromItem instanceof ParenthesedFromItem) {
|
||||||
result.append(" ").append(setOperationList.getOperations().get(i).toString()).append(" ");
|
fromItem = ((ParenthesedFromItem) fromItem).getFromItem();
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
PlainSelect selectBody = ((ParenthesedSelect) fromItem).getSelect().getPlainSelect();
|
|
||||||
Select subSelectTmp = (Select) CCJSqlParserUtil.parse(removeVariables(selectBody.toString(), dsType));
|
|
||||||
((ParenthesedSelect) fromItem).setSelect(subSelectTmp.getSelectBody());
|
|
||||||
if (dsType.equals(DatasourceConfiguration.DatasourceType.oracle.getType())) {
|
|
||||||
if (fromItem.getAlias() != null) {
|
|
||||||
fromItem.setAlias(new Alias(fromItem.getAlias().toString(), false));
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (fromItem.getAlias() == null) {
|
|
||||||
throw new Exception("Failed to parse sql, Every derived table must have its own alias!");
|
|
||||||
}
|
|
||||||
fromItem.setAlias(new Alias(fromItem.getAlias().toString(), false));
|
|
||||||
}
|
}
|
||||||
|
handleParenthesedSelect(fromItem, dsType);
|
||||||
}
|
}
|
||||||
plainSelect.setFromItem(fromItem);
|
plainSelect.setFromItem(fromItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void handleParenthesedSelect(FromItem fromItem, String dsType) throws Exception {
|
||||||
|
if (((ParenthesedSelect) fromItem).getSelect() instanceof SetOperationList) {
|
||||||
|
StringBuilder result = new StringBuilder();
|
||||||
|
SetOperationList setOperationList = (SetOperationList) ((ParenthesedSelect) fromItem).getSelect().getSelectBody();
|
||||||
|
for (int i = 0; i < setOperationList.getSelects().size(); i++) {
|
||||||
|
result.append(handlePlainSelect((PlainSelect) setOperationList.getSelects().get(i), null, dsType));
|
||||||
|
if (i < setOperationList.getSelects().size() - 1) {
|
||||||
|
result.append(" ").append(setOperationList.getOperations().get(i).toString()).append(" ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
PlainSelect selectBody = ((ParenthesedSelect) fromItem).getSelect().getPlainSelect();
|
||||||
|
Select subSelectTmp = (Select) CCJSqlParserUtil.parse(removeVariables(selectBody.toString(), dsType));
|
||||||
|
((ParenthesedSelect) fromItem).setSelect(subSelectTmp.getSelectBody());
|
||||||
|
if (dsType.equals(DatasourceConfiguration.DatasourceType.oracle.getType())) {
|
||||||
|
if (fromItem.getAlias() != null) {
|
||||||
|
fromItem.setAlias(new Alias(fromItem.getAlias().toString(), false));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (fromItem.getAlias() == null) {
|
||||||
|
throw new Exception("Failed to parse sql, Every derived table must have its own alias!");
|
||||||
|
}
|
||||||
|
fromItem.setAlias(new Alias(fromItem.getAlias().toString(), false));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static void handleJoins(PlainSelect plainSelect, String dsType) throws Exception {
|
private static void handleJoins(PlainSelect plainSelect, String dsType) throws Exception {
|
||||||
List<Join> joins = plainSelect.getJoins();
|
List<Join> joins = plainSelect.getJoins();
|
||||||
if (joins != null) {
|
if (joins != null) {
|
||||||
|
@ -30,6 +30,7 @@ public class FieldUtils {
|
|||||||
case "DATETIMEOFFSET":
|
case "DATETIMEOFFSET":
|
||||||
case "SMALLDATETIME":
|
case "SMALLDATETIME":
|
||||||
case "DATETIME64":
|
case "DATETIME64":
|
||||||
|
case "_TIMESTAMPTZ":
|
||||||
return 1;// 时间
|
return 1;// 时间
|
||||||
case "INT":
|
case "INT":
|
||||||
case "SMALLINT":
|
case "SMALLINT":
|
||||||
|
Loading…
Reference in New Issue
Block a user