Search in sources :

Example 1 with XorExpression

use of net.sf.jsqlparser.expression.operators.conditional.XorExpression in project JSqlParser by JSQLParser.

the class ExpressionVisitorAdapterTest method testXorExpression.

@Test
public void testXorExpression() throws JSQLParserException {
    final List<Expression> exprList = new ArrayList<>();
    Select select = (Select) CCJSqlParserUtil.parse("SELECT * FROM table WHERE foo XOR bar");
    PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
    Expression where = plainSelect.getWhere();
    where.accept(new ExpressionVisitorAdapter() {

        @Override
        public void visit(XorExpression expr) {
            super.visit(expr);
            exprList.add(expr.getLeftExpression());
            exprList.add(expr.getRightExpression());
        }
    });
    assertEquals(2, exprList.size());
    assertTrue(exprList.get(0) instanceof Column);
    assertEquals("foo", ((Column) exprList.get(0)).getColumnName());
    assertTrue(exprList.get(1) instanceof Column);
    assertEquals("bar", ((Column) exprList.get(1)).getColumnName());
}
Also used : XorExpression(net.sf.jsqlparser.expression.operators.conditional.XorExpression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) Column(net.sf.jsqlparser.schema.Column) XorExpression(net.sf.jsqlparser.expression.operators.conditional.XorExpression) ArrayList(java.util.ArrayList) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Test(org.junit.jupiter.api.Test)

Example 2 with XorExpression

use of net.sf.jsqlparser.expression.operators.conditional.XorExpression in project JSqlParser by JSQLParser.

the class JSQLParserFluentModelTests method testParseAndBuildForXORComplexCondition.

@Test
public void testParseAndBuildForXORComplexCondition() throws JSQLParserException {
    String statement = "SELECT * FROM tab1 AS t1 WHERE " + "a AND b OR c XOR d";
    Statement parsed = TestUtils.assertSqlCanBeParsedAndDeparsed(statement);
    Table t1 = new Table("tab1").withAlias(new Alias("t1", true));
    XorExpression where = new XorExpression().withLeftExpression(new OrExpression().withLeftExpression(new AndExpression().withLeftExpression(new Column("a")).withRightExpression(new Column("b"))).withRightExpression(new Column("c"))).withRightExpression(new Column("d"));
    Select select = new Select().withSelectBody(new PlainSelect().addSelectItems(new AllColumns()).withFromItem(t1).withWhere(where));
    assertDeparse(select, statement);
    assertEqualsObjectTree(select, parsed);
}
Also used : Table(net.sf.jsqlparser.schema.Table) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Column(net.sf.jsqlparser.schema.Column) Statement(net.sf.jsqlparser.statement.Statement) Alias(net.sf.jsqlparser.expression.Alias) XorExpression(net.sf.jsqlparser.expression.operators.conditional.XorExpression) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) AllColumns(net.sf.jsqlparser.statement.select.AllColumns) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) Test(org.junit.jupiter.api.Test)

Example 3 with XorExpression

use of net.sf.jsqlparser.expression.operators.conditional.XorExpression in project JSqlParser by JSQLParser.

the class JSQLParserFluentModelTests method testParseAndBuildForXORs.

@Test
public void testParseAndBuildForXORs() throws JSQLParserException {
    String statement = "SELECT * FROM tab1 AS t1 WHERE " + "a XOR b XOR c";
    Statement parsed = TestUtils.assertSqlCanBeParsedAndDeparsed(statement);
    Table t1 = new Table("tab1").withAlias(new Alias("t1", true));
    XorExpression where = new XorExpression().withLeftExpression(new XorExpression().withLeftExpression(new Column("a")).withRightExpression(new Column("b"))).withRightExpression(new Column("c"));
    Select select = new Select().withSelectBody(new PlainSelect().addSelectItems(new AllColumns()).withFromItem(t1).withWhere(where));
    assertDeparse(select, statement);
    assertEqualsObjectTree(select, parsed);
}
Also used : Table(net.sf.jsqlparser.schema.Table) Column(net.sf.jsqlparser.schema.Column) Statement(net.sf.jsqlparser.statement.Statement) Alias(net.sf.jsqlparser.expression.Alias) XorExpression(net.sf.jsqlparser.expression.operators.conditional.XorExpression) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) AllColumns(net.sf.jsqlparser.statement.select.AllColumns) Test(org.junit.jupiter.api.Test)

