Search in sources :

Example 1 with MultiExpressionList

use of net.sf.jsqlparser.expression.operators.relational.MultiExpressionList 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 MultiExpressionList

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

the class InsertDeParser method visit.

@Override
public void visit(MultiExpressionList multiExprList) {
    buffer.append(" VALUES ");
    for (Iterator<ExpressionList> it = multiExprList.getExprList().iterator(); it.hasNext(); ) {
        buffer.append("(");
        for (Iterator<Expression> iter = it.next().getExpressions().iterator(); iter.hasNext(); ) {
            Expression expression = iter.next();
            expression.accept(expressionVisitor);
            if (iter.hasNext()) {
                buffer.append(", ");
            }
        }
        buffer.append(")");
        if (it.hasNext()) {
            buffer.append(", ");
        }
    }
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 3 with MultiExpressionList

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

the class ReplaceDeParser method visit.

@Override
public void visit(MultiExpressionList multiExprList) {
    buffer.append("VALUES ");
    for (Iterator<ExpressionList> it = multiExprList.getExprList().iterator(); it.hasNext(); ) {
        buffer.append("(");
        for (Iterator<Expression> iter = it.next().getExpressions().iterator(); iter.hasNext(); ) {
            Expression expression = iter.next();
            expression.accept(expressionVisitor);
            if (iter.hasNext()) {
                buffer.append(", ");
            }
        }
        buffer.append(")");
        if (it.hasNext()) {
            buffer.append(", ");
        }
    }
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 4 with MultiExpressionList

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

the class UpsertDeParser method visit.

@Override
public void visit(MultiExpressionList multiExprList) {
    buffer.append(" VALUES ");
    for (Iterator<ExpressionList> it = multiExprList.getExprList().iterator(); it.hasNext(); ) {
        buffer.append("(");
        for (Iterator<Expression> iter = it.next().getExpressions().iterator(); iter.hasNext(); ) {
            Expression expression = iter.next();
            expression.accept(expressionVisitor);
            if (iter.hasNext()) {
                buffer.append(", ");
            }
        }
        buffer.append(")");
        if (it.hasNext()) {
            buffer.append(", ");
        }
    }
}
Also used : Expression(net.sf.jsqlparser.expression.Expression) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList)

Example 5 with MultiExpressionList

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

the class HerdDBCLI method rewriteQuery.

private static QueryWithParameters rewriteQuery(String query, TableSpaceMapper mapper, boolean frommysqldump) throws ScriptException {
    try {
        List<Object> parameters = new ArrayList<>();
        if (frommysqldump && query.startsWith("INSERT INTO")) {
            // this is faster than CCJSqlParserUtil and will allow the cache to work at "client-side" too
            QueryWithParameters rewriteSimpleInsertStatement = MySqlDumpInsertStatementRewriter.rewriteSimpleInsertStatement(query);
            if (rewriteSimpleInsertStatement != null) {
                query = rewriteSimpleInsertStatement.query;
                parameters.addAll(rewriteSimpleInsertStatement.jdbcParameters);
                String schema = mapper == null ? null : mapper.getTableSpace(rewriteSimpleInsertStatement.tableName);
                return new QueryWithParameters(query, rewriteSimpleInsertStatement.tableName, parameters, schema);
            }
        }
        String _query = query;
        net.sf.jsqlparser.statement.Statement stmt = PARSER_CACHE.get(_query, () -> {
            return CCJSqlParserUtil.parse(_query);
        });
        if (stmt instanceof Insert) {
            boolean somethingdone = false;
            Insert insert = (Insert) stmt;
            ItemsList itemlist = insert.getItemsList();
            if (itemlist instanceof ExpressionList) {
                ExpressionList list = (ExpressionList) itemlist;
                List<Expression> expressions = list.getExpressions();
                for (int i = 0; i < expressions.size(); i++) {
                    Expression e = expressions.get(i);
                    boolean done = false;
                    if (e instanceof StringValue) {
                        StringValue sv = (StringValue) e;
                        parameters.add(sv.getValue());
                        done = true;
                    } else if (e instanceof LongValue) {
                        LongValue sv = (LongValue) e;
                        parameters.add(sv.getValue());
                        done = true;
                    } else if (e instanceof NullValue) {
                        NullValue sv = (NullValue) e;
                        parameters.add(null);
                        done = true;
                    } else if (e instanceof TimestampValue) {
                        TimestampValue sv = (TimestampValue) e;
                        parameters.add(sv.getValue());
                        done = true;
                    } else if (e instanceof DoubleValue) {
                        DoubleValue sv = (DoubleValue) e;
                        parameters.add(sv.getValue());
                        done = true;
                    }
                    if (done) {
                        somethingdone = true;
                        expressions.set(i, new JdbcParameter());
                    }
                }
                if (somethingdone) {
                    StringBuilder queryResult = new StringBuilder();
                    InsertDeParser deparser = new InsertDeParser(new ExpressionDeParser(null, queryResult), null, queryResult);
                    deparser.deParse(insert);
                    query = queryResult.toString();
                }
            } else if (itemlist instanceof MultiExpressionList) {
                MultiExpressionList mlist = (MultiExpressionList) itemlist;
                List<ExpressionList> lists = mlist.getExprList();
                for (ExpressionList list : lists) {
                    List<Expression> expressions = list.getExpressions();
                    for (int i = 0; i < expressions.size(); i++) {
                        Expression e = expressions.get(i);
                        boolean done = false;
                        if (e instanceof StringValue) {
                            StringValue sv = (StringValue) e;
                            parameters.add(sv.getValue());
                            done = true;
                        } else if (e instanceof LongValue) {
                            LongValue sv = (LongValue) e;
                            parameters.add(sv.getValue());
                            done = true;
                        } else if (e instanceof NullValue) {
                            NullValue sv = (NullValue) e;
                            parameters.add(null);
                            done = true;
                        } else if (e instanceof TimestampValue) {
                            TimestampValue sv = (TimestampValue) e;
                            parameters.add(sv.getValue());
                            done = true;
                        } else if (e instanceof DoubleValue) {
                            DoubleValue sv = (DoubleValue) e;
                            parameters.add(sv.getValue());
                            done = true;
                        }
                        if (done) {
                            somethingdone = true;
                            expressions.set(i, new JdbcParameter());
                        }
                    }
                }
                if (somethingdone) {
                    StringBuilder queryResult = new StringBuilder();
                    InsertDeParser deparser = new InsertDeParser(new ExpressionDeParser(null, queryResult), null, queryResult);
                    deparser.deParse(insert);
                    query = queryResult.toString();
                }
            }
            String schema = mapper == null ? null : mapper.getTableSpace(stmt);
            return new QueryWithParameters(query, null, parameters, schema);
        } else {
            String schema = mapper == null ? null : mapper.getTableSpace(stmt);
            return new QueryWithParameters(query, null, Collections.emptyList(), schema);
        }
    } catch (ExecutionException err) {
        System.out.println("error for query: " + query + " -> " + err.getCause());
        return null;
    }
}
Also used : ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) InsertDeParser(net.sf.jsqlparser.util.deparser.InsertDeParser) ArrayList(java.util.ArrayList) Insert(net.sf.jsqlparser.statement.insert.Insert) NullValue(net.sf.jsqlparser.expression.NullValue) TimestampValue(net.sf.jsqlparser.expression.TimestampValue) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) List(java.util.List) ItemsList(net.sf.jsqlparser.expression.operators.relational.ItemsList) ArrayList(java.util.ArrayList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) StringValue(net.sf.jsqlparser.expression.StringValue) ExecutionException(java.util.concurrent.ExecutionException) MultiExpressionList(net.sf.jsqlparser.expression.operators.relational.MultiExpressionList) ExpressionList(net.sf.jsqlparser.expression.operators.relational.ExpressionList) JdbcParameter(net.sf.jsqlparser.expression.JdbcParameter) Expression(net.sf.jsqlparser.expression.Expression) DoubleValue(net.sf.jsqlparser.expression.DoubleValue) LongValue(net.sf.jsqlparser.expression.LongValue) ExpressionDeParser(net.sf.jsqlparser.util.deparser.ExpressionDeParser)

Aggregations

Expression (net.sf.jsqlparser.expression.Expression)5 ExpressionList (net.sf.jsqlparser.expression.operators.relational.ExpressionList)5 MultiExpressionList (net.sf.jsqlparser.expression.operators.relational.MultiExpressionList)5 ArrayList (java.util.ArrayList)2 List (java.util.List)2 JdbcParameter (net.sf.jsqlparser.expression.JdbcParameter)2 ItemsList (net.sf.jsqlparser.expression.operators.relational.ItemsList)2 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 Table (herddb.model.Table)1 InsertStatement (herddb.model.commands.InsertStatement)1 CompiledSQLExpression (herddb.sql.expressions.CompiledSQLExpression)1 IntHolder (herddb.utils.IntHolder)1 ExecutionException (java.util.concurrent.ExecutionException)1