use of herddb.sql.expressions.CompiledFunction in project herddb by diennea.
the class JSQLParserPlanner method planerInsertOrUpsert.
private ExecutionPlan planerInsertOrUpsert(String defaultTableSpace, net.sf.jsqlparser.schema.Table table, List<net.sf.jsqlparser.schema.Column> columns, ItemsList itemsList, boolean returnValues, boolean upsert) throws StatementExecutionException, StatementNotSupportedException {
OpSchema inputSchema = getTableSchema(defaultTableSpace, table);
TableSpaceManager tableSpaceManager = manager.getTableSpaceManager(inputSchema.tableSpace);
AbstractTableManager tableManager = tableSpaceManager.getTableManager(inputSchema.name);
Table tableImpl = tableManager.getTable();
List<CompiledSQLExpression> keyValueExpression = new ArrayList<>();
List<String> keyExpressionToColumn = new ArrayList<>();
List<CompiledSQLExpression> valuesExpressions = new ArrayList<>();
List<String> valuesColumns = new ArrayList<>();
boolean invalid = false;
int index = 0;
if (columns == null) {
// INSERT INTO TABLE VALUES (xxxx) (no column list)
columns = new ArrayList<>();
for (Column c : tableImpl.getColumns()) {
columns.add(new net.sf.jsqlparser.schema.Column(c.name));
}
}
if (itemsList instanceof ExpressionList) {
List<Expression> values = ((ExpressionList) itemsList).getExpressions();
for (net.sf.jsqlparser.schema.Column column : columns) {
CompiledSQLExpression exp = SQLParserExpressionCompiler.compileExpression(values.get(index), inputSchema);
String columnName = fixMySqlBackTicks(column.getColumnName()).toLowerCase();
if (exp instanceof ConstantExpression || exp instanceof JdbcParameterExpression || exp instanceof TypedJdbcParameterExpression || exp instanceof CompiledFunction) {
boolean isAlwaysNull = (exp instanceof ConstantExpression) && ((ConstantExpression) exp).isNull();
if (!isAlwaysNull) {
if (tableImpl.isPrimaryKeyColumn(columnName)) {
keyExpressionToColumn.add(columnName);
keyValueExpression.add(exp);
}
Column columnFromSchema = tableImpl.getColumn(columnName);
if (columnFromSchema == null) {
throw new StatementExecutionException("Column '" + columnName + "' not found in table " + tableImpl.name);
}
valuesColumns.add(columnFromSchema.name);
valuesExpressions.add(exp);
}
index++;
} else {
checkSupported(false, "Unsupported expression type " + exp.getClass().getName());
break;
}
}
// handle default values
for (Column col : tableImpl.getColumns()) {
if (!valuesColumns.contains(col.name)) {
if (col.defaultValue != null) {
valuesColumns.add(col.name);
CompiledSQLExpression defaultValueExpression = makeDefaultValue(col);
valuesExpressions.add(defaultValueExpression);
} else if (ColumnTypes.isNotNullDataType(col.type) && !tableImpl.auto_increment) {
throw new StatementExecutionException("Column '" + col.name + "' has no default value and does not allow NULLs");
}
}
}
DMLStatement statement;
if (!invalid) {
RecordFunction keyfunction;
if (keyValueExpression.isEmpty() && tableImpl.auto_increment) {
keyfunction = new AutoIncrementPrimaryKeyRecordFunction();
} else {
if (keyValueExpression.size() != tableImpl.primaryKey.length) {
throw new StatementExecutionException("you must set a value for the primary key (expressions=" + keyValueExpression.size() + ")");
}
keyfunction = new SQLRecordKeyFunction(keyExpressionToColumn, keyValueExpression, tableImpl);
}
RecordFunction valuesfunction = new SQLRecordFunction(valuesColumns, tableImpl, valuesExpressions);
statement = new InsertStatement(inputSchema.tableSpace, inputSchema.name, keyfunction, valuesfunction, upsert).setReturnValues(returnValues);
} else {
throw new StatementNotSupportedException();
}
PlannerOp op = new SimpleInsertOp(statement.setReturnValues(returnValues));
return optimizePlan(op);
} else if (itemsList instanceof MultiExpressionList) {
List<ExpressionList> records = ((MultiExpressionList) itemsList).getExprList();
ValuesOp values = planValuesForInsertOp(columns, tableImpl, inputSchema, records);
InsertOp op = new InsertOp(tableImpl.tablespace, tableImpl.name, values, returnValues, upsert);
return optimizePlan(op);
} else {
checkSupported(false);
return null;
}
}
Aggregations