use of net.sf.jsqlparser.expression.operators.relational.ExpressionList in project JSqlParser by JSQLParser.
the class ExpressionDeParserTest method shouldDeParseComplexAnalyticExpressionWithPartitionExpressionList.
@Test
public void shouldDeParseComplexAnalyticExpressionWithPartitionExpressionList() {
AnalyticExpression analyticExpression = new AnalyticExpression();
ExpressionList partitionExpressionList = new ExpressionList();
List<Expression> partitionExpressions = new ArrayList<Expression>();
Expression partitionExpression1 = mock(Expression.class);
Expression partitionExpression2 = mock(Expression.class);
analyticExpression.setName("name");
analyticExpression.setPartitionExpressionList(partitionExpressionList);
partitionExpressionList.setExpressions(partitionExpressions);
partitionExpressions.add(partitionExpression1);
partitionExpressions.add(partitionExpression2);
will(appendToBuffer("partition expression 1")).given(partitionExpression1).accept(expressionDeParser);
will(appendToBuffer("partition expression 2")).given(partitionExpression2).accept(expressionDeParser);
expressionDeParser.visit(analyticExpression);
assertEquals("name() OVER (PARTITION BY partition expression 1, partition expression 2 )", buffer.toString());
}
use of net.sf.jsqlparser.expression.operators.relational.ExpressionList in project JSqlParser by JSQLParser.
the class StatementDeParserTest method shouldUseProvidedDeParserWhenDeParsingExecute.
@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeParserWhenDeParsingExecute() {
Execute execute = new Execute();
ExpressionList exprList = new ExpressionList();
List<Expression> expressions = new ArrayList<Expression>();
Expression expression1 = mock(Expression.class);
Expression expression2 = mock(Expression.class);
execute.setExprList(exprList);
exprList.setExpressions(expressions);
expressions.add(expression1);
expressions.add(expression2);
statementDeParser.visit(execute);
then(expression1).should().accept(expressionDeParser);
then(expression2).should().accept(expressionDeParser);
}
use of net.sf.jsqlparser.expression.operators.relational.ExpressionList 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.operators.relational.ExpressionList in project JSqlParser by JSQLParser.
the class ExecuteDeParserTest method shouldUseProvidedExpressionVisitorWhenDeParsingExecute.
@Test
public void shouldUseProvidedExpressionVisitorWhenDeParsingExecute() {
Execute execute = new Execute();
String name = "name";
ExpressionList exprList = new ExpressionList();
List<Expression> expressions = new ArrayList<Expression>();
Expression expression1 = mock(Expression.class);
Expression expression2 = mock(Expression.class);
execute.setName(name);
execute.setExprList(exprList);
exprList.setExpressions(expressions);
expressions.add(expression1);
expressions.add(expression2);
executeDeParser.deParse(execute);
then(expression1).should().accept(expressionVisitor);
then(expression2).should().accept(expressionVisitor);
}
use of net.sf.jsqlparser.expression.operators.relational.ExpressionList in project JSqlParser by JSQLParser.
the class UpsertTest method testUpsert.
@Test
public void testUpsert() throws JSQLParserException {
String statement = "UPSERT INTO TEST (NAME, ID) VALUES ('foo', 123)";
Upsert upsert = (Upsert) parserManager.parse(new StringReader(statement));
assertEquals("TEST", upsert.getTable().getName());
assertTrue(upsert.isUseValues());
assertEquals(2, upsert.getColumns().size());
assertEquals("NAME", ((Column) upsert.getColumns().get(0)).getColumnName());
assertEquals("ID", ((Column) upsert.getColumns().get(1)).getColumnName());
assertEquals(2, ((ExpressionList) upsert.getItemsList()).getExpressions().size());
assertEquals("foo", ((StringValue) ((ExpressionList) upsert.getItemsList()).getExpressions().get(0)).getValue());
assertEquals(123, ((LongValue) ((ExpressionList) upsert.getItemsList()).getExpressions().get(1)).getValue());
assertFalse(upsert.isUseSelectBrackets());
assertFalse(upsert.isUseDuplicate());
assertEquals(statement, "" + upsert);
}
Aggregations