Search in sources :

Example 1 with JdbcNamedParameter

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());
}
Also used : JdbcNamedParameter(net.sf.jsqlparser.expression.JdbcNamedParameter) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Stack(java.util.Stack) SelectVisitorAdapter(net.sf.jsqlparser.statement.select.SelectVisitorAdapter) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) Column(net.sf.jsqlparser.schema.Column) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) ExpressionVisitorAdapter(net.sf.jsqlparser.expression.ExpressionVisitorAdapter) Test(org.junit.Test)

Aggregations

Stack (java.util.Stack)1 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)1 ExpressionVisitorAdapter (net.sf.jsqlparser.expression.ExpressionVisitorAdapter)1 JdbcNamedParameter (net.sf.jsqlparser.expression.JdbcNamedParameter)1 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)1 Column (net.sf.jsqlparser.schema.Column)1 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)1 Select (net.sf.jsqlparser.statement.select.Select)1 SelectVisitorAdapter (net.sf.jsqlparser.statement.select.SelectVisitorAdapter)1 Test (org.junit.Test)1