@@ -47,6 +47,16 @@ public ResultSetImpl(StatementImpl parentStatement, QueryResponse response, Clic
4747 this .defaultCalendar = new GregorianCalendar (TimeZone .getTimeZone ("UTC" ));
4848 }
4949
50+ protected ResultSetImpl (ResultSetImpl resultSet ) {
51+ this .parentStatement = resultSet .parentStatement ;
52+ this .response = resultSet .response ;
53+ this .reader = resultSet .reader ;
54+ this .metaData = resultSet .metaData ;
55+ this .closed = false ;
56+ this .wasNull = false ;
57+ this .defaultCalendar = new GregorianCalendar (TimeZone .getTimeZone ("UTC" ));
58+ }
59+
5060 private void checkClosed () throws SQLException {
5161 if (closed ) {
5262 throw new SQLException ("ResultSet is closed." , ExceptionUtils .SQL_STATE_CONNECTION_EXCEPTION );
@@ -112,246 +122,82 @@ public boolean wasNull() throws SQLException {
112122
113123 @ Override
114124 public String getString (int columnIndex ) throws SQLException {
115- checkClosed ();
116- try {
117- if (reader .hasValue (columnIndex )) {
118- wasNull = false ;
119- return reader .getString (columnIndex );
120- } else {
121- wasNull = true ;
122- return null ;
123- }
124- } catch (Exception e ) {
125- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getString(%s)" , parentStatement .getLastSql (), columnIndex ), e );
126- }
125+ return getString (getSchema ().columnIndexToName (columnIndex ));
127126 }
128127
129128 @ Override
130129 public boolean getBoolean (int columnIndex ) throws SQLException {
131- checkClosed ();
132- try {
133- if (reader .hasValue (columnIndex )) {
134- wasNull = false ;
135- return reader .getBoolean (columnIndex );
136- } else {
137- wasNull = true ;
138- return false ;
139- }
140- } catch (Exception e ) {
141- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getBoolean(%s)" , parentStatement .getLastSql (), columnIndex ), e );
142- }
130+ return getBoolean (getSchema ().columnIndexToName (columnIndex ));
143131 }
144132
145133 @ Override
146134 public byte getByte (int columnIndex ) throws SQLException {
147- checkClosed ();
148- try {
149- if (reader .hasValue (columnIndex )) {
150- wasNull = false ;
151- return reader .getByte (columnIndex );
152- } else {
153- wasNull = true ;
154- return 0 ;
155- }
156- } catch (Exception e ) {
157- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getByte(%s)" , parentStatement .getLastSql (), columnIndex ), e );
158- }
135+ return getByte (getSchema ().columnIndexToName (columnIndex ));
159136 }
160137
161138 @ Override
162139 public short getShort (int columnIndex ) throws SQLException {
163- checkClosed ();
164- try {
165- if (reader .hasValue (columnIndex )) {
166- wasNull = false ;
167- return reader .getShort (columnIndex );
168- } else {
169- wasNull = true ;
170- return 0 ;
171- }
172- } catch (Exception e ) {
173- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getShort(%s)" , parentStatement .getLastSql (), columnIndex ), e );
174- }
140+ return getShort (getSchema ().columnIndexToName (columnIndex ));
175141 }
176142
177143 @ Override
178144 public int getInt (int columnIndex ) throws SQLException {
179- checkClosed ();
180- try {
181- if (reader .hasValue (columnIndex )) {
182- wasNull = false ;
183- return reader .getInteger (columnIndex );
184- } else {
185- wasNull = true ;
186- return 0 ;
187- }
188- } catch (Exception e ) {
189- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getInt(%s)" , parentStatement .getLastSql (), columnIndex ), e );
190- }
145+ return getInt (getSchema ().columnIndexToName (columnIndex ));
191146 }
192147
193148 @ Override
194149 public long getLong (int columnIndex ) throws SQLException {
195- checkClosed ();
196- try {
197- if (reader .hasValue (columnIndex )) {
198- wasNull = false ;
199- return reader .getLong (columnIndex );
200- } else {
201- wasNull = true ;
202- return 0 ;
203- }
204- } catch (Exception e ) {
205- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getLong(%s)" , parentStatement .getLastSql (), columnIndex ), e );
206- }
150+ return getLong (getSchema ().columnIndexToName (columnIndex ));
207151 }
208152
209153 @ Override
210154 public float getFloat (int columnIndex ) throws SQLException {
211- checkClosed ();
212- try {
213- if (reader .hasValue (columnIndex )) {
214- wasNull = false ;
215- return reader .getFloat (columnIndex );
216- } else {
217- wasNull = true ;
218- return 0 ;
219- }
220- } catch (Exception e ) {
221- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getFloat(%s)" , parentStatement .getLastSql (), columnIndex ), e );
222- }
155+ return getFloat (getSchema ().columnIndexToName (columnIndex ));
223156 }
224157
225158 @ Override
226159 public double getDouble (int columnIndex ) throws SQLException {
227- checkClosed ();
228- try {
229- if (reader .hasValue (columnIndex )) {
230- wasNull = false ;
231- return reader .getDouble (columnIndex );
232- } else {
233- wasNull = true ;
234- return 0 ;
235- }
236- } catch (Exception e ) {
237- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getDouble(%s)" , parentStatement .getLastSql (), columnIndex ), e );
238- }
160+ return getDouble (getSchema ().columnIndexToName (columnIndex ));
239161 }
240162
241163 @ Override
242164 public BigDecimal getBigDecimal (int columnIndex , int scale ) throws SQLException {
243- checkClosed ();
244- try {
245- if (reader .hasValue (columnIndex )) {
246- wasNull = false ;
247- return reader .getBigDecimal (columnIndex );
248- } else {
249- wasNull = true ;
250- return null ;
251- }
252- } catch (Exception e ) {
253- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getBigDecimal(%s)" , parentStatement .getLastSql (), columnIndex ), e );
254- }
165+ return getBigDecimal (getSchema ().columnIndexToName (columnIndex ), scale );
255166 }
256167
257168 @ Override
258169 public byte [] getBytes (int columnIndex ) throws SQLException {
259- checkClosed ();
260- try {
261- if (reader .hasValue (columnIndex )) {
262- wasNull = false ;
263- return reader .getByteArray (columnIndex );
264- } else {
265- wasNull = true ;
266- return null ;
267- }
268- } catch (Exception e ) {
269- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getBytes(%s)" , parentStatement .getLastSql (), columnIndex ), e );
270- }
170+ return getBytes (getSchema ().columnIndexToName (columnIndex ));
271171 }
272172
273173 @ Override
274174 public Date getDate (int columnIndex ) throws SQLException {
275- checkClosed ();
276- try {
277- //TODO: Add this to ClickHouseBinaryFormatReader
278- LocalDate localDate = reader .getLocalDate (columnIndex );
279- if (localDate == null ) {
280- wasNull = true ;
281- return null ;
282- }
283-
284- wasNull = false ;
285- return Date .valueOf (localDate );
286- } catch (Exception e ) {
287- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getDate(%s)" , parentStatement .getLastSql (), columnIndex ), e );
288- }
175+ return getDate (getSchema ().columnIndexToName (columnIndex ));
289176 }
290177
291178 @ Override
292179 public Time getTime (int columnIndex ) throws SQLException {
293- checkClosed ();
294- try {
295- LocalDateTime localDateTime = reader .getLocalDateTime (columnIndex );
296- if (localDateTime == null ) {
297- wasNull = true ;
298- return null ;
299- }
300-
301- wasNull = false ;
302- return Time .valueOf (localDateTime .toLocalTime ());
303- } catch (Exception e ) {
304- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getTime(%s)" , parentStatement .getLastSql (), columnIndex ), e );
305- }
180+ return getTime (getSchema ().columnIndexToName (columnIndex ));
306181 }
307182
308183 @ Override
309184 public Timestamp getTimestamp (int columnIndex ) throws SQLException {
310- checkClosed ();
311- try {
312- LocalDateTime localDateTime = reader .getLocalDateTime (columnIndex );
313- if (localDateTime == null ) {
314- wasNull = true ;
315- return null ;
316- }
317-
318- wasNull = false ;
319- return Timestamp .valueOf (localDateTime );
320- } catch (Exception e ) {
321- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getTimestamp(%s)" , parentStatement .getLastSql (), columnIndex ), e );
322- }
185+ return getTimestamp (getSchema ().columnIndexToName (columnIndex ));
323186 }
324187
325188 @ Override
326189 public InputStream getAsciiStream (int columnIndex ) throws SQLException {
327- checkClosed ();
328- //TODO: Add this to ClickHouseBinaryFormatReader
329- if (!parentStatement .connection .config .isIgnoreUnsupportedRequests ()) {
330- throw new SQLFeatureNotSupportedException ("AsciiStream is not yet supported." , ExceptionUtils .SQL_STATE_FEATURE_NOT_SUPPORTED );
331- }
332-
333- return null ;
190+ return getAsciiStream (getSchema ().columnIndexToName (columnIndex ));
334191 }
335192
336193 @ Override
337194 public InputStream getUnicodeStream (int columnIndex ) throws SQLException {
338- checkClosed ();
339- if (!parentStatement .connection .config .isIgnoreUnsupportedRequests ()) {
340- return new ByteArrayInputStream (reader .getString (columnIndex ).getBytes (StandardCharsets .UTF_8 ));
341- }
342-
343- return null ;
195+ return getUnicodeStream (getSchema ().columnIndexToName (columnIndex ));
344196 }
345197
346198 @ Override
347199 public InputStream getBinaryStream (int columnIndex ) throws SQLException {
348- checkClosed ();
349- //TODO: implement
350- if (!parentStatement .connection .config .isIgnoreUnsupportedRequests ()) {
351- throw new SQLFeatureNotSupportedException ("BinaryStream is not yet supported." , ExceptionUtils .SQL_STATE_FEATURE_NOT_SUPPORTED );
352- }
353-
354- return null ;
200+ return getBinaryStream (getSchema ().columnIndexToName (columnIndex ));
355201 }
356202
357203 @ Override
@@ -659,18 +505,7 @@ public Reader getCharacterStream(String columnLabel) throws SQLException {
659505
660506 @ Override
661507 public BigDecimal getBigDecimal (int columnIndex ) throws SQLException {
662- checkClosed ();
663- try {
664- if (reader .hasValue (columnIndex )) {
665- wasNull = false ;
666- return reader .getBigDecimal (columnIndex );
667- } else {
668- wasNull = true ;
669- return null ;
670- }
671- } catch (Exception e ) {
672- throw ExceptionUtils .toSqlState (String .format ("SQL: [%s]; Method: getBigDecimal(%s)" , parentStatement .getLastSql (), columnIndex ), e );
673- }
508+ return getBigDecimal (getSchema ().columnIndexToName (columnIndex ));
674509 }
675510
676511 @ Override
0 commit comments