Search in sources :

Example 71 with Expression

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

the class CloneHelper method modify.

/**
 * This method is used for changing the logical structure of the tree.
 * The main method is to convert and operator and or operator to let
 * them have multiple children (reflected in MultipleExpression.java
 * from the conditional package). Note if the value from the conditional
 * operator has a not attached to it we need to append an not operator
 * ahead of it since the not operator needed to be pushed down during
 * the second step. Also, I will leave out all the parenthesis expression
 * which is connected to the conditional operator.
 * @param express the expression that will be modified
 * @return the modified expression.
 */
public Expression modify(Expression express) {
    if (express instanceof NotExpression) {
        return new NotExpression(modify(((NotExpression) express).getExpression()));
    }
    if (express instanceof Parenthesis) {
        Parenthesis parenthesis = (Parenthesis) express;
        Expression result = modify(parenthesis.getExpression());
        if (parenthesis.isNot()) {
            return new NotExpression(result);
        }
        return result;
    }
    if (express instanceof AndExpression) {
        AndExpression and = (AndExpression) express;
        List<Expression> list = new ArrayList<Expression>();
        list.add(modify(and.getLeftExpression()));
        list.add(modify(and.getRightExpression()));
        MultiAndExpression result = new MultiAndExpression(list);
        if (and.isNot()) {
            return new NotExpression(result);
        }
        return result;
    }
    if (express instanceof OrExpression) {
        OrExpression or = (OrExpression) express;
        List<Expression> list = new ArrayList<Expression>();
        list.add(modify(or.getLeftExpression()));
        list.add(modify(or.getRightExpression()));
        MultiOrExpression result = new MultiOrExpression(list);
        if (or.isNot()) {
            return new NotExpression(result);
        }
        return result;
    }
    if (express instanceof BinaryExpression) {
        BinaryExpression binary = (BinaryExpression) express;
        if (binary.isNot()) {
            binary.removeNot();
            return new NotExpression(modify(binary));
        }
    }
    /* at this stage, there is no need to modify, just simply return. */
    return express;
}
Also used : Parenthesis(net.sf.jsqlparser.expression.Parenthesis) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) NotExpression(net.sf.jsqlparser.expression.NotExpression) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) NotExpression(net.sf.jsqlparser.expression.NotExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression)

Example 72 with Expression

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

the class CCJSqlParserUtilTest method testParseExpression.

/**
 * Test of parseExpression method, of class CCJSqlParserUtil.
 */
@Test
public void testParseExpression() throws Exception {
    Expression result = CCJSqlParserUtil.parseExpression("a+b");
    assertEquals("a + b", result.toString());
    assertTrue(result instanceof Addition);
    Addition add = (Addition) result;
    assertTrue(add.getLeftExpression() instanceof Column);
    assertTrue(add.getRightExpression() instanceof Column);
}
Also used : Addition(net.sf.jsqlparser.expression.operators.arithmetic.Addition) Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) Test(org.junit.Test)

Example 73 with Expression

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

the class CCJSqlParserUtilTest method testParseExpressionNonPartial2.

@Test
public void testParseExpressionNonPartial2() throws Exception {
    Expression result = CCJSqlParserUtil.parseExpression("a+", true);
    assertEquals("a", result.toString());
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) Test(org.junit.Test)

Example 74 with Expression

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

the class CCJSqlParserUtilTest method testParseCondExpressionPartial2.

@Test
public void testParseCondExpressionPartial2() throws Exception {
    Expression result = CCJSqlParserUtil.parseCondExpression("x=92 lasd y=29", true);
    assertEquals("x = 92", result.toString());
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) Test(org.junit.Test)

Example 75 with Expression

use of net.sf.jsqlparser.expression.Expression in project raml-module-builder by folio-org.

the class PostgresClient method parseQuery.

/**
 * returns ParsedQuery with:
 * 1. Original query stripped of the order by, limit and offset clauses (if they existed in the query)
 * 2. Original query stripped of the limit and offset clauses (if they existed in the query)
 * 3. where clause part of query (included in the stripped query)
 * 4. original order by clause that was removed (or null)
 * 5. original limit clause that was removed (or null)
 * 6. original offset clause that was removed (or null)
 * @param query
 * @return
 */
