Search in sources :

Example 1 with ItemsList

use of net.sf.jsqlparser.expression.operators.relational.ItemsList in project herddb by diennea.

the class SQLPlanner method buildInsertStatement.

private ExecutionPlan buildInsertStatement(String defaultTableSpace, Insert s, boolean returnValues) throws StatementExecutionException {
    String tableSpace = s.getTable().getSchemaName();
    String tableName = s.getTable().getName();
    if (tableSpace == null) {
        tableSpace = defaultTableSpace;
    }
    TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(tableSpace);
    if (tableSpaceManager == null) {
        throw new StatementExecutionException("no such tablespace " + tableSpace + " here at " + manager.getNodeId());
    }
    AbstractTableManager tableManager = tableSpaceManager.getTableManager(tableName);
    if (tableManager == null) {
        throw new StatementExecutionException("no such table " + tableName + " in tablespace " + tableSpace);
    }
    Table table = tableManager.getTable();
    ItemsList itemlist = s.getItemsList();
    if (itemlist instanceof ExpressionList) {
        int index = 0;
        List<Expression> keyValueExpression = new ArrayList<>();
        List<String> keyExpressionToColumn = new ArrayList<>();
        List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
        List<net.sf.jsqlparser.schema.Column> valuesColumns = new ArrayList<>();
        ExpressionList list = (ExpressionList) itemlist;
        if (s.getColumns() != null) {
            for (net.sf.jsqlparser.schema.Column c : s.getColumns()) {
                Column column = table.getColumn(c.getColumnName());
                if (column == null) {
                    throw new StatementExecutionException("no such column " + c.getColumnName() + " in table " + tableName + " in tablespace " + tableSpace);
                }
                Expression expression;
                try {
                    expression = list.getExpressions().get(index++);
                } catch (IndexOutOfBoundsException badQuery) {
                    throw new StatementExecutionException("bad number of VALUES in INSERT clause");
                }
                if (table.isPrimaryKeyColumn(column.name)) {
                    keyExpressionToColumn.add(column.name);
                    keyValueExpression.add(expression);
                }
                valuesColumns.add(c);
                valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, expression));
            }
        } else {
            for (Column column : table.columns) {
                Expression expression = list.getExpressions().get(index++);
                if (table.isPrimaryKeyColumn(column.name)) {
                    keyExpressionToColumn.add(column.name);
                    keyValueExpression.add(expression);
                }
                valuesColumns.add(new net.sf.jsqlparser.schema.Column(column.name));
                valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, expression));
            }
        }
        RecordFunction keyfunction;
        if (keyValueExpression.isEmpty() && table.auto_increment) {
            keyfunction = new AutoIncrementPrimaryKeyRecordFunction();
        } else {
            if (keyValueExpression.size() != table.primaryKey.length) {
                throw new StatementExecutionException("you must set a value for the primary key (expressions=" + keyValueExpression.size() + ")");
            }
            keyfunction = new SQLRecordKeyFunction(table, keyExpressionToColumn, keyValueExpression);
        }
        RecordFunction valuesfunction = new SQLRecordFunction(table, valuesColumns, valuesExpressions);
        try {
            return ExecutionPlan.simple(new InsertStatement(tableSpace, tableName, keyfunction, valuesfunction).setReturnValues(returnValues));
        } catch (IllegalArgumentException err) {
            throw new StatementExecutionException(err);
        }
    } else if (itemlist instanceof MultiExpressionList) {
        if (returnValues) {
            throw new StatementExecutionException("cannot 'return values' on multi-values insert");
        }
        MultiExpressionList multilist = (MultiExpressionList) itemlist;
        List<InsertStatement> inserts = new ArrayList<>();
        for (ExpressionList list : multilist.getExprList()) {
            List<Expression> keyValueExpression = new ArrayList<>();
            List<String> keyExpressionToColumn = new ArrayList<>();
            List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
            List<net.sf.jsqlparser.schema.Column> valuesColumns = new ArrayList<>();
            int index = 0;
            if (s.getColumns() != null) {
                for (net.sf.jsqlparser.schema.Column c : s.getColumns()) {
                    Column column = table.getColumn(c.getColumnName());
                    if (column == null) {
                        throw new StatementExecutionException("no such column " + c.getColumnName() + " in table " + tableName + " in tablespace " + tableSpace);
                    }
                    Expression expression;
                    try {
                        expression = list.getExpressions().get(index++);
                    } catch (IndexOutOfBoundsException badQuery) {
                        throw new StatementExecutionException("bad number of VALUES in INSERT clause");
                    }
                    if (table.isPrimaryKeyColumn(column.name)) {
                        keyExpressionToColumn.add(column.name);
                        keyValueExpression.add(expression);
                    }
                    valuesColumns.add(c);
                    valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, expression));
                }
            } else {
                for (Column column : table.columns) {
                    Expression expression = list.getExpressions().get(index++);
                    if (table.isPrimaryKeyColumn(column.name)) {
                        keyExpressionToColumn.add(column.name);
                        keyValueExpression.add(expression);
                    }
                    valuesColumns.add(new net.sf.jsqlparser.schema.Column(column.name));
                    valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, expression));
                }
            }
            RecordFunction keyfunction;
            if (keyValueExpression.isEmpty() && table.auto_increment) {
                keyfunction = new AutoIncrementPrimaryKeyRecordFunction();
            } else {
                if (keyValueExpression.size() != table.primaryKey.length) {
                    throw new StatementExecutionException("you must set a value for the primary key (expressions=" + keyValueExpression.size() + ")");
                }
                keyfunction = new SQLRecordKeyFunction(table, keyExpressionToColumn, keyValueExpression);
            }
            RecordFunction valuesfunction = new SQLRecordFunction(table, valuesColumns, valuesExpressions);
            InsertStatement insert = new InsertStatement(tableSpace, tableName, keyfunction, valuesfunction);
            inserts.add(insert);
        }
        try {
            return ExecutionPlan.multiInsert(inserts);
        } catch (IllegalArgumentException err) {
            throw new StatementExecutionException(err);
        }
    } else {
        List<Expression> keyValueExpression = new ArrayList<>();
        List<String> keyExpressionToColumn = new ArrayList<>();
        List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
        List<net.sf.jsqlparser.schema.Column> valuesColumns = new ArrayList<>();
        Select select = s.getSelect();
        ExecutionPlan datasource = buildSelectStatement(defaultTableSpace, select, true, -1);
        if (s.getColumns() == null) {
            throw new StatementExecutionException("for INSERT ... SELECT you have to declare the columns to be filled in (use INSERT INTO TABLE(c,c,c,) SELECT .....)");
        }
        IntHolder holder = new IntHolder(1);
        for (net.sf.jsqlparser.schema.Column c : s.getColumns()) {
            Column column = table.getColumn(c.getColumnName());
            if (column == null) {
                throw new StatementExecutionException("no such column " + c.getColumnName() + " in table " + tableName + " in tablespace " + tableSpace);
            }
            JdbcParameter readFromResultSetAsJdbcParameter = new JdbcParameter();
            readFromResultSetAsJdbcParameter.setIndex(holder.value++);
            if (table.isPrimaryKeyColumn(column.name)) {
                keyExpressionToColumn.add(column.name);
                keyValueExpression.add(readFromResultSetAsJdbcParameter);
            }
            valuesColumns.add(c);
            valuesExpressions.add(SQLExpressionCompiler.compileExpression(null, readFromResultSetAsJdbcParameter));
        }
        RecordFunction keyfunction;
        if (keyValueExpression.isEmpty() && table.auto_increment) {
            keyfunction = new AutoIncrementPrimaryKeyRecordFunction();
        } else {
            if (keyValueExpression.size() != table.primaryKey.length) {
                throw new StatementExecutionException("you must set a value for the primary key (expressions=" + keyValueExpression.size() + ")");
            }
            keyfunction = new SQLRecordKeyFunction(table, keyExpressionToColumn, keyValueExpression);
        }
        RecordFunction valuesfunction = new SQLRecordFunction(table, valuesColumns, valuesExpressions);
        try {
            return ExecutionPlan.dataManipulationFromSelect(new InsertStatement(tableSpace, tableName, keyfunction, valuesfunction).setReturnValues(returnValues), datasource);
        } catch (IllegalArgumentException err) {
            throw new StatementExecutionException(err);
        }
    }
}
Also used : ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) StatementExecutionException(herddb.model.StatementExecutionException) InsertStatement(herddb.model.commands.InsertStatement) ExecutionPlan(herddb.model.ExecutionPlan) Column(herddb.model.Column) TableSpaceManager(herddb.core.TableSpaceManager) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) IntHolder(herddb.utils.IntHolder) ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) ColumnsList(herddb.model.ColumnsList) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) List(java.util.List) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) Table(herddb.model.Table) CreateTable(net.sf.jsqlparser.statement.create.table.CreateTable) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) AbstractTableManager(herddb.core.AbstractTableManager) Expression(net.sf.jsqlparser.expression.Expression) AlterExpression(net.sf.jsqlparser.statement.alter.AlterExpression) BinaryExpression(net.sf.jsqlparser.expression.BinaryExpression) AndExpression(net.sf.jsqlparser.expression.operators.conditional.AndExpression) SignedExpression(net.sf.jsqlparser.expression.SignedExpression) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction)

