forked from SAP/go-hdb
/
field.go
774 lines (650 loc) · 16.9 KB
/
field.go
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
/*
Copyright 2014 SAP SE
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 protocol
import (
"database/sql/driver"
"fmt"
"math"
"sort"
"time"
"github.com/SAP/go-hdb/internal/bufio"
"github.com/SAP/go-hdb/internal/unicode/cesu8"
)
var test uint32
const (
realNullValue uint32 = ^uint32(0)
doubleNullValue uint64 = ^uint64(0)
)
const noFieldName uint32 = 0xFFFFFFFF
type uint32Slice []uint32
func (p uint32Slice) Len() int { return len(p) }
func (p uint32Slice) Less(i, j int) bool { return p[i] < p[j] }
func (p uint32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
func (p uint32Slice) sort() { sort.Sort(p) }
type fieldNames map[uint32]string
func newFieldNames() fieldNames {
return make(map[uint32]string)
}
func (f fieldNames) addOffset(offset uint32) {
if offset != noFieldName {
f[offset] = ""
}
}
func (f fieldNames) name(offset uint32) string {
if name, ok := f[offset]; ok {
return name
}
return ""
}
func (f fieldNames) setName(offset uint32, name string) {
f[offset] = name
}
func (f fieldNames) sortOffsets() []uint32 {
offsets := make([]uint32, 0, len(f))
for k := range f {
offsets = append(offsets, k)
}
uint32Slice(offsets).sort()
return offsets
}
// FieldValues contains rows read from database.
type FieldValues struct {
rows int
cols int
values []driver.Value
}
func newFieldValues() *FieldValues {
return &FieldValues{}
}
func (f *FieldValues) String() string {
return fmt.Sprintf("rows %d columns %d", f.rows, f.cols)
}
func (f *FieldValues) resize(rows, cols int) {
f.rows, f.cols = rows, cols
f.values = make([]driver.Value, rows*cols)
}
// NumRow returns the number of rows available in FieldValues.
func (f *FieldValues) NumRow() int {
return f.rows
}
// Row fills the dest value slice with row data at index idx.
func (f *FieldValues) Row(idx int, dest []driver.Value) {
copy(dest, f.values[idx*f.cols:(idx+1)*f.cols])
}
const (
tinyintFieldSize = 1
smallintFieldSize = 2
intFieldSize = 4
bigintFieldSize = 8
realFieldSize = 4
doubleFieldSize = 8
dateFieldSize = 4
timeFieldSize = 4
timestampFieldSize = dateFieldSize + timeFieldSize
longdateFieldSize = 8
seconddateFieldSize = 8
daydateFieldSize = 4
secondtimeFieldSize = 4
decimalFieldSize = 16
lobInputDescriptorSize = 9
)
func fieldSize(tc TypeCode, arg driver.NamedValue) (int, error) {
v := arg.Value
if v == nil { //HDB bug: secondtime null value --> see writeField
return 0, nil
}
switch tc {
case tcTinyint:
return tinyintFieldSize, nil
case tcSmallint:
return smallintFieldSize, nil
case tcInteger:
return intFieldSize, nil
case tcBigint:
return bigintFieldSize, nil
case tcReal:
return realFieldSize, nil
case tcDouble:
return doubleFieldSize, nil
case tcDate:
return dateFieldSize, nil
case tcTime:
return timeFieldSize, nil
case tcTimestamp:
return timestampFieldSize, nil
case tcLongdate:
return longdateFieldSize, nil
case tcSeconddate:
return seconddateFieldSize, nil
case tcDaydate:
return daydateFieldSize, nil
case tcSecondtime:
return secondtimeFieldSize, nil
case tcDecimal:
return decimalFieldSize, nil
case tcChar, tcVarchar, tcString:
switch v := v.(type) {
case []byte:
return bytesSize(len(v))
case string:
return bytesSize(len(v))
default:
outLogger.Fatalf("data type %s mismatch %T", tc, v)
}
case tcNchar, tcNvarchar, tcNstring:
switch v := v.(type) {
case []byte:
return bytesSize(cesu8.Size(v))
case string:
return bytesSize(cesu8.StringSize(v))
default:
outLogger.Fatalf("data type %s mismatch %T", tc, v)
}
case tcBinary, tcVarbinary:
v, ok := v.([]byte)
if !ok {
outLogger.Fatalf("data type %s mismatch %T", tc, v)
}
return bytesSize(len(v))
case tcBlob, tcClob, tcNclob:
return lobInputDescriptorSize, nil
}
outLogger.Fatalf("data type %s not implemented", tc)
return 0, nil
}
func readField(session *Session, rd *bufio.Reader, tc TypeCode) (interface{}, error) {
switch tc {
case tcTinyint, tcSmallint, tcInteger, tcBigint:
if !rd.ReadBool() { //null value
return nil, nil
}
switch tc {
case tcTinyint:
return int64(rd.ReadB()), nil
case tcSmallint:
return int64(rd.ReadInt16()), nil
case tcInteger:
return int64(rd.ReadInt32()), nil
case tcBigint:
return rd.ReadInt64(), nil
}
case tcReal:
v := rd.ReadUint32()
if v == realNullValue {
return nil, nil
}
return float64(math.Float32frombits(v)), nil
case tcDouble:
v := rd.ReadUint64()
if v == doubleNullValue {
return nil, nil
}
return math.Float64frombits(v), nil
case tcDate:
year, month, day, null := readDate(rd)
if null {
return nil, nil
}
return time.Date(year, month, day, 0, 0, 0, 0, time.UTC), nil
// time read gives only seconds (cut), no milliseconds
case tcTime:
hour, minute, nanosecs, null := readTime(rd)
if null {
return nil, nil
}
return time.Date(1, 1, 1, hour, minute, 0, nanosecs, time.UTC), nil
case tcTimestamp:
year, month, day, dateNull := readDate(rd)
hour, minute, nanosecs, timeNull := readTime(rd)
if dateNull || timeNull {
return nil, nil
}
return time.Date(year, month, day, hour, minute, 0, nanosecs, time.UTC), nil
case tcLongdate:
time, null := readLongdate(rd)
if null {
return nil, nil
}
return time, nil
case tcSeconddate:
time, null := readSeconddate(rd)
if null {
return nil, nil
}
return time, nil
case tcDaydate:
time, null := readDaydate(rd)
if null {
return nil, nil
}
return time, nil
case tcSecondtime:
time, null := readSecondtime(rd)
if null {
return nil, nil
}
return time, nil
case tcDecimal:
b, null := readDecimal(rd)
if null {
return nil, nil
}
return b, nil
case tcChar, tcVarchar:
value, null := readBytes(rd)
if null {
return nil, nil
}
return value, nil
case tcNchar, tcNvarchar:
value, null := readUtf8(rd)
if null {
return nil, nil
}
return value, nil
case tcBinary, tcVarbinary:
value, null := readBytes(rd)
if null {
return nil, nil
}
return value, nil
case tcBlob, tcClob, tcNclob:
null, writer, err := readLob(session, rd, tc)
if null {
return nil, nil
}
return writer, err
}
outLogger.Fatalf("read field: type code %s not implemented", tc)
return nil, nil
}
func writeField(wr *bufio.Writer, tc TypeCode, arg driver.NamedValue) error {
v := arg.Value
//HDB bug: secondtime null value cannot be set by setting high byte
// trying so, gives
// SQL HdbError 1033 - error while parsing protocol: no such data type: type_code=192, index=2
// null value
//if v == nil && tc != tcSecondtime
if v == nil {
wr.WriteB(byte(tc) | 0x80) //set high bit
return nil
}
// type code
wr.WriteB(byte(tc))
switch tc {
default:
outLogger.Fatalf("write field: type code %s not implemented", tc)
case tcTinyint, tcSmallint, tcInteger, tcBigint:
var i64 int64
switch v := v.(type) {
default:
return fmt.Errorf("invalid argument type %T", v)
case bool:
if v {
i64 = 1
} else {
i64 = 0
}
case int64:
i64 = v
}
switch tc {
case tcTinyint:
wr.WriteB(byte(i64))
case tcSmallint:
wr.WriteInt16(int16(i64))
case tcInteger:
wr.WriteInt32(int32(i64))
case tcBigint:
wr.WriteInt64(i64)
}
case tcReal:
f64, ok := v.(float64)
if !ok {
return fmt.Errorf("invalid argument type %T", v)
}
wr.WriteFloat32(float32(f64))
case tcDouble:
f64, ok := v.(float64)
if !ok {
return fmt.Errorf("invalid argument type %T", v)
}
wr.WriteFloat64(f64)
case tcDate:
t, ok := v.(time.Time)
if !ok {
return fmt.Errorf("invalid argument type %T", v)
}
writeDate(wr, t)
case tcTime:
t, ok := v.(time.Time)
if !ok {
return fmt.Errorf("invalid argument type %T", v)
}
writeTime(wr, t)
case tcTimestamp:
t, ok := v.(time.Time)
if !ok {
return fmt.Errorf("invalid argument type %T", v)
}
writeDate(wr, t)
writeTime(wr, t)
case tcLongdate:
t, ok := v.(time.Time)
if !ok {
return fmt.Errorf("invalid argument type %T", v)
}
writeLongdate(wr, t)
case tcSeconddate:
t, ok := v.(time.Time)
if !ok {
return fmt.Errorf("invalid argument type %T", v)
}
writeSeconddate(wr, t)
case tcDaydate:
t, ok := v.(time.Time)
if !ok {
return fmt.Errorf("invalid argument type %T", v)
}
writeDaydate(wr, t)
case tcSecondtime:
// HDB bug: write null value explicite
if v == nil {
wr.WriteInt32(86401)
return nil
}
t, ok := v.(time.Time)
if !ok {
return fmt.Errorf("invalid argument type %T", v)
}
writeSecondtime(wr, t)
case tcDecimal:
b, ok := v.([]byte)
if !ok {
return fmt.Errorf("invalid argument type %T", v)
}
if len(b) != 16 {
return fmt.Errorf("invalid argument length %d of type %T - expected %d", len(b), v, 16)
}
wr.Write(b)
case tcChar, tcVarchar, tcString:
switch v := v.(type) {
case []byte:
writeBytes(wr, v)
case string:
writeString(wr, v)
default:
return fmt.Errorf("invalid argument type %T", v)
}
case tcNchar, tcNvarchar, tcNstring:
switch v := v.(type) {
case []byte:
writeUtf8Bytes(wr, v)
case string:
writeUtf8String(wr, v)
default:
return fmt.Errorf("invalid argument type %T", v)
}
case tcBinary, tcVarbinary:
v, ok := v.([]byte)
if !ok {
return fmt.Errorf("invalid argument type %T", v)
}
writeBytes(wr, v)
case tcBlob, tcClob, tcNclob:
writeLob(wr)
}
return nil
}
// null values: most sig bit unset
// year: unset second most sig bit (subtract 2^15)
// --> read year as unsigned
// month is 0-based
// day is 1 byte
func readDate(rd *bufio.Reader) (int, time.Month, int, bool) {
year := rd.ReadUint16()
null := ((year & 0x8000) == 0) //null value
year &= 0x3fff
month := rd.ReadInt8()
month++
day := rd.ReadInt8()
return int(year), time.Month(month), int(day), null
}
// year: set most sig bit
// month 0 based
func writeDate(wr *bufio.Writer, t time.Time) {
//store in utc
utc := t.In(time.UTC)
year, month, day := utc.Date()
wr.WriteUint16(uint16(year) | 0x8000)
wr.WriteInt8(int8(month) - 1)
wr.WriteInt8(int8(day))
}
func readTime(rd *bufio.Reader) (int, int, int, bool) {
hour := rd.ReadB()
null := (hour & 0x80) == 0 //null value
hour &= 0x7f
minute := rd.ReadInt8()
millisecs := rd.ReadUint16()
nanosecs := int(millisecs) * 1000000
return int(hour), int(minute), nanosecs, null
}
func writeTime(wr *bufio.Writer, t time.Time) {
//store in utc
utc := t.UTC()
wr.WriteB(byte(utc.Hour()) | 0x80)
wr.WriteInt8(int8(utc.Minute()))
millisecs := utc.Second()*1000 + utc.Round(time.Millisecond).Nanosecond()/1000000
wr.WriteUint16(uint16(millisecs))
}
var zeroTime = time.Date(1, time.January, 1, 0, 0, 0, 0, time.UTC)
func readLongdate(rd *bufio.Reader) (time.Time, bool) {
longdate := rd.ReadInt64()
if longdate == 3155380704000000001 { // null value
return zeroTime, true
}
return convertLongdateToTime(longdate), false
}
func writeLongdate(wr *bufio.Writer, t time.Time) {
wr.WriteInt64(convertTimeToLongdate(t))
}
func readSeconddate(rd *bufio.Reader) (time.Time, bool) {
seconddate := rd.ReadInt64()
if seconddate == 315538070401 { // null value
return zeroTime, true
}
return convertSeconddateToTime(seconddate), false
}
func writeSeconddate(wr *bufio.Writer, t time.Time) {
wr.WriteInt64(convertTimeToSeconddate(t))
}
func readDaydate(rd *bufio.Reader) (time.Time, bool) {
daydate := rd.ReadInt32()
if daydate == 3652062 { // null value
return zeroTime, true
}
return convertDaydateToTime(int64(daydate)), false
}
func writeDaydate(wr *bufio.Writer, t time.Time) {
wr.WriteInt32(int32(convertTimeToDayDate(t)))
}
func readSecondtime(rd *bufio.Reader) (time.Time, bool) {
secondtime := rd.ReadInt32()
if secondtime == 86401 { // null value
return zeroTime, true
}
return convertSecondtimeToTime(int(secondtime)), false
}
func writeSecondtime(wr *bufio.Writer, t time.Time) {
wr.WriteInt32(int32(convertTimeToSecondtime(t)))
}
// nanosecond: HDB - 7 digits precision (not 9 digits)
func convertTimeToLongdate(t time.Time) int64 {
t = t.UTC()
return (((((((int64(convertTimeToDayDate(t))-1)*24)+int64(t.Hour()))*60)+int64(t.Minute()))*60)+int64(t.Second()))*10000000 + int64(t.Nanosecond()/100) + 1
}
func convertLongdateToTime(longdate int64) time.Time {
const dayfactor = 10000000 * 24 * 60 * 60
longdate--
d := (longdate % dayfactor) * 100
t := convertDaydateToTime((longdate / dayfactor) + 1)
return t.Add(time.Duration(d))
}
func convertTimeToSeconddate(t time.Time) int64 {
t = t.UTC()
return (((((int64(convertTimeToDayDate(t))-1)*24)+int64(t.Hour()))*60)+int64(t.Minute()))*60 + int64(t.Second()) + 1
}
func convertSeconddateToTime(seconddate int64) time.Time {
const dayfactor = 24 * 60 * 60
seconddate--
d := (seconddate % dayfactor) * 1000000000
t := convertDaydateToTime((seconddate / dayfactor) + 1)
return t.Add(time.Duration(d))
}
const julianHdb = 1721423 // 1 January 0001 00:00:00 (1721424) - 1
func convertTimeToDayDate(t time.Time) int64 {
return int64(timeToJulianDay(t) - julianHdb)
}
func convertDaydateToTime(daydate int64) time.Time {
return julianDayToTime(int(daydate) + julianHdb)
}
func convertTimeToSecondtime(t time.Time) int {
t = t.UTC()
return (t.Hour()*60+t.Minute())*60 + t.Second() + 1
}
func convertSecondtimeToTime(secondtime int) time.Time {
return time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC).Add(time.Duration(int64(secondtime-1) * 1000000000))
}
func readDecimal(rd *bufio.Reader) ([]byte, bool) {
b := make([]byte, 16)
rd.ReadFull(b)
if (b[15] & 0x70) == 0x70 { //null value (bit 4,5,6 set)
return nil, true
}
return b, false
}
// string / binary length indicators
const (
bytesLenIndNullValue byte = 255
bytesLenIndSmall byte = 245
bytesLenIndMedium byte = 246
bytesLenIndBig byte = 247
)
func bytesSize(size int) (int, error) { //size + length indicator
switch {
default:
return 0, fmt.Errorf("max string length %d exceeded %d", math.MaxInt32, size)
case size <= int(bytesLenIndSmall):
return size + 1, nil
case size <= math.MaxInt16:
return size + 3, nil
case size <= math.MaxInt32:
return size + 5, nil
}
}
func readBytesSize(rd *bufio.Reader) (int, bool) {
ind := rd.ReadB() //length indicator
switch {
default:
return 0, false
case ind == bytesLenIndNullValue:
return 0, true
case ind <= bytesLenIndSmall:
return int(ind), false
case ind == bytesLenIndMedium:
return int(rd.ReadInt16()), false
case ind == bytesLenIndBig:
return int(rd.ReadInt32()), false
}
}
func writeBytesSize(wr *bufio.Writer, size int) error {
switch {
default:
return fmt.Errorf("max argument length %d of string exceeded", size)
case size <= int(bytesLenIndSmall):
wr.WriteB(byte(size))
case size <= math.MaxInt16:
wr.WriteB(bytesLenIndMedium)
wr.WriteInt16(int16(size))
case size <= math.MaxInt32:
wr.WriteB(bytesLenIndBig)
wr.WriteInt32(int32(size))
}
return nil
}
func readBytes(rd *bufio.Reader) ([]byte, bool) {
size, null := readBytesSize(rd)
if null {
return nil, true
}
b := make([]byte, size)
rd.ReadFull(b)
return b, false
}
func readUtf8(rd *bufio.Reader) ([]byte, bool) {
size, null := readBytesSize(rd)
if null {
return nil, true
}
b := rd.ReadCesu8(size)
return b, false
}
// strings with one byte length
func readShortUtf8(rd *bufio.Reader) ([]byte, int) {
size := rd.ReadB()
b := rd.ReadCesu8(int(size))
return b, int(size)
}
func writeBytes(wr *bufio.Writer, b []byte) {
writeBytesSize(wr, len(b))
wr.Write(b)
}
func writeString(wr *bufio.Writer, s string) {
writeBytesSize(wr, len(s))
wr.WriteString(s)
}
func writeUtf8Bytes(wr *bufio.Writer, b []byte) {
size := cesu8.Size(b)
writeBytesSize(wr, size)
wr.WriteCesu8(b)
}
func writeUtf8String(wr *bufio.Writer, s string) {
size := cesu8.StringSize(s)
writeBytesSize(wr, size)
wr.WriteStringCesu8(s)
}
func readLob(s *Session, rd *bufio.Reader, tc TypeCode) (bool, lobChunkWriter, error) {
rd.ReadInt8() // type code (is int here)
opt := rd.ReadInt8()
null := (lobOptions(opt) & loNullindicator) != 0
if null {
return true, nil, nil
}
eof := (lobOptions(opt) & loLastdata) != 0
rd.Skip(2)
charLen := rd.ReadInt64()
byteLen := rd.ReadInt64()
id := rd.ReadUint64()
chunkLen := rd.ReadInt32()
lobChunkWriter := newLobChunkWriter(tc.isCharBased(), s, locatorID(id), charLen, byteLen)
if err := lobChunkWriter.write(rd, int(chunkLen), eof); err != nil {
return null, lobChunkWriter, err
}
return null, lobChunkWriter, nil
}
// TODO: first write: add content? - actually no data transferred
func writeLob(wr *bufio.Writer) {
wr.WriteB(0)
wr.WriteInt32(0)
wr.WriteInt32(0)
}