-
Notifications
You must be signed in to change notification settings - Fork 0
/
zzn.go
451 lines (315 loc) · 7.75 KB
/
zzn.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
/*
* Copyright (c) 2000-2018, 达梦数据库有限公司.
* All rights reserved.
*/
package dm
import (
"database/sql"
"database/sql/driver"
"math"
"reflect"
"strings"
"time"
)
const (
INT8_MAX int8 = math.MaxInt8
INT8_MIN int8 = math.MinInt8
BYTE_MAX byte = math.MaxUint8
BYTE_MIN byte = 0
INT16_MAX int16 = math.MaxInt16
INT16_MIN int16 = math.MinInt16
UINT16_MAX uint16 = math.MaxUint16
UINT16_MIN uint16 = 0
INT32_MAX int32 = math.MaxInt32
INT32_MIN int32 = math.MinInt32
UINT32_MAX uint32 = math.MaxUint32
UINT32_MIN uint32 = 0
INT64_MAX int64 = math.MaxInt64
INT64_MIN int64 = math.MinInt64
UINT64_MAX uint64 = math.MaxUint64
UINT64_MIN uint64 = 0
FLOAT32_MAX float32 = 3.4e+38
FLOAT32_MIN float32 = -3.4e+38
BYTE_SIZE = 1
USINT_SIZE = 2
ULINT_SIZE = 4
DDWORD_SIZE = 8
LINT64_SIZE = 8
CHAR = 0
VARCHAR2 = 1
VARCHAR = 2
BIT = 3
TINYINT = 5
SMALLINT = 6
INT = 7
BIGINT = 8
DECIMAL = 9
REAL = 10
DOUBLE = 11
BLOB = 12
BOOLEAN = 13
DATE = 14
TIME = 15
DATETIME = 16
BINARY = 17
VARBINARY = 18
CLOB = 19
INTERVAL_YM = 20
INTERVAL_DT = 21
TIME_TZ = 22
DATETIME_TZ = 23
NULL = 25
ANY = 31
STAR_ALL = 32
STAR = 33
RECORD = 40
TYPE = 41
TYPE_REF = 42
UNKNOWN = 54
ARRAY = 117
CLASS = 119
CURSOR = 120
PLTYPE_RECORD = 121
SARRAY = 122
CURSOR_ORACLE = -10
BIT_PREC = BYTE_SIZE
TINYINT_PREC = BYTE_SIZE
SMALLINT_PREC = USINT_SIZE
INT_PREC = ULINT_SIZE
BIGINT_PREC = LINT64_SIZE
REAL_PREC = 4
DOUBLE_PREC = 8
DATE_PREC = 3
TIME_PREC = 5
DATETIME_PREC = 8
INTERVAL_YM_PREC = 3 * ULINT_SIZE
INTERVAL_DT_PREC = 6 * ULINT_SIZE
TIME_TZ_PREC = 12
DATETIME_TZ_PREC = 12
VARCHAR_PREC = 8188
VARBINARY_PREC = 8188
BLOB_PREC int32 = INT32_MAX
CLOB_PREC int32 = INT32_MAX
NULL_PREC = 0
LOCAL_TIME_ZONE_SCALE_MASK = 0x00001000
BFILE_PREC = 512
BFILE_SCALE = 6
COMPLEX_SCALE = 5
CURRENCY_PREC = 19
CURRENCY_SCALE = 4
FLOAT_SCALE_MASK = 0x81
)
func resetColType(stmt *DmStatement, i int, colType int32) bool {
parameter := &stmt.params[i]
if parameter.ioType == IO_TYPE_OUT {
stmt.curRowBindIndicator[i] |= BIND_OUT
return false
} else if parameter.ioType == IO_TYPE_IN {
stmt.curRowBindIndicator[i] |= BIND_IN
} else {
stmt.curRowBindIndicator[i] |= BIND_IN
stmt.curRowBindIndicator[i] |= BIND_OUT
}
if parameter.typeFlag != TYPE_FLAG_EXACT {
parameter.colType = colType
parameter.scale = 0
switch colType {
case CHAR, VARCHAR, VARCHAR2:
parameter.prec = VARCHAR_PREC
case CLOB:
parameter.prec = CLOB_PREC
case BINARY, VARBINARY:
parameter.prec = VARBINARY_PREC
case BLOB:
parameter.prec = BLOB_PREC
case BOOLEAN, BIT:
parameter.prec = BIT_PREC
}
}
return true
}
func isBFile(colType int, prec int, scale int) bool {
return colType == VARCHAR && prec == BFILE_PREC && scale == BFILE_SCALE
}
func isComplexType(colType int, scale int) bool {
return (colType == BLOB && scale == COMPLEX_SCALE) || colType == ARRAY || colType == SARRAY || colType == CLASS || colType == PLTYPE_RECORD
}
func isLocalTimeZone(colType int, scale int) bool {
return colType == DATETIME && (scale&LOCAL_TIME_ZONE_SCALE_MASK) != 0
}
func getLocalTimeZoneScale(colType int, scale int) int {
return scale & (^LOCAL_TIME_ZONE_SCALE_MASK)
}
func isFloat(colType int, scale int) bool {
return colType == DECIMAL && scale == FLOAT_SCALE_MASK
}
func getFloatPrec(prec int) int {
return int(math.Round(float64(prec)*0.30103)) + 1
}
func getFloatScale(scale int) int {
return scale & (^FLOAT_SCALE_MASK)
}
var (
scanTypeFloat32 = reflect.TypeOf(float32(0))
scanTypeFloat64 = reflect.TypeOf(float64(0))
scanTypeBool = reflect.TypeOf(false)
scanTypeInt8 = reflect.TypeOf(int8(0))
scanTypeInt16 = reflect.TypeOf(int16(0))
scanTypeInt32 = reflect.TypeOf(int32(0))
scanTypeInt64 = reflect.TypeOf(int64(0))
scanTypeNullBool = reflect.TypeOf(sql.NullBool{})
scanTypeNullFloat = reflect.TypeOf(sql.NullFloat64{})
scanTypeNullInt = reflect.TypeOf(sql.NullInt64{})
scanTypeNullString = reflect.TypeOf(sql.NullString{})
scanTypeNullTime = reflect.TypeOf(sql.NullTime{})
scanTypeRawBytes = reflect.TypeOf(sql.RawBytes{})
scanTypeString = reflect.TypeOf("")
scanTypeTime = reflect.TypeOf(time.Now())
scanTypeUnknown = reflect.TypeOf(new(interface{}))
)
func (column *column) ScanType() reflect.Type {
switch column.colType {
case BOOLEAN:
if column.nullable {
return scanTypeNullBool
}
return scanTypeBool
case BIT:
if strings.ToLower(column.typeName) == "boolean" {
if column.nullable {
return scanTypeNullBool
}
return scanTypeBool
} else {
if column.nullable {
return scanTypeNullInt
}
return scanTypeInt8
}
case TINYINT:
if column.nullable {
return scanTypeNullInt
}
return scanTypeInt8
case SMALLINT:
if column.nullable {
return scanTypeNullInt
}
return scanTypeInt16
case INT:
if column.nullable {
return scanTypeNullInt
}
return scanTypeInt32
case BIGINT:
if column.nullable {
return scanTypeNullInt
}
return scanTypeInt64
case REAL:
if column.nullable {
return scanTypeNullFloat
}
return scanTypeFloat32
case DOUBLE:
if strings.ToLower(column.typeName) == "float" {
if column.nullable {
return scanTypeNullFloat
}
return scanTypeFloat32
}
if column.nullable {
return scanTypeNullFloat
}
return scanTypeFloat64
case DATE, TIME, DATETIME:
if column.nullable {
return scanTypeNullTime
}
return scanTypeTime
case DECIMAL, BINARY, VARBINARY, BLOB:
return scanTypeRawBytes
case CHAR, VARCHAR2, VARCHAR, CLOB:
if column.nullable {
return scanTypeNullString
}
return scanTypeString
}
return scanTypeUnknown
}
func (column *column) Length() (length int64, ok bool) {
switch column.colType {
case BINARY:
case VARBINARY:
case BLOB:
case CHAR:
case VARCHAR2:
case VARCHAR:
case CLOB:
return int64(column.prec), true
}
return int64(0), false
}
func (column *column) PrecisionScale() (precision, scale int64, ok bool) {
switch column.colType {
case DECIMAL:
return int64(column.prec), int64(column.scale), true
}
return int64(0), int64(0), false
}
func (column *column) getColumnData(bytes []byte, conn *Connection) (driver.Value, error) {
if bytes == nil {
return nil, nil
}
switch column.colType {
case BOOLEAN:
return bytes[0] != 0, nil
case BIT:
if strings.ToLower(column.typeName) == "boolean" {
return bytes[0] != 0, nil
}
return int8(bytes[0]), nil
case TINYINT:
return int8(bytes[0]), nil
case SMALLINT:
return Dm_build_1219.Dm_build_1316(bytes, 0), nil
case INT:
return Dm_build_1219.Dm_build_1321(bytes, 0), nil
case BIGINT:
return Dm_build_1219.Dm_build_1326(bytes, 0), nil
case REAL:
return Dm_build_1219.Dm_build_1331(bytes, 0), nil
case DOUBLE:
return Dm_build_1219.Dm_build_1335(bytes, 0), nil
case DATE, TIME, DATETIME, TIME_TZ, DATETIME_TZ:
return DB2G.toTime(bytes, column, conn)
case INTERVAL_DT:
return newDmIntervalDTByBytes(bytes).String(), nil
case INTERVAL_YM:
return newDmIntervalYMByBytes(bytes).String(), nil
case DECIMAL:
tmp, err := DB2G.toDmDecimal(bytes, column, conn)
if err != nil {
return nil, err
}
return tmp.String(), nil
case BINARY, VARBINARY:
return bytes, nil
case BLOB:
return DB2G.toDmBlob(bytes, column, conn), nil
case CHAR, VARCHAR2, VARCHAR:
return Dm_build_1219.Dm_build_1376(bytes, 0, len(bytes), conn.getServerEncoding(), conn), nil
case CLOB:
return DB2G.toDmClob(bytes, conn, column), nil
}
return string(bytes), nil
}
func emptyStringToNil(t int32) bool {
switch t {
case BOOLEAN, BIT, TINYINT, SMALLINT, INT, BIGINT, REAL, DOUBLE, DECIMAL,
DATE, TIME, DATETIME, INTERVAL_DT, INTERVAL_YM, TIME_TZ, DATETIME_TZ:
return true
default:
return false
}
}