Search in sources :

Example 1 with SelectItemVisitorAdapter

use of net.sf.jsqlparser.statement.select.SelectItemVisitorAdapter 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

WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)1 ExpressionVisitorAdapter (net.sf.jsqlparser.expression.ExpressionVisitorAdapter)1 JdbcParameter (net.sf.jsqlparser.expression.JdbcParameter)1 Column (net.sf.jsqlparser.schema.Column)1 Table (net.sf.jsqlparser.schema.Table)1 FromItemVisitorAdapter (net.sf.jsqlparser.statement.select.FromItemVisitorAdapter)1 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)1 SelectBody (net.sf.jsqlparser.statement.select.SelectBody)1 SelectExpressionItem (net.sf.jsqlparser.statement.select.SelectExpressionItem)1 SelectItem (net.sf.jsqlparser.statement.select.SelectItem)1 SelectItemVisitorAdapter (net.sf.jsqlparser.statement.select.SelectItemVisitorAdapter)1 SelectVisitorAdapter (net.sf.jsqlparser.statement.select.SelectVisitorAdapter)1 SetOperationList (net.sf.jsqlparser.statement.select.SetOperationList)1 SubSelect (net.sf.jsqlparser.statement.select.SubSelect)1