use of com.actiontech.dble.route.parser.druid.ReplaceTemp in project dble by actiontech.
the class DruidReplaceParser method parserBatchInsert.
/**
* insert into .... select .... OR insert into table() values (),(),....
*
* @param schemaInfo SchemaInfo
* @param rrs RouteResultset
* @param partitionColumn partitionColumn
* @param replace MySqlReplaceStatement
* @throws SQLNonTransientException if the column size of values is not correct
*/
private void parserBatchInsert(SchemaInfo schemaInfo, RouteResultset rrs, String partitionColumn, MySqlReplaceStatement replace) throws SQLNonTransientException {
// insert into table() values (),(),....
SchemaConfig schema = schemaInfo.getSchemaConfig();
String tableName = schemaInfo.getTable();
// column number
int columnNum = getTableColumns(schemaInfo, replace.getColumns());
int shardingColIndex = tryGetShardingColIndex(schemaInfo, replace, partitionColumn);
List<SQLInsertStatement.ValuesClause> valueClauseList = replace.getValuesList();
Map<Integer, List<SQLInsertStatement.ValuesClause>> nodeValuesMap = new HashMap<>();
TableConfig tableConfig = schema.getTables().get(tableName);
AbstractPartitionAlgorithm algorithm = tableConfig.getRule().getRuleAlgorithm();
for (SQLInsertStatement.ValuesClause valueClause : valueClauseList) {
if (valueClause.getValues().size() != columnNum) {
String msg = "bad insert sql columnSize != valueSize:" + columnNum + " != " + valueClause.getValues().size() + "values:" + valueClause;
LOGGER.info(msg);
throw new SQLNonTransientException(msg);
}
SQLExpr expr = valueClause.getValues().get(shardingColIndex);
String shardingValue = shardingValueToSting(expr);
Integer nodeIndex = algorithm.calculate(shardingValue);
// no part find for this record
if (nodeIndex == null) {
String msg = "can't find any valid datanode :" + tableName + " -> " + partitionColumn + " -> " + shardingValue;
LOGGER.info(msg);
throw new SQLNonTransientException(msg);
}
if (nodeValuesMap.get(nodeIndex) == null) {
nodeValuesMap.put(nodeIndex, new ArrayList<SQLInsertStatement.ValuesClause>());
}
nodeValuesMap.get(nodeIndex).add(valueClause);
}
RouteResultsetNode[] nodes = new RouteResultsetNode[nodeValuesMap.size()];
int count = 0;
for (Map.Entry<Integer, List<SQLInsertStatement.ValuesClause>> node : nodeValuesMap.entrySet()) {
Integer nodeIndex = node.getKey();
List<SQLInsertStatement.ValuesClause> valuesList = node.getValue();
ReplaceTemp temp = new ReplaceTemp(replace);
temp.setValuesList(valuesList);
nodes[count] = new RouteResultsetNode(tableConfig.getDataNodes().get(nodeIndex), rrs.getSqlType(), RouterUtil.removeSchema(temp.toString(), schemaInfo.getSchema()));
count++;
}
rrs.setNodes(nodes);
rrs.setFinishedRoute(true);
}
Aggregations