-
Notifications
You must be signed in to change notification settings - Fork 25
/
CassandraResultSet.java
1874 lines (1690 loc) · 72.1 KB
/
CassandraResultSet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ing.data.cassandra.jdbc;
import com.datastax.oss.driver.api.core.cql.ColumnDefinition;
import com.datastax.oss.driver.api.core.cql.ResultSet;
import com.datastax.oss.driver.api.core.cql.Row;
import com.datastax.oss.driver.api.core.data.CqlDuration;
import com.datastax.oss.driver.api.core.data.CqlVector;
import com.datastax.oss.driver.api.core.data.TupleValue;
import com.datastax.oss.driver.api.core.data.UdtValue;
import com.datastax.oss.driver.api.core.type.DataType;
import com.datastax.oss.driver.api.core.type.ListType;
import com.datastax.oss.driver.api.core.type.MapType;
import com.datastax.oss.driver.api.core.type.SetType;
import com.datastax.oss.driver.api.core.type.TupleType;
import com.datastax.oss.driver.api.core.type.UserDefinedType;
import com.datastax.oss.driver.api.core.type.VectorType;
import com.datastax.oss.driver.internal.core.type.DefaultMapType;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ing.data.cassandra.jdbc.types.AbstractJdbcType;
import com.ing.data.cassandra.jdbc.types.DataTypeEnum;
import com.ing.data.cassandra.jdbc.types.TypesMap;
import com.ing.data.cassandra.jdbc.utils.ArrayImpl;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream;
import java.io.CharArrayReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.sql.Array;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.Date;
import java.sql.NClob;
import java.sql.ResultSetMetaData;
import java.sql.RowId;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.SQLNonTransientException;
import java.sql.SQLRecoverableException;
import java.sql.SQLSyntaxErrorException;
import java.sql.SQLWarning;
import java.sql.SQLXML;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.OffsetTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import static com.ing.data.cassandra.jdbc.types.AbstractJdbcType.DEFAULT_PRECISION;
import static com.ing.data.cassandra.jdbc.types.AbstractJdbcType.DEFAULT_SCALE;
import static com.ing.data.cassandra.jdbc.types.DataTypeEnum.fromCqlTypeName;
import static com.ing.data.cassandra.jdbc.types.DataTypeEnum.fromDataType;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.BAD_FETCH_DIR;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.BAD_FETCH_SIZE;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.FORWARD_ONLY;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.ILLEGAL_FETCH_DIRECTION_FOR_FORWARD_ONLY;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.MALFORMED_URL;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.MUST_BE_POSITIVE;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NOT_SUPPORTED;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.NO_INTERFACE;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNABLE_TO_READ_VALUE;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNSUPPORTED_JSON_TYPE_CONVERSION;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.UNSUPPORTED_TYPE_CONVERSION;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.VALID_LABELS;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.VECTOR_ELEMENTS_NOT_NUMBERS;
import static com.ing.data.cassandra.jdbc.utils.ErrorConstants.WAS_CLOSED_RS;
import static com.ing.data.cassandra.jdbc.utils.JsonUtil.getObjectMapper;
/**
* Cassandra result set: implementation class for {@link java.sql.ResultSet}.
* <p>
* It also implements {@link CassandraResultSetExtras} and {@link CassandraResultSetJsonSupport} interfaces
* providing extra methods not defined in JDBC API to better handle some CQL data types and ease usage of JSON
* features {@code SELECT JSON} and {@code toJson()} provided by Cassandra.
* </p>
* The supported data types in CQL are:
* <table border="1">
* <tr><th>CQL Type </th><th>Java type </th><th>Description</th></tr>
* <tr><td>ascii </td><td>{@link String} </td><td>US-ASCII character string</td></tr>
* <tr><td>bigint </td><td>{@link Long} </td><td>64-bit signed long</td></tr>
* <tr><td>blob </td><td>{@link ByteBuffer} </td><td>Arbitrary bytes (no validation)</td></tr>
* <tr><td>boolean </td><td>{@link Boolean} </td><td>Boolean value: true or false</td></tr>
* <tr><td>counter </td><td>{@link Long} </td><td>Counter column (64-bit long)</td></tr>
* <tr><td>date </td><td>{@link Date} </td><td>A date with no corresponding time value; encoded date
* as a 32-bit integer representing days since epoch (January 1, 1970)</td></tr>
* <tr><td>decimal </td><td>{@link BigDecimal} </td><td>Variable-precision decimal</td></tr>
* <tr><td>double </td><td>{@link Double} </td><td>64-bit IEEE-754 floating point</td></tr>
* <tr><td>duration </td><td>{@link CqlDuration}</td><td>A duration with nanosecond precision</td></tr>
* <tr><td>float </td><td>{@link Float} </td><td>32-bit IEEE-754 floating point</td></tr>
* <tr><td>inet </td><td>{@link InetAddress}</td><td>IP address string in IPv4 or IPv6 format</td></tr>
* <tr><td>int </td><td>{@link Integer} </td><td>32-bit signed integer</td></tr>
* <tr><td>list </td><td>{@link List} </td><td>A collection of one or more ordered elements:
* <code>[literal, literal, literal]</code></td></tr>
* <tr><td>map </td><td>{@link Map} </td><td>A JSON-style array of literals:
* <code>{ literal : literal, literal : literal ... }</code></td></tr>
* <tr><td>set </td><td>{@link Set} </td><td>A collection of one or more elements:
* <code>{ literal, literal, literal }</code></td></tr>
* <tr><td>smallint </td><td>{@link Short} </td><td>16-bit signed integer</td></tr>
* <tr><td>text </td><td>{@link String} </td><td>UTF-8 encoded string</td></tr>
* <tr><td>time </td><td>{@link Time} </td><td>A value encoded as a 64-bit signed integer
* representing the number of nanoseconds since midnight</td></tr>
* <tr><td>timestamp</td><td>{@link Timestamp} </td><td>Date and time with millisecond precision, encoded as
* 8 bytes since epoch</td></tr>
* <tr><td>timeuuid </td><td>{@link UUID} </td><td>Version 1 UUID only</td></tr>
* <tr><td>tinyint </td><td>{@link Byte} </td><td>8-bits signed integer</td></tr>
* <tr><td>tuple </td><td>{@link TupleValue} </td><td>A group of 2-3 fields</td></tr>
* <tr><td>udt </td><td>{@link UdtValue} </td><td>A set of data fields where each field is named and
* typed</td></tr>
* <tr><td>uuid </td><td>{@link UUID} </td><td>A UUID in standard UUID format</td></tr>
* <tr><td>varchar </td><td>{@link String} </td><td>UTF-8 encoded string</td></tr>
* <tr><td>varint </td><td>{@link BigInteger} </td><td>Arbitrary-precision integer</td></tr>
* <tr><td>vector </td><td>{@link CqlVector} </td><td>A n-dimensional vector</td></tr>
* </table>
* See: <a href="https://docs.datastax.com/en/cql-oss/3.x/cql/cql_reference/cql_data_types_c.html">
* CQL data types reference</a> and
* <a href="https://docs.datastax.com/en/developer/java-driver/latest/manual/core/temporal_types/">
* CQL temporal types reference</a>.
*
* @see ResultSet
*/
public class CassandraResultSet extends AbstractResultSet
implements CassandraResultSetExtras, CassandraResultSetJsonSupport {
/**
* An empty Cassandra result set. It can be used to provide default implementations to methods returning
* {@link ResultSet} objects.
*/
public static final CassandraResultSet EMPTY_RESULT_SET = new CassandraResultSet();
/**
* Default result set type for Cassandra implementation: {@link #TYPE_FORWARD_ONLY}.
*/
public static final int DEFAULT_TYPE = TYPE_FORWARD_ONLY;
/**
* Default result set concurrency for Cassandra implementation: {@link #CONCUR_READ_ONLY}.
*/
public static final int DEFAULT_CONCURRENCY = CONCUR_READ_ONLY;
/**
* Default result set holdability for Cassandra implementation: {@link #HOLD_CURSORS_OVER_COMMIT}.
*/
public static final int DEFAULT_HOLDABILITY = HOLD_CURSORS_OVER_COMMIT;
private static final Logger LOG = LoggerFactory.getLogger(CassandraResultSet.class);
int rowNumber = 0;
// Metadata of this result set.
private final CResultSetMetaData metadata;
private final CassandraStatement statement;
private Row currentRow;
private Iterator<Row> rowsIterator;
private int resultSetType;
private int fetchDirection;
private int fetchSize;
private boolean wasNull;
private boolean isClosed;
// Result set from the Cassandra driver.
private ResultSet driverResultSet;
/**
* No argument constructor.
*/
CassandraResultSet() {
this.metadata = new CResultSetMetaData();
this.statement = null;
this.isClosed = false;
}
/**
* Constructor. It instantiates a new Cassandra result set from a {@link ResultSet}.
*
* @param statement The statement.
* @param resultSet The result set from the Cassandra driver.
* @throws SQLException if a database access error occurs or this constructor is called with a closed
* {@link Statement}.
*/
CassandraResultSet(final CassandraStatement statement, final ResultSet resultSet) throws SQLException {
this.metadata = new CResultSetMetaData();
this.statement = statement;
this.resultSetType = statement.getResultSetType();
this.fetchDirection = statement.getFetchDirection();
this.fetchSize = statement.getFetchSize();
this.driverResultSet = resultSet;
this.rowsIterator = resultSet.iterator();
this.isClosed = false;
// Initialize the column values from the first row.
if (hasMoreRows()) {
populateColumns();
}
}
/**
* Constructor. It instantiates a new Cassandra result set from a list of {@link ResultSet}.
*
* @param statement The statement.
* @param resultSets The list of result sets from the Cassandra driver.
* @throws SQLException if a database access error occurs or this constructor is called with a closed
* {@link Statement}.
*/
@SuppressWarnings("unchecked")
CassandraResultSet(final CassandraStatement statement, final ArrayList<ResultSet> resultSets) throws SQLException {
this.metadata = new CResultSetMetaData();
this.statement = statement;
this.resultSetType = statement.getResultSetType();
this.fetchDirection = statement.getFetchDirection();
this.fetchSize = statement.getFetchSize();
this.isClosed = false;
// We have several result sets, but we will use only the first one for metadata needs.
this.driverResultSet = resultSets.get(0);
// Now, we concatenate iterators of the different result sets into a single one.
// This may lead to StackOverflowException when there are too many result sets.
final Iterator<Row>[] resultSetsIterators = new Iterator[resultSets.size()];
resultSetsIterators[0] = this.driverResultSet.iterator();
for (int i = 1; i < resultSets.size(); i++) {
resultSetsIterators[i] = resultSets.get(i).iterator();
}
this.rowsIterator = IteratorUtils.chainedIterator(resultSetsIterators);
// Initialize the column values from the first row.
if (hasMoreRows()) {
populateColumns();
}
}
private void populateColumns() {
this.currentRow = this.rowsIterator.next();
}
@Override
DataType getCqlDataType(final int columnIndex) {
if (this.currentRow != null) {
return this.currentRow.getColumnDefinitions().get(columnIndex - 1).getType();
}
return this.driverResultSet.getColumnDefinitions().get(columnIndex - 1).getType();
}
@Override
DataType getCqlDataType(final String columnLabel) {
if (this.currentRow != null) {
return this.currentRow.getColumnDefinitions().get(columnLabel).getType();
}
return this.driverResultSet.getColumnDefinitions().get(columnLabel).getType();
}
@Override
public void afterLast() throws SQLException {
if (this.resultSetType == TYPE_FORWARD_ONLY) {
throw new SQLNonTransientException(FORWARD_ONLY);
}
throw new SQLFeatureNotSupportedException(NOT_SUPPORTED);
}
@Override
public void beforeFirst() throws SQLException {
if (this.resultSetType == TYPE_FORWARD_ONLY) {
throw new SQLNonTransientException(FORWARD_ONLY);
}
throw new SQLFeatureNotSupportedException(NOT_SUPPORTED);
}
private void checkIndex(final int index) throws SQLException {
if (this.currentRow != null) {
if (index < 1 || index > this.currentRow.getColumnDefinitions().size()) {
throw new SQLSyntaxErrorException(String.format(MUST_BE_POSITIVE, index) + StringUtils.SPACE
+ this.currentRow.getColumnDefinitions().size());
}
this.wasNull = this.currentRow.isNull(index - 1);
} else if (this.driverResultSet != null) {
if (index < 1 || index > this.driverResultSet.getColumnDefinitions().size()) {
throw new SQLSyntaxErrorException(String.format(MUST_BE_POSITIVE, index) + StringUtils.SPACE
+ this.driverResultSet.getColumnDefinitions().size());
}
}
}
private void checkName(final String name) throws SQLException {
if (this.currentRow != null) {
if (!this.currentRow.getColumnDefinitions().contains(name)) {
throw new SQLSyntaxErrorException(String.format(VALID_LABELS, name));
}
this.wasNull = this.currentRow.isNull(name);
} else if (this.driverResultSet != null) {
if (!this.driverResultSet.getColumnDefinitions().contains(name)) {
throw new SQLSyntaxErrorException(String.format(VALID_LABELS, name));
}
}
}
private void checkNotClosed() throws SQLException {
if (isClosed()) {
throw new SQLRecoverableException(WAS_CLOSED_RS);
}
}
@Override
public void clearWarnings() throws SQLException {
// This implementation does not support the collection of warnings so clearing is a no-op, but it still throws
// an exception when called on a closed result set.
checkNotClosed();
}
@Override
public void close() throws SQLException {
if (!isClosed()) {
this.isClosed = true;
}
}
@Override
public int findColumn(final String columnLabel) throws SQLException {
checkNotClosed();
checkName(columnLabel);
if (this.currentRow != null) {
return this.currentRow.getColumnDefinitions().firstIndexOf(columnLabel) + 1;
} else if (this.driverResultSet != null) {
return this.driverResultSet.getColumnDefinitions().firstIndexOf(columnLabel) + 1;
}
throw new SQLSyntaxErrorException(String.format(VALID_LABELS, columnLabel));
}
@Override
public InputStream getAsciiStream(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
final String s = this.currentRow.getString(columnIndex - 1);
if (s != null) {
return new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
} else {
return null;
}
}
@Override
public Array getArray(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
Object o = currentRow.getObject(columnIndex - 1);
return o instanceof List ? toArray((List<?>) o) : null;
}
@Override
public Array getArray(final String columnLabel) throws SQLException {
checkName(columnLabel);
Object o = currentRow.getObject(columnLabel);
return o instanceof List ? toArray((List<?>) o) : null;
}
private Array toArray(final List<?> list) {
Object[] array = new Object[list.size()];
for (int i = 0; i < list.size(); i++) {
array[i] = list.get(i);
}
return new ArrayImpl(array);
}
@Override
public InputStream getAsciiStream(final String columnLabel) throws SQLException {
checkName(columnLabel);
final String s = this.currentRow.getString(columnLabel);
if (s != null) {
return new ByteArrayInputStream(s.getBytes(StandardCharsets.US_ASCII));
} else {
return null;
}
}
@Override
public BigDecimal getBigDecimal(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
return this.currentRow.getBigDecimal(columnIndex - 1);
}
/**
* @deprecated use {@link #getBigDecimal(int)}.
*/
@Override
@Deprecated
public BigDecimal getBigDecimal(final int columnIndex, final int scale) throws SQLException {
checkIndex(columnIndex);
final BigDecimal decimalValue = this.currentRow.getBigDecimal(columnIndex - 1);
if (decimalValue == null) {
return null;
} else {
return decimalValue.setScale(scale, RoundingMode.HALF_UP);
}
}
@Override
public BigDecimal getBigDecimal(final String columnLabel) throws SQLException {
checkName(columnLabel);
return this.currentRow.getBigDecimal(columnLabel);
}
/**
* @deprecated use {@link #getBigDecimal(String)}.
*/
@Override
@Deprecated
public BigDecimal getBigDecimal(final String columnLabel, final int scale) throws SQLException {
checkName(columnLabel);
final BigDecimal decimalValue = this.currentRow.getBigDecimal(columnLabel);
if (decimalValue == null) {
return null;
} else {
return decimalValue.setScale(scale, RoundingMode.HALF_UP);
}
}
@Override
public BigInteger getBigInteger(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
return this.currentRow.getBigInteger(columnIndex - 1);
}
@Override
public BigInteger getBigInteger(final String columnLabel) throws SQLException {
checkName(columnLabel);
return this.currentRow.getBigInteger(columnLabel);
}
@Override
public InputStream getBinaryStream(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
final ByteBuffer byteBuffer = this.currentRow.getByteBuffer(columnIndex - 1);
if (byteBuffer != null) {
final byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes, 0, bytes.length);
return new ByteArrayInputStream(bytes);
} else {
return null;
}
}
@Override
public InputStream getBinaryStream(final String columnLabel) throws SQLException {
checkName(columnLabel);
final ByteBuffer byteBuffer = this.currentRow.getByteBuffer(columnLabel);
if (byteBuffer != null) {
final byte[] bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes, 0, bytes.length);
return new ByteArrayInputStream(bytes);
} else {
return null;
}
}
@Override
public Blob getBlob(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
final ByteBuffer byteBuffer = this.currentRow.getByteBuffer(columnIndex - 1);
if (byteBuffer != null) {
return new javax.sql.rowset.serial.SerialBlob(byteBuffer.array());
} else {
return null;
}
}
@Override
public Blob getBlob(final String columnLabel) throws SQLException {
checkName(columnLabel);
final ByteBuffer byteBuffer = this.currentRow.getByteBuffer(columnLabel);
if (byteBuffer != null) {
return new javax.sql.rowset.serial.SerialBlob(byteBuffer.array());
} else {
return null;
}
}
@Override
public boolean getBoolean(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
return this.currentRow.getBoolean(columnIndex - 1);
}
@Override
public boolean getBoolean(final String columnLabel) throws SQLException {
checkName(columnLabel);
return this.currentRow.getBoolean(columnLabel);
}
@Override
public byte getByte(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
return this.currentRow.getByte(columnIndex - 1);
}
@Override
public byte getByte(final String columnLabel) throws SQLException {
checkName(columnLabel);
return this.currentRow.getByte(columnLabel);
}
@Override
public byte[] getBytes(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
final ByteBuffer byteBuffer = this.currentRow.getByteBuffer(columnIndex - 1);
if (byteBuffer != null) {
return byteBuffer.array();
}
return null;
}
@Override
public byte[] getBytes(final String columnLabel) throws SQLException {
checkName(columnLabel);
final ByteBuffer byteBuffer = this.currentRow.getByteBuffer(columnLabel);
if (byteBuffer != null) {
return byteBuffer.array();
}
return null;
}
@Override
public Reader getCharacterStream(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
final byte[] byteArray = this.getBytes(columnIndex);
if (byteArray != null) {
final InputStream inputStream = new ByteArrayInputStream(byteArray);
try {
return new CharArrayReader(IOUtils.toCharArray(inputStream, StandardCharsets.UTF_8));
} catch (final IOException e) {
throw new SQLException(String.format(UNABLE_TO_READ_VALUE, Reader.class.getSimpleName()), e);
}
} else {
return null;
}
}
@Override
public Reader getCharacterStream(final String columnLabel) throws SQLException {
checkName(columnLabel);
final byte[] byteArray = this.getBytes(columnLabel);
if (byteArray != null) {
final InputStream inputStream = new ByteArrayInputStream(byteArray);
try {
return new CharArrayReader(IOUtils.toCharArray(inputStream, StandardCharsets.UTF_8));
} catch (final IOException e) {
throw new SQLException(String.format(UNABLE_TO_READ_VALUE, Reader.class.getSimpleName()), e);
}
} else {
return null;
}
}
@Override
public Clob getClob(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
final byte[] byteArray = getBytes(columnIndex);
if (byteArray != null) {
final InputStream inputStream = new ByteArrayInputStream(byteArray);
try {
return new javax.sql.rowset.serial.SerialClob(IOUtils.toCharArray(inputStream, StandardCharsets.UTF_8));
} catch (final IOException e) {
throw new SQLException(String.format(UNABLE_TO_READ_VALUE, Clob.class.getSimpleName()), e);
}
} else {
return null;
}
}
@Override
public Clob getClob(final String columnLabel) throws SQLException {
checkName(columnLabel);
final byte[] byteArray = getBytes(columnLabel);
if (byteArray != null) {
final InputStream inputStream = new ByteArrayInputStream(byteArray);
try {
return new javax.sql.rowset.serial.SerialClob(IOUtils.toCharArray(inputStream, StandardCharsets.UTF_8));
} catch (final IOException e) {
throw new SQLException(String.format(UNABLE_TO_READ_VALUE, Clob.class.getSimpleName()), e);
}
} else {
return null;
}
}
@Override
public int getConcurrency() throws SQLException {
checkNotClosed();
return this.statement.getResultSetConcurrency();
}
@Override
public Date getDate(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
final LocalDate localDate = this.currentRow.getLocalDate(columnIndex - 1);
if (localDate == null) {
return null;
} else {
return java.sql.Date.valueOf(localDate);
}
}
@Override
public Date getDate(final int columnIndex, final Calendar calendar) throws SQLException {
// silently ignore the Calendar argument; it's a hint we do not need
return getDate(columnIndex);
}
@Override
public Date getDate(final String columnLabel) throws SQLException {
checkName(columnLabel);
final LocalDate localDate = this.currentRow.getLocalDate(columnLabel);
if (localDate == null) {
return null;
} else {
return java.sql.Date.valueOf(localDate);
}
}
@Override
public Date getDate(final String columnLabel, final Calendar calendar) throws SQLException {
// silently ignore the Calendar argument; it's a hint we do not need
return getDate(columnLabel);
}
@Override
public double getDouble(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
if (isCqlType(columnIndex, DataTypeEnum.FLOAT)) {
return this.currentRow.getFloat(columnIndex - 1);
}
return this.currentRow.getDouble(columnIndex - 1);
}
@Override
public double getDouble(final String columnLabel) throws SQLException {
checkName(columnLabel);
if (isCqlType(columnLabel, DataTypeEnum.FLOAT)) {
return this.currentRow.getFloat(columnLabel);
}
return this.currentRow.getDouble(columnLabel);
}
@Override
public CqlDuration getDuration(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
return this.currentRow.getCqlDuration(columnIndex - 1);
}
@Override
public CqlDuration getDuration(final String columnLabel) throws SQLException {
checkName(columnLabel);
return this.currentRow.getCqlDuration(columnLabel);
}
@Override
public int getFetchDirection() throws SQLException {
checkNotClosed();
return this.fetchDirection;
}
@Override
public void setFetchDirection(final int direction) throws SQLException {
checkNotClosed();
if (direction == FETCH_FORWARD || direction == FETCH_REVERSE || direction == FETCH_UNKNOWN) {
if (getType() == TYPE_FORWARD_ONLY && direction != FETCH_FORWARD) {
throw new SQLSyntaxErrorException(String.format(ILLEGAL_FETCH_DIRECTION_FOR_FORWARD_ONLY, direction));
}
this.fetchDirection = direction;
}
throw new SQLSyntaxErrorException(String.format(BAD_FETCH_DIR, direction));
}
@Override
public int getFetchSize() throws SQLException {
checkNotClosed();
return this.fetchSize;
}
@Override
public void setFetchSize(final int size) throws SQLException {
checkNotClosed();
if (size < 0) {
throw new SQLException(String.format(BAD_FETCH_SIZE, size));
}
this.fetchSize = size;
}
@Override
public float getFloat(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
return this.currentRow.getFloat(columnIndex - 1);
}
@Override
public float getFloat(final String columnLabel) throws SQLException {
checkName(columnLabel);
return this.currentRow.getFloat(columnLabel);
}
@SuppressWarnings("MagicConstant")
@Override
public int getHoldability() throws SQLException {
checkNotClosed();
return this.statement.getResultSetHoldability();
}
@Override
public int getInt(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
return this.currentRow.getInt(columnIndex - 1);
}
@Override
public int getInt(final String columnLabel) throws SQLException {
checkName(columnLabel);
return this.currentRow.getInt(columnLabel);
}
@Override
public List<?> getList(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
final DataType cqlDataType = getCqlDataType(columnIndex);
if (fromCqlTypeName(cqlDataType.asCql(false, false)).isCollection()) {
try {
final ListType listType = (ListType) cqlDataType;
final Class<?> itemsClass = Class.forName(fromDataType(listType.getElementType())
.asJavaClass().getCanonicalName());
final List<?> resultList = this.currentRow.getList(columnIndex - 1, itemsClass);
if (resultList == null) {
return null;
}
return new ArrayList<>(resultList);
} catch (final ClassNotFoundException e) {
LOG.warn("Error while executing getList()", e);
}
}
return this.currentRow.getList(columnIndex - 1, String.class);
}
@Override
public List<?> getList(final String columnLabel) throws SQLException {
checkName(columnLabel);
if (fromCqlTypeName(getCqlDataType(columnLabel).asCql(false, false)).isCollection()) {
try {
final ListType listType = (ListType) getCqlDataType(columnLabel);
final Class<?> itemsClass = Class.forName(fromDataType(listType.getElementType())
.asJavaClass().getCanonicalName());
final List<?> resultList = this.currentRow.getList(columnLabel, itemsClass);
if (resultList == null) {
return null;
}
return new ArrayList<>(resultList);
} catch (final ClassNotFoundException e) {
LOG.warn("Error while executing getList()", e);
}
}
return this.currentRow.getList(columnLabel, String.class);
}
/**
* Retrieves the value of the designated column in the current row of this {@code ResultSet} object as a
* {@link LocalDate}.
*
* @param columnIndex The column index (the first column is 1).
* @return The column value. If the value is SQL {@code NULL}, it should return {@code null}.
* @throws SQLException if the columnIndex is not valid; if a database access error occurs or this method is called
* on a closed result set.
*/
public LocalDate getLocalDate(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
return this.currentRow.getLocalDate(columnIndex - 1);
}
@Override
public long getLong(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
if (isCqlType(columnIndex, DataTypeEnum.INT)) {
return this.currentRow.getInt(columnIndex - 1);
} else if (isCqlType(columnIndex, DataTypeEnum.VARINT)) {
final BigInteger bigintValue = currentRow.getBigInteger(columnIndex - 1);
if (bigintValue != null) {
return bigintValue.longValue();
} else {
return 0;
}
} else {
return this.currentRow.getLong(columnIndex - 1);
}
}
@Override
public long getLong(final String columnLabel) throws SQLException {
checkName(columnLabel);
if (isCqlType(columnLabel, DataTypeEnum.INT)) {
return this.currentRow.getInt(columnLabel);
} else if (isCqlType(columnLabel, DataTypeEnum.VARINT)) {
final BigInteger bigintValue = currentRow.getBigInteger(columnLabel);
if (bigintValue != null) {
return bigintValue.longValue();
} else {
return 0;
}
} else {
return this.currentRow.getLong(columnLabel);
}
}
@Override
public Map<?, ?> getMap(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
final DefaultMapType mapType = (DefaultMapType) getCqlDataType(columnIndex);
final Class<?> keysClass = fromDataType(mapType.getKeyType()).javaType;
final Class<?> valuesClass = fromDataType(mapType.getValueType()).javaType;
return this.currentRow.getMap(columnIndex - 1, keysClass, valuesClass);
}
@Override
public Map<?, ?> getMap(final String columnLabel) throws SQLException {
checkName(columnLabel);
final DefaultMapType mapType = (DefaultMapType) getCqlDataType(columnLabel);
final Class<?> keysClass = fromDataType(mapType.getKeyType()).javaType;
final Class<?> valuesClass = fromDataType(mapType.getValueType()).javaType;
return this.currentRow.getMap(columnLabel, keysClass, valuesClass);
}
@Override
public ResultSetMetaData getMetaData() {
return this.metadata;
}
@Override
public NClob getNClob(final int columnIndex) throws SQLException {
return (NClob) getClob(columnIndex);
}
@Override
public NClob getNClob(final String columnLabel) throws SQLException {
return (NClob) getClob(columnLabel);
}
@Override
public Object getObject(final int columnIndex) throws SQLException {
checkIndex(columnIndex);
final DataType cqlDataType = getCqlDataType(columnIndex);
final DataTypeEnum dataType = fromDataType(cqlDataType);
// User-defined types
if (isCqlType(columnIndex, DataTypeEnum.UDT)) {
return this.currentRow.getUdtValue(columnIndex - 1);
}
// Tuples
if (isCqlType(columnIndex, DataTypeEnum.TUPLE)) {
return currentRow.getTupleValue(columnIndex - 1);
}
// Collections: sets, lists, vectors & maps
if (dataType.isCollection()) {
// Sets
if (isCqlType(columnIndex, DataTypeEnum.SET)) {
final SetType setType = (SetType) cqlDataType;
final DataType elementsType = setType.getElementType();
final Set<?> resultSet;
if (elementsType instanceof UserDefinedType) {
resultSet = this.currentRow.getSet(columnIndex - 1,
TypesMap.getTypeForComparator(DataTypeEnum.UDT.asLowercaseCql()).getType());
} else if (elementsType instanceof TupleType) {
resultSet = this.currentRow.getSet(columnIndex - 1,
TypesMap.getTypeForComparator(DataTypeEnum.TUPLE.asLowercaseCql()).getType());
} else {
resultSet = this.currentRow.getSet(columnIndex - 1,
TypesMap.getTypeForComparator(elementsType.asCql(false, false)).getType());
}
if (resultSet == null) {
return null;
}
return new LinkedHashSet<>(resultSet);
}
// Lists
if (isCqlType(columnIndex, DataTypeEnum.LIST)) {
final ListType listType = (ListType) cqlDataType;
final DataType elementsType = listType.getElementType();
final List<?> resultList;
if (elementsType instanceof TupleType) {
resultList = this.currentRow.getList(columnIndex - 1,
TypesMap.getTypeForComparator(DataTypeEnum.TUPLE.asLowercaseCql()).getType());
} else {
resultList = this.currentRow.getList(columnIndex - 1,
TypesMap.getTypeForComparator(elementsType.asCql(false, false)).getType());
}
if (resultList == null) {
return null;
}
return new ArrayList<>(resultList);
}
// Vectors
if (isCqlType(columnIndex, DataTypeEnum.VECTOR)) {
return getVector(columnIndex);
}
// Maps
if (isCqlType(columnIndex, DataTypeEnum.MAP)) {
final MapType mapType = (MapType) cqlDataType;
final DataType keyType = mapType.getKeyType();
final DataType valueType = mapType.getValueType();
Class<?> keyClass = TypesMap.getTypeForComparator(keyType.asCql(false, false)).getType();
if (keyType instanceof UserDefinedType) {
keyClass = TypesMap.getTypeForComparator(DataTypeEnum.UDT.asLowercaseCql()).getType();
} else if (keyType instanceof TupleType) {
keyClass = TypesMap.getTypeForComparator(DataTypeEnum.TUPLE.asLowercaseCql()).getType();
}
Class<?> valueClass = TypesMap.getTypeForComparator(valueType.asCql(false, false)).getType();
if (valueType instanceof UserDefinedType) {
valueClass = TypesMap.getTypeForComparator(DataTypeEnum.UDT.asLowercaseCql()).getType();
} else if (valueType instanceof TupleType) {
valueClass = TypesMap.getTypeForComparator(DataTypeEnum.TUPLE.asLowercaseCql()).getType();
}
final Map<?, ?> resultMap = this.currentRow.getMap(columnIndex - 1, keyClass, valueClass);
if (resultMap == null) {
return null;
}
return new HashMap<>(resultMap);
}
} else {
// Other types.
switch (dataType) {
case VARCHAR:
case ASCII:
case TEXT:
return this.currentRow.getString(columnIndex - 1);
case INT:
case VARINT:
return this.currentRow.getInt(columnIndex - 1);
case SMALLINT:
return this.currentRow.getShort(columnIndex - 1);
case TINYINT:
return this.currentRow.getByte(columnIndex - 1);
case BIGINT:
case COUNTER:
return this.currentRow.getLong(columnIndex - 1);
case BLOB:
return this.currentRow.getByteBuffer(columnIndex - 1);
case BOOLEAN:
return this.currentRow.getBoolean(columnIndex - 1);
case DECIMAL:
return this.currentRow.getBigDecimal(columnIndex - 1);
case DOUBLE:
return this.currentRow.getDouble(columnIndex - 1);
case FLOAT:
return this.currentRow.getFloat(columnIndex - 1);
case INET: