-
Notifications
You must be signed in to change notification settings - Fork 225
/
AbstractCursor.java
1487 lines (1301 loc) · 43.3 KB
/
AbstractCursor.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 to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you 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 org.apache.calcite.avatica.util;
import org.apache.calcite.avatica.AvaticaSite;
import org.apache.calcite.avatica.AvaticaUtils;
import org.apache.calcite.avatica.ColumnMetaData;
import java.io.InputStream;
import java.io.Reader;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URL;
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.Ref;
import java.sql.SQLDataException;
import java.sql.SQLException;
import java.sql.SQLXML;
import java.sql.Struct;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
/**
* Base class for implementing a cursor.
*
* <p>Derived class needs to provide {@link Getter} and can override
* {@link org.apache.calcite.avatica.util.Cursor.Accessor} implementations if it
* wishes.</p>
*/
public abstract class AbstractCursor implements Cursor {
/**
* Slot into which each accessor should write whether the
* value returned was null.
*/
protected final boolean[] wasNull = {false};
protected AbstractCursor() {
}
public boolean wasNull() {
return wasNull[0];
}
public List<Accessor> createAccessors(List<ColumnMetaData> types,
Calendar localCalendar, ArrayImpl.Factory factory) {
List<Accessor> accessors = new ArrayList<>();
for (ColumnMetaData type : types) {
accessors.add(
createAccessor(type, accessors.size(), localCalendar, factory));
}
return accessors;
}
protected Accessor createAccessor(ColumnMetaData columnMetaData, int ordinal,
Calendar localCalendar, ArrayImpl.Factory factory) {
// Create an accessor appropriate to the underlying type; the accessor
// can convert to any type in the same family.
Getter getter = createGetter(ordinal);
return createAccessor(columnMetaData, getter, localCalendar, factory);
}
protected Accessor createAccessor(ColumnMetaData columnMetaData,
Getter getter, Calendar localCalendar, ArrayImpl.Factory factory) {
switch (columnMetaData.type.rep) {
case NUMBER:
switch (columnMetaData.type.id) {
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
case Types.BIGINT:
case Types.REAL:
case Types.FLOAT:
case Types.DOUBLE:
case Types.NUMERIC:
case Types.DECIMAL:
return new NumberAccessor(getter, columnMetaData.scale);
}
}
switch (columnMetaData.type.id) {
case Types.TINYINT:
return new ByteAccessor(getter);
case Types.SMALLINT:
return new ShortAccessor(getter);
case Types.INTEGER:
return new IntAccessor(getter);
case Types.BIGINT:
return new LongAccessor(getter);
case Types.BOOLEAN:
return new BooleanAccessor(getter);
case Types.REAL:
return new FloatAccessor(getter);
case Types.FLOAT:
case Types.DOUBLE:
return new DoubleAccessor(getter);
case Types.DECIMAL:
return new BigDecimalAccessor(getter);
case Types.CHAR:
switch (columnMetaData.type.rep) {
case PRIMITIVE_CHAR:
case CHARACTER:
return new StringFromCharAccessor(getter, columnMetaData.displaySize);
default:
return new FixedStringAccessor(getter, columnMetaData.displaySize);
}
case Types.VARCHAR:
return new StringAccessor(getter);
case Types.BINARY:
case Types.VARBINARY:
switch (columnMetaData.type.rep) {
case STRING:
return new BinaryFromStringAccessor(getter);
default:
return new BinaryAccessor(getter);
}
case Types.DATE:
switch (columnMetaData.type.rep) {
case PRIMITIVE_INT:
case INTEGER:
case NUMBER:
return new DateFromNumberAccessor(getter, localCalendar);
case JAVA_SQL_DATE:
return new DateAccessor(getter);
default:
throw new AssertionError("bad " + columnMetaData.type.rep);
}
case Types.TIME:
switch (columnMetaData.type.rep) {
case PRIMITIVE_INT:
case INTEGER:
case NUMBER:
return new TimeFromNumberAccessor(getter, localCalendar);
case JAVA_SQL_TIME:
return new TimeAccessor(getter);
default:
throw new AssertionError("bad " + columnMetaData.type.rep);
}
case Types.TIMESTAMP:
switch (columnMetaData.type.rep) {
case PRIMITIVE_LONG:
case LONG:
case NUMBER:
return new TimestampFromNumberAccessor(getter, localCalendar);
case JAVA_SQL_TIMESTAMP:
return new TimestampAccessor(getter);
case JAVA_UTIL_DATE:
return new TimestampFromUtilDateAccessor(getter, localCalendar);
default:
throw new AssertionError("bad " + columnMetaData.type.rep);
}
case 2013: // TIME_WITH_TIMEZONE
switch (columnMetaData.type.rep) {
case STRING:
return new StringAccessor(getter);
default:
throw new AssertionError("bad " + columnMetaData.type.rep);
}
case 2014: // TIMESTAMP_WITH_TIMEZONE
switch (columnMetaData.type.rep) {
case STRING:
return new StringAccessor(getter);
default:
throw new AssertionError("bad " + columnMetaData.type.rep);
}
case Types.ARRAY:
final ColumnMetaData.ArrayType arrayType =
(ColumnMetaData.ArrayType) columnMetaData.type;
final SlotGetter componentGetter = new SlotGetter();
final Accessor componentAccessor =
createAccessor(ColumnMetaData.dummy(arrayType.getComponent(), true),
componentGetter, localCalendar, factory);
return new ArrayAccessor(getter, arrayType.getComponent(), componentAccessor,
componentGetter, factory);
case Types.STRUCT:
switch (columnMetaData.type.rep) {
case OBJECT:
final ColumnMetaData.StructType structType =
(ColumnMetaData.StructType) columnMetaData.type;
List<Accessor> accessors = new ArrayList<>();
for (ColumnMetaData column : structType.columns) {
final Getter fieldGetter =
structType.columns.size() == 1
? getter
: new StructGetter(getter, column);
accessors.add(
createAccessor(column, fieldGetter, localCalendar, factory));
}
return new StructAccessor(getter, accessors);
default:
throw new AssertionError("bad " + columnMetaData.type.rep);
}
case Types.JAVA_OBJECT:
case Types.OTHER: // e.g. map
if (columnMetaData.type.getName().startsWith("INTERVAL_")) {
int end = columnMetaData.type.getName().indexOf("(");
if (end < 0) {
end = columnMetaData.type.getName().length();
}
TimeUnitRange range =
TimeUnitRange.valueOf(
columnMetaData.type.getName().substring("INTERVAL_".length(), end));
if (range.monthly()) {
return new IntervalYearMonthAccessor(getter, range);
} else {
return new IntervalDayTimeAccessor(getter, range,
columnMetaData.scale);
}
}
return new ObjectAccessor(getter);
default:
throw new RuntimeException("unknown type " + columnMetaData.type.id);
}
}
protected abstract Getter createGetter(int ordinal);
public abstract boolean next();
/** Accesses a timestamp value as a string.
* The timestamp is in SQL format (e.g. "2013-09-22 22:30:32"),
* not Java format ("2013-09-22 22:30:32.123"). */
private static String timestampAsString(long v, Calendar calendar) {
if (calendar != null) {
v -= calendar.getTimeZone().getOffset(v);
}
return DateTimeUtils.unixTimestampToString(v);
}
/** Accesses a date value as a string, e.g. "2013-09-22". */
private static String dateAsString(int v, Calendar calendar) {
AvaticaUtils.discard(calendar); // time zone shift doesn't make sense
return DateTimeUtils.unixDateToString(v);
}
/** Accesses a time value as a string, e.g. "22:30:32". */
private static String timeAsString(int v, Calendar calendar) {
if (calendar != null) {
v -= calendar.getTimeZone().getOffset(v);
}
return DateTimeUtils.unixTimeToString(v);
}
private static Date longToDate(long v, Calendar calendar) {
if (calendar != null) {
v -= calendar.getTimeZone().getOffset(v);
}
return new Date(v);
}
static Time intToTime(int v, Calendar calendar) {
if (calendar != null) {
v -= calendar.getTimeZone().getOffset(v);
}
return new Time(v);
}
static Timestamp longToTimestamp(long v, Calendar calendar) {
if (calendar != null) {
v -= calendar.getTimeZone().getOffset(v);
}
return new Timestamp(v);
}
/** Implementation of {@link Cursor.Accessor}. */
static class AccessorImpl implements Accessor {
protected final Getter getter;
AccessorImpl(Getter getter) {
assert getter != null;
this.getter = getter;
}
public boolean wasNull() throws SQLException {
return getter.wasNull();
}
public String getString() throws SQLException {
final Object o = getObject();
return o == null ? null : o.toString();
}
public boolean getBoolean() throws SQLException {
return getLong() != 0L;
}
public byte getByte() throws SQLException {
return (byte) getLong();
}
public short getShort() throws SQLException {
return (short) getLong();
}
public int getInt() throws SQLException {
return (int) getLong();
}
public long getLong() throws SQLException {
throw cannotConvert("long");
}
public float getFloat() throws SQLException {
return (float) getDouble();
}
public double getDouble() throws SQLException {
throw cannotConvert("double");
}
public BigDecimal getBigDecimal() throws SQLException {
throw cannotConvert("BigDecimal");
}
public BigDecimal getBigDecimal(int scale) throws SQLException {
throw cannotConvert("BigDecimal with scale");
}
public byte[] getBytes() throws SQLException {
throw cannotConvert("byte[]");
}
public InputStream getAsciiStream() throws SQLException {
throw cannotConvert("InputStream (ascii)");
}
public InputStream getUnicodeStream() throws SQLException {
throw cannotConvert("InputStream (unicode)");
}
public InputStream getBinaryStream() throws SQLException {
throw cannotConvert("InputStream (binary)");
}
public Object getObject() throws SQLException {
return getter.getObject();
}
public Reader getCharacterStream() throws SQLException {
throw cannotConvert("Reader");
}
private SQLException cannotConvert(String targetType) throws SQLException {
return new SQLDataException("cannot convert to " + targetType + " ("
+ this + ")");
}
public Object getObject(Map<String, Class<?>> map) throws SQLException {
throw cannotConvert("Object (with map)");
}
public Ref getRef() throws SQLException {
throw cannotConvert("Ref");
}
public Blob getBlob() throws SQLException {
throw cannotConvert("Blob");
}
public Clob getClob() throws SQLException {
throw cannotConvert("Clob");
}
public Array getArray() throws SQLException {
throw cannotConvert("Array");
}
public Struct getStruct() throws SQLException {
throw cannotConvert("Struct");
}
public Date getDate(Calendar calendar) throws SQLException {
throw cannotConvert("Date");
}
public Time getTime(Calendar calendar) throws SQLException {
throw cannotConvert("Time");
}
public Timestamp getTimestamp(Calendar calendar) throws SQLException {
throw cannotConvert("Timestamp");
}
public URL getURL() throws SQLException {
throw cannotConvert("URL");
}
public NClob getNClob() throws SQLException {
throw cannotConvert("NClob");
}
public SQLXML getSQLXML() throws SQLException {
throw cannotConvert("SQLXML");
}
public String getNString() throws SQLException {
throw cannotConvert("NString");
}
public Reader getNCharacterStream() throws SQLException {
throw cannotConvert("NCharacterStream");
}
public <T> T getObject(Class<T> type) throws SQLException {
throw cannotConvert("Object (with type)");
}
}
/**
* Accessor of exact numeric values. The subclass must implement the
* {@link #getLong()} method.
*/
private abstract static class ExactNumericAccessor extends AccessorImpl {
private ExactNumericAccessor(Getter getter) {
super(getter);
}
public BigDecimal getBigDecimal(int scale) throws SQLException {
final long v = getLong();
if (v == 0 && getter.wasNull()) {
return null;
}
return BigDecimal.valueOf(v).setScale(scale, RoundingMode.DOWN);
}
public BigDecimal getBigDecimal() throws SQLException {
final long val = getLong();
if (val == 0 && getter.wasNull()) {
return null;
}
return BigDecimal.valueOf(val);
}
public double getDouble() throws SQLException {
return getLong();
}
public float getFloat() throws SQLException {
return getLong();
}
public abstract long getLong() throws SQLException;
}
/**
* Accessor that assumes that the underlying value is a {@link Boolean};
* corresponds to {@link java.sql.Types#BOOLEAN}.
*/
private static class BooleanAccessor extends ExactNumericAccessor {
private BooleanAccessor(Getter getter) {
super(getter);
}
public boolean getBoolean() throws SQLException {
Boolean o = (Boolean) getObject();
return o != null && o;
}
public long getLong() throws SQLException {
return getBoolean() ? 1 : 0;
}
}
/**
* Accessor that assumes that the underlying value is a {@link Byte};
* corresponds to {@link java.sql.Types#TINYINT}.
*/
private static class ByteAccessor extends ExactNumericAccessor {
private ByteAccessor(Getter getter) {
super(getter);
}
public byte getByte() throws SQLException {
Object obj = getObject();
if (null == obj) {
return 0;
} else if (obj instanceof Integer) {
return ((Integer) obj).byteValue();
}
return (Byte) obj;
}
public long getLong() throws SQLException {
return getByte();
}
}
/**
* Accessor that assumes that the underlying value is a {@link Short};
* corresponds to {@link java.sql.Types#SMALLINT}.
*/
private static class ShortAccessor extends ExactNumericAccessor {
private ShortAccessor(Getter getter) {
super(getter);
}
public short getShort() throws SQLException {
Object obj = getObject();
if (null == obj) {
return 0;
} else if (obj instanceof Integer) {
return ((Integer) obj).shortValue();
}
return (Short) obj;
}
public long getLong() throws SQLException {
return getShort();
}
}
/**
* Accessor that assumes that the underlying value is an {@link Integer};
* corresponds to {@link java.sql.Types#INTEGER}.
*/
private static class IntAccessor extends ExactNumericAccessor {
private IntAccessor(Getter getter) {
super(getter);
}
public int getInt() throws SQLException {
Integer o = (Integer) super.getObject();
return o == null ? 0 : o;
}
public long getLong() throws SQLException {
return getInt();
}
}
/**
* Accessor that assumes that the underlying value is a {@link Long};
* corresponds to {@link java.sql.Types#BIGINT}.
*/
private static class LongAccessor extends ExactNumericAccessor {
private LongAccessor(Getter getter) {
super(getter);
}
public long getLong() throws SQLException {
Long o = (Long) super.getObject();
return o == null ? 0 : o;
}
}
/**
* Accessor of values that are {@link Double} or null.
*/
private abstract static class ApproximateNumericAccessor
extends AccessorImpl {
private ApproximateNumericAccessor(Getter getter) {
super(getter);
}
public BigDecimal getBigDecimal(int scale) throws SQLException {
final double v = getDouble();
if (v == 0d && getter.wasNull()) {
return null;
}
return BigDecimal.valueOf(v).setScale(scale, RoundingMode.DOWN);
}
public BigDecimal getBigDecimal() throws SQLException {
final double v = getDouble();
if (v == 0 && getter.wasNull()) {
return null;
}
return BigDecimal.valueOf(v);
}
public abstract double getDouble() throws SQLException;
public long getLong() throws SQLException {
return (long) getDouble();
}
}
/**
* Accessor that assumes that the underlying value is a {@link Float};
* corresponds to {@link java.sql.Types#FLOAT}.
*/
private static class FloatAccessor extends ApproximateNumericAccessor {
private FloatAccessor(Getter getter) {
super(getter);
}
public float getFloat() throws SQLException {
Float o = (Float) getObject();
return o == null ? 0f : o;
}
public double getDouble() throws SQLException {
return getFloat();
}
}
/**
* Accessor that assumes that the underlying value is a {@link Double};
* corresponds to {@link java.sql.Types#DOUBLE}.
*/
private static class DoubleAccessor extends ApproximateNumericAccessor {
private DoubleAccessor(Getter getter) {
super(getter);
}
public double getDouble() throws SQLException {
Object obj = getObject();
if (null == obj) {
return 0d;
} else if (obj instanceof BigDecimal) {
return ((BigDecimal) obj).doubleValue();
}
return (Double) obj;
}
}
/**
* Accessor of exact numeric values. The subclass must implement the
* {@link #getLong()} method.
*/
private abstract static class BigNumberAccessor extends AccessorImpl {
private BigNumberAccessor(Getter getter) {
super(getter);
}
protected abstract Number getNumber() throws SQLException;
public double getDouble() throws SQLException {
Number number = getNumber();
return number == null ? 0d : number.doubleValue();
}
public float getFloat() throws SQLException {
Number number = getNumber();
return number == null ? 0f : number.floatValue();
}
public long getLong() throws SQLException {
Number number = getNumber();
return number == null ? 0L : number.longValue();
}
public int getInt() throws SQLException {
Number number = getNumber();
return number == null ? 0 : number.intValue();
}
public short getShort() throws SQLException {
Number number = getNumber();
return number == null ? 0 : number.shortValue();
}
public byte getByte() throws SQLException {
Number number = getNumber();
return number == null ? 0 : number.byteValue();
}
public boolean getBoolean() throws SQLException {
Number number = getNumber();
return number != null && number.doubleValue() != 0;
}
}
/**
* Accessor that assumes that the underlying value is a {@link BigDecimal};
* corresponds to {@link java.sql.Types#DECIMAL}.
*/
private static class BigDecimalAccessor extends BigNumberAccessor {
private BigDecimalAccessor(Getter getter) {
super(getter);
}
protected Number getNumber() throws SQLException {
return (Number) getObject();
}
public BigDecimal getBigDecimal(int scale) throws SQLException {
return (BigDecimal) getObject();
}
public BigDecimal getBigDecimal() throws SQLException {
return (BigDecimal) getObject();
}
}
/**
* Accessor that assumes that the underlying value is a {@link Number};
* corresponds to {@link java.sql.Types#NUMERIC}.
*
* <p>This is useful when numbers have been translated over JSON. JSON
* converts a 0L (0 long) value to the string "0" and back to 0 (0 int).
* So you cannot be sure that the source and target type are the same.
*/
static class NumberAccessor extends BigNumberAccessor {
private final int scale;
NumberAccessor(Getter getter, int scale) {
super(getter);
this.scale = scale;
}
protected Number getNumber() throws SQLException {
return (Number) super.getObject();
}
public BigDecimal getBigDecimal(int scale) throws SQLException {
Number n = getNumber();
if (n == null) {
return null;
}
BigDecimal decimal = AvaticaSite.toBigDecimal(n);
if (0 != scale) {
return decimal.setScale(scale, RoundingMode.UNNECESSARY);
}
return decimal;
}
public BigDecimal getBigDecimal() throws SQLException {
return getBigDecimal(scale);
}
}
/**
* Accessor that assumes that the underlying value is a {@link String};
* corresponds to {@link java.sql.Types#CHAR}
* and {@link java.sql.Types#VARCHAR}.
*/
private static class StringAccessor extends AccessorImpl {
private StringAccessor(Getter getter) {
super(getter);
}
public String getString() throws SQLException {
final Object obj = getObject();
if (obj instanceof String) {
return (String) obj;
}
return null == obj ? null : obj.toString();
}
@Override public byte[] getBytes() throws SQLException {
return super.getBytes();
}
}
/**
* Accessor that assumes that the underlying value is a {@link String};
* corresponds to {@link java.sql.Types#CHAR}.
*/
private static class FixedStringAccessor extends StringAccessor {
protected final Spacer spacer;
private FixedStringAccessor(Getter getter, int length) {
super(getter);
this.spacer = new Spacer(length);
}
public String getString() throws SQLException {
String s = super.getString();
if (s == null) {
return null;
}
return spacer.padRight(s);
}
}
/**
* Accessor that assumes that the underlying value is a {@link String};
* corresponds to {@link java.sql.Types#CHAR}.
*/
private static class StringFromCharAccessor extends FixedStringAccessor {
private StringFromCharAccessor(Getter getter, int length) {
super(getter, length);
}
public String getString() throws SQLException {
Character s = (Character) super.getObject();
if (s == null) {
return null;
}
return spacer.padRight(s.toString());
}
}
/**
* Accessor that assumes that the underlying value is an array of
* {@link org.apache.calcite.avatica.util.ByteString} values;
* corresponds to {@link java.sql.Types#BINARY}
* and {@link java.sql.Types#VARBINARY}.
*/
private static class BinaryAccessor extends AccessorImpl {
private BinaryAccessor(Getter getter) {
super(getter);
}
//FIXME: Protobuf gets byte[]
@Override public byte[] getBytes() throws SQLException {
Object obj = getObject();
if (null == obj) {
return null;
}
if (obj instanceof ByteString) {
return ((ByteString) obj).getBytes();
} else if (obj instanceof String) {
// Need to unwind the base64 for JSON
return ByteString.parseBase64((String) obj);
} else if (obj instanceof byte[]) {
// Protobuf would have a byte array
return (byte[]) obj;
} else {
throw new RuntimeException("Cannot handle " + obj.getClass() + " as bytes");
}
}
@Override public String getString() throws SQLException {
Object o = getObject();
if (null == o) {
return null;
}
if (o instanceof byte[]) {
return new String((byte[]) o, StandardCharsets.UTF_8);
} else if (o instanceof ByteString) {
return ((ByteString) o).toString();
}
throw new IllegalStateException("Unhandled value type: " + o.getClass());
}
}
/**
* Accessor that assumes that the underlying value is a {@link String},
* encoding {@link java.sql.Types#BINARY}
* and {@link java.sql.Types#VARBINARY} values in Base64 format.
*/
private static class BinaryFromStringAccessor extends StringAccessor {
private BinaryFromStringAccessor(Getter getter) {
super(getter);
}
@Override public Object getObject() throws SQLException {
return super.getObject();
}
@Override public byte[] getBytes() throws SQLException {
// JSON sends this as a base64-enc string, protobuf can do binary.
Object obj = getObject();
if (obj instanceof byte[]) {
// If we already have bytes, just send them back.
return (byte[]) obj;
}
return getBase64Decoded();
}
private byte[] getBase64Decoded() throws SQLException {
final String string = super.getString();
if (null == string) {
return null;
}
// Need to base64 decode the string.
return ByteString.parseBase64(string);
}
@Override public String getString() throws SQLException {
final byte[] bytes = getBase64Decoded();
if (null == bytes) {
return null;
}
// Need to base64 decode the string.
return new String(bytes, StandardCharsets.UTF_8);
}
}
/**
* Accessor that assumes that the underlying value is a DATE,
* in its default representation {@code int};
* corresponds to {@link java.sql.Types#DATE}.
*/
private static class DateFromNumberAccessor extends NumberAccessor {
private final Calendar localCalendar;
private DateFromNumberAccessor(Getter getter, Calendar localCalendar) {
super(getter, 0);
this.localCalendar = localCalendar;
}
@Override public Object getObject() throws SQLException {
return getDate(localCalendar);
}
@Override public Date getDate(Calendar calendar) throws SQLException {
final Number v = getNumber();
if (v == null) {
return null;
}
return longToDate(v.longValue() * DateTimeUtils.MILLIS_PER_DAY, calendar);
}
@Override public Timestamp getTimestamp(Calendar calendar) throws SQLException {
final Number v = getNumber();
if (v == null) {
return null;
}
return longToTimestamp(v.longValue() * DateTimeUtils.MILLIS_PER_DAY,
calendar);
}
@Override public String getString() throws SQLException {
final Number v = getNumber();
if (v == null) {
return null;
}
return dateAsString(v.intValue(), null);
}
}
/**
* Accessor that assumes that the underlying value is a Time,
* in its default representation {@code int};
* corresponds to {@link java.sql.Types#TIME}.
*/
private static class TimeFromNumberAccessor extends NumberAccessor {
private final Calendar localCalendar;
private TimeFromNumberAccessor(Getter getter, Calendar localCalendar) {
super(getter, 0);
this.localCalendar = localCalendar;
}
@Override public Object getObject() throws SQLException {
return getTime(localCalendar);
}
@Override public Time getTime(Calendar calendar) throws SQLException {
final Number v = getNumber();
if (v == null) {
return null;
}
return intToTime(v.intValue(), calendar);
}
@Override public Timestamp getTimestamp(Calendar calendar) throws SQLException {
final Number v = getNumber();
if (v == null) {
return null;
}
return longToTimestamp(v.longValue(), calendar);
}
@Override public String getString() throws SQLException {
final Number v = getNumber();
if (v == null) {
return null;
}
return timeAsString(v.intValue(), null);
}
}
/**
* Accessor that assumes that the underlying value is a TIMESTAMP,
* in its default representation {@code long};
* corresponds to {@link java.sql.Types#TIMESTAMP}.
*/
private static class TimestampFromNumberAccessor extends NumberAccessor {
private final Calendar localCalendar;
private TimestampFromNumberAccessor(Getter getter, Calendar localCalendar) {
super(getter, 0);
this.localCalendar = localCalendar;
}
@Override public Object getObject() throws SQLException {
return getTimestamp(localCalendar);
}
@Override public Timestamp getTimestamp(Calendar calendar) throws SQLException {
final Number v = getNumber();