Search in sources :

Example 1 with SubSelect

use of net.sf.jsqlparser.statement.select.SubSelect in project herddb by diennea.

the class JdbcQueryRewriter method visit.

@Override
public void visit(InExpression inExpression) {
    inExpression.getLeftExpression().accept(this);
    if (inExpression.getLeftItemsList() != null) {
        inExpression.getLeftItemsList().accept(this);
    }
    if (inExpression.getRightItemsList() instanceof SubSelect) {
        SubSelect ss = (SubSelect) inExpression.getRightItemsList();
        if (!(ss.getSelectBody() instanceof PlainSelect)) {
            throw new StatementExecutionException("unsupported operand " + inExpression.getClass() + " with subquery of type " + ss.getClass() + "(" + ss + ")");
        }
        visit(ss);
    }
}
Also used : PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) LateralSubSelect(net.sf.jsqlparser.statement.select.LateralSubSelect) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) StatementExecutionException(herddb.model.StatementExecutionException)

Example 2 with SubSelect

use of net.sf.jsqlparser.statement.select.SubSelect in project JSqlParser by JSQLParser.

the class ReplaceTest method testReplaceSyntax3.

@Test
public void testReplaceSyntax3() throws JSQLParserException {
    String statement = "REPLACE mytable (col1, col2, col3) SELECT * FROM mytable3";
    Replace replace = (Replace) PARSER_MANAGER.parse(new StringReader(statement));
    assertEquals("mytable", replace.getTable().getName());
    assertEquals(3, replace.getColumns().size());
    assertEquals("col1", ((Column) replace.getColumns().get(0)).getColumnName());
    assertEquals("col2", ((Column) replace.getColumns().get(1)).getColumnName());
    assertEquals("col3", ((Column) replace.getColumns().get(2)).getColumnName());
    assertTrue(replace.getItemsList() instanceof SubSelect);
}
Also used : Replace(net.sf.jsqlparser.statement.replace.Replace) StringReader(java.io.StringReader) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) Test(org.junit.Test)

Example 3 with SubSelect

use of net.sf.jsqlparser.statement.select.SubSelect in project herddb by diennea.

the class CompiledInExpression method create.

public static CompiledInExpression create(InExpression in, String validatedTableAlias) {
    if (in.getLeftItemsList() != null) {
        throw new StatementExecutionException("Unsupported operand " + in.getClass() + " with a non-expression left argument (" + in + ")");
    }
    CompiledSQLExpression left = compileExpression(validatedTableAlias, in.getLeftExpression());
    if (left == null) {
        return null;
    }
    if (in.getRightItemsList() instanceof ExpressionList) {
        List<CompiledSQLExpression> expList = new ArrayList<>();
        ExpressionList exps = (ExpressionList) in.getRightItemsList();
        for (Expression exp : exps.getExpressions()) {
            CompiledSQLExpression newExp = compileExpression(validatedTableAlias, exp);
            if (newExp == null) {
                return null;
            }
            expList.add(newExp);
        }
        return new CompiledInExpression(in.isNot(), left, expList, null);
    }
    if (in.getRightItemsList() instanceof SubSelect) {
        SubSelect ss = (SubSelect) in.getRightItemsList();
        if (!(ss.getSelectBody() instanceof PlainSelect)) {
            throw new StatementExecutionException("unsupported operand " + in.getClass() + " with subquery of type " + ss.getClass() + "(" + ss + ")");
        }
        return new CompiledInExpression(in.isNot(), left, null, ss);
    }
    throw new StatementExecutionException("unsupported operand " + in.getClass() + " with argument of type " + in.getRightItemsList().getClass() + "(" + in + ")");
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) SQLExpressionCompiler.compileExpression(herddb.sql.expressions.SQLExpressionCompiler.compileExpression) ArrayList(java.util.ArrayList) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SubSelect(net.sf.jsqlparser.statement.select.SubSelect) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) StatementExecutionException(herddb.model.StatementExecutionException)

Example 4 with SubSelect

use of net.sf.jsqlparser.statement.select.SubSelect in project yyl_example by Relucent.

the class CountSqlParser method sqlToCount.

/**
 * 将 SQL 转换为 COUNT 查询
 */
public void sqlToCount(Select select) {
    SelectBody selectBody = select.getSelectBody();
    List<SelectItem> countItem = new ArrayList<SelectItem>();
    countItem.add(new SelectExpressionItem(new Column("count(*)")));
    if (selectBody instanceof PlainSelect && isSimpleCount((PlainSelect) selectBody)) {
        ((PlainSelect) selectBody).setSelectItems(countItem);
    } else {
        SubSelect subSelect = new SubSelect();
        subSelect.setSelectBody(selectBody);
        subSelect.setAlias(TABLE_ALIAS);
        PlainSelect plainSelect = new PlainSelect();
        plainSelect.setFromItem(subSelect);
        plainSelect.setSelectItems(countItem);
        select.setSelectBody(plainSelect);
    }
}
Also used : Column(net.sf.jsqlparser.schema.Column) SelectItem(net.sf.jsqlparser.statement.select.SelectItem) SelectExpressionItem(net.sf.jsqlparser.statement.select.SelectExpressionItem) ArrayList(java.util.ArrayList) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) SelectBody(net.sf.jsqlparser.statement.select.SelectBody) LateralSubSelect(net.sf.jsqlparser.statement.select.LateralSubSelect) SubSelect(net.sf.jsqlparser.statement.select.SubSelect)

Example 5 with SubSelect

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

SubSelect (net.sf.jsqlparser.statement.select.SubSelect)5 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)4 StatementExecutionException (herddb.model.StatementExecutionException)2 ArrayList (java.util.ArrayList)2 Column (net.sf.jsqlparser.schema.Column)2 LateralSubSelect (net.sf.jsqlparser.statement.select.LateralSubSelect)2 SelectBody (net.sf.jsqlparser.statement.select.SelectBody)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 SQLExpressionCompiler.compileExpression (herddb.sql.expressions.SQLExpressionCompiler.compileExpression)1 StringReader (java.io.StringReader)1 Expression (net.sf.jsqlparser.expression.Expression)1 ExpressionVisitorAdapter (net.sf.jsqlparser.expression.ExpressionVisitorAdapter)1 JdbcParameter (net.sf.jsqlparser.expression.JdbcParameter)1 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)1 InExpression (net.sf.jsqlparser.expression.operators.relational.InExpression)1 Table (net.sf.jsqlparser.schema.Table)1 Replace (net.sf.jsqlparser.statement.replace.Replace)1 FromItemVisitorAdapter (net.sf.jsqlparser.statement.select.FromItemVisitorAdapter)1