Search in sources :

Example 46 with Expression

use of net.sf.jsqlparser.expression.Expression in project spanner-jdbc by olavloite.

the class AbstractSpannerExpressionVisitorAdapter method visit.

@Override
public void visit(SignedExpression value) {
    Expression underlyingValue = value.getExpression();
    if (underlyingValue instanceof DoubleValue) {
        DoubleValue doubleValue = (DoubleValue) underlyingValue;
        doubleValue.setValue(value.getSign() == '-' ? -doubleValue.getValue() : doubleValue.getValue());
        visit(doubleValue);
    } else if (underlyingValue instanceof LongValue) {
        LongValue longValue = (LongValue) underlyingValue;
        longValue.setValue(value.getSign() == '-' ? -longValue.getValue() : longValue.getValue());
        visit(longValue);
    } else {
        super.visit(value);
    }
}
Also used : SignedExpression(net.sf.jsqlparser.expression.SignedExpression) Expression(net.sf.jsqlparser.expression.Expression) TimeKeyExpression(net.sf.jsqlparser.expression.TimeKeyExpression) DoubleValue(net.sf.jsqlparser.expression.DoubleValue) LongValue(net.sf.jsqlparser.expression.LongValue)

Example 47 with Expression

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

the class SQLSemanticProcessor method patchSelectQuery.

private static boolean patchSelectQuery(DBRProgressMonitor monitor, DBPDataSource dataSource, PlainSelect select, DBDDataFilter filter) throws JSQLParserException, DBException {
    // WHERE
    if (filter.hasConditions()) {
        for (DBDAttributeConstraint co : filter.getConstraints()) {
            if (co.hasCondition()) {
                Table table = getConstraintTable(select, co);
                if (!isValidTableColumn(monitor, dataSource, table, co)) {
                    table = null;
                }
                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()) {
            String columnName = co.getAttributeName();
            boolean forceNumeric = filter.hasNameDuplicates(columnName) || !SQLUtils.PATTERN_SIMPLE_NAME.matcher(columnName).matches();
            Expression orderExpr = getOrderConstraintExpression(monitor, dataSource, select, co, forceNumeric);
            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 48 with Expression

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

the class SQLSemanticProcessor method addWhereToSelect.

public static void addWhereToSelect(PlainSelect select, String condString) throws JSQLParserException {
    Expression filterWhere;
    try {
        filterWhere = CCJSqlParserUtil.parseCondExpression(condString);
    } catch (JSQLParserException e) {
        throw new JSQLParserException("Bad query condition: [" + condString + "]", e);
    }
    addWhereToSelect(select, filterWhere);
}
Also used : AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Expression(net.sf.jsqlparser.expression.Expression) JSQLParserException(net.sf.jsqlparser.JSQLParserException)

Example 49 with Expression

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

the class SQLSemanticProcessor method getOrderConstraintExpression.

private static Expression getOrderConstraintExpression(DBRProgressMonitor monitor, DBPDataSource dataSource, PlainSelect select, DBDAttributeConstraint co, boolean forceNumeric) throws JSQLParserException, DBException {
    Expression orderExpr;
    String attrName = DBUtils.getQuotedIdentifier(dataSource, co.getAttributeName());
    if (forceNumeric || attrName.isEmpty()) {
        orderExpr = new LongValue(co.getOrderPosition());
    } else if (CommonUtils.isJavaIdentifier(attrName)) {
        // Use column table only if there are multiple source tables (joins)
        Table orderTable = CommonUtils.isEmpty(select.getJoins()) ? null : getConstraintTable(select, co);
        if (!isValidTableColumn(monitor, dataSource, orderTable, co)) {
            orderTable = null;
        }
        orderExpr = new Column(orderTable, attrName);
    } else {
        // TODO: set tableAlias for all column references in expression
        orderExpr = CCJSqlParserUtil.parseExpression(attrName);
    // orderExpr = new CustomExpression(attrName);
    // orderExpr = new LongValue(co.getAttribute().getOrdinalPosition() + 1);
    }
    return orderExpr;
}
Also used : Table(net.sf.jsqlparser.schema.Table) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) LongValue(net.sf.jsqlparser.expression.LongValue)

Example 50 with Expression

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

the class SQLSemanticProcessor method getConstraintExpression.

private static Expression getConstraintExpression(PlainSelect select, DBDAttributeConstraint co) throws JSQLParserException {
    Expression orderExpr;
    String attrName = co.getAttribute().getName();
    if (attrName.isEmpty()) {
        orderExpr = new LongValue(co.getAttribute().getOrdinalPosition() + 1);
    } else if (CommonUtils.isJavaIdentifier(attrName)) {
        // Use column table only if there are multiple source tables (joins)
        Table orderTable = CommonUtils.isEmpty(select.getJoins()) ? null : getConstraintTable(select, co);
        orderExpr = new Column(orderTable, attrName);
    } else {
        // TODO: set tableAlias for all column references in expression
        orderExpr = CCJSqlParserUtil.parseExpression(attrName);
    //orderExpr = new CustomExpression(attrName);
    //orderExpr = new LongValue(co.getAttribute().getOrdinalPosition() + 1);
    }
    return orderExpr;
}
Also used : Table(net.sf.jsqlparser.schema.Table) Expression(net.sf.jsqlparser.expression.Expression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Column(net.sf.jsqlparser.schema.Column) LongValue(net.sf.jsqlparser.expression.LongValue)

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