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.proxy.backend.response.header.update;
19  
20  import lombok.AccessLevel;
21  import lombok.Getter;
22  import org.apache.shardingsphere.infra.executor.sql.execute.result.update.UpdateResult;
23  import org.apache.shardingsphere.proxy.backend.response.header.ResponseHeader;
24  import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
25  
26  import java.util.ArrayList;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.LinkedList;
30  import java.util.List;
31  
32  /**
33   * Update response header.
34   */
35  @Getter
36  public final class UpdateResponseHeader implements ResponseHeader {
37      
38      private final SQLStatement sqlStatement;
39      
40      private final long lastInsertId;
41      
42      @Getter(AccessLevel.NONE)
43      private final Collection<Integer> updateCounts = new LinkedList<>();
44      
45      private long updateCount;
46      
47      public UpdateResponseHeader(final SQLStatement sqlStatement) {
48          this(sqlStatement, Collections.emptyList(), Collections.emptyList());
49      }
50      
51      public UpdateResponseHeader(final SQLStatement sqlStatement, final Collection<UpdateResult> updateResults) {
52          this(sqlStatement, updateResults, Collections.emptyList());
53      }
54      
55      public UpdateResponseHeader(final SQLStatement sqlStatement, final Collection<UpdateResult> updateResults, final Collection<Comparable<?>> autoIncrementGeneratedValues) {
56          this.sqlStatement = sqlStatement;
57          lastInsertId = getLastInsertId(updateResults, autoIncrementGeneratedValues);
58          updateCount = updateResults.iterator().hasNext() ? updateResults.iterator().next().getUpdateCount() : 0;
59          for (UpdateResult each : updateResults) {
60              updateCounts.add(each.getUpdateCount());
61          }
62      }
63      
64      private long getLastInsertId(final Collection<UpdateResult> updateResults, final Collection<Comparable<?>> autoIncrementGeneratedValues) {
65          List<Long> lastInsertIds = new ArrayList<>(updateResults.size() + autoIncrementGeneratedValues.size());
66          for (UpdateResult each : updateResults) {
67              if (each.getLastInsertId() > 0L) {
68                  lastInsertIds.add(each.getLastInsertId());
69              }
70          }
71          for (Comparable<?> each : autoIncrementGeneratedValues) {
72              if (each instanceof Number) {
73                  lastInsertIds.add(((Number) each).longValue());
74              }
75          }
76          return lastInsertIds.isEmpty() ? 0L : getMinLastInsertId(lastInsertIds);
77      }
78      
79      private long getMinLastInsertId(final List<Long> lastInsertIds) {
80          Collections.sort(lastInsertIds);
81          return lastInsertIds.iterator().next();
82      }
83      
84      /**
85       * Merge updated counts.
86       */
87      public void mergeUpdateCount() {
88          updateCount = 0L;
89          for (int each : updateCounts) {
90              updateCount += each;
91          }
92      }
93  }