Example 2 with ItemsList

use of net.sf.jsqlparser.expression.operators.relational.ItemsList in project JSqlParser by JSQLParser.

the class StatementDeParserTest method shouldUseProvidedDeParsersWhenDeParsingReplaceWithItemsList.

@Test
@SuppressWarnings("PMD.JUnitTestsShouldIncludeAssert")
public void shouldUseProvidedDeParsersWhenDeParsingReplaceWithItemsList() {
    Replace replace = new Replace();
    Table table = new Table();
    ItemsList itemsList = mock(ItemsList.class);
    replace.setTable(table);
    replace.setItemsList(itemsList);
    statementDeParser.visit(replace);
    then(itemsList).should().accept(argThat(is(replaceDeParserWithDeParsers(equalTo(expressionDeParser), equalTo(selectDeParser)))));
}
Also used : Replace(net.sf.jsqlparser.statement.replace.Replace) Table(net.sf.jsqlparser.schema.Table) ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) Test(org.junit.Test)

Example 3 with ItemsList

use of net.sf.jsqlparser.expression.operators.relational.ItemsList in project JSqlParser by JSQLParser.

the class ExpressionVisitorAdapterTest method testInExpressionProblem.

@Test
public void testInExpressionProblem() throws JSQLParserException {
    final List exprList = new ArrayList();
    Select select = (Select) CCJSqlParserUtil.parse("select * from foo where x in (?,?,?)");
    PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
    Expression where = plainSelect.getWhere();
    where.accept(new ExpressionVisitorAdapter() {

        @Override
        public void visit(InExpression expr) {
            super.visit(expr);
            exprList.add(expr.getLeftExpression());
            exprList.add(expr.getLeftItemsList());
            exprList.add(expr.getRightItemsList());
        }
    });
    assertTrue(exprList.get(0) instanceof Expression);
    assertNull(exprList.get(1));
    assertTrue(exprList.get(2) instanceof ItemsList);
}
Also used : ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) ArrayList(java.util.ArrayList) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) ArrayList(java.util.ArrayList) List(java.util.List) ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) Test(org.junit.Test)

