Browse Source

dbeaver/pro#3140 Update JSQLParser to 5.1 (#36892)

* dbeaver/pro#3140 Update JSQLParser to 5.1

* dbeaver/pro#3140 Fix JSQLParser version in pom

* dbeaver/pro#3140 Remove unused import

* dbeaver/pro#3140 Move check for single expression in parenthesis to utils for reuse

* Revert "dbeaver/pro#3140 Move check for single expression in parenthesis to utils for reuse"

* dbeaver/pro#3140 Remove redundant getSelectBody call

---------

Co-authored-by: Daria Markaryan <[email protected]>
Elizabeth 1 year ago
parent
commit
4c3fef08b0

+ 3 - 4
plugins/org.jkiss.dbeaver.ext.cubrid/src/org/jkiss/dbeaver/ext/cubrid/model/QueryTransformerLimitCubrid.java

@@ -37,14 +37,13 @@ public class QueryTransformerLimitCubrid extends QueryTransformerLimit
     }
 
     public boolean isLimitApplicable(Statement statement) {
-        if (statement instanceof Select select
-                && select.getSelectBody() instanceof PlainSelect selectBody) {
-            String where = String.valueOf(selectBody.getWhere()).toUpperCase();
+        if (statement instanceof PlainSelect select) {
+            String where = String.valueOf(select.getWhere()).toUpperCase();
             if (where.contains("ROWNUM") || where.contains("INST_NUM")) {
                 return false;
             }
 
-            String having = String.valueOf(selectBody.getHaving()).toUpperCase();
+            String having = String.valueOf(select.getHaving()).toUpperCase();
             if (having.contains("GROUPBY_NUM")) {
                 return false;
             }

+ 11 - 12
plugins/org.jkiss.dbeaver.ext.mssql/src/org/jkiss/dbeaver/ext/mssql/model/SQLServerDataSource.java

@@ -20,8 +20,7 @@ import net.sf.jsqlparser.expression.NextValExpression;
 import net.sf.jsqlparser.statement.Statement;
 import net.sf.jsqlparser.statement.select.PlainSelect;
 import net.sf.jsqlparser.statement.select.Select;
-import net.sf.jsqlparser.statement.select.SelectBody;
-import net.sf.jsqlparser.statement.select.SelectExpressionItem;
+import net.sf.jsqlparser.statement.select.SelectItem;
 import org.jkiss.code.NotNull;
 import org.jkiss.code.Nullable;
 import org.jkiss.dbeaver.DBException;
@@ -556,15 +555,11 @@ public class SQLServerDataSource extends JDBCDataSource implements DBSInstanceCo
         boolean hasNextValExpr = false;
         try {
             Statement statement = SQLSemanticProcessor.parseQuery(this.sqlDialect, query.getText());
-            if (statement instanceof Select) {
-                SelectBody selectBody = ((Select) statement).getSelectBody();
-                if (selectBody instanceof PlainSelect plainSelect) {
-                    if (plainSelect.getFromItem() == null) {
-                        hasNextValExpr = plainSelect.getSelectItems().stream().anyMatch(
-                            item -> (item instanceof SelectExpressionItem) 
-                                && (((SelectExpressionItem) item).getExpression() instanceof NextValExpression)
-                        );
-                    }
+            if (statement instanceof PlainSelect plainSelect) {
+                if (plainSelect.getFromItem() == null) {
+                    hasNextValExpr = plainSelect.getSelectItems()
+                        .stream()
+                        .anyMatch(item -> item.getExpression() instanceof NextValExpression);
                 }
             }
         } catch (DBCException e) {
@@ -659,7 +654,11 @@ public class SQLServerDataSource extends JDBCDataSource implements DBSInstanceCo
 
         @Nullable
         @Override
-        protected SQLServerLogin fetchObject(@NotNull JDBCSession session, @NotNull SQLServerDataSource dataSource, @NotNull JDBCResultSet resultSet) {
+        protected SQLServerLogin fetchObject(
+            @NotNull JDBCSession session,
+            @NotNull SQLServerDataSource dataSource,
+            @NotNull JDBCResultSet resultSet
+        ) {
             String loginName = JDBCUtils.safeGetString(resultSet, "name");
             if (CommonUtils.isNotEmpty(loginName)) {
                 return new SQLServerLogin(dataSource, loginName, resultSet);

+ 3 - 8
plugins/org.jkiss.dbeaver.ext.oracle/src/org/jkiss/dbeaver/ext/oracle/data/QueryTransformerRowNum.java

@@ -48,14 +48,9 @@ public class QueryTransformerRowNum implements DBCQueryTransformer {
         if (query.isPlainSelect()) {
             try {
                 Statement statement = query.getStatement();
-                if (statement instanceof Select) {
-                    Select select = (Select) statement;
-                    if (select.getSelectBody() instanceof PlainSelect) {
-                        SQLSemanticProcessor.addWhereToSelect(
-                            (PlainSelect) select.getSelectBody(),
-                            "ROWNUM <= " + totalRows);
-                        return statement.toString();
-                    }
+                if (statement instanceof PlainSelect select) {
+                    SQLSemanticProcessor.addWhereToSelect(select, "ROWNUM <= " + totalRows);
+                    return statement.toString();
                 }
             } catch (Throwable e) {
                 // ignore

+ 3 - 7
plugins/org.jkiss.dbeaver.ext.vertica/src/org/jkiss/dbeaver/ext/vertica/model/QueryTransformerLimitVertica.java

@@ -40,13 +40,9 @@ public class QueryTransformerLimitVertica extends QueryTransformerLimit implemen
     }
 
     public boolean isLimitApplicable(Statement statement) {
-        if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
-            PlainSelect selectBody = (PlainSelect) ((Select) statement).getSelectBody();
-            return selectBody.getFromItem() != null &&
-                CommonUtils.isEmpty(selectBody.getIntoTables()) &&
-                selectBody.getLimit() == null &&
-                selectBody.getTop() == null &&
-                !selectBody.isForUpdate();
+        if (statement instanceof PlainSelect plainSelect) {
+            return plainSelect.getFromItem() != null && CommonUtils.isEmpty(plainSelect.getIntoTables())
+                && plainSelect.getLimit() == null && plainSelect.getTop() == null && plainSelect.getForUpdateTable() == null;
         }
         return false;
     }

+ 4 - 5
plugins/org.jkiss.dbeaver.model.sql/src/org/jkiss/dbeaver/model/sql/SQLGroupingQueryGenerator.java

@@ -22,7 +22,6 @@ import net.sf.jsqlparser.schema.Table;
 import net.sf.jsqlparser.statement.Statement;
 import net.sf.jsqlparser.statement.select.PlainSelect;
 import net.sf.jsqlparser.statement.select.Select;
-import net.sf.jsqlparser.statement.select.SelectExpressionItem;
 import net.sf.jsqlparser.statement.select.SelectItem;
 import org.jkiss.code.NotNull;
 import org.jkiss.dbeaver.DBException;
@@ -133,7 +132,7 @@ public class SQLGroupingQueryGenerator {
             sql.append("\n) ").append(subqueryAlias);
         } else {
             subqueryAlias = null;
-            if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect select) {
+            if (statement instanceof PlainSelect select) {
                 select.setOrderByElements(null);
                 SQLDialect sqlDialect = dataSource.getSQLDialect();
                 if (select.getFromItem() instanceof Table table) {
@@ -144,15 +143,15 @@ public class SQLGroupingQueryGenerator {
                     select.setFromItem(formattedTable);
                 }
 
-                List<SelectItem> selectItems = new ArrayList<>();
+                List<SelectItem<?>> selectItems = new ArrayList<>();
                 select.setSelectItems(selectItems);
                 for (SQLGroupingAttribute groupAttribute : groupAttributes) {
-                    selectItems.add(new SelectExpressionItem(groupAttribute.prepareExpression()));
+                    selectItems.add(new SelectItem<>(groupAttribute.prepareExpression()));
                 }
                 for (int i = 0; i < groupFunctions.size(); i++) {
                     String func = groupFunctions.get(i);
                     Expression expression = SQLSemanticProcessor.parseExpression(func);
-                    SelectExpressionItem sei = new SelectExpressionItem(expression);
+                    SelectItem<?> sei = new SelectItem<>(expression);
                     if (useAliasForColumns) {
                         sei.setAlias(new Alias(funcAliases[i]));
                     }

+ 12 - 18
plugins/org.jkiss.dbeaver.model.sql/src/org/jkiss/dbeaver/model/sql/transformers/SQLQueryTransformerCount.java

@@ -61,13 +61,10 @@ public class SQLQueryTransformerCount implements SQLQueryTransformer {
         try {
             // Remove orderings (#4652)
             Statement statement = SQLSemanticProcessor.parseQuery(query.getText());
-            if (statement instanceof Select) {
-                SelectBody selectBody = ((Select) statement).getSelectBody();
-                if (selectBody instanceof PlainSelect) {
-                    if (!CommonUtils.isEmpty(((PlainSelect) selectBody).getOrderByElements())) {
-                        ((PlainSelect) selectBody).setOrderByElements(null);
-                        queryText = statement.toString();
-                    }
+            if (statement instanceof PlainSelect plainSelect) {
+                if (!CommonUtils.isEmpty(plainSelect.getOrderByElements())) {
+                    plainSelect.setOrderByElements(null);
+                    queryText = statement.toString();
                 }
             }
         } catch (Throwable e) {
@@ -85,12 +82,11 @@ public class SQLQueryTransformerCount implements SQLQueryTransformer {
     private SQLQuery tryInjectCount(DBPDataSource dataSource, SQLQuery query) throws DBException {
         try {
             Statement statement = SQLSemanticProcessor.parseQuery(query.getText());
-            if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
-                PlainSelect select = (PlainSelect) ((Select) statement).getSelectBody();
+            if (statement instanceof PlainSelect select) {
                 if (select.getHaving() != null) {
                     throw new DBException("Can't inject COUNT into query with HAVING clause");
                 }
-                if (select.getGroupBy() != null && !CommonUtils.isEmpty(select.getGroupBy().getGroupByExpressions())) {
+                if (select.getGroupBy() != null && !CommonUtils.isEmpty(select.getGroupBy().getGroupByExpressionList())) {
                     throw new DBException("Can't inject COUNT into query with GROUP BY clause");
                 }
 
@@ -105,21 +101,19 @@ public class SQLQueryTransformerCount implements SQLQueryTransformer {
                 if (selectDistinct != null) {
                     countFunc.setDistinct(true);
                     List<Expression> exprs = new ArrayList<>();
-                    for (SelectItem item : select.getSelectItems()) {
-                        if (item instanceof SelectExpressionItem) {
-                            exprs.add(((SelectExpressionItem)item).getExpression());
-                        }
+                    for (SelectItem<?> item : select.getSelectItems()) {
+                        exprs.add(item.getExpression());
                     }
                     if (!exprs.isEmpty()) {
-                        countFunc.setParameters(new ExpressionList(exprs));
+                        countFunc.setParameters(new ExpressionList<>(exprs));
                     }
                 } else {
                     //countFunc.setAllColumns(true); // We can't use setAllColumns now (since JSQLParser 4.2), it will return COUNT(ALL). Replaced by AllColumns Expression
-                    countFunc.setParameters(new ExpressionList(new AllColumns()));
+                    countFunc.setParameters(new ExpressionList<>(new AllColumns()));
                 }
 
-                List<SelectItem> selectItems = new ArrayList<>();
-                selectItems.add(new SelectExpressionItem(countFunc));
+                List<SelectItem<?>> selectItems = new ArrayList<>();
+                selectItems.add(new SelectItem(countFunc));
                 select.setSelectItems(selectItems);
                 select.setOrderByElements(null);
                 return new SQLQuery(dataSource, select.toString(), query, false);

+ 11 - 20
plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/impl/sql/QueryTransformerTop.java

@@ -20,7 +20,6 @@ import net.sf.jsqlparser.expression.LongValue;
 import net.sf.jsqlparser.statement.Statement;
 import net.sf.jsqlparser.statement.select.PlainSelect;
 import net.sf.jsqlparser.statement.select.Select;
-import net.sf.jsqlparser.statement.select.SelectBody;
 import net.sf.jsqlparser.statement.select.Top;
 import org.jkiss.dbeaver.Log;
 import org.jkiss.dbeaver.model.exec.DBCException;
@@ -53,19 +52,15 @@ public class QueryTransformerTop implements DBCQueryTransformer, DBCQueryTransfo
         if (query.isPlainSelect()) {
             try {
                 Statement statement = query.getStatement();
-                if (statement instanceof Select) {
-                    Select select = (Select) statement;
-                    if (select.getSelectBody() instanceof PlainSelect) {
-                        PlainSelect selectBody = (PlainSelect) select.getSelectBody();
-                        if (selectBody.getTop() == null && CommonUtils.isEmpty(selectBody.getIntoTables())) {
-                            Top top = new Top();
-                            top.setPercentage(false);
-                            top.setExpression(new LongValue(offset.longValue() + length.longValue()));
-                            selectBody.setTop(top);
+                if (statement instanceof PlainSelect selectBody) {
+                    if (selectBody.getTop() == null && CommonUtils.isEmpty(selectBody.getIntoTables())) {
+                        Top top = new Top();
+                        top.setPercentage(false);
+                        top.setExpression(new LongValue(offset.longValue() + length.longValue()));
+                        selectBody.setTop(top);
 
-                            limitSet = true;
-                            return statement.toString();
-                        }
+                        limitSet = true;
+                        return statement.toString();
                     }
                 }
             } catch (Throwable e) {
@@ -89,13 +84,9 @@ public class QueryTransformerTop implements DBCQueryTransformer, DBCQueryTransfo
         // and for queries without FROM (See #16526)
         if (query.isPlainSelect()) {
             final Statement statement = query.getStatement();
-            if (statement instanceof Select) {
-                final SelectBody selectBody = ((Select) statement).getSelectBody();
-                if (selectBody instanceof PlainSelect) {
-                    PlainSelect body = (PlainSelect) selectBody;
-                    return ((PlainSelect) body).getOffset() == null && body.getLimit() == null && body.getTop() == null
-                        && body.getFromItem() != null && CommonUtils.isEmpty(body.getIntoTables()) && !body.isForUpdate();
-                }
+            if (statement instanceof PlainSelect select) {
+                return select.getOffset() == null && select.getLimit() == null && select.getTop() == null
+                    && select.getFromItem() != null && CommonUtils.isEmpty(select.getIntoTables()) && select.getForUpdateTable() == null;
             }
         }
         return false;

+ 35 - 47
plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/sql/SQLQuery.java

@@ -91,7 +91,7 @@ public class SQLQuery implements SQLScriptElement {
     private List<SQLSelectItem> selectItems;
     private String queryTitle;
     private String extraErrorMessage;
-    private List<String> allSelectEntitiesNames = new ArrayList<>();
+    private final List<String> allSelectEntitiesNames = new ArrayList<>();
 
     public SQLQuery(@Nullable DBPDataSource dataSource, @NotNull String text) {
         this(dataSource, text, 0, text.length());
@@ -129,6 +129,7 @@ public class SQLQuery implements SQLScriptElement {
         }
     }
 
+    @Nullable
     public DBPDataSource getDataSource() {
         return dataSource;
     }
@@ -145,31 +146,27 @@ public class SQLQuery implements SQLScriptElement {
                 return;
             }
             statement = SQLSemanticProcessor.parseQuery(dataSource == null ? null : dataSource.getSQLDialect(), text);
-            if (statement instanceof Select) {
+            if (statement instanceof PlainSelect plainSelect) {
                 type = SQLQueryType.SELECT;
                 // Detect single source table (no joins, no group by, no sub-selects)
-                SelectBody selectBody = ((Select) statement).getSelectBody();
-                if (selectBody instanceof PlainSelect) {
-                    PlainSelect plainSelect = (PlainSelect) selectBody;
+                {
                     FromItem fromItem = plainSelect.getFromItem();
-
-                    if (fromItem instanceof SubSelect &&
+                    if (fromItem instanceof ParenthesedSelect ps &&
                         isPotentiallySingleSourceSelect(plainSelect) &&
-                        ((SubSelect) fromItem).getSelectBody() instanceof PlainSelect &&
-                        isPotentiallySingleSourceSelect((PlainSelect) ((SubSelect) fromItem).getSelectBody()))
-                    {
+                        ps.getPlainSelect() != null &&
+                        isPotentiallySingleSourceSelect(ps.getPlainSelect())
+                    ) {
                         // Real select is in sub-select
-                        plainSelect = (PlainSelect) ((SubSelect) fromItem).getSelectBody();
+                        plainSelect = ps.getPlainSelect();
                         fromItem = plainSelect.getFromItem();
                     }
-                    if (fromItem instanceof Table fromTable &&
-                        isPotentiallySingleSourceSelect(plainSelect))
-                    {
-                        boolean hasSubSelects = false, hasDirectSelects = false;
-                        for (SelectItem si : plainSelect.getSelectItems()) {
-                            if (si instanceof SelectExpressionItem && ((SelectExpressionItem) si).getExpression() instanceof SubSelect) {
+                    if (fromItem instanceof Table fromTable && isPotentiallySingleSourceSelect(plainSelect)) {
+                        boolean hasSubSelects = false;
+                        boolean hasDirectSelects = false;
+                        for (SelectItem<?> si : plainSelect.getSelectItems()) {
+                            if (si.getExpression() instanceof ParenthesedSelect) {
                                 hasSubSelects = true;
-                            } else if (si instanceof SelectExpressionItem && ((SelectExpressionItem) si).getExpression() instanceof Column) {
+                            } else if (si.getExpression() instanceof Column) {
                                 hasDirectSelects = true;
                             }
                         }
@@ -230,10 +227,10 @@ public class SQLQuery implements SQLScriptElement {
         }
     }
 
-    private boolean isValidSelectItem(@NotNull SelectItem item) {
+    private boolean isValidSelectItem(@NotNull SelectItem<?> item) {
         // Workaround for JSQLParser not respecting the `#` comment in MySQL and treating them as valid values
-        if (item instanceof SelectExpressionItem && dataSource != null) {
-            final Expression expr = ((SelectExpressionItem) item).getExpression();
+        if (dataSource != null) {
+            final Expression expr = item.getExpression();
             if (expr instanceof Column) {
                 final String name = CommonUtils.trim(((Column) expr).getColumnName());
                 if (CommonUtils.isNotEmpty(name)) {
@@ -251,7 +248,7 @@ public class SQLQuery implements SQLScriptElement {
 
     private boolean isPotentiallySingleSourceSelect(PlainSelect plainSelect) {
         return CommonUtils.isEmpty(plainSelect.getJoins()) &&
-            (plainSelect.getGroupBy() == null || CommonUtils.isEmpty(plainSelect.getGroupBy().getGroupByExpressionList().getExpressions())) &&
+            (plainSelect.getGroupBy() == null || CommonUtils.isEmpty(plainSelect.getGroupBy().getGroupByExpressionList())) &&
             CommonUtils.isEmpty(plainSelect.getIntoTables());
     }
 
@@ -295,12 +292,11 @@ public class SQLQuery implements SQLScriptElement {
      */
     public boolean isPlainSelect() {
         parseQuery();
-        if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
-            PlainSelect selectBody = (PlainSelect) ((Select) statement).getSelectBody();
-            return CommonUtils.isEmpty(selectBody.getIntoTables()) &&
-                selectBody.getLimit() == null &&
-                selectBody.getTop() == null &&
-                !selectBody.isForUpdate();
+        if (statement instanceof PlainSelect plainSelect) {
+            return CommonUtils.isEmpty(plainSelect.getIntoTables()) &&
+                plainSelect.getLimit() == null &&
+                plainSelect.getTop() == null &&
+                plainSelect.getForUpdateTable() == null;
         }
         return false;
     }
@@ -398,6 +394,7 @@ public class SQLQuery implements SQLScriptElement {
         return parseError;
     }
 
+    @Nullable
     public List<SQLQueryParameter> getParameters() {
         return parameters;
     }
@@ -447,10 +444,10 @@ public class SQLQuery implements SQLScriptElement {
 
     public DBCEntityMetaData getEntityMetadata(boolean raw) {
         parseQuery();
-        return raw? rawSingleTableMetadata : singleTableMeta;
+        return raw ? rawSingleTableMetadata : singleTableMeta;
     }
 
-    public void setParameters(List<SQLQueryParameter> parameters) {
+    public void setParameters(@Nullable List<SQLQueryParameter> parameters) {
         this.parameters = parameters;
     }
 
@@ -474,7 +471,7 @@ public class SQLQuery implements SQLScriptElement {
         if (CommonUtils.isEmpty(this.extraErrorMessage)) {
             this.extraErrorMessage = extraErrorMessage;
         } else {
-            this.extraErrorMessage = this.extraErrorMessage + System.getProperty(StandardConstants.ENV_LINE_SEPARATOR) + extraErrorMessage;
+            this.extraErrorMessage = this.extraErrorMessage + System.lineSeparator() + extraErrorMessage;
         }
     }
 
@@ -499,14 +496,10 @@ public class SQLQuery implements SQLScriptElement {
         if (statement == null) {
             return false;
         }
-        if (statement instanceof Delete) {
-            if (((Delete) statement).getWhere() == null) {
-                return true;
-            }
-        } else if (statement instanceof Update) {
-            if (((Update) statement).getWhere() == null) {
-                return true;
-            }
+        if (statement instanceof Delete delete) {
+            return delete.getWhere() == null;
+        } else if (statement instanceof Update update) {
+            return update.getWhere() == null;
         }
         return false;
     }
@@ -521,12 +514,8 @@ public class SQLQuery implements SQLScriptElement {
         if (getType() == SQLQueryType.UNKNOWN) {
             return false;
         }
-        if (statement instanceof Select) {
-            SelectBody selectBody = ((Select) statement).getSelectBody();
-            if (selectBody instanceof PlainSelect plainSelectBody) {
-                return plainSelectBody.isForUpdate() || plainSelectBody.getIntoTables() != null;
-            }
-            return false;
+        if (statement instanceof PlainSelect plainSelect) {
+            return plainSelect.getForUpdateTable() != null || plainSelect.getIntoTables() != null;
         } else {
             return true;
         }
@@ -591,10 +580,9 @@ public class SQLQuery implements SQLScriptElement {
 
         @Override
         public boolean equals(Object obj) {
-            if (!(obj instanceof SingleTableMeta)) {
+            if (!(obj instanceof SingleTableMeta md2)) {
                 return false;
             }
-            SingleTableMeta md2 = (SingleTableMeta) obj;
             return CommonUtils.equalObjects(catalogName, md2.catalogName) &&
                 CommonUtils.equalObjects(schemaName, md2.schemaName) &&
                 CommonUtils.equalObjects(tableName, md2.tableName);

+ 17 - 22
plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/sql/SQLSelectItem.java

@@ -32,38 +32,33 @@ import org.jkiss.utils.CommonUtils;
  */
 public class SQLSelectItem {
     private final SQLQuery query;
-    private final SelectItem source;
+    private final SelectItem<?> source;
     private final Table table;
     private final String name;
     private boolean plainColumn;
 
-    SQLSelectItem(SQLQuery query, SelectItem item) {
+    SQLSelectItem(SQLQuery query, SelectItem<?> item) {
         this.query = query;
         this.source = item;
-        if (item instanceof SelectExpressionItem) {
-            final Expression itemExpression = ((SelectExpressionItem) item).getExpression();
-            if (itemExpression instanceof Column) {
-                table = ((Column) itemExpression).getTable();
-                name = ((Column) itemExpression).getColumnName();
-                plainColumn = true;
-            } else {
-                table = null;
-                final Alias alias = ((SelectExpressionItem) item).getAlias();
-                if (alias != null) {
-                    name = alias.getName();
-                } else {
-                    name = item.toString();
-                }
-            }
-        } else if (item instanceof AllColumns) {
-            table = null;
+        Expression itemExpression = item.getExpression();
+        if (itemExpression instanceof Column) {
+            table = ((Column) itemExpression).getTable();
+            name = ((Column) itemExpression).getColumnName();
+            plainColumn = true;
+        } else if (itemExpression instanceof AllTableColumns atc) {
+            table = atc.getTable();
             name = "*";
-        } else if (item instanceof AllTableColumns) {
-            table = ((AllTableColumns) item).getTable();
+        } else if (itemExpression instanceof AllColumns) {
+            table = null;
             name = "*";
         } else {
             table = null;
-            name = "?";
+            final Alias alias = item.getAlias();
+            if (alias != null) {
+                name = alias.getName();
+            } else {
+                name = item.toString();
+            }
         }
     }
 

+ 5 - 0
plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/sql/parser/CustomExpression.java

@@ -38,6 +38,11 @@ public class CustomExpression implements Expression {
         expressionVisitor.visit(new StringValue(expression));
     }
 
+    @Override
+    public <T, S> T accept(ExpressionVisitor<T> expressionVisitor, S context) {
+        return expressionVisitor.visit(new StringValue(), context);
+    }
+
     @Override
     public String toString() {
         return expression;

+ 8 - 13
plugins/org.jkiss.dbeaver.model/src/org/jkiss/dbeaver/model/sql/parser/SQLSemanticProcessor.java

@@ -19,8 +19,8 @@ package org.jkiss.dbeaver.model.sql.parser;
 import net.sf.jsqlparser.JSQLParserException;
 import net.sf.jsqlparser.expression.Expression;
 import net.sf.jsqlparser.expression.LongValue;
-import net.sf.jsqlparser.expression.Parenthesis;
 import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
+import net.sf.jsqlparser.expression.operators.relational.ParenthesedExpressionList;
 import net.sf.jsqlparser.parser.CCJSqlParser;
 import net.sf.jsqlparser.parser.CCJSqlParserUtil;
 import net.sf.jsqlparser.parser.StringProvider;
@@ -111,14 +111,10 @@ public class SQLSemanticProcessor {
         }
     }
 
-    public static boolean isSelectQuery(SQLDialect dialect, String query)
-    {
+    public static boolean isSelectQuery(SQLDialect dialect, String query) {
         try {
             Statement statement = parseQuery(dialect, query);
-            return
-                statement instanceof Select select &&
-                select.getSelectBody() instanceof PlainSelect plainSelect &&
-                CommonUtils.isEmpty(plainSelect.getIntoTables());
+            return statement instanceof PlainSelect plainSelect && CommonUtils.isEmpty(plainSelect.getIntoTables());
         } catch (Throwable e) {
             //log.debug(e);
             return false;
@@ -160,10 +156,10 @@ public class SQLSemanticProcessor {
     ) throws DBException {
         try {
             Statement statement = parseQuery(dataSource.getSQLDialect(), sqlQuery);
-            if (statement instanceof Select select && select.getSelectBody() instanceof PlainSelect plainSelect) {
+            if (statement instanceof PlainSelect plainSelect) {
                 if (patchSelectQuery(monitor, dataSource, plainSelect, dataFilter)) {
                     return statement.toString();
-                } else if (select.getWithItemsList() != null && !select.getWithItemsList().isEmpty()) {
+                } else if (plainSelect.getWithItemsList() != null && !plainSelect.getWithItemsList().isEmpty()) {
                     addWhereCondition(dataSource, plainSelect, dataFilter);
                     if (dataFilter.hasOrdering()) {
                         addOrderByClause(monitor, dataSource, plainSelect, dataFilter);
@@ -369,7 +365,7 @@ public class SQLSemanticProcessor {
 
     @Nullable
     public static Table getTableFromSelect(Select select) {
-        if (select.getSelectBody() instanceof PlainSelect plainSelect) {
+        if (select instanceof PlainSelect plainSelect) {
             FromItem fromItem = plainSelect.getFromItem();
             if (fromItem instanceof Table table) {
                 return table;
@@ -389,8 +385,7 @@ public class SQLSemanticProcessor {
 
     @Nullable
     public static Table findTableByNameOrAlias(Select select, String tableName) {
-        SelectBody selectBody = select.getSelectBody();
-        if (selectBody instanceof PlainSelect plainSelect) {
+        if (select instanceof PlainSelect plainSelect) {
             FromItem fromItem = plainSelect.getFromItem();
             if (fromItem instanceof Table table && equalTables(table, tableName)) {
                 return table;
@@ -425,7 +420,7 @@ public class SQLSemanticProcessor {
         if (sourceWhere == null) {
             select.setWhere(conditionExpr);
         } else {
-            select.setWhere(new AndExpression(new Parenthesis(sourceWhere), conditionExpr));
+            select.setWhere(new AndExpression(new ParenthesedExpressionList<>(sourceWhere), conditionExpr));
         }
     }
 

+ 1 - 4
plugins/org.jkiss.dbeaver.ui.editors.sql/src/org/jkiss/dbeaver/ui/editors/sql/execute/SQLQueryJob.java

@@ -608,10 +608,7 @@ public class SQLQueryJob extends DataSourceJob
         if (statement instanceof Insert ||
             statement instanceof Delete ||
             statement instanceof Update ||
-            (statement instanceof Select &&
-                ((Select) statement).getSelectBody() instanceof PlainSelect &&
-                !CommonUtils.isEmpty(((PlainSelect) ((Select) statement).getSelectBody()).getIntoTables())))
-        {
+            (statement instanceof PlainSelect select && !CommonUtils.isEmpty(select.getIntoTables()))) {
             return false;
         }
         return true;

+ 1 - 1
product/localRepository/pom.xml

@@ -44,7 +44,7 @@
 						<id>default-cli</id>
 						<configuration>
 							<artifacts>
-								<artifact><id>com.github.jsqlparser:jsqlparser:4.5</id></artifact>
+								<artifact><id>com.github.jsqlparser:jsqlparser:5.1</id></artifact>
 <!--								<artifact><id>com.manticore-projects.jsqlformatter:jsqlformatter:0.1.7</id></artifact>-->
 
 								<artifact><id>org.apache.commons:commons-jexl3:3.1</id></artifact>