Search in sources :

Example 6 with ExpressionList

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());
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) KeepExpression(net.sf.jsqlparser.expression.KeepExpression) AnalyticExpression(net.sf.jsqlparser.expression.AnalyticExpression) ArrayList(java.util.ArrayList) AnalyticExpression(net.sf.jsqlparser.expression.AnalyticExpression) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) Test(org.junit.Test)

Example 7 with ExpressionList

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);
}
Also used : Execute(net.sf.jsqlparser.statement.execute.Execute) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) Test(org.junit.Test)

Example 8 with ExpressionList

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);
    }
}
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 9 with ExpressionList

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);
}
Also used : Execute(net.sf.jsqlparser.statement.execute.Execute) Expression(net.sf.jsqlparser.expression.Expression) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) Test(org.junit.Test)

Example 10 with ExpressionList

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);
}
Also used : Upsert(net.sf.jsqlparser.statement.upsert.Upsert) StringReader(java.io.StringReader) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) Test(org.junit.Test)

Aggregations

ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)21 Expression (net.sf.jsqlparser.expression.Expression)16 ArrayList (java.util.ArrayList)11 Test (org.junit.Test)9 MultiExpressionList (net.sf.jsqlparser.expression.operators.relational.MultiExpressionList)6 StringReader (java.io.StringReader)5 JSQLParserException (net.sf.jsqlparser.JSQLParserException)4 Function (net.sf.jsqlparser.expression.Function)4 Statement (net.sf.jsqlparser.statement.Statement)4 DBException (org.jkiss.dbeaver.DBException)4 JdbcParameter (net.sf.jsqlparser.expression.JdbcParameter)3 ItemsList (net.sf.jsqlparser.expression.operators.relational.ItemsList)3 Insert (net.sf.jsqlparser.statement.insert.Insert)3 StatementExecutionException (herddb.model.StatementExecutionException)2 List (java.util.List)2 AnalyticExpression (net.sf.jsqlparser.expression.AnalyticExpression)2 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)2 KeepExpression (net.sf.jsqlparser.expression.KeepExpression)2 SignedExpression (net.sf.jsqlparser.expression.SignedExpression)2 InExpression (net.sf.jsqlparser.expression.operators.relational.InExpression)2