use of net.sf.jsqlparser.expression.JdbcNamedParameter 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());
}
Aggregations