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