-
Notifications
You must be signed in to change notification settings - Fork 3
/
Row.scala
942 lines (870 loc) · 22 KB
/
Row.scala
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
/*
* Copyright 2016 rdbc contributors
*
* 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 io.rdbc.sapi
import java.time._
import java.util.UUID
import scala.reflect.ClassTag
/** Represents a row of a result returned by a database engine.
*
* This class defines a set of methods that can be used to get values from the
* row either by a column name or by a column index. Each method has a version
* returning an [[scala.Option Option]] to allow null-safe handling of SQL
* `null` values.
*
* @groupname generic Generic getters
* @groupprio generic 10
* @groupdesc generic Methods in this group can be used for fetching values
* of types not supported by rdbc API out of the box.
*
* @groupname unb Unbounded number getters
* @groupprio unb 20
*
* @groupname bool Bool getters
* @groupprio bool 30
*
* @groupname binary Binary getters
* @groupprio binary 40
*
* @groupname char Char getters
* @groupprio char 50
*
* @groupname float Floating point number getters
* @groupprio float 60
*
* @groupname int Integral number getters
* @groupprio int 70
*
* @groupname date Date/time getters
* @groupprio date 80
*
* @groupname string String getters
* @groupprio string 90
*
* @groupname uuid UUID getters
* @groupprio uuid 100
*
* @define exceptions
* Throws:
* - [[io.rdbc.api.exceptions.ConversionException ConversionException]]
* when database value could not be converted to the desired type
* @define nullSafetyNote
* For SQL `null` values, [[io.rdbc.api.exceptions.ConversionException ConversionException]] is thrown.
* For null-safety consider using corresponding `*Opt` method.
* @define returningNone
* For SQL `null` values [[scala.None None]] is returned.
* @define boolValues
* - A single 'T', 'Y' or '1' character values or `1` numeric value are
* considered `true`.
* - A single 'F', 'N' or '0' character values or `0` numeric value are
* considered `false`.
*/
trait Row {
/** Returns an object of type `A` from column with a given index.
*
* $nullSafetyNote
*
* $exceptions
*
* @group generic
* @usecase def col[A](idx: Int): A
* @inheritdoc
*/
def col[A: ClassTag](idx: Int): A
/** Returns an object of type `A` from column with a given index.
*
* $returningNone
*
* $exceptions
*
* @group generic
* @usecase def colOpt[A](idx: Int): Option[A]
* @inheritdoc
*/
def colOpt[A: ClassTag](idx: Int): Option[A]
/** Returns an object of type `A` from column with a given name.
*
* $nullSafetyNote
*
* $exceptions
*
* @group generic
* @usecase def col[A](name: String): A
* @inheritdoc
*/
def col[A: ClassTag](name: String): A
/** Returns an object of type `A` from column with a given name.
*
* $returningNone
*
* $exceptions
*
* @group generic
* @usecase def colOpt[A](name: String): Option[A]
* @inheritdoc
*/
def colOpt[A: ClassTag](name: String): Option[A]
/** Returns a `String` from column with a given name.
*
* $nullSafetyNote
*
* $exceptions
*
* @group string
*/
def str(name: String): String
/** Returns a `String` from column with a given name.
*
* $returningNone
*
* $exceptions
*
* @group string
*/
def strOpt(name: String): Option[String]
/** Returns a `String` from column with a given index.
*
* $nullSafetyNote
*
* $exceptions
*
* @group string
*/
def str(idx: Int): String
/** Returns a `String` from column with a given index.
*
* $returningNone
*
* $exceptions
*
* @group string
*/
def strOpt(idx: Int): Option[String]
/** Returns a boolean value from column with a given name.
*
* $boolValues
*
* $nullSafetyNote
*
* $exceptions
*
* @group bool
*/
def bool(name: String): Boolean
/** Returns a boolean value from column with a given name.
*
* $boolValues
*
* $returningNone
*
* $exceptions
*
* @group bool
*/
def boolOpt(name: String): Option[Boolean]
/** Returns a boolean value from column with a given index.
*
* $boolValues
*
* $nullSafetyNote
*
* $exceptions
*
* @group bool
*/
def bool(idx: Int): Boolean
/** Returns a boolean value from column with a given index.
*
* $boolValues
*
* $returningNone
*
* $exceptions
*
* @group bool
*/
def boolOpt(idx: Int): Option[Boolean]
/** Returns a character from column with a given name.
*
* Varchar types with a single character are convertible to a `Char`.
*
* $nullSafetyNote
*
* $exceptions
*
* @group char
*/
def char(name: String): Char
/** Returns a character from column with a given name.
*
* Varchar types with a single character are convertible to a `Char`.
*
* $returningNone
*
* $exceptions
*
* @group char
*/
def charOpt(name: String): Option[Char]
/** Returns a character from column with a given index.
*
* Varchar types with a single character are convertible to a `Char`.
*
* $nullSafetyNote
*
* $exceptions
*
* @group char
*/
def char(idx: Int): Char
/** Returns a character from column with a given index.
*
* Varchar types with a single character are convertible to a `Char`.
*
* $returningNone
*
* $exceptions
*
* @group char
*/
def charOpt(idx: Int): Option[Char]
/** Returns a `Short` from column with a given name.
*
* All numeric types can be converted to `Short`, but some conversions may
* involve rounding or truncation.
*
* $nullSafetyNote
*
* $exceptions
*
* @group int
*/
def short(name: String): Short
/** Returns a `Short` from column with a given name.
*
* All numeric types can be converted to `Short`, but some conversions may
* involve rounding or truncation.
*
* $returningNone
*
* $exceptions
*
* @group int
*/
def shortOpt(name: String): Option[Short]
/** Returns a `Short` from column with a given index.
*
* All numeric types can be converted to `Short`, but some conversions may
* involve rounding or truncation.
*
* $nullSafetyNote
*
* $exceptions
*
* @group int
*/
def short(idx: Int): Short
/** Returns a `Short` from column with a given index.
*
* All numeric types can be converted to `Short`, but some conversions may
* involve rounding or truncation.
*
* $returningNone
*
* $exceptions
*
* @group int
*/
def shortOpt(idx: Int): Option[Short]
/** Returns an `Int` from column with a given name.
*
* All numeric types can be converted to `Int`, but some conversions may
* involve rounding or truncation.
*
* $nullSafetyNote
*
* $exceptions
*
* @group int
*/
def int(name: String): Int
/** Returns an `Int` from column with a given name.
*
* All numeric types can be converted to `Int`, but some conversions may
* involve rounding or truncation.
*
* $returningNone
*
* $exceptions
*
* @group int
*/
def intOpt(name: String): Option[Int]
/** Returns an `Int` from column with a given index.
*
* All numeric types can be converted to `Int`, but some conversions may
* involve rounding or truncation.
*
* $returningNone
*
* $exceptions
*
* @group int
*/
def int(idx: Int): Int
/** Returns an `Int` from column with a given index.
*
* All numeric types can be converted to `Int`, but some conversions may
* involve rounding or truncation.
*
* $returningNone
*
* $exceptions
*
* @group int
*/
def intOpt(idx: Int): Option[Int]
/** Returns a `Long` from column with a given name.
*
* All numeric types can be converted to `Long`, but some conversions may
* involve rounding or truncation.
*
* $nullSafetyNote
*
* $exceptions
*
* @group int
*/
def long(name: String): Long
/** Returns a `Long` from column with a given name.
*
* All numeric types can be converted to `Long`, but some conversions may
* involve rounding or truncation.
*
* $returningNone
*
* $exceptions
*
* @group int
*/
def longOpt(name: String): Option[Long]
/** Returns a `Long` from column with a given index.
*
* All numeric types can be converted to `Long`, but some conversions may
* involve rounding or truncation.
*
* $nullSafetyNote
*
* $exceptions
*
* @group int
*/
def long(idx: Int): Long
/** Returns a `Long` from column with a given index.
*
* All numeric types can be converted to `Long`, but some conversions may
* involve rounding or truncation.
*
* $returningNone
*
* $exceptions
*
* @group int
*/
def longOpt(idx: Int): Option[Long]
/** Returns a [[scala.math.BigDecimal BigDecimal]] from column with a given name.
*
* All numeric types can be converted to [[scala.math.BigDecimal BigDecimal]], note however that
* NaN value is not representable by a [[scala.math.BigDecimal BigDecimal]]. If you expect values
* to be NaN use `numeric` method instead.
*
* $nullSafetyNote
*
* $exceptions
*
* @group unb
*/
def bigDecimal(name: String): BigDecimal
/** Returns a [[scala.math.BigDecimal BigDecimal]] from column with a given name.
*
* All numeric types can be converted to [[scala.math.BigDecimal BigDecimal]], note however that
* NaN value is not representable by a [[scala.math.BigDecimal BigDecimal]]. If you expect values
* to be NaN use `numeric` method instead.
*
* $returningNone
*
* $exceptions
*
* @group unb
*/
def bigDecimalOpt(name: String): Option[BigDecimal]
/** Returns a [[scala.math.BigDecimal BigDecimal]] from column with a given index.
*
* All numeric types can be converted to [[scala.math.BigDecimal BigDecimal]], note however that
* NaN value is not representable by a [[scala.math.BigDecimal BigDecimal]]. If you expect values
* to be NaN use `numeric` method instead.
*
* $nullSafetyNote
*
* $exceptions
*
* @group unb
*/
def bigDecimal(idx: Int): BigDecimal
/** Returns a [[scala.math.BigDecimal BigDecimal]] from column with a given index.
*
* All numeric types can be converted to [[scala.math.BigDecimal BigDecimal]], note however that
* NaN value is not representable by a [[scala.math.BigDecimal BigDecimal]]. If you expect values
* to be NaN use `numeric` method instead.
*
* $returningNone
*
* $exceptions
*
* @group unb
*/
def bigDecimalOpt(idx: Int): Option[BigDecimal]
/** Returns a [[SqlNumeric]] from column with a given name.
*
* All numeric types can be converted to [[SqlNumeric]].
*
* $nullSafetyNote
*
* $exceptions
*
* @group unb
*/
def numeric(name: String): SqlNumeric
/** Returns a [[SqlNumeric]] from column with a given name.
*
* All numeric types can be converted to [[SqlNumeric]].
*
* $returningNone
*
* $exceptions
*
* @group unb
*/
def numericOpt(name: String): Option[SqlNumeric]
/** Returns a [[SqlNumeric]] from column with a given index.
*
* All numeric types can be converted to [[SqlNumeric]].
*
* $nullSafetyNote
*
* $exceptions
*
* @group unb
*/
def numeric(idx: Int): SqlNumeric
/** Returns a [[SqlNumeric]] from column with a given index.
*
* All numeric types can be converted to [[SqlNumeric]].
*
* $returningNone
*
* $exceptions
*
* @group unb
*/
def numericOpt(idx: Int): Option[SqlNumeric]
/** Returns a `Double` from column with a given name.
*
* All numeric types can be converted to `Double`, but some conversions
* may involve rounding or truncation.
*
* $nullSafetyNote
*
* $exceptions
*
* @group float
*/
def double(name: String): Double
/** Returns a `Double` from column with a given name.
*
* All numeric types can be converted to `Double`, but some conversions may
* involve rounding or truncation.
*
* $returningNone
*
* $exceptions
*
* @group float
*/
def doubleOpt(name: String): Option[Double]
/** Returns a `Double` from column with a given index.
*
* All numeric types can be converted to `Double`, but some conversions
* may involve rounding or truncation.
*
* $nullSafetyNote
*
* $exceptions
*
* @group float
*/
def double(idx: Int): Double
/** Returns a `Double` from column with a given index.
*
* All numeric types can be converted to `Double`, but some conversions may
* involve rounding or truncation.
*
* $returningNone
*
* $exceptions
*
* @group float
*/
def doubleOpt(idx: Int): Option[Double]
/** Returns a `Float` from column with a given name.
*
* All numeric types can be converted to `Float`, but some conversions may
* involve rounding or truncation.
*
* $nullSafetyNote
*
* $exceptions
*
* @group float
*/
def float(name: String): Float
/** Returns a `Float` from column with a given name.
*
* All numeric types can be converted to `Float`, but some conversions may
* involve rounding or truncation.
*
* $returningNone
*
* $exceptions
*
* @group float
*/
def floatOpt(name: String): Option[Float]
/** Returns a `Float` from column with a given index.
*
* All numeric types can be converted to `Float`, but some conversions may
* involve rounding or truncation.
*
* $nullSafetyNote
*
* $exceptions
*
* @group float
*/
def float(idx: Int): Float
/** Returns a `Float` from column with a given index.
*
* All numeric types can be converted to `Float`, but some conversions may
* involve rounding or truncation.
*
* $returningNone
*
* $exceptions
*
* @group float
*/
def floatOpt(idx: Int): Option[Float]
/** Returns an `Instant` from column with a given name.
*
* Note that regular timestamp values are not convertible to an `Instant`
* because timestamp values do not hold a time zone information.
*
* $nullSafetyNote
*
* $exceptions
*
* @group date
*/
def instant(name: String): Instant
/** Returns an `Instant` from column with a given name.
*
* Note that regular timestamp values are not convertible to an `Instant`
* because timestamp values do not hold a time zone information.
*
* $returningNone
*
* $exceptions
*
* @group date
*/
def instantOpt(name: String): Option[Instant]
/** Returns an `Instant` from column with a given index.
*
* Note that regular timestamp values are not convertible to an `Instant`
* because timestamp values do not hold a time zone information.
*
* $nullSafetyNote
*
* $exceptions
*
* @group date
*/
def instant(idx: Int): Instant
/** Returns an `Instant` from column with a given index.
*
* Note that regular timestamp values are not convertible to an `Instant`
* because timestamp values do not hold a time zone information.
*
* $returningNone
*
* $exceptions
*
* @group date
*/
def instantOpt(idx: Int): Option[Instant]
/** Returns a `LocalDateTime` from column with a given name.
*
* For SQL date type that does not hold a time, `LocalDateTime` at start
* of day is returned.
*
* $nullSafetyNote
*
* $exceptions
*
* @group date
*/
def localDateTime(name: String): LocalDateTime
/** Returns a `LocalDateTime` from column with a given name.
*
* For SQL date type that does not hold a time, `LocalDateTime` at start
* of day is returned.
*
* $returningNone
*
* $exceptions
*
* @group date
*/
def localDateTimeOpt(name: String): Option[LocalDateTime]
/** Returns a `LocalDateTime` from column with a given index.
*
* For SQL date type that does not hold a time, `LocalDateTime` at start
* of day is returned.
*
* $nullSafetyNote
*
* $exceptions
*
* @group date
*/
def localDateTime(idx: Int): LocalDateTime
/** Returns a `LocalDateTime` from column with a given index.
*
* For SQL date type that does not hold a time, `LocalDateTime` at start
* of day is returned.
*
* $returningNone
*
* $exceptions
*
* @group date
*/
def localDateTimeOpt(idx: Int): Option[LocalDateTime]
/** Returns a `LocalDate` from column with a given name.
*
* SQL types that represent a date with a time are convertible to
* `LocalDate` - a time part is truncated.
*
* $nullSafetyNote
*
* $exceptions
*
* @group date
*/
def localDate(name: String): LocalDate
/** Returns a `LocalDate` from column with a given name.
*
* SQL types that represent a date with a time are convertible to
* `LocalDate` - a time part is truncated.
*
* $returningNone
*
* $exceptions
*
* @group date
*/
def localDateOpt(name: String): Option[LocalDate]
/** Returns a `LocalDate` from column with a given index.
*
* SQL types that represent a date with a time are convertible to
* `LocalDate` - a time part is truncated.
*
* $nullSafetyNote
*
* $exceptions
*
* @group date
*/
def localDate(idx: Int): LocalDate
/** Returns a `LocalDate` from column with a given index.
*
* SQL types that represent a date with a time are convertible to
* `LocalDate` - a time part is truncated.
*
* $returningNone
*
* $exceptions
*
* @group date
*/
def localDateOpt(idx: Int): Option[LocalDate]
/** Returns a `LocalDate` from column with a given name.
*
* SQL types that represent a date with a time are convertible to
* `LocalTime` - a date part is truncated.
*
* $nullSafetyNote
*
* $exceptions
*
* @group date
*/
def localTime(name: String): LocalTime
/** Returns a `LocalDate` from column with a given name.
*
* SQL types that represent a date with a time are convertible to
* `LocalTime` - a date part is truncated.
*
* $returningNone
*
* $exceptions
*
* @group date
*/
def localTimeOpt(name: String): Option[LocalTime]
/** Returns a `LocalDate` from column with a given index.
*
* SQL types that represent a date with a time are convertible to
* `LocalTime` - a date part is truncated.
*
* $nullSafetyNote
*
* $exceptions
*
* @group date
*/
def localTime(idx: Int): LocalTime
/** Returns a `LocalDate` from column with a given index.
*
* SQL types that represent a date with a time are convertible to
* `LocalTime` - a date part is truncated.
*
* $returningNone
*
* $exceptions
*
* @group date
*/
def localTimeOpt(idx: Int): Option[LocalTime]
/** Returns a byte array from column with a given name.
*
* Note that this method cannot be used to fetch raw value of any type from
* the database, it should be used only to fetch binary data.
*
* $nullSafetyNote
*
* $exceptions
*
* @group binary
*/
def bytes(name: String): Array[Byte]
/** Returns a byte array from column with a given name.
*
* Note that this method cannot be used to fetch raw value of any type from
* the database, it should be used
* only to fetch binary data.
*
* $returningNone
*
* $exceptions
*
* @group binary
*/
def bytesOpt(name: String): Option[Array[Byte]]
/** Returns a byte array from column with a given index.
*
* Note that this method cannot be used to fetch raw value of any type from
* the database, it should be used only to fetch binary data.
*
* $nullSafetyNote
*
* $exceptions
*
* @group binary
*/
def bytes(idx: Int): Array[Byte]
/** Returns a byte array from column with a given index.
*
* Note that this method cannot be used to fetch raw value of any type from
* the database, it should be used
* only to fetch binary data.
*
* $returningNone
*
* $exceptions
*
* @group binary
*/
def bytesOpt(idx: Int): Option[Array[Byte]]
/** Returns an `UUID` from column with a given name.
*
* A string type with a standard UUID representation as defined by
* `UUID.fromString` is convertible to UUID.
*
* $nullSafetyNote
*
* $exceptions
*
* @group uuid
*/
def uuid(name: String): UUID
/** Returns an `UUID` from column with a given name.
*
* A string type with a standard UUID representation as defined by
* `UUID.fromString` is convertible to UUID.
*
* $returningNone
*
* $exceptions
*
* @group uuid
*/
def uuidOpt(name: String): Option[UUID]
/** Returns an `UUID` from column with a given index.
*
* A string type with a standard UUID representation as defined by
* `UUID.fromString` is convertible to UUID.
*
* $nullSafetyNote
*
* $exceptions
*
* @group uuid
*/
def uuid(idx: Int): UUID
/** Returns an `UUID` from column with a given index.
*
* A string type with a standard UUID representation as defined by
* `UUID.fromString` is convertible to UUID.
*
* $returningNone
*
* $exceptions
*
* @group uuid
*/
def uuidOpt(idx: Int): Option[UUID]
//TODO support arrays?
}