Search in sources :

Example 1 with Expression

use of net.sf.jsqlparser.expression.Expression in project dbeaver by serge-rider.

the class SQLQueryTransformerCount method transformQuery.

@Override
public SQLQuery transformQuery(SQLDataSource dataSource, SQLQuery query) throws DBException {
    try {
        Statement statement = CCJSqlParserUtil.parse(query.getQuery());
        if (statement instanceof Select && ((Select) statement).getSelectBody() instanceof PlainSelect) {
            PlainSelect select = (PlainSelect) ((Select) statement).getSelectBody();
            List<SelectItem> selectItems = new ArrayList<>();
            Function countFunc = new Function();
            countFunc.setName("count");
            countFunc.setParameters(new ExpressionList(Collections.<Expression>singletonList(new Column("*"))));
            SelectItem countItem = new SelectExpressionItem(countFunc);
            selectItems.add(countItem);
            select.setSelectItems(selectItems);
            return new SQLQuery(dataSource, select.toString(), query, false);
        } else {
            throw new DBException("Query [" + query.getQuery() + "] 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) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) JSQLParserException(net.sf.jsqlparser.JSQLParserException) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) ArrayList(java.util.ArrayList) SQLQuery(org.jkiss.dbeaver.model.sql.SQLQuery) Function(net.sf.jsqlparser.expression.Function) Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 2 with Expression

use of net.sf.jsqlparser.expression.Expression in project dbeaver by serge-rider.

the class SQLSemanticProcessor method patchSelectQuery.

private static boolean patchSelectQuery(DBPDataSource dataSource, PlainSelect select, DBDDataFilter filter) throws JSQLParserException {
    // WHERE
    if (filter.hasConditions()) {
        for (DBDAttributeConstraint co : filter.getConstraints()) {
            if (co.hasCondition()) {
                Table table = getConstraintTable(select, co);
                if (table != null) {
                    if (table.getAlias() != null) {
                        co.setEntityAlias(table.getAlias().getName());
                    } else {
                        co.setEntityAlias(table.getName());
                    }
                } else {
                    co.setEntityAlias(null);
                }
            }
        }
        StringBuilder whereString = new StringBuilder();
        SQLUtils.appendConditionString(filter, dataSource, null, whereString, true);
        String condString = whereString.toString();
        addWhereToSelect(select, condString);
    }
    // ORDER
    if (filter.hasOrdering()) {
        List<OrderByElement> orderByElements = select.getOrderByElements();
        if (orderByElements == null) {
            orderByElements = new ArrayList<>();
            select.setOrderByElements(orderByElements);
        }
        for (DBDAttributeConstraint co : filter.getOrderConstraints()) {
            Expression orderExpr = getConstraintExpression(select, co);
            OrderByElement element = new OrderByElement();
            element.setExpression(orderExpr);
            if (co.isOrderDescending()) {
                element.setAsc(false);
                element.setAscDescPresent(true);
            }
            orderByElements.add(element);
        }
    }
    return true;
}
Also used : Table(net.sf.jsqlparser.schema.Table) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) Expression(net.sf.jsqlparser.expression.Expression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression)

Example 3 with Expression

use of net.sf.jsqlparser.expression.Expression in project dbeaver by dbeaver.

the class SQLSemanticProcessor method patchSelectQuery.

private static boolean patchSelectQuery(DBPDataSource dataSource, PlainSelect select, DBDDataFilter filter) throws JSQLParserException {
    // WHERE
    if (filter.hasConditions()) {
        for (DBDAttributeConstraint co : filter.getConstraints()) {
            if (co.hasCondition()) {
                Table table = getConstraintTable(select, co);
                if (table != null) {
                    if (table.getAlias() != null) {
                        co.setEntityAlias(table.getAlias().getName());
                    } else {
                        co.setEntityAlias(table.getName());
                    }
                } else {
                    co.setEntityAlias(null);
                }
            }
        }
        StringBuilder whereString = new StringBuilder();
        SQLUtils.appendConditionString(filter, dataSource, null, whereString, true);
        String condString = whereString.toString();
        addWhereToSelect(select, condString);
    }
    // ORDER
    if (filter.hasOrdering()) {
        List<OrderByElement> orderByElements = select.getOrderByElements();
        if (orderByElements == null) {
            orderByElements = new ArrayList<>();
            select.setOrderByElements(orderByElements);
        }
        for (DBDAttributeConstraint co : filter.getOrderConstraints()) {
            Expression orderExpr = getConstraintExpression(dataSource, select, co);
            OrderByElement element = new OrderByElement();
            element.setExpression(orderExpr);
            if (co.isOrderDescending()) {
                element.setAsc(false);
                element.setAscDescPresent(true);
            }
            orderByElements.add(element);
        }
    }
    return true;
}
Also used : Table(net.sf.jsqlparser.schema.Table) DBDAttributeConstraint(org.jkiss.dbeaver.model.data.DBDAttributeConstraint) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Expression(net.sf.jsqlparser.expression.Expression)

Example 4 with Expression

use of net.sf.jsqlparser.expression.Expression in project xwiki-platform by xwiki.

the class EscapeLikeParametersQuery method modifyStatement.

/**
 * Handle the case of MySQL: in MySQL a '\' character is a special escape character. In addition we often
 * use '\' in Entity References. For example to find nested pages in a page with a dot would result in
 * something like "LIKE '.%.a\.b.%'" which wouldn't work on MySQL. Thus we need to replace the default
 * escape character with another one. To be safe we verify that the statement doesn't already specify an ESCAPE
 * term.
 */
private String modifyStatement(String statementString) throws JSQLParserException {
    Statement statement = CCJSqlParserUtil.parse(statementString);
    if (statement instanceof Select) {
        Select select = (Select) statement;
        SelectBody selectBody = select.getSelectBody();
        if (selectBody instanceof PlainSelect) {
            PlainSelect plainSelect = (PlainSelect) selectBody;
            Expression where = plainSelect.getWhere();
            where.accept(new XWikiExpressionVisitor());
        }
    }
    return statement.toString();
}
Also used : LikeExpression(net.sf.jsqlparser.expression.operators.relational.LikeExpression) Expression(net.sf.jsqlparser.expression.Expression) Statement(net.sf.jsqlparser.statement.Statement) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SelectBody(net.sf.jsqlparser.statement.select.SelectBody)

Example 5 with Expression

use of net.sf.jsqlparser.expression.Expression in project herddb by diennea.

the class SQLAggregator method createOutputColumns.

private static Column[] createOutputColumns(List<SelectItem> selectItems, DataScanner wrapped) throws StatementExecutionException {
    Column[] columns = new Column[selectItems.size()];
    int i = 0;
    for (SelectItem item : selectItems) {
        boolean done = false;
        if (item instanceof SelectExpressionItem) {
            SelectExpressionItem sei = (SelectExpressionItem) item;
            Expression expression = sei.getExpression();
            if (expression instanceof Function) {
                Function f = (Function) expression;
                String fieldName = f.toString();
                if (sei.getAlias() != null && sei.getAlias().getName() != null) {
                    fieldName = sei.getAlias().getName();
                }
                if (fieldName == null) {
                    fieldName = "field" + i;
                }
                Column aggregated = BuiltinFunctions.toAggregatedOutputColumn(fieldName, f);
                if (aggregated != null) {
                    columns[i] = aggregated;
                    done = true;
                }
            } else if (expression instanceof net.sf.jsqlparser.schema.Column) {
                net.sf.jsqlparser.schema.Column c = (net.sf.jsqlparser.schema.Column) expression;
                String name = c.getColumnName();
                boolean found = false;
                for (Column co : wrapped.getSchema()) {
                    if (co.name.equals(name)) {
                        columns[i] = co;
                        found = true;
                        break;
                    }
                }
                if (!found) {
                    throw new StatementExecutionException("cannot find column " + name + " is upstream scanner");
                }
                done = true;
            } else {
                throw new StatementExecutionException("unhandled aggregate query selectable item:" + expression);
            }
        }
        i++;
        if (!done) {
            throw new StatementExecutionException("unhandled aggregate function " + item);
        }
    }
    return columns;
}
Also used : SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) RawString(herddb.utils.RawString) StatementExecutionException(herddb.model.StatementExecutionException) Function(net.sf.jsqlparser.expression.Function) Column(herddb.model.Column) Expression(net.sf.jsqlparser.expression.Expression) SelectItem(net.sf.jsqlparser.statement.select.SelectItem)

Aggregations

Expression (net.sf.jsqlparser.expression.Expression)92 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)30 Test (org.junit.Test)30 ArrayList (java.util.ArrayList)26 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)25 Column (net.sf.jsqlparser.schema.Column)19 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)18 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)17 Table (net.sf.jsqlparser.schema.Table)14 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)12 LikeExpression (net.sf.jsqlparser.expression.operators.relational.LikeExpression)12 AlterExpression (net.sf.jsqlparser.statement.alter.AlterExpression)12 NotExpression (net.sf.jsqlparser.expression.NotExpression)11 StatementExecutionException (herddb.model.StatementExecutionException)10 Select (net.sf.jsqlparser.statement.select.Select)10 AnalyticExpression (net.sf.jsqlparser.expression.AnalyticExpression)9 CaseExpression (net.sf.jsqlparser.expression.CaseExpression)9 KeepExpression (net.sf.jsqlparser.expression.KeepExpression)9 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)9 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)9