View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.shardingsphere.driver.executor.batch;
19  
20  import com.google.common.collect.Lists;
21  import lombok.AccessLevel;
22  import lombok.EqualsAndHashCode;
23  import lombok.Getter;
24  import lombok.RequiredArgsConstructor;
25  import lombok.ToString;
26  import org.apache.shardingsphere.infra.executor.sql.context.ExecutionUnit;
27  
28  import java.util.Collections;
29  import java.util.LinkedHashMap;
30  import java.util.LinkedList;
31  import java.util.List;
32  import java.util.Map;
33  
34  /**
35   * Batch execution unit.
36   */
37  @RequiredArgsConstructor
38  @Getter
39  @EqualsAndHashCode(of = "executionUnit")
40  @ToString
41  public final class BatchExecutionUnit {
42      
43      private final ExecutionUnit executionUnit;
44      
45      private final Map<Integer, Integer> jdbcAndActualAddBatchCallTimesMap = new LinkedHashMap<>();
46      
47      @Getter(AccessLevel.NONE)
48      private int actualCallAddBatchTimes;
49      
50      /**
51       * Map times of use JDBC API call addBatch and times of actual call addBatch after route.
52       *
53       * @param jdbcAddBatchTimes times of use JDBC API call addBatch
54       */
55      public void mapAddBatchCount(final int jdbcAddBatchTimes) {
56          jdbcAndActualAddBatchCallTimesMap.put(jdbcAddBatchTimes, actualCallAddBatchTimes++);
57      }
58      
59      /**
60       * Get parameter sets.
61       * 
62       * @return parameter sets
63       */
64      public List<List<Object>> getParameterSets() {
65          List<List<Object>> result = new LinkedList<>();
66          if (executionUnit.getSqlUnit().getParameters().isEmpty() || 0 == actualCallAddBatchTimes) {
67              result.add(Collections.emptyList());
68          } else {
69              result.addAll(Lists.partition(executionUnit.getSqlUnit().getParameters(), executionUnit.getSqlUnit().getParameters().size() / actualCallAddBatchTimes));
70          }
71          return result;
72      }
73  }