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;
}
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);
}
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());
}
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());
}
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;
}
Aggregations