Proper way to create a duplicate Expression object
See original GitHub issueI recently faced a situation where I needed to set the same criteria expression in all the SELECTs in a UNION query. Though the criteria is same, I would want distinct objects that represent the same criteria i.e., changing the criteria in one of the SELECT queries should not affect the criteria in the other SELECT queries. Let me demonstrate what I was trying to achieve:
Select selectStatement = (Select) CCJSqlParserUtil.parse(
"SELECT id FROM table1 UNION SELECT id FROM table2"
);
Expression criteriaExpr = CCJSqlParserUtil.parseCondExpression("NAME LIKE '%M%'");
selectStatement.getSelectBody().accept(new SelectVisitorAdapter() {
@Override
public void visit(PlainSelect plainSelect) {
plainSelect.setWhere(criteriaExpr);
}
@Override
public void visit(SetOperationList setOpList) {
setOpList.getSelects().forEach(
selectBody -> selectBody.accept(this)
);
}
});
The program properly sets the criteria for both the queries. But as it uses the same criteriaExpr
object for both the queries in the UNION, changing the criteria in one of the queries also changes the criteria in the other query. This is an issue in my case.
One way to overcome this is by moving the creation of the criteriaExpr
into the visit(PlainSelect plainSelect)
method. But it has the limitation that there is no direct way to communicate to the caller, the case in which the criteria could not be parsed (i.e., when an JSQLParserException
occurs) as the signature of the visit
method cannot be changed.
Coming to the question, is there a way to create a clone of the creteriaExpr
without having to parse the expression again so that a unique object can be assigned to all the queries in the UNION?
Issue Analytics
- State:
- Created 4 years ago
- Comments:9 (9 by maintainers)
Top GitHub Comments
With my proposal #901 this would be more simple.
Sure.