forked from sijms/go-ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parameter_encode.go
564 lines (548 loc) · 13.3 KB
/
parameter_encode.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
package go_ora
import (
"bytes"
"database/sql/driver"
"errors"
"fmt"
"reflect"
"strings"
"time"
"github.com/sijms/go-ora/v2/converters"
)
func (par *ParameterInfo) setDataType(goType reflect.Type, value driver.Value, conn *Connection) error {
if goType == nil {
par.DataType = NCHAR
return nil
}
for goType.Kind() == reflect.Ptr {
goType = goType.Elem()
}
if goType == tyObject {
val, err := getValue(value)
if err != nil {
return err
}
if obj, ok := val.(Object); ok {
par.DataType = XMLType
par.Value = obj.Value
// set custom type
for name, cusTyp := range conn.cusTyp {
if strings.EqualFold(name, obj.Name) {
par.cusType = new(customType)
*par.cusType = cusTyp
par.ToID = cusTyp.toid
if cusTyp.isArray {
par.MaxNoOfArrayElements = 1
} else {
par.Version = 1
}
break
}
}
if par.cusType == nil {
return fmt.Errorf("type %s is not created or not registered", obj.Name)
}
return nil
}
}
if goType != tyBytes && (goType.Kind() == reflect.Array || goType.Kind() == reflect.Slice) {
val, err := getValue(value)
if err != nil {
return err
}
var inVal driver.Value = nil
if val != nil {
rValue := reflect.ValueOf(val)
size := rValue.Len()
if size > 0 && rValue.Index(0).CanInterface() {
inVal = rValue.Index(0).Interface()
}
}
par.Flag = 0x43
err = par.setDataType(goType.Elem(), inVal, conn)
if err != nil {
return err
}
if par.DataType == XMLType {
// par.cusType is for item I should get that of array
found := false
for _, cust := range conn.cusTyp {
if cust.isArray && len(cust.attribs) > 0 {
if par.cusType.name == cust.attribs[0].cusType.name {
found = true
//par.TypeName = name
par.ToID = cust.toid
*par.cusType = cust
par.Flag = 0x3
break
}
}
}
if !found {
return fmt.Errorf("can't get the collection of type %s", par.cusType.name)
}
}
par.MaxNoOfArrayElements = 1
return nil
}
if tNumber(goType) || tNullNumber(goType) {
par.DataType = NUMBER
par.MaxLen = converters.MAX_LEN_NUMBER
return nil
}
switch goType {
case tyNumber:
par.DataType = NUMBER
par.MaxLen = converters.MAX_LEN_NUMBER
case tyPLBool:
par.DataType = Boolean
par.MaxLen = converters.MAX_LEN_BOOL
case tyString, tyNullString:
par.DataType = NCHAR
par.CharsetForm = 1
par.ContFlag = 16
par.CharsetID = conn.tcpNego.ServerCharset
case tyNVarChar, tyNullNVarChar:
par.DataType = NCHAR
par.CharsetForm = 2
par.ContFlag = 16
par.CharsetID = conn.tcpNego.ServernCharset
case tyTime, tyNullTime:
if par.Flag&0x40 > 0 {
par.DataType = DATE
par.MaxLen = converters.MAX_LEN_DATE
} else {
par.DataType = TimeStampTZ_DTY
par.MaxLen = converters.MAX_LEN_TIMESTAMP
}
case tyTimeStamp, tyNullTimeStamp:
//if par.Flag&0x43 > 0 {
par.DataType = TIMESTAMP
par.MaxLen = converters.MAX_LEN_DATE
//} else {
// par.DataType = TimeStampTZ_DTY
// par.MaxLen = converters.MAX_LEN_TIMESTAMP
//}
case tyTimeStampTZ, tyNullTimeStampTZ:
par.DataType = TimeStampTZ_DTY
par.MaxLen = converters.MAX_LEN_TIMESTAMP
//case tyTime, tyNullTime:
// if par.Direction == Input {
// par.DataType = TIMESTAMP
// par.MaxLen = converters.MAX_LEN_TIMESTAMP
// } else {
// par.DataType = DATE
// par.MaxLen = converters.MAX_LEN_DATE
// }
//case tyTimeStamp, tyNullTimeStamp:
// if par.Direction == Input {
// par.DataType = TIMESTAMP
// par.MaxLen = converters.MAX_LEN_TIMESTAMP
// } else {
// par.DataType = TIMESTAMP
// par.MaxLen = converters.MAX_LEN_DATE
// }
//case tyTimeStampTZ, tyNullTimeStampTZ:
// par.DataType = TimeStampTZ_DTY
// par.MaxLen = converters.MAX_LEN_TIMESTAMP
case tyBytes:
par.DataType = RAW
case tyClob:
par.DataType = OCIClobLocator
par.CharsetForm = 1
par.CharsetID = conn.tcpNego.ServerCharset
case tyNClob:
par.DataType = OCIClobLocator
par.CharsetForm = 2
par.CharsetID = conn.tcpNego.ServernCharset
case tyBlob:
par.DataType = OCIBlobLocator
case tyBFile:
par.DataType = OCIFileLocator
case tyRefCursor:
par.DataType = REFCURSOR
default:
rOriginal := reflect.ValueOf(value)
if value != nil && !(rOriginal.Kind() == reflect.Ptr && rOriginal.IsNil()) {
proVal := reflect.Indirect(rOriginal)
if valuer, ok := proVal.Interface().(driver.Valuer); ok {
val, err := valuer.Value()
if err != nil {
return err
}
if val == nil {
par.DataType = NCHAR
return nil
}
if val != value {
return par.setDataType(reflect.TypeOf(val), val, conn)
}
}
}
//val, err := getValue(value)
//if err != nil {
// return err
//}
//if val == nil {
// par.DataType = NCHAR
// return nil
//}
if goType.Kind() == reflect.Struct {
// see if the struct is support valuer interface
for _, cusTyp := range conn.cusTyp {
if goType == cusTyp.typ {
par.cusType = new(customType)
*par.cusType = cusTyp
par.ToID = cusTyp.toid
//par.TypeName = cusTyp.name
}
}
if par.cusType == nil {
return errors.New("call register type before use user defined type (UDT)")
}
par.Version = 1
par.DataType = XMLType
par.MaxLen = 2000
} else {
return fmt.Errorf("unsupported go type: %v", goType.Name())
}
}
return nil
}
func (par *ParameterInfo) encodeWithType(connection *Connection) error {
var err error
var val driver.Value
val, err = getValue(par.Value)
if err != nil {
return err
}
if val == nil {
par.IsNull = true
par.iPrimValue = nil
return nil
}
// check if array
//if par.MaxNoOfArrayElements > 0 && par.cusType == nil {
if par.MaxNoOfArrayElements > 0 {
if !isArrayValue(val) {
return fmt.Errorf("parameter %s require array value", par.Name)
}
var size int
rValue := reflect.ValueOf(val)
if isArrayValue(val) {
size = rValue.Len()
}
if size == 0 {
par.IsNull = true
par.iPrimValue = nil
return nil
}
if size > par.MaxNoOfArrayElements {
par.MaxNoOfArrayElements = size
}
pars := make([]ParameterInfo, 0, size)
var tempPar ParameterInfo
for x := 0; x < size; x++ {
if par.cusType != nil && par.cusType.isArray {
tempPar = par.cusType.attribs[0].clone()
} else {
tempPar = par.clone()
}
if rValue.Index(x).CanInterface() {
tempPar.Value = rValue.Index(x).Interface()
}
err = tempPar.encodeWithType(connection)
if err != nil {
return err
}
pars = append(pars, tempPar)
}
par.iPrimValue = pars
return nil
}
switch par.DataType {
case Boolean:
par.iPrimValue, err = getBool(val)
if err != nil {
return err
}
case NUMBER:
par.iPrimValue, err = NewNumber(val)
if err != nil {
return err
}
case NCHAR:
tempString := getString(val)
par.MaxCharLen = len(tempString)
par.iPrimValue = tempString
case DATE:
fallthrough
case TIMESTAMP:
fallthrough
case TimeStampTZ_DTY:
par.iPrimValue, err = getDate(val)
if err != nil {
return err
}
case RAW:
var tempByte []byte
tempByte, err = getBytes(val)
if err != nil {
return err
}
par.MaxLen = len(tempByte)
par.iPrimValue = tempByte
if par.MaxLen == 0 {
par.MaxLen = 1
}
case OCIClobLocator:
fallthrough
case OCIBlobLocator:
var temp *Lob
temp, err = getLob(val, connection)
if err != nil {
return err
}
par.iPrimValue = temp
if temp == nil {
//if par.Direction == Input {
// par.DataType = NCHAR
//}
par.MaxLen = 1
par.iPrimValue = nil
par.IsNull = true
}
case OCIFileLocator:
if value, ok := val.(BFile); ok {
if value.Valid {
if par.Direction == Input && !value.isInit() {
return errors.New("BFile should be initialized first")
}
par.iPrimValue = &value
} else {
par.iPrimValue = nil
par.IsNull = true
}
}
case REFCURSOR:
par.iPrimValue = nil
par.IsNull = true
case XMLType:
rValue := reflect.ValueOf(val)
pars := make([]ParameterInfo, 0, 10)
// if value is null or value is not struct ==> pass null for the object
if !rValue.IsValid() || rValue.Kind() != reflect.Struct || (rValue.Kind() == reflect.Ptr && rValue.IsNil()) {
par.IsNull = true
par.iPrimValue = nil
return nil
}
for _, attrib := range par.cusType.attribs {
attrib.Direction = par.Direction
attrib.parent = par
if fieldIndex, ok := par.cusType.fieldMap[attrib.Name]; ok {
if rValue.Field(fieldIndex).CanInterface() {
attrib.Value = rValue.Field(fieldIndex).Interface()
}
if attrib.cusType != nil && attrib.cusType.isArray {
attrib.MaxNoOfArrayElements = 1
}
err = attrib.encodeWithType(connection)
if err != nil {
return err
}
pars = append(pars, attrib)
}
}
par.iPrimValue = pars
}
return nil
}
func (par *ParameterInfo) encodePrimValue(conn *Connection) error {
var err error
switch value := par.iPrimValue.(type) {
case nil:
if par.DataType == XMLType && par.IsNull {
if par.cusType.isArray {
par.BValue = []byte{0xFF}
} else {
par.BValue = []byte{0xFD}
}
par.MaxNoOfArrayElements = 0
par.Flag = 0x3
} else {
par.BValue = nil
}
//case float64:
// par.BValue, err = converters.EncodeDouble(value)
// if err != nil {
// return err
// }
//case int64:
// par.BValue = converters.EncodeInt64(value)
//case uint64:
// par.BValue = converters.EncodeUint64(value)
case *Number:
par.BValue = value.data
case bool:
par.BValue = converters.EncodeBool(value)
case string:
conv, err := conn.getStrConv(par.CharsetID)
if err != nil {
return err
}
par.BValue = conv.Encode(value)
par.MaxLen = len(par.BValue)
if par.MaxLen == 0 {
par.MaxLen = 1
}
case time.Time:
switch par.DataType {
case DATE:
par.BValue = converters.EncodeDate(value)
case TIMESTAMP:
par.BValue = converters.EncodeTimeStamp(value, false, true)
case TimeStampTZ_DTY:
par.BValue = converters.EncodeTimeStamp(value, true, conn.dataNego.serverTZVersion > 0 && conn.dataNego.clientTZVersion != conn.dataNego.serverTZVersion)
}
case *Lob:
par.BValue = value.sourceLocator
case *BFile:
par.BValue = value.lob.sourceLocator
case []byte:
par.BValue = value
case []ParameterInfo:
session := conn.session
if par.MaxNoOfArrayElements > 0 {
if len(value) > 0 {
arrayBuffer := bytes.Buffer{}
if par.DataType == XMLType {
arrayBuffer.Write([]byte{1, 3})
session.WriteUint(&arrayBuffer, par.MaxNoOfArrayElements, 2, true, false)
} else {
session.WriteUint(&arrayBuffer, par.MaxNoOfArrayElements, 4, true, true)
}
for _, attrib := range value {
attrib.parent = nil
err = attrib.encodePrimValue(conn)
if err != nil {
return err
}
if attrib.DataType == XMLType {
session.WriteFixedClr(&arrayBuffer, attrib.BValue)
} else {
if attrib.IsNull && par.DataType == XMLType {
arrayBuffer.WriteByte(0xff)
} else {
session.WriteClr(&arrayBuffer, attrib.BValue)
}
}
if par.MaxCharLen < attrib.MaxCharLen {
par.MaxCharLen = attrib.MaxCharLen
}
if par.MaxLen < attrib.MaxLen {
par.MaxLen = attrib.MaxLen
}
}
par.BValue = arrayBuffer.Bytes()
}
if par.DataType == NCHAR {
par.MaxLen = conn.maxLen.nvarchar
par.MaxCharLen = par.MaxLen // / converters.MaxBytePerChar(par.CharsetID)
}
if par.DataType == RAW {
par.MaxLen = conn.maxLen.raw
}
if par.DataType == XMLType {
par.BValue = encodeObject(session, par.BValue, true)
par.MaxNoOfArrayElements = 0
par.Flag = 3
}
} else {
var objectBuffer bytes.Buffer
for _, attrib := range value {
err = attrib.encodePrimValue(conn)
if err != nil {
return err
}
if attrib.DataType == OCIFileLocator && attrib.MaxLen == 0 {
attrib.MaxLen = 4000
}
if attrib.DataType == XMLType {
if attrib.cusType.isArray {
session.WriteFixedClr(&objectBuffer, attrib.BValue)
} else {
objectBuffer.Write(attrib.BValue)
}
} else {
session.WriteClr(&objectBuffer, attrib.BValue)
}
}
if par.parent == nil {
par.BValue = encodeObject(session, objectBuffer.Bytes(), false)
} else {
par.BValue = objectBuffer.Bytes()
}
}
default:
return fmt.Errorf("unsupported primitive type: %v", reflect.TypeOf(par.iPrimValue).Name())
}
return nil
}
func (par *ParameterInfo) init() {
par.DataType = 0
par.Flag = 3
par.ContFlag = 0
par.CharsetID = 0
par.CharsetForm = 0
par.MaxLen = 1
par.MaxCharLen = 0
par.MaxNoOfArrayElements = 0
par.BValue = nil
par.iPrimValue = nil
par.oPrimValue = nil
}
func (par *ParameterInfo) encodeValue(size int, connection *Connection) error {
par.init()
err := par.setDataType(reflect.TypeOf(par.Value), par.Value, connection)
if err != nil {
return err
}
if par.MaxNoOfArrayElements > 0 && par.MaxNoOfArrayElements < size {
par.MaxNoOfArrayElements = size
}
err = par.encodeWithType(connection)
if err != nil {
return err
}
err = par.encodePrimValue(connection)
if err != nil {
return err
}
if par.DataType == OCIFileLocator {
par.MaxLen = size
if par.MaxLen == 0 {
par.MaxLen = 4000
}
}
if par.Direction != Input {
if par.DataType == NCHAR {
if par.MaxCharLen < size {
par.MaxCharLen = size
}
conv, err := connection.getStrConv(par.CharsetID)
if err != nil {
return err
}
par.MaxLen = par.MaxCharLen * converters.MaxBytePerChar(conv.GetLangID())
}
if par.DataType == RAW {
if par.MaxLen < size {
par.MaxLen = size
}
}
}
if par.Direction == Output && !(par.DataType == XMLType) {
par.BValue = nil
}
return nil
}