Search in sources :

Example 46 with InsertStatement

use of herddb.model.commands.InsertStatement in project herddb by diennea.

the class CalcitePlanner method planInsert.

private PlannerOp planInsert(EnumerableTableModify dml, boolean returnValues) {
    final String tableSpace = dml.getTable().getQualifiedName().get(0);
    final String tableName = dml.getTable().getQualifiedName().get(1);
    DMLStatement statement = null;
    if (dml.getInput() instanceof EnumerableProject) {
        // fastest path for insert into TABLE(s,b,c) values(?,?,?)
        EnumerableProject project = (EnumerableProject) dml.getInput();
        if (project.getInput() instanceof EnumerableValues) {
            EnumerableValues values = (EnumerableValues) project.getInput();
            if (values.getTuples().size() == 1) {
                final TableImpl tableImpl = (TableImpl) dml.getTable().unwrap(org.apache.calcite.schema.Table.class);
                Table table = tableImpl.tableManager.getTable();
                int index = 0;
                List<RexNode> projects = project.getProjects();
                List<CompiledSQLExpression> keyValueExpression = new ArrayList<>();
                List<String> keyExpressionToColumn = new ArrayList<>();
                List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
                List<String> valuesColumns = new ArrayList<>();
                boolean invalid = false;
                for (Column column : table.getColumns()) {
                    CompiledSQLExpression exp = SQLExpressionCompiler.compileExpression(projects.get(index));
                    if (exp instanceof ConstantExpression || exp instanceof JdbcParameterExpression || exp instanceof TypedJdbcParameterExpression) {
                        boolean isAlwaysNull = (exp instanceof ConstantExpression) && ((ConstantExpression) exp).isNull();
                        if (!isAlwaysNull) {
                            if (table.isPrimaryKeyColumn(column.name)) {
                                keyExpressionToColumn.add(column.name);
                                keyValueExpression.add(exp);
                            }
                            valuesColumns.add(column.name);
                            valuesExpressions.add(exp);
                        }
                        index++;
                    } else {
                        invalid = true;
                        break;
                    }
                }
                if (!invalid) {
                    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(keyExpressionToColumn, keyValueExpression, table);
                    }
                    RecordFunction valuesfunction = new SQLRecordFunction(valuesColumns, table, valuesExpressions);
                    statement = new InsertStatement(tableSpace, tableName, keyfunction, valuesfunction).setReturnValues(returnValues);
                }
            }
        }
    }
    if (statement != null) {
        return new SimpleInsertOp(statement);
    }
    PlannerOp input = convertRelNode(dml.getInput(), null, false);
    try {
        return new InsertOp(tableSpace, tableName, input, returnValues);
    } catch (IllegalArgumentException err) {
        throw new StatementExecutionException(err);
    }
}
Also used : ConstantExpression(herddb.sql.expressions.ConstantExpression) ArrayList(java.util.ArrayList) CompiledSQLExpression(herddb.sql.expressions.CompiledSQLExpression) StatementExecutionException(herddb.model.StatementExecutionException) InsertStatement(herddb.model.commands.InsertStatement) SimpleInsertOp(herddb.model.planner.SimpleInsertOp) Column(herddb.model.Column) RecordFunction(herddb.model.RecordFunction) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) Table(herddb.model.Table) RelOptTable(org.apache.calcite.plan.RelOptTable) ProjectableFilterableTable(org.apache.calcite.schema.ProjectableFilterableTable) ScannableTable(org.apache.calcite.schema.ScannableTable) AbstractTable(org.apache.calcite.schema.impl.AbstractTable) ModifiableTable(org.apache.calcite.schema.ModifiableTable) PlannerOp(herddb.model.planner.PlannerOp) JdbcParameterExpression(herddb.sql.expressions.JdbcParameterExpression) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) EnumerableProject(org.apache.calcite.adapter.enumerable.EnumerableProject) TypedJdbcParameterExpression(herddb.sql.expressions.TypedJdbcParameterExpression) EnumerableValues(org.apache.calcite.adapter.enumerable.EnumerableValues) DMLStatement(herddb.model.DMLStatement) AutoIncrementPrimaryKeyRecordFunction(herddb.model.AutoIncrementPrimaryKeyRecordFunction) RexNode(org.apache.calcite.rex.RexNode) SimpleInsertOp(herddb.model.planner.SimpleInsertOp) InsertOp(herddb.model.planner.InsertOp)

Example 47 with InsertStatement

use of herddb.model.commands.InsertStatement in project herddb by diennea.

the class AutoTransactionTest method testAutoTransactionOnErrorScan.

