github.com/wanlay/gorm-dm8@v1.0.5/dmr/i.go (about) 1 /* 2 * Copyright (c) 2000-2018, 达梦数据库有限公司. 3 * All rights reserved. 4 */ 5 package dmr 6 7 import ( 8 "strconv" 9 "time" 10 11 "github.com/wanlay/gorm-dm8/dmr/util" 12 ) 13 14 var DB2G db2g 15 16 type db2g struct { 17 } 18 19 func (DB2G db2g) processVarchar2(bytes []byte, prec int) []byte { 20 rbytes := make([]byte, prec) 21 copy(rbytes[:len(bytes)], bytes[:]) 22 for i := len(bytes); i < len(rbytes); i++ { 23 rbytes[i] = ' ' 24 } 25 return rbytes 26 } 27 28 func (DB2G db2g) charToString(bytes []byte, column *column, conn *DmConnection) string { 29 if column.colType == VARCHAR2 { 30 bytes = DB2G.processVarchar2(bytes, int(column.prec)) 31 } else if column.colType == CLOB { 32 clob := newClobFromDB(bytes, conn, column, true) 33 clobLen, _ := clob.GetLength() 34 clobStr, _ := clob.getSubString(1, int32(clobLen)) 35 return clobStr 36 } 37 return Dm_build_1220.Dm_build_1470(bytes, conn.serverEncoding, conn) 38 } 39 40 func (DB2G db2g) charToFloat64(bytes []byte, column *column, conn *DmConnection) (float64, error) { 41 str := DB2G.charToString(bytes, column, conn) 42 val, err := strconv.ParseFloat(str, 64) 43 if err != nil { 44 return 0, ECGO_DATA_CONVERTION_ERROR.throw() 45 } 46 47 return val, nil 48 } 49 50 func (DB2G db2g) charToDeciaml(bytes []byte, column *column, conn *DmConnection) (*DmDecimal, error) { 51 str := DB2G.charToString(bytes, column, conn) 52 return NewDecimalFromString(str) 53 } 54 55 func (DB2G db2g) BinaryToInt64(bytes []byte, column *column, conn *DmConnection) (int64, error) { 56 if column.colType == BLOB { 57 blob := newBlobFromDB(bytes, conn, column, true) 58 blobLen, err := blob.GetLength() 59 if err != nil { 60 return 0, err 61 } 62 bytes, err = blob.getBytes(1, int32(blobLen)) 63 if err != nil { 64 return 0, err 65 } 66 } 67 var n, b int64 = 0, 0 68 69 startIndex := 0 70 var length int 71 if len(bytes) > 8 { 72 length = 8 73 for j := 0; j < len(bytes)-8; j++ { 74 if bytes[j] != 0 { 75 return 0, ECGO_DATA_CONVERTION_ERROR.throw() 76 } 77 78 startIndex = len(bytes) - 8 79 length = 8 80 } 81 } else { 82 length = len(bytes) 83 } 84 85 for j := startIndex; j < startIndex+length; j++ { 86 b = int64(0xff & bytes[j]) 87 n = b | (n << 8) 88 } 89 90 return n, nil 91 } 92 93 func (DB2G db2g) decToDecimal(bytes []byte, prec int, scale int, compatibleOracle bool) (*DmDecimal, error) { 94 95 if compatibleOracle { 96 prec = -1 97 scale = -1 98 } 99 return newDecimal(bytes, prec, scale) 100 } 101 102 func (DB2G db2g) toBytes(bytes []byte, column *column, conn *DmConnection) ([]byte, error) { 103 retBytes := Dm_build_1220.Dm_build_1371(bytes, 0, len(bytes)) 104 switch column.colType { 105 case CLOB: 106 clob := newClobFromDB(retBytes, conn, column, true) 107 str, err := clob.getSubString(1, int32(clob.length)) 108 if err != nil { 109 return nil, err 110 } 111 112 return Dm_build_1220.Dm_build_1433(str, conn.getServerEncoding(), conn), nil 113 case BLOB: 114 blob := newBlobFromDB(retBytes, conn, column, true) 115 bs, err := blob.getBytes(1, int32(blob.length)) 116 if err != nil { 117 return nil, err 118 } 119 120 return bs, nil 121 } 122 return nil, ECGO_DATA_CONVERTION_ERROR.throw() 123 } 124 125 func (DB2G db2g) toString(bytes []byte, column *column, conn *DmConnection) string { 126 switch column.colType { 127 case CHAR, VARCHAR, VARCHAR2: 128 return DB2G.charToString(bytes, column, conn) 129 case BIT, BOOLEAN, TINYINT: 130 return strconv.FormatInt(int64(bytes[0]), 10) 131 case SMALLINT: 132 return strconv.FormatInt(int64(Dm_build_1220.Dm_build_1441(bytes)), 10) 133 case INT: 134 return strconv.FormatInt(int64(Dm_build_1220.Dm_build_1444(bytes)), 10) 135 case BIGINT: 136 return strconv.FormatInt(int64(Dm_build_1220.Dm_build_1447(bytes)), 10) 137 case REAL: 138 return strconv.FormatFloat(float64(Dm_build_1220.Dm_build_1450(bytes)), 'f', -1, 32) 139 case DOUBLE: 140 return strconv.FormatFloat(float64(Dm_build_1220.Dm_build_1453(bytes)), 'f', -1, 64) 141 case DECIMAL: 142 143 case BINARY, VARBINARY: 144 util.StringUtil.BytesToHexString(bytes, false) 145 case BLOB: 146 147 case CLOB: 148 149 case DATE: 150 dt := decode(bytes, column.isBdta, int(column.colType), int(column.scale), int(conn.dmConnector.localTimezone), int(conn.DbTimezone)) 151 if conn.FormatDate != "" { 152 return dtToStringByOracleFormat(dt, conn.FormatDate, int(conn.OracleDateLanguage)) 153 } 154 case TIME: 155 dt := decode(bytes, column.isBdta, int(column.colType), int(column.scale), int(conn.dmConnector.localTimezone), int(conn.DbTimezone)) 156 if conn.FormatTime != "" { 157 return dtToStringByOracleFormat(dt, conn.FormatTime, int(conn.OracleDateLanguage)) 158 } 159 case DATETIME: 160 dt := decode(bytes, column.isBdta, int(column.colType), int(column.scale), int(conn.dmConnector.localTimezone), int(conn.DbTimezone)) 161 if conn.FormatTimestamp != "" { 162 return dtToStringByOracleFormat(dt, conn.FormatTimestamp, int(conn.OracleDateLanguage)) 163 } 164 case TIME_TZ: 165 dt := decode(bytes, column.isBdta, int(column.colType), int(column.scale), int(conn.dmConnector.localTimezone), int(conn.DbTimezone)) 166 if conn.FormatTimeTZ != "" { 167 return dtToStringByOracleFormat(dt, conn.FormatTimeTZ, int(conn.OracleDateLanguage)) 168 } 169 case DATETIME_TZ: 170 dt := decode(bytes, column.isBdta, int(column.colType), int(column.scale), int(conn.dmConnector.localTimezone), int(conn.DbTimezone)) 171 if conn.FormatTimestampTZ != "" { 172 return dtToStringByOracleFormat(dt, conn.FormatTimestampTZ, int(conn.OracleDateLanguage)) 173 } 174 case INTERVAL_DT: 175 return newDmIntervalDTByBytes(bytes).String() 176 case INTERVAL_YM: 177 return newDmIntervalYMByBytes(bytes).String() 178 case ARRAY: 179 180 case SARRAY: 181 182 case CLASS: 183 184 case PLTYPE_RECORD: 185 186 } 187 return "" 188 } 189 190 func (DB2G db2g) toBool(bytes []byte, column *column, conn *DmConnection) (bool, error) { 191 switch column.colType { 192 case BIT, BOOLEAN, TINYINT: 193 return bytes[0] != 0, nil 194 case SMALLINT: 195 return Dm_build_1220.Dm_build_1317(bytes, 0) != 0, nil 196 case INT: 197 return Dm_build_1220.Dm_build_1322(bytes, 0) != 0, nil 198 case BIGINT: 199 return Dm_build_1220.Dm_build_1327(bytes, 0) != 0, nil 200 case REAL: 201 return Dm_build_1220.Dm_build_1332(bytes, 0) != 0, nil 202 case DOUBLE: 203 return Dm_build_1220.Dm_build_1336(bytes, 0) != 0, nil 204 case DECIMAL: 205 206 case CHAR, VARCHAR, VARCHAR2, CLOB: 207 return G2DB.toBool(DB2G.charToString(bytes, column, conn)) 208 } 209 210 return false, ECGO_DATA_CONVERTION_ERROR.throw() 211 } 212 213 func (DB2G db2g) toByte(bytes []byte, column *column, conn *DmConnection) (byte, error) { 214 switch column.colType { 215 case BIT, BOOLEAN, TINYINT: 216 217 if bytes == nil || len(bytes) == 0 { 218 return 0, nil 219 } else { 220 return bytes[0], nil 221 } 222 case SMALLINT: 223 tval := Dm_build_1220.Dm_build_1317(bytes, 0) 224 if tval < int16(BYTE_MIN) || tval > int16(BYTE_MAX) { 225 return 0, ECGO_DATA_OVERFLOW.throw() 226 } 227 return byte(tval), nil 228 case INT: 229 tval := Dm_build_1220.Dm_build_1322(bytes, 0) 230 if tval < int32(BYTE_MIN) || tval > int32(BYTE_MAX) { 231 return 0, ECGO_DATA_OVERFLOW.throw() 232 } 233 return byte(tval), nil 234 case BIGINT: 235 tval := Dm_build_1220.Dm_build_1327(bytes, 0) 236 if tval < int64(BYTE_MIN) || tval > int64(BYTE_MAX) { 237 return 0, ECGO_DATA_OVERFLOW.throw() 238 } 239 return byte(tval), nil 240 case REAL: 241 tval := Dm_build_1220.Dm_build_1332(bytes, 0) 242 if tval < float32(BYTE_MIN) || tval > float32(BYTE_MAX) { 243 return 0, ECGO_DATA_OVERFLOW.throw() 244 } 245 return byte(tval), nil 246 case DOUBLE: 247 tval := Dm_build_1220.Dm_build_1336(bytes, 0) 248 if tval < float64(BYTE_MIN) || tval > float64(BYTE_MAX) { 249 return 0, ECGO_DATA_OVERFLOW.throw() 250 } 251 return byte(tval), nil 252 case DECIMAL: 253 254 case CHAR, VARCHAR, VARCHAR2, CLOB: 255 tval, err := DB2G.charToFloat64(bytes, column, conn) 256 if err != nil { 257 return 0, err 258 } 259 260 if tval < float64(BYTE_MIN) || tval > float64(BYTE_MAX) { 261 return 0, ECGO_DATA_OVERFLOW.throw() 262 } 263 return byte(tval), nil 264 case BINARY, VARBINARY, BLOB: 265 { 266 tval, err := DB2G.BinaryToInt64(bytes, column, conn) 267 if err != nil { 268 return 0, err 269 } 270 271 if tval < int64(BYTE_MIN) || tval > int64(BYTE_MAX) { 272 return 0, ECGO_DATA_OVERFLOW.throw() 273 } 274 return byte(tval), nil 275 } 276 } 277 278 return 0, ECGO_DATA_CONVERTION_ERROR.throw() 279 } 280 281 func (DB2G db2g) toInt8(bytes []byte, column *column, conn *DmConnection) (int8, error) { 282 switch column.colType { 283 case BIT, BOOLEAN, TINYINT: 284 if bytes == nil || len(bytes) == 0 { 285 return 0, nil 286 } 287 288 return int8(bytes[0]), nil 289 case SMALLINT: 290 tval := Dm_build_1220.Dm_build_1317(bytes, 0) 291 if tval < int16(INT8_MIN) || tval < int16(INT8_MAX) { 292 return 0, ECGO_DATA_OVERFLOW.throw() 293 } 294 return int8(tval), nil 295 case INT: 296 297 tval := Dm_build_1220.Dm_build_1322(bytes, 0) 298 if tval < int32(INT8_MIN) || tval > int32(INT8_MAX) { 299 return 0, ECGO_DATA_OVERFLOW.throw() 300 } 301 return int8(tval), nil 302 case BIGINT: 303 tval := Dm_build_1220.Dm_build_1327(bytes, 0) 304 if tval < int64(INT8_MIN) || tval > int64(INT8_MAX) { 305 return 0, ECGO_DATA_OVERFLOW.throw() 306 } 307 return int8(tval), nil 308 case REAL: 309 tval := Dm_build_1220.Dm_build_1332(bytes, 0) 310 if tval < float32(INT8_MIN) || tval > float32(INT8_MAX) { 311 return 0, ECGO_DATA_OVERFLOW.throw() 312 } 313 return int8(tval), nil 314 case DOUBLE: 315 tval := Dm_build_1220.Dm_build_1336(bytes, 0) 316 if tval < float64(INT8_MIN) || tval > float64(INT8_MAX) { 317 return 0, ECGO_DATA_OVERFLOW.throw() 318 } 319 return int8(tval), nil 320 case DECIMAL: 321 322 case CHAR, VARCHAR, VARCHAR2, CLOB: 323 tval, err := DB2G.charToFloat64(bytes, column, conn) 324 if err != nil { 325 return 0, err 326 } 327 328 if tval < float64(INT8_MIN) || tval > float64(INT8_MAX) { 329 return 0, ECGO_DATA_OVERFLOW.throw() 330 } 331 return int8(tval), nil 332 case BINARY, VARBINARY, BLOB: 333 { 334 tval, err := DB2G.BinaryToInt64(bytes, column, conn) 335 if err != nil { 336 return 0, err 337 } 338 339 if tval < int64(INT8_MIN) || tval > int64(INT8_MAX) { 340 return 0, ECGO_DATA_OVERFLOW.throw() 341 } 342 return int8(tval), nil 343 } 344 } 345 346 return 0, ECGO_DATA_CONVERTION_ERROR.throw() 347 } 348 349 func (DB2G db2g) toInt16(bytes []byte, column *column, conn *DmConnection) (int16, error) { 350 switch column.colType { 351 case BIT, BOOLEAN, TINYINT: 352 if bytes == nil || len(bytes) == 0 { 353 return 0, nil 354 } 355 356 return int16(bytes[0]), nil 357 case SMALLINT: 358 return Dm_build_1220.Dm_build_1317(bytes, 0), nil 359 case INT: 360 361 tval := Dm_build_1220.Dm_build_1322(bytes, 0) 362 if tval < int32(INT16_MIN) || tval > int32(INT16_MAX) { 363 return 0, ECGO_DATA_OVERFLOW.throw() 364 } 365 return int16(tval), nil 366 case BIGINT: 367 tval := Dm_build_1220.Dm_build_1327(bytes, 0) 368 if tval < int64(INT16_MIN) || tval > int64(INT16_MAX) { 369 return 0, ECGO_DATA_OVERFLOW.throw() 370 } 371 return int16(tval), nil 372 case REAL: 373 tval := Dm_build_1220.Dm_build_1332(bytes, 0) 374 if tval < float32(INT16_MIN) || tval > float32(INT16_MAX) { 375 return 0, ECGO_DATA_OVERFLOW.throw() 376 } 377 return int16(tval), nil 378 case DOUBLE: 379 tval := Dm_build_1220.Dm_build_1336(bytes, 0) 380 if tval < float64(INT16_MIN) || tval > float64(INT16_MAX) { 381 return 0, ECGO_DATA_OVERFLOW.throw() 382 } 383 return int16(tval), nil 384 case DECIMAL: 385 386 case CHAR, VARCHAR, VARCHAR2, CLOB: 387 tval, err := DB2G.charToFloat64(bytes, column, conn) 388 if err != nil { 389 return 0, err 390 } 391 392 if tval < float64(INT16_MIN) || tval > float64(INT16_MAX) { 393 return 0, ECGO_DATA_OVERFLOW.throw() 394 } 395 return int16(tval), nil 396 case BINARY, VARBINARY, BLOB: 397 { 398 tval, err := DB2G.BinaryToInt64(bytes, column, conn) 399 if err != nil { 400 return 0, err 401 } 402 403 if tval < int64(INT16_MIN) || tval > int64(INT16_MAX) { 404 return 0, ECGO_DATA_OVERFLOW.throw() 405 } 406 return int16(tval), nil 407 } 408 } 409 410 return 0, ECGO_DATA_CONVERTION_ERROR.throw() 411 } 412 413 func (DB2G db2g) toUInt16(bytes []byte, column *column, conn *DmConnection) (uint16, error) { 414 switch column.colType { 415 case BIT, BOOLEAN, TINYINT: 416 if bytes == nil || len(bytes) == 0 { 417 return 0, nil 418 } 419 420 return uint16(bytes[0]), nil 421 case SMALLINT: 422 return uint16(Dm_build_1220.Dm_build_1317(bytes, 0)), nil 423 case INT: 424 tval := Dm_build_1220.Dm_build_1322(bytes, 0) 425 if tval < int32(UINT16_MIN) || tval > int32(UINT16_MAX) { 426 return 0, ECGO_DATA_OVERFLOW.throw() 427 } 428 return uint16(tval), nil 429 case BIGINT: 430 tval := Dm_build_1220.Dm_build_1327(bytes, 0) 431 if tval < int64(UINT16_MIN) || tval > int64(UINT16_MAX) { 432 return 0, ECGO_DATA_OVERFLOW.throw() 433 } 434 return uint16(tval), nil 435 case REAL: 436 tval := Dm_build_1220.Dm_build_1332(bytes, 0) 437 if tval < float32(UINT16_MIN) || tval > float32(UINT16_MAX) { 438 return 0, ECGO_DATA_OVERFLOW.throw() 439 } 440 return uint16(tval), nil 441 case DOUBLE: 442 tval := Dm_build_1220.Dm_build_1336(bytes, 0) 443 if tval < float64(UINT16_MIN) || tval > float64(UINT16_MAX) { 444 return 0, ECGO_DATA_OVERFLOW.throw() 445 } 446 return uint16(tval), nil 447 case DECIMAL: 448 449 case CHAR, VARCHAR, VARCHAR2, CLOB: 450 tval, err := DB2G.charToFloat64(bytes, column, conn) 451 if err != nil { 452 return 0, err 453 } 454 455 if tval < float64(UINT16_MIN) || tval > float64(UINT16_MAX) { 456 return 0, ECGO_DATA_OVERFLOW.throw() 457 } 458 return uint16(tval), nil 459 case BINARY, VARBINARY, BLOB: 460 { 461 tval, err := DB2G.BinaryToInt64(bytes, column, conn) 462 if err != nil { 463 return 0, err 464 } 465 466 if tval < int64(UINT16_MIN) || tval > int64(UINT16_MAX) { 467 return 0, ECGO_DATA_OVERFLOW.throw() 468 } 469 return uint16(tval), nil 470 } 471 } 472 473 return 0, ECGO_DATA_CONVERTION_ERROR.throw() 474 } 475 476 func (DB2G db2g) toInt32(bytes []byte, column *column, conn *DmConnection) (int32, error) { 477 switch column.colType { 478 case BIT, BOOLEAN, TINYINT: 479 if bytes == nil || len(bytes) == 0 { 480 return 0, nil 481 } 482 483 return int32(bytes[0]), nil 484 case SMALLINT: 485 return int32(Dm_build_1220.Dm_build_1317(bytes, 0)), nil 486 case INT: 487 return Dm_build_1220.Dm_build_1322(bytes, 0), nil 488 case BIGINT: 489 tval := Dm_build_1220.Dm_build_1327(bytes, 0) 490 if tval < int64(INT32_MIN) || tval > int64(INT32_MAX) { 491 return 0, ECGO_DATA_OVERFLOW.throw() 492 } 493 return int32(tval), nil 494 case REAL: 495 tval := Dm_build_1220.Dm_build_1332(bytes, 0) 496 if tval < float32(INT32_MIN) || tval > float32(INT32_MAX) { 497 return 0, ECGO_DATA_OVERFLOW.throw() 498 } 499 return int32(tval), nil 500 case DOUBLE: 501 tval := Dm_build_1220.Dm_build_1336(bytes, 0) 502 if tval < float64(INT32_MIN) || tval > float64(INT32_MAX) { 503 return 0, ECGO_DATA_OVERFLOW.throw() 504 } 505 return int32(tval), nil 506 case DECIMAL: 507 508 case CHAR, VARCHAR, VARCHAR2, CLOB: 509 tval, err := DB2G.charToFloat64(bytes, column, conn) 510 if err != nil { 511 return 0, err 512 } 513 514 if tval < float64(INT32_MIN) || tval > float64(INT32_MAX) { 515 return 0, ECGO_DATA_OVERFLOW.throw() 516 } 517 return int32(tval), nil 518 case BINARY, VARBINARY, BLOB: 519 { 520 tval, err := DB2G.BinaryToInt64(bytes, column, conn) 521 if err != nil { 522 return 0, err 523 } 524 525 if tval < int64(INT32_MIN) || tval > int64(INT32_MAX) { 526 return 0, ECGO_DATA_OVERFLOW.throw() 527 } 528 return int32(tval), nil 529 } 530 } 531 532 return 0, ECGO_DATA_CONVERTION_ERROR.throw() 533 } 534 535 func (DB2G db2g) toUInt32(bytes []byte, column *column, conn *DmConnection) (uint32, error) { 536 switch column.colType { 537 case BIT, BOOLEAN, TINYINT: 538 if bytes == nil || len(bytes) == 0 { 539 return 0, nil 540 } 541 542 return uint32(bytes[0]), nil 543 case SMALLINT: 544 return uint32(Dm_build_1220.Dm_build_1317(bytes, 0)), nil 545 case INT: 546 return uint32(Dm_build_1220.Dm_build_1322(bytes, 0)), nil 547 case BIGINT: 548 tval := Dm_build_1220.Dm_build_1327(bytes, 0) 549 if tval < int64(UINT32_MIN) || tval > int64(UINT32_MAX) { 550 return 0, ECGO_DATA_OVERFLOW.throw() 551 } 552 return uint32(tval), nil 553 case REAL: 554 tval := Dm_build_1220.Dm_build_1332(bytes, 0) 555 if tval < float32(UINT32_MIN) || tval > float32(UINT32_MAX) { 556 return 0, ECGO_DATA_OVERFLOW.throw() 557 } 558 return uint32(tval), nil 559 case DOUBLE: 560 tval := Dm_build_1220.Dm_build_1336(bytes, 0) 561 if tval < float64(UINT32_MIN) || tval > float64(UINT32_MAX) { 562 return 0, ECGO_DATA_OVERFLOW.throw() 563 } 564 return uint32(tval), nil 565 case DECIMAL: 566 567 case CHAR, VARCHAR, VARCHAR2, CLOB: 568 tval, err := DB2G.charToFloat64(bytes, column, conn) 569 if err != nil { 570 return 0, err 571 } 572 573 if tval < float64(UINT32_MIN) || tval > float64(UINT32_MAX) { 574 return 0, ECGO_DATA_OVERFLOW.throw() 575 } 576 return uint32(tval), nil 577 case BINARY, VARBINARY, BLOB: 578 { 579 tval, err := DB2G.BinaryToInt64(bytes, column, conn) 580 if err != nil { 581 return 0, err 582 } 583 584 if tval < int64(UINT32_MIN) || tval > int64(UINT32_MAX) { 585 return 0, ECGO_DATA_OVERFLOW.throw() 586 } 587 return uint32(tval), nil 588 } 589 } 590 591 return 0, ECGO_DATA_CONVERTION_ERROR.throw() 592 } 593 594 func (DB2G db2g) toInt64(bytes []byte, column *column, conn *DmConnection) (int64, error) { 595 switch column.colType { 596 case BOOLEAN, BIT, TINYINT: 597 if bytes == nil || len(bytes) == 0 { 598 return int64(0), nil 599 } else { 600 return int64(bytes[0]), nil 601 } 602 case SMALLINT: 603 return int64(Dm_build_1220.Dm_build_1441(bytes)), nil 604 case INT: 605 return int64(Dm_build_1220.Dm_build_1444(bytes)), nil 606 case BIGINT: 607 return int64(Dm_build_1220.Dm_build_1447(bytes)), nil 608 case REAL: 609 return int64(Dm_build_1220.Dm_build_1450(bytes)), nil 610 case DOUBLE: 611 return int64(Dm_build_1220.Dm_build_1453(bytes)), nil 612 613 case CHAR, VARCHAR2, VARCHAR, CLOB: 614 tval, err := DB2G.charToFloat64(bytes, column, conn) 615 if err != nil { 616 return 0, err 617 } 618 619 if int64(tval) < INT64_MIN || int64(tval) > INT64_MAX { 620 return 0, ECGO_DATA_OVERFLOW.throw() 621 } 622 return int64(tval), nil 623 case BINARY, VARBINARY, BLOB: 624 tval, err := DB2G.BinaryToInt64(bytes, column, conn) 625 if err != nil { 626 return 0, err 627 } 628 629 return tval, nil 630 } 631 return 0, ECGO_DATA_CONVERTION_ERROR.throw() 632 } 633 634 func (DB2G db2g) toUInt64(bytes []byte, column *column, conn *DmConnection) (uint64, error) { 635 switch column.colType { 636 case BOOLEAN, BIT, TINYINT: 637 if bytes == nil || len(bytes) == 0 { 638 return uint64(0), nil 639 } else { 640 return uint64(bytes[0]), nil 641 } 642 case SMALLINT: 643 return uint64(Dm_build_1220.Dm_build_1441(bytes)), nil 644 case INT: 645 return uint64(Dm_build_1220.Dm_build_1444(bytes)), nil 646 case BIGINT: 647 return uint64(Dm_build_1220.Dm_build_1447(bytes)), nil 648 case REAL: 649 return uint64(Dm_build_1220.Dm_build_1450(bytes)), nil 650 case DOUBLE: 651 return uint64(Dm_build_1220.Dm_build_1453(bytes)), nil 652 653 case CHAR, VARCHAR2, VARCHAR, CLOB: 654 tval, err := DB2G.charToFloat64(bytes, column, conn) 655 if err != nil { 656 return 0, err 657 } 658 659 if uint64(tval) < UINT64_MIN || uint64(tval) > UINT64_MAX { 660 return 0, ECGO_DATA_OVERFLOW.throw() 661 } 662 return uint64(tval), nil 663 case BINARY, VARBINARY, BLOB: 664 tval, err := DB2G.BinaryToInt64(bytes, column, conn) 665 if err != nil { 666 return 0, err 667 } 668 669 return uint64(tval), nil 670 } 671 return 0, ECGO_DATA_CONVERTION_ERROR.throw() 672 } 673 674 func (DB2G db2g) toInt(bytes []byte, column *column, conn *DmConnection) (int, error) { 675 if strconv.IntSize == 32 { 676 tmp, err := DB2G.toInt32(bytes, column, conn) 677 return int(tmp), err 678 } else { 679 tmp, err := DB2G.toInt64(bytes, column, conn) 680 return int(tmp), err 681 } 682 } 683 684 func (DB2G db2g) toUInt(bytes []byte, column *column, conn *DmConnection) (uint, error) { 685 if strconv.IntSize == 32 { 686 tmp, err := DB2G.toUInt32(bytes, column, conn) 687 return uint(tmp), err 688 } else { 689 tmp, err := DB2G.toUInt64(bytes, column, conn) 690 return uint(tmp), err 691 } 692 } 693 694 func (DB2G db2g) toFloat32(bytes []byte, column *column, conn *DmConnection) (float32, error) { 695 switch column.colType { 696 case BIT, BOOLEAN, TINYINT: 697 if bytes == nil || len(bytes) == 0 { 698 return 0, nil 699 } 700 return float32(bytes[0]), nil 701 case SMALLINT: 702 return float32(Dm_build_1220.Dm_build_1317(bytes, 0)), nil 703 case INT: 704 return float32(Dm_build_1220.Dm_build_1322(bytes, 0)), nil 705 case BIGINT: 706 return float32(Dm_build_1220.Dm_build_1327(bytes, 0)), nil 707 case REAL: 708 return Dm_build_1220.Dm_build_1332(bytes, 0), nil 709 case DOUBLE: 710 dval := Dm_build_1220.Dm_build_1336(bytes, 0) 711 return float32(dval), nil 712 case DECIMAL: 713 dval, err := DB2G.decToDecimal(bytes, int(column.prec), int(column.scale), conn.CompatibleOracle()) 714 if err != nil { 715 return 0, err 716 } 717 return float32(dval.ToFloat64()), nil 718 case CHAR, VARCHAR2, VARCHAR, CLOB: 719 dval, err := DB2G.charToDeciaml(bytes, column, conn) 720 if err != nil { 721 return 0, err 722 } 723 return float32(dval.ToFloat64()), nil 724 } 725 return 0, ECGO_DATA_CONVERTION_ERROR.throw() 726 } 727 728 func (DB2G db2g) toFloat64(bytes []byte, column *column, conn *DmConnection) (float64, error) { 729 switch column.colType { 730 case BIT, BOOLEAN, TINYINT: 731 if bytes == nil || len(bytes) == 0 { 732 return 0, nil 733 } 734 return float64(bytes[0]), nil 735 case SMALLINT: 736 return float64(Dm_build_1220.Dm_build_1317(bytes, 0)), nil 737 case INT: 738 return float64(Dm_build_1220.Dm_build_1322(bytes, 0)), nil 739 case BIGINT: 740 return float64(Dm_build_1220.Dm_build_1327(bytes, 0)), nil 741 case REAL: 742 return float64(Dm_build_1220.Dm_build_1332(bytes, 0)), nil 743 case DOUBLE: 744 return Dm_build_1220.Dm_build_1336(bytes, 0), nil 745 case DECIMAL: 746 dval, err := DB2G.decToDecimal(bytes, int(column.prec), int(column.scale), conn.CompatibleOracle()) 747 if err != nil { 748 return 0, err 749 } 750 return dval.ToFloat64(), nil 751 case CHAR, VARCHAR2, VARCHAR, CLOB: 752 dval, err := DB2G.charToDeciaml(bytes, column, conn) 753 if err != nil { 754 return 0, err 755 } 756 return dval.ToFloat64(), nil 757 } 758 759 return 0, ECGO_DATA_CONVERTION_ERROR.throw() 760 } 761 762 func (DB2G db2g) toDmBlob(value []byte, column *column, conn *DmConnection) *DmBlob { 763 764 switch column.colType { 765 case BLOB: 766 return newBlobFromDB(value, conn, column, conn.lobFetchAll()) 767 } 768 769 return nil 770 } 771 772 func (DB2G db2g) toDmClob(value []byte, conn *DmConnection, column *column) *DmClob { 773 774 switch column.colType { 775 case CLOB: 776 return newClobFromDB(value, conn, column, conn.lobFetchAll()) 777 } 778 779 return nil 780 } 781 782 func (DB2G db2g) toDmDecimal(value []byte, column *column, conn *DmConnection) (*DmDecimal, error) { 783 784 switch column.colType { 785 case BIT, BOOLEAN, TINYINT: 786 if value == nil || len(value) == 0 { 787 return NewDecimalFromInt64(0) 788 } else { 789 return NewDecimalFromInt64(int64(value[0])) 790 } 791 case SMALLINT: 792 return NewDecimalFromInt64(int64(Dm_build_1220.Dm_build_1317(value, 0))) 793 case INT: 794 return NewDecimalFromInt64(int64(Dm_build_1220.Dm_build_1322(value, 0))) 795 case BIGINT: 796 return NewDecimalFromInt64(Dm_build_1220.Dm_build_1327(value, 0)) 797 case REAL: 798 return NewDecimalFromFloat64(float64(Dm_build_1220.Dm_build_1332(value, 0))) 799 case DOUBLE: 800 return NewDecimalFromFloat64(Dm_build_1220.Dm_build_1336(value, 0)) 801 case DECIMAL: 802 return decodeDecimal(value, int(column.prec), int(column.scale)) 803 case CHAR, VARCHAR, VARCHAR2, CLOB: 804 return DB2G.charToDeciaml(value, column, conn) 805 } 806 807 return nil, ECGO_DATA_CONVERTION_ERROR 808 } 809 810 func (DB2G db2g) toTime(bytes []byte, column *column, conn *DmConnection) (time.Time, error) { 811 switch column.colType { 812 case DATE, TIME, TIME_TZ, DATETIME_TZ, DATETIME: 813 dt := decode(bytes, column.isBdta, int(column.colType), int(column.scale), int(conn.dmConnector.localTimezone), int(conn.DbTimezone)) 814 return toTimeFromDT(dt, int(conn.dmConnector.localTimezone)), nil 815 case CHAR, VARCHAR2, VARCHAR, CLOB: 816 return toTimeFromString(DB2G.charToString(bytes, column, conn), int(conn.dmConnector.localTimezone)), nil 817 } 818 return time.Now(), ECGO_DATA_CONVERTION_ERROR.throw() 819 } 820 821 func (DB2G db2g) toObject(bytes []byte, column *column, conn *DmConnection) (interface{}, error) { 822 823 switch column.colType { 824 case BIT, BOOLEAN: 825 return bytes[0] != 0, nil 826 827 case TINYINT: 828 829 return Dm_build_1220.Dm_build_1313(bytes, 0), nil 830 case SMALLINT: 831 return Dm_build_1220.Dm_build_1317(bytes, 0), nil 832 case INT: 833 return Dm_build_1220.Dm_build_1322(bytes, 0), nil 834 case BIGINT: 835 return Dm_build_1220.Dm_build_1327(bytes, 0), nil 836 case DECIMAL: 837 838 case REAL: 839 return Dm_build_1220.Dm_build_1332(bytes, 0), nil 840 case DOUBLE: 841 return Dm_build_1220.Dm_build_1336(bytes, 0), nil 842 case DATE, TIME, DATETIME, TIME_TZ, DATETIME_TZ: 843 dt := decode(bytes, column.isBdta, int(column.colType), int(column.scale), int(conn.dmConnector.localTimezone), int(conn.DbTimezone)) 844 return toTimeFromDT(dt, int(conn.dmConnector.localTimezone)), nil 845 case BINARY, VARBINARY: 846 return bytes, nil 847 case BLOB: 848 blob := newBlobFromDB(bytes, conn, column, conn.lobFetchAll()) 849 850 if util.StringUtil.EqualsIgnoreCase(column.typeName, "LONGVARBINARY") { 851 852 l, err := blob.GetLength() 853 if err != nil { 854 return nil, err 855 } 856 return blob.getBytes(1, int32(l)) 857 } else { 858 return blob, nil 859 } 860 case CHAR, VARCHAR, VARCHAR2: 861 val := DB2G.charToString(bytes, column, conn) 862 if isBFile(int(column.colType), int(column.prec), int(column.scale)) { 863 864 } 865 866 return val, nil 867 case CLOB: 868 clob := newClobFromDB(bytes, conn, column, conn.lobFetchAll()) 869 if util.StringUtil.EqualsIgnoreCase(column.typeName, "LONGVARCHAR") { 870 871 l, err := clob.GetLength() 872 if err != nil { 873 return nil, err 874 } 875 return clob.getSubString(1, int32(l)) 876 } else { 877 return clob, nil 878 } 879 case INTERVAL_YM: 880 return newDmIntervalYMByBytes(bytes), nil 881 case INTERVAL_DT: 882 return newDmIntervalDTByBytes(bytes), nil 883 case ARRAY: 884 return TypeDataSV.bytesToArray(bytes, nil, column.typeDescriptor) 885 case SARRAY: 886 return TypeDataSV.bytesToSArray(bytes, nil, column.typeDescriptor) 887 case CLASS: 888 889 case PLTYPE_RECORD: 890 891 default: 892 return nil, ECGO_DATA_CONVERTION_ERROR.throw() 893 } 894 895 return nil, ECGO_DATA_CONVERTION_ERROR.throw() 896 }