-
Notifications
You must be signed in to change notification settings - Fork 54
/
parts.go
200 lines (184 loc) · 7.44 KB
/
parts.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
package protocol
import (
"fmt"
"reflect"
"github.com/SAP/go-hdb/driver/internal/protocol/encoding"
hdbreflect "github.com/SAP/go-hdb/driver/internal/reflect"
)
// Part represents a protocol part.
type Part interface {
String() string // should support Stringer interface
kind() PartKind
}
type partDecoder interface {
Part
decode(dec *encoding.Decoder) error
}
type numArgPartDecoder interface {
Part
decodeNumArg(dec *encoding.Decoder, numArg int) error
}
type bufLenPartDecoder interface {
Part
decodeBufLen(dec *encoding.Decoder, bufLen int) error
}
type resultPartDecoder interface {
Part
decodeResult(dec *encoding.Decoder, numArg int, readFn lobReadFn, lobChunkSize int) error
}
// PartEncoder represents a protocol part the driver is able to encode.
type PartEncoder interface {
Part
numArg() int
size() int
encode(enc *encoding.Encoder) error
}
func (*HdbErrors) kind() PartKind { return pkError }
func (*AuthInitRequest) kind() PartKind { return PkAuthentication }
func (*AuthInitReply) kind() PartKind { return PkAuthentication }
func (*AuthFinalRequest) kind() PartKind { return PkAuthentication }
func (*AuthFinalReply) kind() PartKind { return PkAuthentication }
func (ClientID) kind() PartKind { return PkClientID }
func (clientInfo) kind() PartKind { return PkClientInfo }
func (*TopologyInformation) kind() PartKind { return PkTopologyInformation }
func (Command) kind() PartKind { return PkCommand }
func (*rowsAffected) kind() PartKind { return pkRowsAffected }
func (StatementID) kind() PartKind { return PkStatementID }
func (*ParameterMetadata) kind() PartKind { return PkParameterMetadata }
func (*InputParameters) kind() PartKind { return PkParameters }
func (*OutputParameters) kind() PartKind { return PkOutputParameters }
func (*ResultMetadata) kind() PartKind { return PkResultMetadata }
func (ResultsetID) kind() PartKind { return PkResultsetID }
func (*Resultset) kind() PartKind { return PkResultset }
func (Fetchsize) kind() PartKind { return PkFetchSize }
func (*ReadLobRequest) kind() PartKind { return PkReadLobRequest }
func (*ReadLobReply) kind() PartKind { return PkReadLobReply }
func (*WriteLobRequest) kind() PartKind { return PkWriteLobRequest }
func (*WriteLobReply) kind() PartKind { return PkWriteLobReply }
func (*ClientContext) kind() PartKind { return PkClientContext }
func (*ConnectOptions) kind() PartKind { return PkConnectOptions }
func (*DBConnectInfo) kind() PartKind { return PkDBConnectInfo }
func (*statementContext) kind() PartKind { return PkStatementContext }
func (*transactionFlags) kind() PartKind { return PkTransactionFlags }
// numArg methods (result == 1).
func (*AuthInitRequest) numArg() int { return 1 }
func (*AuthFinalRequest) numArg() int { return 1 }
func (ClientID) numArg() int { return 1 }
func (Command) numArg() int { return 1 }
func (StatementID) numArg() int { return 1 }
func (ResultsetID) numArg() int { return 1 }
func (Fetchsize) numArg() int { return 1 }
func (*ReadLobRequest) numArg() int { return 1 }
// size methods (fixed size).
const (
statementIDSize = 8
resultsetIDSize = 8
fetchsizeSize = 4
readLobRequestSize = 24
)
func (StatementID) size() int { return statementIDSize }
func (ResultsetID) size() int { return resultsetIDSize }
func (Fetchsize) size() int { return fetchsizeSize }
func (ReadLobRequest) size() int { return readLobRequestSize }
// func (lobFlags) size() int { return tinyintFieldSize }
// check if part types implement the part encoder interface.
var (
_ PartEncoder = (*AuthInitRequest)(nil)
_ PartEncoder = (*AuthFinalRequest)(nil)
_ PartEncoder = (*ClientID)(nil)
_ PartEncoder = (*clientInfo)(nil)
_ PartEncoder = (*Command)(nil)
_ PartEncoder = (*StatementID)(nil)
_ PartEncoder = (*InputParameters)(nil)
_ PartEncoder = (*ResultsetID)(nil)
_ PartEncoder = (*Fetchsize)(nil)
_ PartEncoder = (*ReadLobRequest)(nil)
_ PartEncoder = (*WriteLobRequest)(nil)
_ PartEncoder = (*ClientContext)(nil)
_ PartEncoder = (*ConnectOptions)(nil)
_ PartEncoder = (*DBConnectInfo)(nil)
)
// check if part types implement the right part decoder interface.
var (
_ numArgPartDecoder = (*HdbErrors)(nil)
_ partDecoder = (*AuthInitRequest)(nil)
_ partDecoder = (*AuthInitReply)(nil)
_ partDecoder = (*AuthFinalRequest)(nil)
_ partDecoder = (*AuthFinalReply)(nil)
_ bufLenPartDecoder = (*ClientID)(nil)
_ numArgPartDecoder = (*clientInfo)(nil)
_ numArgPartDecoder = (*TopologyInformation)(nil)
_ bufLenPartDecoder = (*Command)(nil)
_ numArgPartDecoder = (*rowsAffected)(nil)
_ partDecoder = (*StatementID)(nil)
_ numArgPartDecoder = (*ParameterMetadata)(nil)
_ numArgPartDecoder = (*InputParameters)(nil)
_ resultPartDecoder = (*OutputParameters)(nil)
_ numArgPartDecoder = (*ResultMetadata)(nil)
_ partDecoder = (*ResultsetID)(nil)
_ resultPartDecoder = (*Resultset)(nil)
_ partDecoder = (*Fetchsize)(nil)
_ partDecoder = (*ReadLobRequest)(nil)
_ numArgPartDecoder = (*WriteLobRequest)(nil)
_ numArgPartDecoder = (*ReadLobReply)(nil)
_ numArgPartDecoder = (*WriteLobReply)(nil)
_ numArgPartDecoder = (*ClientContext)(nil)
_ numArgPartDecoder = (*ConnectOptions)(nil)
_ numArgPartDecoder = (*DBConnectInfo)(nil)
_ numArgPartDecoder = (*statementContext)(nil)
_ numArgPartDecoder = (*transactionFlags)(nil)
)
var genPartTypeMap = map[PartKind]reflect.Type{
pkError: hdbreflect.TypeFor[HdbErrors](),
PkClientID: hdbreflect.TypeFor[ClientID](),
PkClientInfo: hdbreflect.TypeFor[clientInfo](),
PkTopologyInformation: hdbreflect.TypeFor[TopologyInformation](),
PkCommand: hdbreflect.TypeFor[Command](),
pkRowsAffected: hdbreflect.TypeFor[rowsAffected](),
PkStatementID: hdbreflect.TypeFor[StatementID](),
PkResultsetID: hdbreflect.TypeFor[ResultsetID](),
PkFetchSize: hdbreflect.TypeFor[Fetchsize](),
PkReadLobRequest: hdbreflect.TypeFor[ReadLobRequest](),
PkReadLobReply: hdbreflect.TypeFor[ReadLobReply](),
PkWriteLobReply: hdbreflect.TypeFor[WriteLobReply](),
PkWriteLobRequest: hdbreflect.TypeFor[WriteLobRequest](),
PkClientContext: hdbreflect.TypeFor[ClientContext](),
PkConnectOptions: hdbreflect.TypeFor[ConnectOptions](),
PkTransactionFlags: hdbreflect.TypeFor[transactionFlags](),
PkStatementContext: hdbreflect.TypeFor[statementContext](),
PkDBConnectInfo: hdbreflect.TypeFor[DBConnectInfo](),
/*
parts that cannot be used generically as additional parameters are needed
PkParameterMetadata
PkParameters
PkOutputParameters
PkResultMetadata
PkResultset
*/
}
// to be implemented by parts needing initialization
// in case the part is instatiated generically.
type initer interface {
init()
}
// newGenPartReader returns a generic part reader.
func newGenPartReader(kind PartKind) Part {
if kind == PkAuthentication {
return nil // cannot instantiate generically
}
pt, ok := genPartTypeMap[kind]
if !ok {
// whether part cannot be instantiated generically or
// part is not (yet) known to the driver
return nil
}
// create instance
part, ok := reflect.New(pt).Interface().(Part)
if !ok {
panic(fmt.Sprintf("part kind %s does not implement part reader interface", kind)) // should never happen
}
if part, ok := part.(initer); ok {
part.init()
}
return part
}