use of io.reactivex.rxjava3.functions.Supplier in project Mycat2 by MyCATApache.
the class VertxExecuter method explainInsert.
@SneakyThrows
public static List<EachSQL> explainInsert(SQLInsertStatement statementArg, List<Object> paramArg) {
final SQLInsertStatement statement = statementArg.clone();
SQLInsertStatement template = statement.clone();
template.getColumns().clear();
template.getValuesList().clear();
SQLExprTableSource tableSource = statement.getTableSource();
String tableName = SQLUtils.normalize(tableSource.getTableName());
String schemaName = SQLUtils.normalize(tableSource.getSchema());
MetadataManager metadataManager = MetaClusterCurrent.wrapper(MetadataManager.class);
ShardingTable table = (ShardingTable) metadataManager.getTable(schemaName, tableName);
SimpleColumnInfo autoIncrementColumn = table.getAutoIncrementColumn();
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////
List<SQLName> columns = (List) statement.getColumns();
if (columns.isEmpty()) {
if (statement.getValues().getValues().size() == table.getColumns().size()) {
for (SimpleColumnInfo column : table.getColumns()) {
statement.addColumn(new SQLIdentifierExpr("`" + column.getColumnName() + "`"));
}
}
}
boolean fillAutoIncrement = needFillAutoIncrement(table, columns);
if (fillAutoIncrement) {
columns.add(new SQLIdentifierExpr(autoIncrementColumn.getColumnName()));
}
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////
Map<String, Integer> columnMap = new HashMap<>();
int index = 0;
for (SQLName column : columns) {
columnMap.put(SQLUtils.normalize(column.getSimpleName()), index);
++index;
}
List<List> paramsList = (!paramArg.isEmpty() && paramArg.get(0) instanceof List) ? (List) paramArg : Collections.singletonList(paramArg);
return paramsList.stream().flatMap(params -> {
List<EachSQL> sqls = new LinkedList<>();
for (SQLInsertStatement.ValuesClause valuesClause : statement.getValuesList()) {
valuesClause = valuesClause.clone();
SQLInsertStatement primaryStatement = template.clone();
primaryStatement.getColumns().addAll(columns);
primaryStatement.getValuesList().add(valuesClause);
List<SQLExpr> values = primaryStatement.getValues().getValues();
if (fillAutoIncrement) {
Supplier<Number> stringSupplier = table.nextSequence();
values.add(PreparedStatement.fromJavaObject(stringSupplier.get()));
}
Map<String, List<RangeVariable>> variables = compute(columns, values, params);
Partition mPartition = table.getShardingFuntion().calculateOne((Map) variables);
SQLExprTableSource exprTableSource = primaryStatement.getTableSource();
exprTableSource.setSimpleName(mPartition.getTable());
exprTableSource.setSchema(mPartition.getSchema());
sqls.add(new EachSQL(mPartition.getTargetName(), primaryStatement.toString(), getNewParams(params, primaryStatement)));
for (ShardingTable indexTable : table.getIndexTables()) {
// fillIndexTableShardingKeys(variables, indexTable);
Partition sPartition = indexTable.getShardingFuntion().calculateOne((Map) variables);
SQLInsertStatement eachStatement = template.clone();
eachStatement.getColumns().clear();
fillIndexTableShardingKeys(columnMap, values, indexTable.getColumns(), eachStatement);
SQLExprTableSource eachTableSource = eachStatement.getTableSource();
eachTableSource.setSimpleName(sPartition.getTable());
eachTableSource.setSchema(sPartition.getSchema());
sqls.add(new EachSQL(sPartition.getTargetName(), eachStatement.toString(), getNewParams(params, eachStatement)));
}
}
return sqls.stream();
}).collect(Collectors.toList());
}
Aggregations