use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project JSqlParser by JSQLParser.
the class AdaptersTest method testAdapters.
/**
* Test extracting JDBC named parameters using adapters
*/
@Test
public void testAdapters() throws JSQLParserException {
String sql = "SELECT * FROM MYTABLE WHERE COLUMN_A = :paramA AND COLUMN_B <> :paramB";
Statement stmnt = CCJSqlParserUtil.parse(sql);
final Stack<Pair<String, String>> params = new Stack<Pair<String, String>>();
stmnt.accept(new StatementVisitorAdapter() {
@Override
public void visit(Select select) {
select.getSelectBody().accept(new SelectVisitorAdapter() {
@Override
public void visit(PlainSelect plainSelect) {
plainSelect.getWhere().accept(new ExpressionVisitorAdapter() {
@Override
protected void visitBinaryExpression(BinaryExpression expr) {
if (!(expr instanceof AndExpression)) {
params.push(new Pair<String, String>(null, null));
}
super.visitBinaryExpression(expr);
}
@Override
public void visit(Column column) {
params.push(new Pair<String, String>(column.getColumnName(), params.pop().getRight()));
}
@Override
public void visit(JdbcNamedParameter parameter) {
params.push(new Pair<String, String>(params.pop().getLeft(), parameter.getName()));
}
});
}
});
}
});
assertEquals(2, params.size());
Pair<String, String> param2 = params.pop();
assertEquals("COLUMN_B", param2.getLeft());
assertEquals("paramB", param2.getRight());
Pair<String, String> param1 = params.pop();
assertEquals("COLUMN_A", param1.getLeft());
assertEquals("paramA", param1.getRight());
}
use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project herddb by diennea.
the class SQLPlanner method validateColumnConstaintToExpression.
private static Expression validateColumnConstaintToExpression(Expression testExpression, String columnName, String tableAlias, Class<? extends BinaryExpression> expressionType) throws StatementExecutionException {
Expression result = null;
if (expressionType.isAssignableFrom(testExpression.getClass())) {
BinaryExpression e = (BinaryExpression) testExpression;
if (e.getLeftExpression() instanceof net.sf.jsqlparser.schema.Column) {
net.sf.jsqlparser.schema.Column c = (net.sf.jsqlparser.schema.Column) e.getLeftExpression();
boolean okAlias = true;
if (c.getTable() != null && c.getTable().getName() != null && !c.getTable().getName().equals(tableAlias)) {
okAlias = false;
}
if (okAlias && columnName.equalsIgnoreCase(c.getColumnName()) && SQLRecordPredicate.isConstant(e.getRightExpression())) {
return e.getRightExpression();
}
} else if (e.getLeftExpression() instanceof AndExpression) {
result = findConstraintOnColumn(e.getLeftExpression(), columnName, tableAlias, expressionType);
if (result != null) {
return result;
}
} else if (e.getRightExpression() instanceof AndExpression) {
result = findConstraintOnColumn(e.getRightExpression(), columnName, tableAlias, expressionType);
if (result != null) {
return result;
}
}
}
return result;
}
use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project herddb by diennea.
the class SQLPlanner method findConstraintExpressionOnColumn.
private Expression findConstraintExpressionOnColumn(Expression where, String columnName, String tableAlias, Class<? extends BinaryExpression> expressionType) throws StatementExecutionException {
if (where instanceof AndExpression) {
AndExpression and = (AndExpression) where;
Expression keyOnLeft = findConstraintExpressionOnColumn(and.getLeftExpression(), columnName, tableAlias, expressionType);
if (keyOnLeft != null) {
return keyOnLeft;
}
Expression keyOnRight = findConstraintExpressionOnColumn(and.getRightExpression(), columnName, tableAlias, expressionType);
if (keyOnRight != null) {
return keyOnRight;
}
} else if (expressionType.isAssignableFrom(where.getClass())) {
Expression keyDirect = validateColumnConstaintExpressionToExpression(where, columnName, tableAlias, expressionType);
if (keyDirect != null) {
return keyDirect;
}
}
return null;
}
use of net.sf.jsqlparser.expression.operators.conditional.AndExpression in project herddb by diennea.
the class SQLPlanner method findConstraintOnColumn.
private static Expression findConstraintOnColumn(Expression where, String columnName, String tableAlias, Class<? extends BinaryExpression> expressionType) throws StatementExecutionException {
if (where instanceof AndExpression) {
AndExpression and = (AndExpression) where;
Expression keyOnLeft = findConstraintOnColumn(and.getLeftExpression(), columnName, tableAlias, expressionType);
if (keyOnLeft != null) {
return keyOnLeft;
}
Expression keyOnRight = findConstraintOnColumn(and.getRightExpression(), columnName, tableAlias, expressionType);
if (keyOnRight != null) {
return keyOnRight;
}
} else if (expressionType.isAssignableFrom(where.getClass())) {
Expression keyDirect = validateColumnConstaintToExpression(where, columnName, tableAlias, expressionType);
if (keyDirect != null) {
return keyDirect;
}
}
return null;
}
Aggregations