-
Notifications
You must be signed in to change notification settings - Fork 54
/
optiontype.go
145 lines (132 loc) · 4.42 KB
/
optiontype.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
package protocol
import (
"fmt"
"github.com/SAP/go-hdb/driver/internal/protocol/encoding"
)
type optType interface {
fmt.Stringer
typeCode() typeCode
size(v any) int
encode(e *encoding.Encoder, v any)
decode(d *encoding.Decoder) any
}
var (
optBooleanType = _optBooleanType{}
optTinyintType = _optTinyintType{}
optIntegerType = _optIntegerType{}
optBigintType = _optBigintType{}
optDoubleType = _optDoubleType{}
optStringType = _optStringType{}
optBstringType = _optBstringType{}
)
type (
_optBooleanType struct{}
_optTinyintType struct{}
_optIntegerType struct{}
_optBigintType struct{}
_optDoubleType struct{}
_optStringType struct{}
_optBstringType struct{}
)
var (
_ optType = (*_optBooleanType)(nil)
_ optType = (*_optTinyintType)(nil)
_ optType = (*_optIntegerType)(nil)
_ optType = (*_optBigintType)(nil)
_ optType = (*_optDoubleType)(nil)
_ optType = (*_optStringType)(nil)
_ optType = (*_optBstringType)(nil)
)
func (_optBooleanType) String() string { return "booleanType" }
func (_optTinyintType) String() string { return "tinyintType" }
func (_optIntegerType) String() string { return "integerType" }
func (_optBigintType) String() string { return "bigintType" }
func (_optDoubleType) String() string { return "doubleType" }
func (_optStringType) String() string { return "dateType" }
func (_optBstringType) String() string { return "timeType" }
func (_optBooleanType) typeCode() typeCode { return tcBoolean }
func (_optTinyintType) typeCode() typeCode { return tcTinyint }
func (_optIntegerType) typeCode() typeCode { return tcInteger }
func (_optBigintType) typeCode() typeCode { return tcBigint }
func (_optDoubleType) typeCode() typeCode { return tcDouble }
func (_optStringType) typeCode() typeCode { return tcString }
func (_optBstringType) typeCode() typeCode { return tcBstring }
func (_optBooleanType) size(any) int { return encoding.BooleanFieldSize }
func (_optTinyintType) size(any) int { return encoding.TinyintFieldSize }
func (_optIntegerType) size(any) int { return encoding.IntegerFieldSize }
func (_optBigintType) size(any) int { return encoding.BigintFieldSize }
func (_optDoubleType) size(any) int { return encoding.DoubleFieldSize }
func (_optStringType) size(v any) int { return 2 + len(v.(string)) } // length int16 + string length
func (_optBstringType) size(v any) int { return 2 + len(v.([]byte)) } // length int16 + bytes length
func (_optBooleanType) encode(e *encoding.Encoder, v any) { e.Bool(v.(bool)) }
func (_optTinyintType) encode(e *encoding.Encoder, v any) { e.Int8(v.(int8)) }
func (_optIntegerType) encode(e *encoding.Encoder, v any) { e.Int32(v.(int32)) }
func (_optBigintType) encode(e *encoding.Encoder, v any) { e.Int64(v.(int64)) }
func (_optDoubleType) encode(e *encoding.Encoder, v any) { e.Float64(v.(float64)) }
func (_optStringType) encode(e *encoding.Encoder, v any) {
s := v.(string)
e.Int16(int16(len(s)))
e.Bytes([]byte(s))
}
func (_optBstringType) encode(e *encoding.Encoder, v any) {
b := v.([]byte)
e.Int16(int16(len(b)))
e.Bytes(b)
}
func (_optBooleanType) decode(d *encoding.Decoder) any { return d.Bool() }
func (_optTinyintType) decode(d *encoding.Decoder) any { return d.Int8() }
func (_optIntegerType) decode(d *encoding.Decoder) any { return d.Int32() }
func (_optBigintType) decode(d *encoding.Decoder) any { return d.Int64() }
func (_optDoubleType) decode(d *encoding.Decoder) any { return d.Float64() }
func (_optStringType) decode(d *encoding.Decoder) any {
l := d.Int16()
b := make([]byte, l)
d.Bytes(b)
return string(b)
}
func (_optBstringType) decode(d *encoding.Decoder) any {
l := d.Int16()
b := make([]byte, l)
d.Bytes(b)
return b
}
func optTypeViaType(v any) optType {
switch v := v.(type) {
case bool:
return optBooleanType
case int8:
return optTinyintType
case int32:
return optIntegerType
case int64:
return optBigintType
case float64:
return optDoubleType
case string:
return optStringType
case []byte:
return optBstringType
default:
panic(fmt.Sprintf("type %T not implemented", v)) // should never happen
}
}
func optTypeViaTypeCode(tc typeCode) optType {
switch tc {
case tcBoolean:
return optBooleanType
case tcTinyint:
return optTinyintType
case tcInteger:
return optIntegerType
case tcBigint:
return optBigintType
case tcDouble:
return optDoubleType
case tcString:
return optStringType
case tcBstring:
return optBstringType
default:
panic(fmt.Sprintf("missing optType for typeCode %s", tc))
}
}