forked from sijms/go-ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameter.go
351 lines (337 loc) · 8.28 KB
/
parameter.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
package go_ora
import (
"database/sql/driver"
"math"
"strings"
"github.com/sijms/go-ora/network"
)
type OracleType int
type ParameterDirection int
const (
Input ParameterDirection = 1
Output ParameterDirection = 2
InOut ParameterDirection = 3
RetVal ParameterDirection = 9
)
//internal enum BindDirection
//{
//Output = 16,
//Input = 32,
//InputOutput = 48,
//}
//go:generate stringer -type=OracleType
const (
NCHAR OracleType = 1
NUMBER OracleType = 2
SB1 OracleType = 3
SB2 OracleType = 3
SB4 OracleType = 3
FLOAT OracleType = 4
NullStr OracleType = 5
VarNum OracleType = 6
LONG OracleType = 8
VARCHAR OracleType = 9
ROWID OracleType = 11
DATE OracleType = 12
VarRaw OracleType = 15
BFloat OracleType = 21
BDouble OracleType = 22
RAW OracleType = 23
LongRaw OracleType = 24
UINT OracleType = 68
LongVarChar OracleType = 94
LongVarRaw OracleType = 95
CHAR OracleType = 96
CHARZ OracleType = 97
IBFloat OracleType = 100
IBDouble OracleType = 101
REFCURSOR OracleType = 102
NOT OracleType = 108
XMLType OracleType = 108
OCIRef OracleType = 110
OCIClobLocator OracleType = 112
OCIBlobLocator OracleType = 113
OCIFileLocator OracleType = 114
ResultSet OracleType = 116
OCIString OracleType = 155
OCIDate OracleType = 156
TimeStampDTY OracleType = 180
TimeStampTZ_DTY OracleType = 181
IntervalYM_DTY OracleType = 182
IntervalDS_DTY OracleType = 183
TimeTZ OracleType = 186
TimeStamp OracleType = 187
TimeStampTZ OracleType = 188
IntervalYM OracleType = 189
IntervalDS OracleType = 190
UROWID OracleType = 208
TimeStampLTZ_DTY OracleType = 231
TimeStampeLTZ OracleType = 232
)
type ParameterType int
const (
Number ParameterType = 1
String ParameterType = 2
)
type ParameterInfo struct {
Name string
Direction ParameterDirection
IsNull bool
AllowNull bool
ColAlias string
DataType OracleType
IsXmlType bool
Flag uint8
Precision uint8
Scale uint8
MaxLen int
MaxCharLen int
MaxNoOfArrayElements int
ContFlag int
ToID []byte
Version int
CharsetID int
CharsetForm int
BValue []byte
Value driver.Value
getDataFromServer bool
}
func (par *ParameterInfo) load(session *network.Session) error {
par.getDataFromServer = true
dataType, err := session.GetByte()
if err != nil {
return err
}
par.DataType = OracleType(dataType)
par.Flag, err = session.GetByte()
if err != nil {
return err
}
par.Precision, err = session.GetByte()
//precision, err := session.GetInt(1, false, false)
//var scale int
switch par.DataType {
case NUMBER:
fallthrough
case TimeStampDTY:
fallthrough
case TimeStampTZ_DTY:
fallthrough
case IntervalDS_DTY:
fallthrough
case TimeStamp:
fallthrough
case TimeStampTZ:
fallthrough
case IntervalDS:
fallthrough
case TimeStampLTZ_DTY:
fallthrough
case TimeStampeLTZ:
if scale, err := session.GetInt(2, true, true); err != nil {
return err
} else {
if scale == -127 {
par.Precision = uint8(math.Ceil(float64(par.Precision) * 0.30103))
par.Scale = 0xFF
} else {
par.Scale = uint8(scale)
}
}
default:
par.Scale, err = session.GetByte()
//scale, err = session.GetInt(1, false, false)
}
//if par.Scale == uint8(-127) {
//
//}
if par.DataType == NUMBER && par.Precision == 0 && (par.Scale == 0 || par.Scale == 0xFF) {
par.Precision = 38
par.Scale = 0xFF
}
//par.Scale = uint16(scale)
//par.Precision = uint16(precision)
par.MaxLen, err = session.GetInt(4, true, true)
if err != nil {
return err
}
switch par.DataType {
case ROWID:
par.MaxLen = 128
case DATE:
par.MaxLen = 7
case IBFloat:
par.MaxLen = 4
case IBDouble:
par.MaxLen = 8
case TimeStampTZ_DTY:
par.MaxLen = 13
case IntervalYM_DTY:
fallthrough
case IntervalDS_DTY:
fallthrough
case IntervalYM:
fallthrough
case IntervalDS:
par.MaxLen = 11
}
par.MaxNoOfArrayElements, err = session.GetInt(4, true, true)
if err != nil {
return err
}
par.ContFlag, err = session.GetInt(4, true, true)
if err != nil {
return err
}
par.ToID, err = session.GetDlc()
par.Version, err = session.GetInt(2, true, true)
if err != nil {
return err
}
par.CharsetID, err = session.GetInt(2, true, true)
if err != nil {
return err
}
par.CharsetForm, err = session.GetInt(1, false, false)
if err != nil {
return err
}
par.MaxCharLen, err = session.GetInt(4, true, true)
if err != nil {
return err
}
num1, err := session.GetInt(1, false, false)
if err != nil {
return err
}
par.AllowNull = num1 > 0
_, err = session.GetByte() // session.GetInt(1, false, false)
if err != nil {
return err
}
bName, err := session.GetDlc()
if err != nil {
return err
}
par.Name = session.StrConv.Decode(bName)
_, err = session.GetDlc()
bName, err = session.GetDlc()
if err != nil {
return err
}
if strings.ToUpper(string(bName)) == "XMLTYPE" {
par.DataType = XMLType
par.IsXmlType = true
}
if session.TTCVersion < 3 {
return nil
}
_, err = session.GetInt(2, true, true)
if session.TTCVersion < 6 {
return nil
}
_, err = session.GetInt(4, true, true)
return nil
}
func (par *ParameterInfo) write(session *network.Session) error {
session.PutBytes(uint8(par.DataType), par.Flag, par.Precision, par.Scale)
//session.PutUint(int(par.DataType), 1, false, false)
//session.PutUint(par.Flag, 1, false, false)
//session.PutUint(par.Precision, 1, false, false)
//session.PutUint(par.Scale, 1, false, false)
session.PutUint(par.MaxLen, 4, true, true)
session.PutInt(par.MaxNoOfArrayElements, 4, true, true)
session.PutInt(par.ContFlag, 4, true, true)
if par.ToID == nil {
session.PutBytes(0)
//session.PutInt(0, 1, false, false)
} else {
session.PutInt(len(par.ToID), 4, true, true)
session.PutClr(par.ToID)
}
session.PutUint(par.Version, 2, true, true)
session.PutUint(par.CharsetID, 2, true, true)
session.PutBytes(uint8(par.CharsetForm))
//session.PutUint(par.CharsetForm, 1, false, false)
session.PutUint(par.MaxCharLen, 4, true, true)
return nil
}
//func NewIntegerParameter(name string, val int, direction ParameterDirection) *ParameterInfo {
// ret := ParameterInfo{
// Name: name,
// Direction: direction,
// flag: 3,
// ContFlag: 0,
// DataType: NUMBER,
// MaxCharLen: 22,
// MaxLen: 22,
// CharsetID: 871,
// CharsetForm: 1,
// BValue: converters.EncodeInt(val),
// }
// return &ret
//}
//func NewStringParameter(name string, val string, size int, direction ParameterDirection) *ParameterInfo {
// ret := ParameterInfo{
// Name: name,
// Direction: direction,
// flag: 3,
// ContFlag: 16,
// DataType: NCHAR,
// MaxCharLen: size,
// MaxLen: size,
// CharsetID: 871,
// CharsetForm: 1,
// BValue: []byte(val),
// }
// return &ret
//}
//func NewParamInfo(name string, parType ParameterType, size int, direction ParameterDirection) *ParameterInfo {
// ret := new(ParameterInfo)
// ret.Name = name
// ret.Direction = direction
// ret.flag = 3
// //ret.DataType = dataType
// switch parType {
// case String:
// ret.ContFlag = 16
// default:
// ret.ContFlag = 0
// }
// switch parType {
// case Number:
// ret.DataType = NUMBER
// ret.MaxLen = 22
// case String:
// ret.CharsetForm = 1
// ret.DataType = NCHAR
// ret.MaxCharLen = size
// ret.MaxLen = size
// }
// //ret.MaxCharLen = 0 // number of character to write
// //ret.MaxLen = ret.MaxCharLen * 1 // number of character * byte per character
// ret.CharsetID = 871
// return ret
// // if duplicateBind ret.flag = 128 else ret.flag = 3
// // if collection type is assocative array ret.Flat |= 64
//
// //num3 := 0
// //switch dataType {
// //case LONG:
// // fallthrough
// //case LongRaw:
// // fallthrough
// //case CHAR:
// // fallthrough
// //case RAW:
// // fallthrough
// //case NCHAR:
// // num3 = 1
// //default:
// // num3 = 0
// //}
// //if num3 != 0 {
// //
// //}
// //return ret
//}