static ParsedQuery parseQuery(String query) {
    List<OrderByElement> orderBy = null;
    net.sf.jsqlparser.statement.select.Limit limit = null;
    Expression where = null;
    net.sf.jsqlparser.statement.select.Offset offset = null;
    long start = System.nanoTime();
    String queryWithoutLimitOffset = "";
    try {
        try {
            net.sf.jsqlparser.statement.Statement statement = CCJSqlParserUtil.parse(query);
            Select selectStatement = (Select) statement;
            orderBy = ((PlainSelect) selectStatement.getSelectBody()).getOrderByElements();
            limit = ((PlainSelect) selectStatement.getSelectBody()).getLimit();
            offset = ((PlainSelect) selectStatement.getSelectBody()).getOffset();
            where = ((PlainSelect) selectStatement.getSelectBody()).getWhere();
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        int startOfLimit = getLastStartPos(query, "limit");
        if (limit != null) {
            String suffix = Pattern.compile(limit.toString().trim(), Pattern.CASE_INSENSITIVE).matcher(query.substring(startOfLimit)).replaceFirst("");
            query = query.substring(0, startOfLimit) + suffix;
        } else if (startOfLimit != -1) {
            // offset returns null if it was placed before the limit although postgres does allow this
            // we are here if offset appears in the query and not within quotes
            query = query.substring(0, startOfLimit) + Pattern.compile("limit\\s+[\\d]+", Pattern.CASE_INSENSITIVE).matcher(query.substring(startOfLimit)).replaceFirst("");
        }
        int startOfOffset = getLastStartPos(query, "offset");
        if (offset != null) {
            String suffix = Pattern.compile(offset.toString().trim(), Pattern.CASE_INSENSITIVE).matcher(query.substring(startOfOffset)).replaceFirst("");
            query = query.substring(0, startOfOffset) + suffix;
        } else if (startOfOffset != -1) {
            // offset returns null if it was placed before the limit although postgres does allow this
            // we are here if offset appears in the query and not within quotes
            query = query.substring(0, startOfOffset) + Pattern.compile("offset\\s+[\\d]+", Pattern.CASE_INSENSITIVE).matcher(query.substring(startOfOffset)).replaceFirst("");
        }
        queryWithoutLimitOffset = query;
        // in the rare case where the order by clause somehow appears in the where clause
        int startOfOrderBy = getLastStartPos(query, "order by");
        if (orderBy != null) {
            StringBuilder sb = new StringBuilder("order by[ ]+");
            int size = orderBy.size();
            for (int i = 0; i < size; i++) {
                sb.append(orderBy.get(i).toString().replaceAll(" ", "[ ]+"));
                if (i < size - 1) {
                    sb.append(",?[ ]+");
                }
            }
            String regex = escape(sb.toString().trim());
            query = query.substring(0, startOfOrderBy) + Pattern.compile(regex, Pattern.CASE_INSENSITIVE).matcher(query.substring(startOfOrderBy)).replaceFirst("");
        } else if (startOfOrderBy != -1) {
            // offset returns null if it was placed before the limit although postgres does allow this
            // we are here if offset appears in the query and not within quotes
            query = query.substring(0, startOfOrderBy) + Pattern.compile("order by.*", Pattern.CASE_INSENSITIVE).matcher(query.substring(startOfOrderBy)).replaceFirst("");
        }
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }
    ParsedQuery pq = new ParsedQuery();
    pq.setCountFuncQuery(query);
    pq.setQueryWithoutLimOff(queryWithoutLimitOffset);
    if (where != null) {
        pq.setWhereClause(where.toString());
    }
    if (orderBy != null) {
        pq.setOrderByClause(orderBy.toString());
    }
    if (limit != null) {
        pq.setLimitClause(limit.toString());
    }
    if (offset != null) {
        pq.setOffsetClause(offset.toString());
    }
    long end = System.nanoTime();
    log.debug("Parse query for count_estimate function (ns) " + (end - start));
    return pq;
}
Also used : ParsedQuery(org.folio.rest.persist.facets.ParsedQuery) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) SQLException(java.sql.SQLException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Expression(net.sf.jsqlparser.expression.Expression) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) OrderByElement(net.sf.jsqlparser.statement.select.OrderByElement)

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