Search in sources :

Example 1 with ReplaceTemp

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);
}
Also used : AbstractPartitionAlgorithm(com.actiontech.dble.route.function.AbstractPartitionAlgorithm) SchemaConfig(com.actiontech.dble.config.model.SchemaConfig) ReplaceTemp(com.actiontech.dble.route.parser.druid.ReplaceTemp) SQLExpr(com.alibaba.druid.sql.ast.SQLExpr) SQLNonTransientException(java.sql.SQLNonTransientException) RouteResultsetNode(com.actiontech.dble.route.RouteResultsetNode) SQLInsertStatement(com.alibaba.druid.sql.ast.statement.SQLInsertStatement) TableConfig(com.actiontech.dble.config.model.TableConfig)

Aggregations

SchemaConfig (com.actiontech.dble.config.model.SchemaConfig)1 TableConfig (com.actiontech.dble.config.model.TableConfig)1 RouteResultsetNode (com.actiontech.dble.route.RouteResultsetNode)1 AbstractPartitionAlgorithm (com.actiontech.dble.route.function.AbstractPartitionAlgorithm)1 ReplaceTemp (com.actiontech.dble.route.parser.druid.ReplaceTemp)1 SQLExpr (com.alibaba.druid.sql.ast.SQLExpr)1 SQLInsertStatement (com.alibaba.druid.sql.ast.statement.SQLInsertStatement)1 SQLNonTransientException (java.sql.SQLNonTransientException)1