Search in sources :

Example 51 with Expression

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

the class SQLQueryTransformerCount method tryInjectCount.

private SQLQuery tryInjectCount(SQLDataSource 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 (!CommonUtils.isEmpty(select.getGroupByColumnReferences())) {
                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)

Example 52 with Expression

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

the class SQLSemanticProcessor method getConstraintExpression.

private static Expression getConstraintExpression(DBPDataSource dataSource, PlainSelect select, DBDAttributeConstraint co) throws JSQLParserException {
    Expression orderExpr;
    String attrName = DBUtils.getQuotedIdentifier(dataSource, 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) 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 53 with Expression

use of net.sf.jsqlparser.expression.Expression in project JSqlParser by JSQLParser.

the class CNFTest method test2.

/**
 * The purpose is to test the double negation law. As you can
 * see when you build the tree, there will be two Not Operators
 * together on the line. It is there when we use the double negation law.
 *
 * Here is the expression tree:
 *                               ( )
 *                                |
 *                                OR
 *                      /                    \
 *                    ( )                    ( )
 *                     |                      |
 *                    NOT                    AND
 *                     |                  /       \
 *                    ( )               LIKE       =
 *                     |              /      \   /    \
 *                     OR           S.A   "%%%" S.B  "orz"
 *                /        \
 *               NOT        <
 *                |       /   \
 *               >=      3.3  4.5
 *            /      \
 *          1.1      2.3
 *
 * Here is the converted expression tree:
 *
 *                                                           AND
 *                                          /                              \
 *                                        AND                              ( )
 *                         /                        \                       |
 *                        AND                       ( )                     OR
 *             /                    \                |                /            \
 *           ( )                    ( )              OR             NOT              =
 *            |                      |           /        \          |            /     \
 *            OR                     OR         NOT       LIKE       <           S.B   "orz"
 *       /         \             /       \       |      /      \   /   \
 *      >=         LIKE        >=        =       <     S.A   "%%%" 3.3 4.5
 *   /      \    /      \   /     \    /    \
 *  1.1     2.3 S.A   "%%%" 1.1   2.3 S.B   "orz"
 */
@Test
public void test2() throws Exception {
    Expression expr = CCJSqlParserUtil.parseCondExpression("((NOT (NOT 1.1 >= 2.3 OR 3.3 < 4.5)) OR " + "(S.A LIKE '\"%%%\"' AND S.B = '\"orz\"'))");
    Expression expected = CCJSqlParserUtil.parseCondExpression("(1.1 >= 2.3 OR S.A LIKE '\"%%%\"') AND (1.1 >= 2.3 OR S.B = '\"orz\"')" + " AND (NOT 3.3 < 4.5 OR S.A LIKE '\"%%%\"') AND (NOT 3.3 < 4.5 OR S.B = '\"orz\"')");
    Expression result = CNFConverter.convertToCNF(expr);
    assertEquals(expected.toString(), result.toString());
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) Test(org.junit.Test)

Example 54 with Expression

use of net.sf.jsqlparser.expression.Expression in project JSqlParser by JSQLParser.

the class CNFTest method test1.

/**
 * The purpose of this method is to check when there is a Not Operator
 * at the root. Which means the root must be switched.
 *
 * Here is the expression tree:
 *
 *                            NOT
 *                             |
 *                            ( )
 *                             |
 *                            AND
 *                /                         \
 *              ( )                         ( )
 *               |                           |
 *               OR                          OR
 *        /              \              /          \
 *       <               =             !=          >=
 *    /      \         /    \        /    \       /   \
 *   1.2     2.3      3.5   4.6     1.1   2.5    8.0  7.2
 *
 * Here is the converted expression tree:
 *
 *                                                            AND
 *                                           /                             \
 *                                         AND                             ( )
 *                          /                          \                    |
 *                       AND                           ( )                  OR
 *              /                     \                 |            /             \
 *             ( )                    ( )               OR         NOT             NOT
 *              |                      |           /         \      |               |
 *              OR                     OR         NOT        NOT    =               >=
 *         /         \             /       \       |          |   /   \           /    \
 *       NOT         NOT          NOT      NOT     =         !=  3.5  4.6        8.0   7.2
 *        |           |            |        |    /   \      /   \
 *        <           !=           <        >=
 *     /     \      /    \       /    \   /    \
 *    1.2    2.3   1.1   2.5    1.2  2.3 8.0   7.2
 */
@Test
public void test1() throws Exception {
    Expression expr = CCJSqlParserUtil.parseCondExpression("NOT ((1.2 < 2.3 OR 3.5 = 4.6) AND (1.1 <> 2.5 OR 8.0 >= 7.2))");
    Expression expected = CCJSqlParserUtil.parseCondExpression("(NOT 1.2 < 2.3 OR NOT 1.1 <> 2.5) AND (NOT 1.2 < 2.3 OR NOT 8.0 >= 7.2) AND" + " (NOT 3.5 = 4.6 OR NOT 1.1 <> 2.5) AND (NOT 3.5 = 4.6 OR NOT 8.0 >= 7.2)");
    Expression result = CNFConverter.convertToCNF(expr);
    assertEquals(expected.toString(), result.toString());
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) Test(org.junit.Test)

Example 55 with Expression

use of net.sf.jsqlparser.expression.Expression in project JSqlParser by JSQLParser.

the class CNFTest method test4.

/**
 * This is the case when we test a very simple tree structure that
 * has neither AND operator or OR operator.
 *
 * Here is the expression tree:
 *
 *                         NOT
 *                          |
 *                          >
 *                   /             \
 *                  S.D           {d '2017-03-25'}
 *
 * Here is the converted expression tree:
 *
 *                    NOT
 *                     |
 *                     >
 *                 /       \
 *                S.D     {d '2017-03-25'}
 */
@Test
public void test4() throws Exception {
    Expression expr = CCJSqlParserUtil.parseCondExpression("NOT S.D > {d '2017-03-25'}");
    Expression expected = CCJSqlParserUtil.parseCondExpression("NOT S.D > {d '2017-03-25'}");
    Expression result = CNFConverter.convertToCNF(expr);
    assertEquals(expected.toString(), result.toString());
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) Test(org.junit.Test)

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