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.jdbc.core.resultset;
19  
20  import com.google.common.base.Preconditions;
21  import lombok.RequiredArgsConstructor;
22  import org.apache.shardingsphere.driver.jdbc.unsupported.AbstractUnsupportedGeneratedKeysResultSet;
23  import org.apache.shardingsphere.infra.annotation.HighFrequencyInvocation;
24  
25  import java.math.BigDecimal;
26  import java.math.RoundingMode;
27  import java.nio.charset.StandardCharsets;
28  import java.sql.ResultSetMetaData;
29  import java.sql.Statement;
30  import java.util.Collections;
31  import java.util.Iterator;
32  
33  /**
34   * ResultSet for generated keys.
35   */
36  @HighFrequencyInvocation
37  @RequiredArgsConstructor
38  public final class GeneratedKeysResultSet extends AbstractUnsupportedGeneratedKeysResultSet {
39      
40      private final String column;
41      
42      private final Iterator<Comparable<?>> values;
43      
44      private final Statement statement;
45      
46      private Comparable<?> currentValue;
47      
48      private boolean closed;
49      
50      public GeneratedKeysResultSet() {
51          column = null;
52          values = Collections.emptyIterator();
53          statement = null;
54      }
55      
56      @Override
57      public boolean isClosed() {
58          return closed;
59      }
60      
61      @Override
62      public boolean next() {
63          if (closed || !values.hasNext()) {
64              currentValue = null;
65              return false;
66          }
67          currentValue = values.next();
68          return true;
69      }
70      
71      @Override
72      public void close() {
73          closed = true;
74      }
75      
76      @Override
77      public ResultSetMetaData getMetaData() {
78          checkState();
79          return new GeneratedKeysResultSetMetaData(column);
80      }
81      
82      @Override
83      public boolean wasNull() {
84          checkState();
85          return false;
86      }
87      
88      @Override
89      public String getString(final int columnIndex) {
90          checkStateForGetData();
91          return currentValue.toString();
92      }
93      
94      @Override
95      public String getString(final String columnLabel) {
96          return getString(1);
97      }
98      
99      @Override
100     public String getNString(final int columnIndex) {
101         return getString(columnIndex);
102     }
103     
104     @Override
105     public String getNString(final String columnLabel) {
106         return getString(columnLabel);
107     }
108     
109     @Override
110     public byte getByte(final int columnIndex) {
111         checkStateForGetData();
112         return Byte.parseByte(getString(columnIndex));
113     }
114     
115     @Override
116     public byte getByte(final String columnLabel) {
117         return getByte(1);
118     }
119     
120     @Override
121     public short getShort(final int columnIndex) {
122         checkStateForGetData();
123         return Short.parseShort(getString(columnIndex));
124     }
125     
126     @Override
127     public short getShort(final String columnLabel) {
128         return getShort(1);
129     }
130     
131     @Override
132     public int getInt(final int columnIndex) {
133         checkStateForGetData();
134         return Integer.parseInt(getString(columnIndex));
135     }
136     
137     @Override
138     public int getInt(final String columnLabel) {
139         return getInt(1);
140     }
141     
142     @Override
143     public long getLong(final int columnIndex) {
144         checkStateForGetData();
145         return Long.parseLong(getString(columnIndex));
146     }
147     
148     @Override
149     public long getLong(final String columnLabel) {
150         return getLong(1);
151     }
152     
153     @Override
154     public float getFloat(final int columnIndex) {
155         checkStateForGetData();
156         return Float.parseFloat(getString(columnIndex));
157     }
158     
159     @Override
160     public float getFloat(final String columnLabel) {
161         return getFloat(1);
162     }
163     
164     @Override
165     public double getDouble(final int columnIndex) {
166         checkStateForGetData();
167         return Double.parseDouble(getString(columnIndex));
168     }
169     
170     @Override
171     public double getDouble(final String columnLabel) {
172         return getDouble(1);
173     }
174     
175     @Override
176     public BigDecimal getBigDecimal(final int columnIndex, final int scale) {
177         checkStateForGetData();
178         return new BigDecimal(getString(columnIndex)).setScale(scale, RoundingMode.HALF_UP);
179     }
180     
181     @Override
182     public BigDecimal getBigDecimal(final String columnLabel, final int scale) {
183         return getBigDecimal(1, scale);
184     }
185     
186     @Override
187     public BigDecimal getBigDecimal(final int columnIndex) {
188         checkStateForGetData();
189         return new BigDecimal(getString(columnIndex));
190     }
191     
192     @Override
193     public BigDecimal getBigDecimal(final String columnLabel) {
194         return getBigDecimal(1);
195     }
196     
197     @Override
198     public byte[] getBytes(final int columnIndex) {
199         checkStateForGetData();
200         return getString(columnIndex).getBytes(StandardCharsets.UTF_8);
201     }
202     
203     @Override
204     public byte[] getBytes(final String columnLabel) {
205         return getBytes(1);
206     }
207     
208     @Override
209     public Object getObject(final int columnIndex) {
210         checkStateForGetData();
211         return currentValue;
212     }
213     
214     @Override
215     public Object getObject(final String columnLabel) {
216         return getObject(1);
217     }
218     
219     @Override
220     public int findColumn(final String columnLabel) {
221         checkState();
222         return 1;
223     }
224     
225     @Override
226     public int getType() {
227         checkState();
228         return TYPE_FORWARD_ONLY;
229     }
230     
231     @Override
232     public int getConcurrency() {
233         checkState();
234         return CONCUR_READ_ONLY;
235     }
236     
237     @Override
238     public Statement getStatement() {
239         checkState();
240         return statement;
241     }
242     
243     private void checkStateForGetData() {
244         checkState();
245         Preconditions.checkNotNull(currentValue, "ResultSet should call next or has no more data.");
246     }
247     
248     private void checkState() {
249         Preconditions.checkState(!closed, "ResultSet has closed.");
250     }
251 }