Search in sources :

Example 21 with ExpressionList

use of net.sf.jsqlparser.expression.operators.relational.ExpressionList in project dbeaver by dbeaver.

the class SQLQueryTransformerCount method tryInjectCount.

private SQLQuery tryInjectCount(DBPDataSource dataSource, SQLQuery query) throws DBException {
    try {
        Statement statement = CCJSqlParserUtil.parse(query.getText());
        if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
            PlainSelect select = (PlainSelect) ((Select) statement).getSelectBody();
            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())) {
                throw new DBException("Can't inject COUNT into query with GROUP BY clause");
            }
            Distinct selectDistinct = select.getDistinct();
            if (selectDistinct != null) {
                // Remove distinct
                select.setDistinct(null);
            }
            Function countFunc = new Function();
            countFunc.setName("count");
            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());
                    }
                }
                if (!exprs.isEmpty()) {
                    countFunc.setParameters(new ExpressionList(exprs));
                }
            }
            countFunc.setAllColumns(true);
            List<SelectItem> selectItems = new ArrayList<>();
            selectItems.add(new SelectExpressionItem(countFunc));
            select.setSelectItems(selectItems);
            select.setOrderByElements(null);
            return new SQLQuery(dataSource, select.toString(), query, false);
        } else {
            throw new DBException("Query [" + query.getText() + "] can't be modified");
        }
    } catch (JSQLParserException e) {
        throw new DBException("Can't transform query to SELECT count(*)", e);
    }
}
Also used : DBException(org.jkiss.dbeaver.DBException) Statement(net.sf.jsqlparser.statement.Statement) JSQLParserException(net.sf.jsqlparser.JSQLParserException) ArrayList(java.util.ArrayList) Function(net.sf.jsqlparser.expression.Function) Expression(net.sf.jsqlparser.expression.Expression) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Aggregations

ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)21 Expression (net.sf.jsqlparser.expression.Expression)16 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)9 MultiExpressionList (net.sf.jsqlparser.expression.operators.relational.MultiExpressionList)6 StringReader (java.io.StringReader)5 JSQLParserException (net.sf.jsqlparser.JSQLParserException)4 Function (net.sf.jsqlparser.expression.Function)4 Statement (net.sf.jsqlparser.statement.Statement)4 DBException (org.jkiss.dbeaver.DBException)4 JdbcParameter (net.sf.jsqlparser.expression.JdbcParameter)3 ItemsList (net.sf.jsqlparser.expression.operators.relational.ItemsList)3 Insert (net.sf.jsqlparser.statement.insert.Insert)3 StatementExecutionException (herddb.model.StatementExecutionException)2 List (java.util.List)2 AnalyticExpression (net.sf.jsqlparser.expression.AnalyticExpression)2 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)2 KeepExpression (net.sf.jsqlparser.expression.KeepExpression)2 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)2 InExpression (net.sf.jsqlparser.expression.operators.relational.InExpression)2