Example 4 with ItemsList

use of net.sf.jsqlparser.expression.operators.relational.ItemsList in project spanner-jdbc by olavloite.

the class CloudSpannerPreparedStatement method createInsertMutation.

private Mutation createInsertMutation(Insert insert, boolean generateParameterMetaData) throws SQLException {
    ItemsList items = insert.getItemsList();
    if (generateParameterMetaData && items == null && insert.getSelect() != null) {
        // Just initialize the parameter meta data of the select statement
        createSelectBuilder(insert.getSelect(), insert.getSelect().toString());
        return null;
    }
    if (!(items instanceof ExpressionList)) {
        throw new CloudSpannerSQLException("Insert statement must specify a list of values", Code.INVALID_ARGUMENT);
    }
    if (insert.getColumns() == null || insert.getColumns().isEmpty()) {
        throw new CloudSpannerSQLException("Insert statement must specify a list of column names", Code.INVALID_ARGUMENT);
    }
    List<Expression> expressions = ((ExpressionList) items).getExpressions();
    String table = unquoteIdentifier(insert.getTable().getFullyQualifiedName());
    getParameterStore().setTable(table);
    WriteBuilder builder;
    if (insert.isUseDuplicate()) {
        /**
         * Do an insert-or-update. BUT: Cloud Spanner does not support
         * supplying different values for the insert and update statements,
         * meaning that only the values specified in the INSERT part of the
         * statement will be considered. Anything specified in the 'ON
         * DUPLICATE KEY UPDATE ...' statement will be ignored.
         */
        if (this.forceUpdate)
            builder = Mutation.newUpdateBuilder(table);
        else
            builder = Mutation.newInsertOrUpdateBuilder(table);
    } else {
        /**
         * Just do an insert and throw an error if a row with the specified
         * key alread exists.
         */
        builder = Mutation.newInsertBuilder(table);
    }
    int index = 0;
    for (Column col : insert.getColumns()) {
        String columnName = unquoteIdentifier(col.getFullyQualifiedName());
        expressions.get(index).accept(new ValueBinderExpressionVisitorAdapter<>(getParameterStore(), builder.set(columnName), columnName));
        index++;
    }
    return builder.build();
}
Also used : ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) WriteBuilder(com.google.cloud.spanner.Mutation.WriteBuilder) CloudSpannerSQLException(nl.topicus.jdbc.exception.CloudSpannerSQLException) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 5 with ItemsList