Example 4 with XorExpression

use of net.sf.jsqlparser.expression.operators.conditional.XorExpression in project JSqlParser by JSQLParser.

the class JSQLParserFluentModelTests method testParseAndBuildForXOR.

@Test
public void testParseAndBuildForXOR() throws JSQLParserException {
    String statement = "SELECT * FROM tab1 AS t1 JOIN tab2 t2 ON t1.ref = t2.id " + "WHERE (t1.col1 XOR t2.col2) AND t1.col3 IN ('B', 'C') XOR t2.col4";
    Statement parsed = TestUtils.assertSqlCanBeParsedAndDeparsed(statement);
    Table t1 = new Table("tab1").withAlias(new Alias("t1", true));
    Table t2 = new Table("tab2").withAlias(new Alias("t2", false));
    XorExpression where = new XorExpression().withLeftExpression(new AndExpression().withLeftExpression(new Parenthesis(new XorExpression().withLeftExpression(new Column(asList("t1", "col1"))).withRightExpression(new Column(asList("t2", "col2"))))).withRightExpression(new InExpression().withLeftExpression(new Column(asList("t1", "col3"))).withRightItemsList(new ExpressionList(new StringValue("B"), new StringValue("C"))))).withRightExpression(new Column(asList("t2", "col4")));
    Select select = new Select().withSelectBody(new PlainSelect().addSelectItems(new AllColumns()).withFromItem(t1).addJoins(new Join().withRightItem(t2).withOnExpression(new EqualsTo(new Column(asList("t1", "ref")), new Column(asList("t2", "id"))))).withWhere(where));
    ExpressionList list = select.getSelectBody(PlainSelect.class).getWhere(XorExpression.class).getLeftExpression(AndExpression.class).getRightExpression(InExpression.class).getRightItemsList(ExpressionList.class);
    List<Expression> elist = list.getExpressions();
    list.setExpressions(elist);
    assertDeparse(select, statement);
    assertEqualsObjectTree(parsed, select);
}
Also used : Table(net.sf.jsqlparser.schema.Table) Statement(net.sf.jsqlparser.statement.Statement) XorExpression(net.sf.jsqlparser.expression.operators.conditional.XorExpression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Join(net.sf.jsqlparser.statement.select.Join) AllColumns(net.sf.jsqlparser.statement.select.AllColumns) Parenthesis(net.sf.jsqlparser.expression.Parenthesis) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) Column(net.sf.jsqlparser.schema.Column) XorExpression(net.sf.jsqlparser.expression.operators.conditional.XorExpression) Expression(net.sf.jsqlparser.expression.Expression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) OrExpression(net.sf.jsqlparser.expression.operators.conditional.OrExpression) Alias(net.sf.jsqlparser.expression.Alias) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) EqualsTo(net.sf.jsqlparser.expression.operators.relational.EqualsTo) StringValue(net.sf.jsqlparser.expression.StringValue) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) Test(org.junit.jupiter.api.Test)

Aggregations

XorExpression (net.sf.jsqlparser.expression.operators.conditional.XorExpression)4 Column (net.sf.jsqlparser.schema.Column)4 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)4 Select (net.sf.jsqlparser.statement.select.Select)4 Test (org.junit.jupiter.api.Test)4 Alias (net.sf.jsqlparser.expression.Alias)3 Table (net.sf.jsqlparser.schema.Table)3 Statement (net.sf.jsqlparser.statement.Statement)3 AllColumns (net.sf.jsqlparser.statement.select.AllColumns)3 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)2 OrExpression (net.sf.jsqlparser.expression.operators.conditional.OrExpression)2 InExpression (net.sf.jsqlparser.expression.operators.relational.InExpression)2 ArrayList (java.util.ArrayList)1 Expression (net.sf.jsqlparser.expression.Expression)1 Parenthesis (net.sf.jsqlparser.expression.Parenthesis)1 StringValue (net.sf.jsqlparser.expression.StringValue)1 EqualsTo (net.sf.jsqlparser.expression.operators.relational.EqualsTo)1 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)1 Join (net.sf.jsqlparser.statement.select.Join)1 SubSelect (net.sf.jsqlparser.statement.select.SubSelect)1