/
field.go
1550 lines (1322 loc) · 38.8 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
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
// SPDX-FileCopyrightText: 2020 SAP SE
// SPDX-FileCopyrightText: 2021 SAP SE
// SPDX-FileCopyrightText: 2022 SAP SE
// SPDX-FileCopyrightText: 2023 SAP SE
//
// SPDX-License-Identifier: Apache-2.0
// The types in this file are format fields for TDS_PARAMFMT,
// TDS_PARAMFMT2, TDS_ROWFMT, TDS_ROWFMT2 and data fields for
// TDS_PARAMS and TDS_ROW.
//
// To make handling and identification of the different data types
// easier in Go each data type has their own field structure, created by
// embedding abstract types.
//
// Note: The data types are embedded instead of aliased to provide the
// methods of the embedded types - aliasing does not provide access to
// the methods of the source type.
package tds
import (
"fmt"
"github.com/SAP/go-dblib/asetypes"
)
// Format and data status
// Both Param- and RowFmtStatus are uints communicated using
// TDS_PARAMFMT* and TDS_ROWFMT*. Depending on the token they have
// slightly different meanings.
//
// fmtStatus is only used for legibility in the code when e.g. checking
// for column status or if null types are allowed. The methods setting
// and getting status convert it from and to fmtStatus.
type fmtStatus uint
const (
tdsFmtColumnStatus fmtStatus = 0x8
// TODO: Nulltypes
// tdsFmtNullAllowed fmtStatus = 0x20
)
//go:generate stringer -type=ParamFmtStatus
// ParamFmtStatus is the type for bitmask values of a ParamFmt status.
type ParamFmtStatus uint
const (
TDS_PARAM_NOSTATUS ParamFmtStatus = 0x0
TDS_PARAM_RETURN ParamFmtStatus = 0x1
TDS_PARAM_COLUMNSTATUS ParamFmtStatus = 0x8
TDS_PARAM_NULLALLOWED ParamFmtStatus = 0x20
)
//go:generate stringer -type=RowFmtStatus
// RowFmtStatus is the type for bitmask values of a RowFmt status.
type RowFmtStatus uint
const (
TDS_ROW_NOSTATUS RowFmtStatus = 0x0
TDS_ROW_HIDDEN RowFmtStatus = 0x1
TDS_ROW_KEY RowFmtStatus = 0x2
TDS_ROW_VERSION RowFmtStatus = 0x4
TDS_ROW_COLUMNSTATUS RowFmtStatus = 0x8
TDS_ROW_UPDATEABLE RowFmtStatus = 0x10
TDS_ROW_NULLALLOWED RowFmtStatus = 0x20
TDS_ROW_IDENTITY RowFmtStatus = 0x40
TDS_ROW_PADCHAR RowFmtStatus = 0x80
)
// DataStatus defines the valid states of data in fields.
type DataStatus uint
const (
TDS_DATA_NONNULL DataStatus = 0x0
TDS_DATA_NULL DataStatus = 0x1
TDS_DATA_ZEROLENGTHNONNULL DataStatus = 0x2
TDS_DATA_RESERVED DataStatus = 0xfc
)
// Interfaces
// FieldFmt defines the interface for datatype formats.
type FieldFmt interface {
// Format information as sent to or received from TDS server
DataType() asetypes.DataType
SetDataType(asetypes.DataType)
SetName(string)
Name() string
// specific to TDS_ROWFMT2
SetColumnLabel(string)
ColumnLabel() string
SetCatalogue(string)
Catalogue() string
SetSchema(string)
Schema() string
SetTable(string)
Table() string
SetStatus(uint)
Status() uint
SetUserType(int32)
UserType() int32
SetLocaleInfo(string)
LocaleInfo() string
// Interface methods for go-ase
// Returns true if the data type has a fixed length.
IsFixedLength() bool
// The return value of LengthBytes depends on IsFixedLength.
// If the data type has a fixed length LengthBytes returns the
// total number of bytes of the data portion (not the entire data
// field - only the actual data).
// If the data type has a variable length LengthBytes returns the
// number of bytes to be read from the data stream for the length in
// bytes of the data portion.
LengthBytes() int
// Length returns the maximum length of the column
// TODO: is this actually required when sending from client?
MaxLength() int64
setMaxLength(int64)
// DisplayMaxLength returns a best guess of the maximum length
// required to display the values of this column.
DisplayMaxLength() int64
setDisplayMaxLength(int64)
// ReadFrom reads bytes from the passed channel until either the
// channel is closed or the package has all required information.
// The read bytes are usually stored in fieldFmt* structs.
ReadFrom(BytesChannel) (int, error)
// WriteTo writes bytes to the passed channel until either the
// channel is closed or the package has written all required
// information. The information are usually based on fieldFmt*
// structs.
WriteTo(BytesChannel) (int, error)
FormatByteLength() int
}
// FieldData defines the interface for datatype data.
type FieldData interface {
// Format information send by TDS server
Status() DataStatus
// Interface methods for go-ase
Format() FieldFmt
setFormat(FieldFmt)
// ReadFrom reads bytes from the passed channel until either the
// channel is closed or the package has all required information.
// The read bytes are usually stored in fieldData* structs.
ReadFrom(BytesChannel) (int, error)
// WriteTo writes bytes to the passed channel until either the
// channel is closed or the package has written all required
// information. The information are usually based on fieldData*
// structs.
WriteTo(BytesChannel) (int, error)
Value() interface{}
SetValue(interface{})
}
// Base structs and methods
type fieldFmtBase struct {
dataType asetypes.DataType
name string
// specific to TDS_ROWFMT2
// wide_row controls if the TDS_ROWFMT2 specific members are filled
// and written. It is set by TDS_ROWFMT2 when creating a field.
// Marked as comment to prevent failure of golangci-lint
// wide_row bool
columnLabel string
catalogue string
schema string
table string
status fmtStatus
userType int32
localeInfo string
// length is the maximum length of the data type
maxLength int64
// displayMaxLength is set manually
displayMaxLength int64
}
// DataType implements the tds.FieldFmt interface.
func (field fieldFmtBase) DataType() asetypes.DataType {
return field.dataType
}
func (field *fieldFmtBase) SetDataType(t asetypes.DataType) {
field.dataType = t
}
// SetName implements the tds.FieldFmt interface.
func (field *fieldFmtBase) SetName(name string) {
field.name = name
}
// Name implements the tds.FieldFmt interface.
func (field fieldFmtBase) Name() string {
return field.name
}
// SetColumnLabel implements the tds.FieldFmt interface.
func (field *fieldFmtBase) SetColumnLabel(columnLabel string) {
field.columnLabel = columnLabel
}
// ColumnLabel implements the tds.FieldFmt interface.
func (field fieldFmtBase) ColumnLabel() string {
return field.columnLabel
}
// SetCatalogue implements the tds.FieldFmt interface.
func (field *fieldFmtBase) SetCatalogue(catalogue string) {
field.catalogue = catalogue
}
// Catalogue implements the tds.FieldFmt interface.
func (field fieldFmtBase) Catalogue() string {
return field.catalogue
}
// SetSchema implements the tds.FieldFmt interface.
func (field *fieldFmtBase) SetSchema(schema string) {
field.schema = schema
}
// Schema implements the tds.FieldFmt interface.
func (field fieldFmtBase) Schema() string {
return field.schema
}
// SetTable implements the tds.FieldFmt interface.
func (field *fieldFmtBase) SetTable(table string) {
field.table = table
}
// Table implements the tds.FieldFmt interface.
func (field fieldFmtBase) Table() string {
return field.table
}
// SetStatus implements the tds.FieldFmt interface.
func (field *fieldFmtBase) SetStatus(status uint) {
field.status = fmtStatus(status)
}
// Status implements the tds.FieldFmt interface.
func (field fieldFmtBase) Status() uint {
return uint(field.status)
}
// SetUserType implements the tds.FieldFmt interface.
func (field *fieldFmtBase) SetUserType(userType int32) {
field.userType = userType
}
// UserType implements the tds.FieldFmt interface.
func (field fieldFmtBase) UserType() int32 {
return field.userType
}
// SetLocaleInfo implements the tds.FieldFmt interface.
func (field *fieldFmtBase) SetLocaleInfo(localeInfo string) {
field.localeInfo = localeInfo
}
// LocaleInfo implements the tds.FieldFmt interface.
func (field fieldFmtBase) LocaleInfo() string {
return field.localeInfo
}
// IsFixedLength implements the tds.FieldFmt interface.
func (field fieldFmtBase) IsFixedLength() bool {
return field.DataType().ByteSize() != -1
}
// LengthBytes implements the tds.FieldFmt interface.
func (field fieldFmtBase) LengthBytes() int {
if field.IsFixedLength() {
return field.DataType().ByteSize()
}
return field.DataType().LengthBytes()
}
func (field *fieldFmtBase) setMaxLength(i int64) {
field.maxLength = i
}
// MaxLength implements the tds.FieldFmt interface.
func (field fieldFmtBase) MaxLength() int64 {
return field.maxLength
}
func (field *fieldFmtBase) setDisplayMaxLength(i int64) {
field.displayMaxLength = i
}
// DisplayMaxLength implements the tds.FieldFmt interface.
func (field fieldFmtBase) DisplayMaxLength() int64 {
if field.displayMaxLength > 0 {
return field.displayMaxLength
}
if field.MaxLength() > 0 && field.MaxLength() <= 30 {
return field.MaxLength()
}
return 30
}
func (field *fieldFmtBase) readFromBase(ch BytesChannel) (int, error) {
if field.IsFixedLength() {
return 0, nil
}
length, err := readLengthBytes(ch, field.LengthBytes())
if err != nil {
return 0, ErrNotEnoughBytes
}
field.maxLength = int64(length)
return field.LengthBytes(), nil
}
func (field fieldFmtBase) writeToBase(ch BytesChannel) (int, error) {
if field.IsFixedLength() {
return 0, nil
}
return field.LengthBytes(), writeLengthBytes(ch, field.LengthBytes(), field.MaxLength())
}
type fieldDataBase struct {
fmt FieldFmt
status DataStatus
value interface{}
}
func (field *fieldDataBase) setFormat(f FieldFmt) {
field.fmt = f
}
// Format implements the tds.FieldData interface.
func (field fieldDataBase) Format() FieldFmt {
return field.fmt
}
// Status implements the tds.FieldData interface.
func (field fieldDataBase) Status() DataStatus {
return field.status
}
// Value implements the tds.FieldData interface.
func (field *fieldDataBase) Value() interface{} {
// TODO set a default?
return field.value
}
// SetValue implements the tds.FieldData interface.
func (field *fieldDataBase) SetValue(value interface{}) {
field.value = value
}
func (field *fieldDataBase) readFromStatus(ch BytesChannel) (int, error) {
if fmtStatus(field.fmt.Status())&tdsFmtColumnStatus != tdsFmtColumnStatus {
return 0, nil
}
status, err := ch.Uint8()
if err != nil {
return 0, ErrNotEnoughBytes
}
field.status = DataStatus(status)
return 1, nil
}
func (field fieldDataBase) writeToStatus(ch BytesChannel) (int, error) {
if fmtStatus(field.fmt.Status())&tdsFmtColumnStatus != tdsFmtColumnStatus {
return 0, nil
}
// TODO depends on wide
if err := ch.WriteUint8(uint8(field.status)); err != nil {
return 0, fmt.Errorf("failed to write status: %w", err)
}
return 1, nil
}
func (field *fieldDataBase) readFrom(ch BytesChannel) (int, error) {
n, err := field.readFromStatus(ch)
if err != nil {
return n, err
}
length := field.fmt.LengthBytes()
if !field.fmt.IsFixedLength() {
var err error
length, err = readLengthBytes(ch, field.fmt.LengthBytes())
if err != nil {
return n, fmt.Errorf("failed to read %d bytes of length: %w", field.fmt.LengthBytes(), err)
}
n += field.fmt.LengthBytes()
}
bs, err := ch.Bytes(length)
if err != nil {
return n, fmt.Errorf("failed to read %d bytes: %w", length, err)
}
field.value, err = field.fmt.DataType().GoValue(endian, bs)
if err != nil {
return n, fmt.Errorf("failed to parse field data: %w", err)
}
if len(bs) != length {
return n, fmt.Errorf("expected to read %d bytes of data, read %d", length, len(bs))
}
return n, nil
}
func (field fieldDataBase) writeTo(ch BytesChannel) (int, error) {
n, err := field.writeToStatus(ch)
if err != nil {
return n, err
}
bs, err := field.fmt.DataType().Bytes(endian, field.value, field.fmt.MaxLength())
if err != nil {
return n, fmt.Errorf("error converting field value to bytes: %w", err)
}
if !field.fmt.IsFixedLength() {
if err := writeLengthBytes(ch, field.fmt.LengthBytes(), int64(len(bs))); err != nil {
return n, fmt.Errorf("failed to write data length: %w", err)
}
n += field.fmt.LengthBytes()
}
if err = ch.WriteBytes(bs); err != nil {
return n, fmt.Errorf("failed to write field data: %w", err)
}
n += len(bs)
return n, nil
}
// Implementations: fieldFmtLength, fieldData
type fieldFmtLength struct {
fieldFmtBase
}
// FormatByteLength implements the tds.FieldFmt interface.
func (field fieldFmtLength) FormatByteLength() int {
if field.IsFixedLength() {
return 0
}
return field.LengthBytes()
}
// ReadFrom implements the tds.FieldFmt interface.
func (field *fieldFmtLength) ReadFrom(ch BytesChannel) (int, error) {
return field.readFromBase(ch)
}
// WriteTo implements the tds.FieldFmt interface.
func (field fieldFmtLength) WriteTo(ch BytesChannel) (int, error) {
return field.writeToBase(ch)
}
type BitFieldFmt struct{ fieldFmtLength }
type DateTimeFieldFmt struct{ fieldFmtLength }
type DateFieldFmt struct{ fieldFmtLength }
type ShortDateFieldFmt struct{ fieldFmtLength }
type Flt4FieldFmt struct{ fieldFmtLength }
type Flt8FieldFmt struct{ fieldFmtLength }
type Int1FieldFmt struct{ fieldFmtLength }
type Int2FieldFmt struct{ fieldFmtLength }
type Int4FieldFmt struct{ fieldFmtLength }
type Int8FieldFmt struct{ fieldFmtLength }
type IntervalFieldFmt struct{ fieldFmtLength }
type Sint1FieldFmt struct{ fieldFmtLength }
type Uint2FieldFmt struct{ fieldFmtLength }
type Uint4FieldFmt struct{ fieldFmtLength }
type Uint8FieldFmt struct{ fieldFmtLength }
type MoneyFieldFmt struct{ fieldFmtLength }
type ShortMoneyFieldFmt struct{ fieldFmtLength }
type TimeFieldFmt struct{ fieldFmtLength }
type BinaryFieldFmt struct{ fieldFmtLength }
type BoundaryFieldFmt struct{ fieldFmtLength }
type CharFieldFmt struct{ fieldFmtLength }
type DateNFieldFmt struct{ fieldFmtLength }
type DateTimeNFieldFmt struct{ fieldFmtLength }
type FltNFieldFmt struct{ fieldFmtLength }
type IntNFieldFmt struct{ fieldFmtLength }
type UintNFieldFmt struct{ fieldFmtLength }
type LongBinaryFieldFmt struct{ fieldFmtLength }
type LongCharFieldFmt struct{ fieldFmtLength }
type MoneyNFieldFmt struct{ fieldFmtLength }
type SensitivityFieldFmt struct{ fieldFmtLength }
type TimeNFieldFmt struct{ fieldFmtLength }
type VarBinaryFieldFmt struct{ fieldFmtLength }
type VarCharFieldFmt struct{ fieldFmtLength }
type fieldData struct{ fieldDataBase }
// ReadFrom implements the tds.FieldData interface.
func (field *fieldData) ReadFrom(ch BytesChannel) (int, error) {
return field.readFrom(ch)
}
// WriteTo implements the tds.FieldData interface.
func (field fieldData) WriteTo(ch BytesChannel) (int, error) {
return field.writeTo(ch)
}
type BitFieldData struct{ fieldData }
type DateTimeFieldData struct{ fieldData }
type DateFieldData struct{ fieldData }
type ShortDateFieldData struct{ fieldData }
type Flt4FieldData struct{ fieldData }
type Flt8FieldData struct{ fieldData }
type Int1FieldData struct{ fieldData }
type Int2FieldData struct{ fieldData }
type Int4FieldData struct{ fieldData }
type Int8FieldData struct{ fieldData }
type IntervalFieldData struct{ fieldData }
type Sint1FieldData struct{ fieldData }
type Uint2FieldData struct{ fieldData }
type Uint4FieldData struct{ fieldData }
type Uint8FieldData struct{ fieldData }
type MoneyFieldData struct{ fieldData }
type ShortMoneyFieldData struct{ fieldData }
type TimeFieldData struct{ fieldData }
type BinaryFieldData struct{ fieldData }
type BoundaryFieldData struct{ fieldData }
type CharFieldData struct{ fieldData }
type DateNFieldData struct{ fieldData }
type DateTimeNFieldData struct{ fieldData }
type FltNFieldData struct{ fieldData }
type IntNFieldData struct{ fieldData }
type UintNFieldData struct{ fieldData }
type LongBinaryFieldData struct{ fieldData }
type LongCharFieldData struct{ fieldData }
type MoneyNFieldData struct{ fieldData }
type SensitivityFieldData struct{ fieldData }
type TimeNFieldData struct{ fieldData }
type VarBinaryFieldData struct{ fieldData }
type VarCharFieldData struct{ fieldData }
// Implementations: fieldFmtLengthScale, fieldData
type fieldFmtBaseScale struct {
scale uint8
}
// Scale returns the scale stored in a field format.
func (field fieldFmtBaseScale) Scale() uint8 {
return field.scale
}
func (field *fieldFmtBaseScale) readFromScale(ch BytesChannel) (int, error) {
var err error
field.scale, err = ch.Uint8()
if err != nil {
return 0, ErrNotEnoughBytes
}
return 1, nil
}
func (field fieldFmtBaseScale) writeToScale(ch BytesChannel) (int, error) {
if err := ch.WriteUint8(field.scale); err != nil {
return 0, fmt.Errorf("failed to write scale: %w", err)
}
return 1, nil
}
type fieldFmtLengthScale struct {
fieldFmtBase
fieldFmtBaseScale
}
// FormatByteLength implements the tds.FieldFmt interface.
func (field fieldFmtLengthScale) FormatByteLength() int {
// 1 byte scale
// 1 to 4 bytes length
return 1 + field.LengthBytes()
}
// ReadFrom implements the tds.FieldFmt interface.
func (field *fieldFmtLengthScale) ReadFrom(ch BytesChannel) (int, error) {
n, err := field.readFromBase(ch)
if err != nil {
return n, err
}
n2, err := field.readFromScale(ch)
return n + n2, err
}
// WriteTo implements the tds.FieldFmt interface.
func (field fieldFmtLengthScale) WriteTo(ch BytesChannel) (int, error) {
n, err := field.writeToBase(ch)
if err != nil {
return n, err
}
n2, err := field.writeToScale(ch)
return n + n2, err
}
type BigDateTimeNFieldFmt struct{ fieldFmtLengthScale }
type BigTimeNFieldFmt struct{ fieldFmtLengthScale }
type BigDateTimeNFieldData struct{ fieldData }
type BigTimeNFieldData struct{ fieldData }
// Implementations: fieldFmtLengthPrecisionScale,
// fieldDataPrecisionScale
type fieldFmtBasePrecision struct {
precision uint8
}
// Precision returns the precision stored in a field format.
func (field fieldFmtBasePrecision) Precision() uint8 {
return field.precision
}
func (field *fieldFmtBasePrecision) readFromPrecision(ch BytesChannel) (int, error) {
var err error
field.precision, err = ch.Uint8()
if err != nil {
return 0, ErrNotEnoughBytes
}
return 1, nil
}
func (field fieldFmtBasePrecision) writeToPrecision(ch BytesChannel) (int, error) {
if err := ch.WriteUint8(field.precision); err != nil {
return 0, fmt.Errorf("failed to write precision: %w", err)
}
return 1, nil
}
type fieldFmtLengthPrecisionScale struct {
fieldFmtBase
fieldFmtBasePrecision
fieldFmtBaseScale
}
// FormatByteLength implements the tds.FieldFmt interface.
func (field fieldFmtLengthPrecisionScale) FormatByteLength() int {
return 2 + field.LengthBytes()
}
// ReadFrom implements the tds.FieldFmt interface.
func (field *fieldFmtLengthPrecisionScale) ReadFrom(ch BytesChannel) (int, error) {
n, err := field.readFromBase(ch)
if err != nil {
return n, err
}
n2, err := field.readFromPrecision(ch)
if err != nil {
return n + n2, err
}
n3, err := field.readFromScale(ch)
return n + n2 + n3, err
}
// WriteTo implements the tds.FieldFmt interface.
func (field fieldFmtLengthPrecisionScale) WriteTo(ch BytesChannel) (int, error) {
n, err := field.writeToBase(ch)
if err != nil {
return n, err
}
n2, err := field.writeToPrecision(ch)
if err != nil {
return n + n2, err
}
n3, err := field.writeToScale(ch)
return n + n2 + n3, err
}
type DecNFieldFmt struct{ fieldFmtLengthPrecisionScale }
type NumNFieldFmt struct{ fieldFmtLengthPrecisionScale }
type fieldDataPrecisionScale struct {
fieldData
}
// ReadFrom implements the tds.FieldData interface.
func (field *fieldDataPrecisionScale) ReadFrom(ch BytesChannel) (int, error) {
n, err := field.readFrom(ch)
if err != nil {
return n, err
}
dec, ok := field.value.(*asetypes.Decimal)
if !ok {
return n, fmt.Errorf("%T is not of type decimal", field.value)
}
switch fieldFmt := field.fmt.(type) {
case *DecNFieldFmt:
dec.Precision = int(fieldFmt.precision)
dec.Scale = int(fieldFmt.scale)
case *NumNFieldFmt:
dec.Precision = int(fieldFmt.precision)
dec.Scale = int(fieldFmt.scale)
default:
return n, fmt.Errorf("%T is neither of type DecNFieldFmt nor NumNFieldFmt", field.value)
}
return n, nil
}
type DecNFieldData struct{ fieldDataPrecisionScale }
type NumNFieldData struct{ fieldDataPrecisionScale }
// Implementations: fieldFmtBlob, fieldDataBlob
//go:generate stringer -type=BlobType
// BlobType defines the type of a data blob.
type BlobType uint8
const (
TDS_BLOB_FULLCLASSNAME BlobType = iota + 1
TDS_BLOB_DBID_CLASSDEF
TDS_BLOB_CHAR
TDS_BLOB_BINARY
TDS_BLOB_UNICHAR
TDS_LOBLOC_CHAR
TDS_LOBLOC_BINARY
TDS_LOBLOC_UNICHAR
)
//go:generate stringer -type=BlobSerializationType
// BlobSerializationType defines the seralization type of a data blob.
type BlobSerializationType uint8
const (
NativeJavaSerialization BlobSerializationType = iota
NativeCharacterFormat
BinaryData
UnicharUTF16
UnicharUTF8
UnicharSCSU
)
type fieldFmtBlob struct {
fieldFmtBase
blobType BlobType
classID string
}
// FormatByteLength implements the tds.FieldFmt interface.
func (field fieldFmtBlob) FormatByteLength() int {
return 1 + 1 + len(field.classID) + field.LengthBytes()
}
// ReadFrom implements the tds.FieldFmt interface.
func (field *fieldFmtBlob) ReadFrom(ch BytesChannel) (int, error) {
n, err := field.readFromBase(ch)
if err != nil {
return n, err
}
blobType, err := ch.Uint8()
if err != nil {
return 0, ErrNotEnoughBytes
}
field.blobType = BlobType(blobType)
n++
if field.blobType == TDS_BLOB_FULLCLASSNAME || field.blobType == TDS_BLOB_DBID_CLASSDEF {
classIdLength, err := ch.Uint16()
if err != nil {
return 0, ErrNotEnoughBytes
}
n += 2
field.classID, err = ch.String(int(classIdLength))
if err != nil {
return 0, ErrNotEnoughBytes
}
n += int(classIdLength)
}
return n, nil
}
// WriteTo implements the tds.FieldFmt interface.
func (field fieldFmtBlob) WriteTo(ch BytesChannel) (int, error) {
n, err := field.writeToBase(ch)
if err != nil {
return n, err
}
if err := ch.WriteUint8(uint8(field.blobType)); err != nil {
return n, fmt.Errorf("failed to write blobtype: %w", err)
}
n++
if field.blobType == TDS_BLOB_FULLCLASSNAME || field.blobType == TDS_BLOB_DBID_CLASSDEF {
if err := ch.WriteUint16(uint16(len(field.classID))); err != nil {
return n, fmt.Errorf("failed to write ClassID length: %w", err)
}
n += 2
if len(field.classID) > 0 {
if err := ch.WriteString(field.classID); err != nil {
return n, fmt.Errorf("failed to write ClassID: %w", err)
}
n += len(field.classID)
}
}
return n, nil
}
type BlobFieldFmt struct{ fieldFmtBlob }
type fieldDataBlob struct {
fieldData
serializationType BlobSerializationType
subClassID string
locator string
}
const fieldDataBlobHighBit uint32 = 0x80000000
// ReadFrom implements the tds.FieldData interface.
func (field *fieldDataBlob) ReadFrom(ch BytesChannel) (int, error) {
fieldFmt, ok := field.fmt.(*BlobFieldFmt)
if !ok {
return 0, fmt.Errorf("field.fmt is not of type BlobFieldfmt")
}
n, err := field.readFromStatus(ch)
if err != nil {
return n, err
}
serialization, err := ch.Uint8()
if err != nil {
return 0, ErrNotEnoughBytes
}
n++
switch serialization {
case 0:
switch fieldFmt.blobType {
case TDS_BLOB_FULLCLASSNAME, TDS_BLOB_DBID_CLASSDEF:
field.serializationType = NativeJavaSerialization
case TDS_BLOB_CHAR:
field.serializationType = NativeCharacterFormat
case TDS_BLOB_BINARY:
field.serializationType = BinaryData
case TDS_BLOB_UNICHAR:
field.serializationType = UnicharUTF16
}
case 1:
if fieldFmt.blobType != TDS_BLOB_UNICHAR {
return n, fmt.Errorf("invalid blob (%s) and serialization (%d) type combination",
fieldFmt.blobType, serialization)
}
field.serializationType = UnicharUTF8
case 2:
if fieldFmt.blobType != TDS_BLOB_UNICHAR {
return n, fmt.Errorf("invalid blob (%s) and serialization (%d) type combination",
fieldFmt.blobType, serialization)
}
field.serializationType = UnicharSCSU
default:
return n, fmt.Errorf("unhandled serialization type %d", serialization)
}
switch fieldFmt.blobType {
case TDS_BLOB_FULLCLASSNAME, TDS_BLOB_DBID_CLASSDEF:
subClassIdLength, err := ch.Uint16()
if err != nil {
return 0, ErrNotEnoughBytes
}
n += 2
if subClassIdLength > 0 {
field.subClassID, err = ch.String(int(subClassIdLength))
if err != nil {
return 0, ErrNotEnoughBytes
}
n += int(subClassIdLength)
}
case TDS_LOBLOC_CHAR, TDS_LOBLOC_BINARY, TDS_LOBLOC_UNICHAR:
locatorLength, err := ch.Uint16()
if err != nil {
return 0, ErrNotEnoughBytes
}
n += 2
field.locator, err = ch.String(int(locatorLength))
if err != nil {
return 0, ErrNotEnoughBytes
}
n += int(locatorLength)
}
// TODO better data type
data := []byte{}
for {
dataLen, err := ch.Uint32()
if err != nil {
return 0, ErrNotEnoughBytes
}
n += 4
// extract high bit:
// 0 -> last data set
// 1 -> another data set follows
highBitSet := dataLen&fieldDataBlobHighBit == fieldDataBlobHighBit
dataLen = dataLen &^ fieldDataBlobHighBit
// if high bit is set and dataLen is zero no data array follows,
// instead read the next data length immediately
if highBitSet {
break
}
if dataLen == 0 {
continue
}
dataPart, err := ch.Bytes(int(dataLen))
if err != nil {
return 0, ErrNotEnoughBytes
}
n += int(dataLen)
// TODO this is inefficient for large datasets - must be
// replaced by a low-overhead extensible byte storage (so - not
// a slice)
data = append(data, dataPart...)
}
field.value = data
return n, nil
}
// WriteTo implements the tds.FieldData interface.
func (field fieldDataBlob) WriteTo(ch BytesChannel) (int, error) {
fieldFmt, ok := field.fmt.(*BlobFieldFmt)
if !ok {
return 0, fmt.Errorf("field.fmt is not of type BlobFieldFmt")
}
n, err := field.writeToStatus(ch)
if err != nil {
return n, err
}
var serialization uint8
switch field.serializationType {
case NativeJavaSerialization, NativeCharacterFormat, BinaryData, UnicharUTF16:
serialization = 0
case UnicharUTF8:
serialization = 1
case UnicharSCSU:
serialization = 2
}
if err := ch.WriteUint8(serialization); err != nil {
return n, fmt.Errorf("failed to write SerializationType: %w", err)
}
n++
switch fieldFmt.blobType {
case TDS_BLOB_FULLCLASSNAME, TDS_BLOB_DBID_CLASSDEF:
if err := ch.WriteUint16(uint16(len(field.subClassID))); err != nil {
return n, fmt.Errorf("failed to write SubClassID length: %w", err)