use of net.sf.jsqlparser.expression.operators.relational.ItemsList in project JSqlParser by JSQLParser.

the class ExpressionVisitorAdapterTest method testInExpression.

@Test
public void testInExpression() throws JSQLParserException {
    final List exprList = new ArrayList();
    Select select = (Select) CCJSqlParserUtil.parse("select * from foo where (a,b) in (select a,b from foo2)");
    PlainSelect plainSelect = (PlainSelect) select.getSelectBody();
    Expression where = plainSelect.getWhere();
    where.accept(new ExpressionVisitorAdapter() {

        @Override
        public void visit(InExpression expr) {
            super.visit(expr);
            exprList.add(expr.getLeftExpression());
            exprList.add(expr.getLeftItemsList());
            exprList.add(expr.getRightItemsList());
        }
    });
    assertNull(exprList.get(0));
    assertTrue(exprList.get(1) instanceof ItemsList);
    assertTrue(exprList.get(2) instanceof ItemsList);
}
Also used : ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) InExpression(net.sf.jsqlparser.expression.operators.relational.InExpression) ArrayList(java.util.ArrayList) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) Select(net.sf.jsqlparser.statement.select.Select) PlainSelect(net.sf.jsqlparser.statement.select.PlainSelect) ArrayList(java.util.ArrayList) List(java.util.List) ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) Test(org.junit.Test)

Aggregations

ItemsList (net.sf.jsqlparser.expression.operators.relational.ItemsList)6 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Expression (net.sf.jsqlparser.expression.Expression)3 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)3 PlainSelect (net.sf.jsqlparser.statement.select.PlainSelect)3 Select (net.sf.jsqlparser.statement.select.Select)3 Test (org.junit.Test)3 JdbcParameter (net.sf.jsqlparser.expression.JdbcParameter)2 InExpression (net.sf.jsqlparser.expression.operators.relational.InExpression)2 MultiExpressionList (net.sf.jsqlparser.expression.operators.relational.MultiExpressionList)2 WriteBuilder (com.google.cloud.spanner.Mutation.WriteBuilder)1 AbstractTableManager (herddb.core.AbstractTableManager)1 TableSpaceManager (herddb.core.TableSpaceManager)1 AutoIncrementPrimaryKeyRecordFunction (herddb.model.AutoIncrementPrimaryKeyRecordFunction)1 Column (herddb.model.Column)1 ColumnsList (herddb.model.ColumnsList)1 ExecutionPlan (herddb.model.ExecutionPlan)1 RecordFunction (herddb.model.RecordFunction)1 StatementExecutionException (herddb.model.StatementExecutionException)1