@Test
public void testAutoTransactionOnErrorScan() throws Exception {
    int i = 1;
    Map<String, Object> data = new HashMap<>();
    Bytes key = Bytes.from_string("key_" + i);
    data.put("id", "key_" + i);
    data.put("number", i);
    Record record = RecordSerializer.toRecord(data, table);
    InsertStatement st = new InsertStatement(tableSpace, tableName, record);
    DMLStatementExecutionResult executeUpdate = manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    long tx;
    try (DataScanner scan = manager.scan(new ScanStatement(tableSpace, "no_table", Projection.IDENTITY(table.columnNames, table.getColumns()), null, null, null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.AUTOTRANSACTION_TRANSACTION)) {
        fail();
    } catch (TableDoesNotExistException ok) {
    }
    assertTrue(manager.getTableSpaceManager(tableSpace).getOpenTransactions().isEmpty());
}
Also used : HashMap(java.util.HashMap) InsertStatement(herddb.model.commands.InsertStatement) TableDoesNotExistException(herddb.model.TableDoesNotExistException) Bytes(herddb.utils.Bytes) DataScanner(herddb.model.DataScanner) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) Record(herddb.model.Record) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Example 48 with InsertStatement

use of herddb.model.commands.InsertStatement in project herddb by diennea.

the class AutoTransactionTest method testAutoTransactionOnGetWithError.

@Test
public void testAutoTransactionOnGetWithError() throws Exception {
    int i = 1;
    Map<String, Object> data = new HashMap<>();
    Bytes key = Bytes.from_string("key_" + i);
    data.put("id", "key_" + i);
    data.put("number", i);
    Record record = RecordSerializer.toRecord(data, table);
    InsertStatement st = new InsertStatement(tableSpace, tableName, record);
    manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    try {
        GetResult get = manager.get(new GetStatement(tableSpace, "no_table_exists", key, null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
        fail();
    } catch (TableDoesNotExistException ok) {
    }
    assertTrue(manager.getTableSpaceManager(tableSpace).getOpenTransactions().isEmpty());
}
Also used : TableDoesNotExistException(herddb.model.TableDoesNotExistException) Bytes(herddb.utils.Bytes) GetResult(herddb.model.GetResult) HashMap(java.util.HashMap) GetStatement(herddb.model.commands.GetStatement) Record(herddb.model.Record) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Example 49 with InsertStatement

use of herddb.model.commands.InsertStatement in project herddb by diennea.

the class AutoTransactionTest method testAutoTransactionOnInsert.

@Test
public void testAutoTransactionOnInsert() throws Exception {
    int i = 1;
    Map<String, Object> data = new HashMap<>();
    data.put("id", "key_" + i);
    data.put("number", i);
    Record record = RecordSerializer.toRecord(data, table);
    InsertStatement st = new InsertStatement(tableSpace, tableName, record);
    DMLStatementExecutionResult executeUpdate = manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
    long tx = executeUpdate.transactionId;
    TestUtils.commitTransaction(manager, tableSpace, tx);
}
Also used : HashMap(java.util.HashMap) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) Record(herddb.model.Record) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Example 50 with InsertStatement

use of herddb.model.commands.InsertStatement in project herddb by diennea.

the class AutoTransactionTest method testAutoTransactionOnFailedInsert.

@Test
public void testAutoTransactionOnFailedInsert() throws Exception {
    int i = 1;
    {
        Map<String, Object> data = new HashMap<>();
        data.put("number", i);
        data.put("id", i);
        Record record = RecordSerializer.toRecord(data, table);
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
    }
    try {
        Map<String, Object> data = new HashMap<>();
        data.put("number", i);
        data.put("id", i);
        Record record = RecordSerializer.toRecord(data, table);
        InsertStatement st = new InsertStatement(tableSpace, tableName, record);
        manager.executeUpdate(st, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.AUTOTRANSACTION_TRANSACTION);
        fail();
    } catch (DuplicatePrimaryKeyException ok) {
    }
    assertTrue(manager.getTableSpaceManager(tableSpace).getOpenTransactions().isEmpty());
}
Also used : HashMap(java.util.HashMap) DuplicatePrimaryKeyException(herddb.model.DuplicatePrimaryKeyException) Record(herddb.model.Record) HashMap(java.util.HashMap) Map(java.util.Map) InsertStatement(herddb.model.commands.InsertStatement) Test(org.junit.Test)

Aggregations

InsertStatement (herddb.model.commands.InsertStatement)78 Test (org.junit.Test)73 Record (herddb.model.Record)53 GetStatement (herddb.model.commands.GetStatement)51 GetResult (herddb.model.GetResult)50 Table (herddb.model.Table)50 CreateTableStatement (herddb.model.commands.CreateTableStatement)48 Bytes (herddb.utils.Bytes)39 TransactionContext (herddb.model.TransactionContext)38 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)28 Path (java.nio.file.Path)25 FileDataStorageManager (herddb.file.FileDataStorageManager)24 FileCommitLogManager (herddb.file.FileCommitLogManager)23 FileMetadataStorageManager (herddb.file.FileMetadataStorageManager)23 DeleteStatement (herddb.model.commands.DeleteStatement)21 TransactionResult (herddb.model.TransactionResult)19 BeginTransactionStatement (herddb.model.commands.BeginTransactionStatement)19 CommitTransactionStatement (herddb.model.commands.CommitTransactionStatement)17 UpdateStatement (herddb.model.commands.UpdateStatement)16 DataScanner (herddb.model.DataScanner)15