Skip to content

Commit

Permalink
Separate test case for XOR added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Szabó Miklós committed May 26, 2021
1 parent 38b7abd commit 83ecbdc
Showing 1 changed file with 56 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,11 @@
*/
package net.sf.jsqlparser.statement.builder;

import static net.sf.jsqlparser.test.TestUtils.*;

import java.util.List;

import net.sf.jsqlparser.expression.operators.conditional.XorExpression;
import org.junit.Test;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Alias;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.JdbcParameter;
import net.sf.jsqlparser.expression.Parenthesis;
import net.sf.jsqlparser.expression.StringValue;
import net.sf.jsqlparser.expression.*;
import net.sf.jsqlparser.expression.operators.conditional.AndExpression;
import net.sf.jsqlparser.expression.operators.conditional.OrExpression;
import net.sf.jsqlparser.expression.operators.conditional.XorExpression;
import net.sf.jsqlparser.expression.operators.relational.EqualsTo;
import net.sf.jsqlparser.expression.operators.relational.ExpressionList;
import net.sf.jsqlparser.expression.operators.relational.InExpression;
Expand All @@ -34,41 +25,44 @@
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.Test;

import java.util.List;

import static net.sf.jsqlparser.test.TestUtils.*;

public class JSQLParserFluentModelTests {

@Test
public void testParseAndBuild() throws JSQLParserException {
String statement = "SELECT * FROM tab1 AS t1 " //
+ "JOIN tab2 t2 ON t1.ref = t2.id WHERE (t1.col1 = ? OR t1.col2 = ?) AND t1.col3 IN ('A') XOR t1.col4";
+ "JOIN tab2 t2 ON t1.ref = t2.id WHERE (t1.col1 = ? OR t1.col2 = ?) AND t1.col3 IN ('A')";

Statement parsed = TestUtils.assertSqlCanBeParsedAndDeparsed(statement);

Table t1 = new Table("tab1").withAlias(new Alias("t1").withUseAs(true));
Table t2 = new Table("tab2").withAlias(new Alias("t2", false));

XorExpression where =
new XorExpression().withLeftExpression(new AndExpression().withLeftExpression(
AndExpression where = new AndExpression().withLeftExpression(
new Parenthesis(new OrExpression().withLeftExpression(
new EqualsTo()
.withLeftExpression(new Column(asList("t1", "col1")))
.withRightExpression(new JdbcParameter().withIndex(1)))
.withLeftExpression(new Column(asList("t1", "col1")))
.withRightExpression(new JdbcParameter().withIndex(1)))
.withRightExpression(new EqualsTo(
new Column(asList("t1", "col2")),
new JdbcParameter().withIndex(
2)))
)).withRightExpression(
new InExpression()
.withLeftExpression(new Column(asList("t1", "col3")))
.withRightItemsList(new ExpressionList().addExpressions(new StringValue("A")))))
.withRightExpression( new Column(asList("t1", "col4")) );
)).withRightExpression(
new InExpression()
.withLeftExpression(new Column(asList("t1", "col3")))
.withRightItemsList(new ExpressionList().addExpressions(new StringValue("A"))));
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)
ExpressionList list = select.getSelectBody(PlainSelect.class).getWhere(AndExpression.class)
.getRightExpression(InExpression.class).getRightItemsList(ExpressionList.class);
List<Expression> elist = list.getExpressions();
list.setExpressions(elist);
Expand All @@ -77,4 +71,44 @@ public void testParseAndBuild() throws JSQLParserException {
assertEqualsObjectTree(parsed, select);
}

@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()
.addExpressions(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);
}
}

0 comments on commit 83ecbdc

Please sign in to comment.