Search in sources :

Example 1 with ExpressionVisitorAdapter

use of net.sf.jsqlparser.expression.ExpressionVisitorAdapter in project spanner-jdbc by olavloite.

the class CloudSpannerResultSetMetaData method initColumns.

private void initColumns(Select select) {
    columns = new ArrayList<>();
    aliases = new ArrayList<>();
    select.getSelectBody().accept(new SelectVisitorAdapter() {

        @Override
        public void visit(PlainSelect plainSelect) {
            for (SelectItem selectItem : plainSelect.getSelectItems()) {
                selectItem.accept(new SelectItemVisitor() {

                    private boolean foundColumn = false;

                    @Override
                    public void visit(SelectExpressionItem selectExpressionItem) {
                        selectExpressionItem.getExpression().accept(new ExpressionVisitorAdapter() {

                            @Override
                            public void visit(Column column) {
                                registerColumn(column, selectExpressionItem.getAlias());
                                foundColumn = true;
                            }
                        });
                        if (!foundColumn) {
                            registerColumn(null, selectExpressionItem.getAlias());
                        }
                    }

                    @Override
                    public void visit(AllTableColumns allTableColumns) {
                        registerAllTableColumns(allTableColumns.getTable());
                    }

                    @Override
                    public void visit(AllColumns allColumns) {
                        for (Table table : tables) {
                            registerAllTableColumns(table);
                        }
                    }
                });
            }
        }
    });
}
Also used : SelectVisitorAdapter(net.sf.jsqlparser.statement.select.SelectVisitorAdapter) Table(net.sf.jsqlparser.schema.Table) Column(net.sf.jsqlparser.schema.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) AllTableColumns(net.sf.jsqlparser.statement.select.AllTableColumns) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) ExpressionVisitorAdapter(net.sf.jsqlparser.expression.ExpressionVisitorAdapter) SelectItemVisitor(net.sf.jsqlparser.statement.select.SelectItemVisitor) AllColumns(net.sf.jsqlparser.statement.select.AllColumns)

Example 2 with ExpressionVisitorAdapter

use of net.sf.jsqlparser.expression.ExpressionVisitorAdapter 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)

Example 3 with ExpressionVisitorAdapter

use of net.sf.jsqlparser.expression.ExpressionVisitorAdapter in project sandbox by irof.

the class ParsingTest method postgres_sequence.

@Test
void postgres_sequence() throws Exception {
    try (InputStream inputStream = this.getClass().getResourceAsStream("/select-postgres-sequence.sql")) {
        Statement statement = CCJSqlParserUtil.parse(inputStream);
        Select select = Select.class.cast(statement);
        class SequenceFinder extends SelectVisitorAdapter {

            String sequenceExpression;

            @Override
            public void visit(PlainSelect plainSelect) {
                List<SelectItem> selectItems = plainSelect.getSelectItems();
                SelectItem selectItem = selectItems.get(0);
                SelectItemVisitor expressionVisitor = new ExpressionVisitorAdapter() {

                    @Override
                    public void visit(Function function) {
                        sequenceExpression = function.toString();
                        super.visit(function);
                    }
                };
                selectItem.accept(expressionVisitor);
            }
        }
        SequenceFinder sequenceFinder = new SequenceFinder();
        select.getSelectBody().accept(sequenceFinder);
        assertThat(sequenceFinder.sequenceExpression).isEqualTo("nextval('hoge_seq')");
    }
}
Also used : Function(net.sf.jsqlparser.expression.Function) InputStream(java.io.InputStream) Statement(net.sf.jsqlparser.statement.Statement) ExpressionVisitorAdapter(net.sf.jsqlparser.expression.ExpressionVisitorAdapter) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with ExpressionVisitorAdapter

use of net.sf.jsqlparser.expression.ExpressionVisitorAdapter in project spanner-jdbc by olavloite.

the class CloudSpannerPreparedStatement method setPlainSelectParameters.

