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);
}
}
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());
}
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());
}
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);
}
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());
}
Aggregations