private void setPlainSelectParameters(PlainSelect plainSelect, com.google.cloud.spanner.Statement.Builder builder) {
    if (plainSelect.getFromItem() != null) {
        plainSelect.getFromItem().accept(new FromItemVisitorAdapter() {

            private int tableCount = 0;

            @Override
            public void visit(Table table) {
                tableCount++;
                if (tableCount == 1)
                    getParameterStore().setTable(unquoteIdentifier(table.getFullyQualifiedName()));
                else
                    getParameterStore().setTable(null);
            }

            @Override
            public void visit(SubSelect subSelect) {
                if (subSelect.getSelectBody() instanceof PlainSelect) {
                    setPlainSelectParameters((PlainSelect) subSelect.getSelectBody(), builder);
                } else {
                    subSelect.getSelectBody().accept(new SelectVisitorAdapter() {

                        @Override
                        public void visit(PlainSelect plainSelect) {
                            setPlainSelectParameters(plainSelect, builder);
                        }

                        @Override
                        public void visit(SetOperationList setOpList) {
                            for (SelectBody body : setOpList.getSelects()) {
                                setSelectParameters(body, builder);
                            }
                        }
                    });
                }
            }
        });
    }
    if (plainSelect.getSelectItems() != null) {
        for (SelectItem selectItem : plainSelect.getSelectItems()) {
            selectItem.accept(new SelectItemVisitorAdapter() {

                @Override
                public void visit(SelectExpressionItem item) {
                    item.getExpression().accept(new ExpressionVisitorAdapter() {

                        private String currentCol = null;

                        @Override
                        public void visit(Column col) {
                            currentCol = unquoteIdentifier(col.getFullyQualifiedName());
                        }

                        @Override
                        public void visit(JdbcParameter parameter) {
                            parameter.accept(new ValueBinderExpressionVisitorAdapter<>(getParameterStore(), builder.bind("p" + parameter.getIndex()), currentCol));
                            currentCol = null;
                        }

                        @Override
                        public void visit(SubSelect subSelect) {
                            setSelectParameters(subSelect.getSelectBody(), builder);
                        }
                    });
                }
            });
        }
    }
    setWhereParameters(plainSelect.getWhere(), builder);
    if (plainSelect.getLimit() != null) {
        setWhereParameters(plainSelect.getLimit().getRowCount(), builder);
    }
    if (plainSelect.getOffset() != null && plainSelect.getOffset().getOffsetJdbcParameter() != null) {
        ValueBinderExpressionVisitorAdapter<com.google.cloud.spanner.Statement.Builder> binder = new ValueBinderExpressionVisitorAdapter<>(getParameterStore(), builder.bind("p" + getParameterStore().getHighestIndex()), null);
        binder.setValue(getParameterStore().getParameter(getParameterStore().getHighestIndex()), Types.BIGINT);
        getParameterStore().setType(getParameterStore().getHighestIndex(), Types.BIGINT);
    }
}
Also used : Table(net.sf.jsqlparser.schema.Table) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SelectBody(net.sf.jsqlparser.statement.select.SelectBody) SetOperationList(net.sf.jsqlparser.statement.select.SetOperationList) SelectVisitorAdapter(net.sf.jsqlparser.statement.select.SelectVisitorAdapter) SelectItemVisitorAdapter(net.sf.jsqlparser.statement.select.SelectItemVisitorAdapter) Column(net.sf.jsqlparser.schema.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) FromItemVisitorAdapter(net.sf.jsqlparser.statement.select.FromItemVisitorAdapter) ExpressionVisitorAdapter(net.sf.jsqlparser.expression.ExpressionVisitorAdapter) SubSelect(net.sf.jsqlparser.statement.select.SubSelect)

Aggregations

ExpressionVisitorAdapter (net.sf.jsqlparser.expression.ExpressionVisitorAdapter)4 Column (net.sf.jsqlparser.schema.Column)3 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)3 SelectVisitorAdapter (net.sf.jsqlparser.statement.select.SelectVisitorAdapter)3 Table (net.sf.jsqlparser.schema.Table)2 SelectExpressionItem (net.sf.jsqlparser.statement.select.SelectExpressionItem)2 SelectItem (net.sf.jsqlparser.statement.select.SelectItem)2 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)1 InputStream (java.io.InputStream)1 Stack (java.util.Stack)1 BinaryExpression (net.sf.jsqlparser.expression.BinaryExpression)1 Function (net.sf.jsqlparser.expression.Function)1 JdbcNamedParameter (net.sf.jsqlparser.expression.JdbcNamedParameter)1 JdbcParameter (net.sf.jsqlparser.expression.JdbcParameter)1 AndExpression (net.sf.jsqlparser.expression.operators.conditional.AndExpression)1 Statement (net.sf.jsqlparser.statement.Statement)1 AllColumns (net.sf.jsqlparser.statement.select.AllColumns)1 AllTableColumns (net.sf.jsqlparser.statement.select.AllTableColumns)1 FromItemVisitorAdapter (net.sf.jsqlparser.statement.select.FromItemVisitorAdapter)1 Select (net.sf.jsqlparser.statement.select.Select)1