gitee.com/curryzheng/dm@v0.0.1/zzq.go (about) 1 /* 2 * Copyright (c) 2000-2018, 达梦数据库有限公司. 3 * All rights reserved. 4 */ 5 package dm 6 7 import ( 8 "database/sql/driver" 9 ) 10 11 const ( 12 OBJ_BLOB_MAGIC = 78111999 13 14 CLTN_TYPE_IND_TABLE = 3 15 16 CLTN_TYPE_NST_TABLE = 2 17 18 CLTN_TYPE_VARRAY = 1 19 ) 20 21 type TypeDescriptor struct { 22 column *column 23 24 m_sqlName *sqlName 25 26 m_objId int 27 28 m_objVersion int 29 30 m_outerId int 31 32 m_outerVer int 33 34 m_subId int 35 36 m_cltnType int 37 38 m_maxCnt int 39 40 m_length int 41 42 m_size int 43 44 m_conn *DmConnection 45 46 m_serverEncoding string 47 48 m_arrObj *TypeDescriptor 49 50 m_fieldsObj []TypeDescriptor 51 52 m_descBuf []byte 53 } 54 55 func newTypeDescriptorWithFulName(fulName string, conn *DmConnection) *TypeDescriptor { 56 td := new(TypeDescriptor) 57 td.init() 58 td.m_sqlName = newSqlNameByFulName(fulName) 59 td.m_conn = conn 60 return td 61 } 62 63 func newTypeDescriptor(conn *DmConnection) *TypeDescriptor { 64 td := new(TypeDescriptor) 65 td.init() 66 td.m_sqlName = newSqlNameByConn(conn) 67 td.m_conn = conn 68 return td 69 } 70 71 func (typeDescriptor *TypeDescriptor) init() { 72 typeDescriptor.column = new(column).InitColumn() 73 74 typeDescriptor.m_sqlName = nil 75 76 typeDescriptor.m_objId = -1 77 78 typeDescriptor.m_objVersion = -1 79 80 typeDescriptor.m_outerId = 0 81 82 typeDescriptor.m_outerVer = 0 83 84 typeDescriptor.m_subId = 0 85 86 typeDescriptor.m_cltnType = 0 87 88 typeDescriptor.m_maxCnt = 0 89 90 typeDescriptor.m_length = 0 91 92 typeDescriptor.m_size = 0 93 94 typeDescriptor.m_conn = nil 95 96 typeDescriptor.m_serverEncoding = "" 97 98 typeDescriptor.m_arrObj = nil 99 100 typeDescriptor.m_fieldsObj = nil 101 102 typeDescriptor.m_descBuf = nil 103 } 104 105 func (typeDescriptor *TypeDescriptor) parseDescByName() error { 106 sql := "BEGIN ? = SF_DESCRIBE_TYPE(?); END;" 107 108 params := make([]driver.Value, 2) 109 params[1] = typeDescriptor.m_sqlName.m_fulName 110 111 rs, err := typeDescriptor.m_conn.query(sql, params) 112 if err != nil { 113 return err 114 } 115 rs.close() 116 l, err := params[0].(*DmBlob).GetLength() 117 if err != nil { 118 return err 119 } 120 121 buf, err := params[0].(*DmBlob).getBytes(1, int32(l)) 122 if err != nil { 123 return err 124 } 125 typeDescriptor.m_serverEncoding = typeDescriptor.m_conn.getServerEncoding() 126 err = typeDescriptor.unpack(Dm_build_363(buf)) 127 if err != nil { 128 return err 129 } 130 return nil 131 } 132 133 func (typeDescriptor *TypeDescriptor) getFulName() (string, error) { 134 return typeDescriptor.m_sqlName.getFulName() 135 } 136 137 func (typeDescriptor *TypeDescriptor) getDType() int { 138 return int(typeDescriptor.column.colType) 139 } 140 141 func (typeDescriptor *TypeDescriptor) getPrec() int { 142 return int(typeDescriptor.column.prec) 143 } 144 145 func (typeDescriptor *TypeDescriptor) getScale() int { 146 return int(typeDescriptor.column.scale) 147 } 148 149 func (typeDescriptor *TypeDescriptor) getServerEncoding() string { 150 if typeDescriptor.m_serverEncoding == "" { 151 return typeDescriptor.m_conn.getServerEncoding() 152 } else { 153 return typeDescriptor.m_serverEncoding 154 } 155 } 156 157 func (typeDescriptor *TypeDescriptor) getObjId() int { 158 return typeDescriptor.m_objId 159 } 160 161 func (typeDescriptor *TypeDescriptor) getStaticArrayLength() int { 162 return typeDescriptor.m_length 163 } 164 165 func (typeDescriptor *TypeDescriptor) getStrctMemSize() int { 166 return typeDescriptor.m_size 167 } 168 169 func (typeDescriptor *TypeDescriptor) getOuterId() int { 170 return typeDescriptor.m_outerId 171 } 172 173 func (typeDescriptor *TypeDescriptor) getCltnType() int { 174 return typeDescriptor.m_cltnType 175 } 176 177 func (typeDescriptor *TypeDescriptor) getMaxCnt() int { 178 return typeDescriptor.m_maxCnt 179 } 180 181 func getPackSize(typeDesc *TypeDescriptor) (int, error) { 182 len := 0 183 184 switch typeDesc.column.colType { 185 case ARRAY, SARRAY: 186 return getPackArraySize(typeDesc) 187 188 case CLASS: 189 return getPackClassSize(typeDesc) 190 191 case PLTYPE_RECORD: 192 return getPackRecordSize(typeDesc) 193 } 194 195 len += ULINT_SIZE 196 197 len += ULINT_SIZE 198 199 len += ULINT_SIZE 200 201 return len, nil 202 } 203 204 func pack(typeDesc *TypeDescriptor, msg *Dm_build_358) error { 205 switch typeDesc.column.colType { 206 case ARRAY, SARRAY: 207 return packArray(typeDesc, msg) 208 case CLASS: 209 return packClass(typeDesc, msg) 210 case PLTYPE_RECORD: 211 return packRecord(typeDesc, msg) 212 } 213 214 msg.Dm_build_409(typeDesc.column.colType) 215 216 msg.Dm_build_409(typeDesc.column.prec) 217 218 msg.Dm_build_409(typeDesc.column.scale) 219 return nil 220 } 221 222 func getPackArraySize(arrDesc *TypeDescriptor) (int, error) { 223 l := 0 224 225 l += ULINT_SIZE 226 227 name := arrDesc.m_sqlName.m_name 228 l += USINT_SIZE 229 230 serverEncoding := arrDesc.getServerEncoding() 231 ret := Dm_build_1.Dm_build_214(name, serverEncoding, arrDesc.m_conn) 232 l += len(ret) 233 234 l += ULINT_SIZE 235 236 l += ULINT_SIZE 237 238 l += ULINT_SIZE 239 240 i, err := getPackSize(arrDesc.m_arrObj) 241 if err != nil { 242 return 0, err 243 } 244 245 l += i 246 247 return l, nil 248 } 249 250 func packArray(arrDesc *TypeDescriptor, msg *Dm_build_358) error { 251 252 msg.Dm_build_409(arrDesc.column.colType) 253 254 msg.Dm_build_465(arrDesc.m_sqlName.m_name, arrDesc.getServerEncoding(), arrDesc.m_conn) 255 256 msg.Dm_build_409(int32(arrDesc.m_objId)) 257 258 msg.Dm_build_409(int32(arrDesc.m_objVersion)) 259 260 msg.Dm_build_409(int32(arrDesc.m_length)) 261 262 return pack(arrDesc.m_arrObj, msg) 263 } 264 265 func packRecord(strctDesc *TypeDescriptor, msg *Dm_build_358) error { 266 267 msg.Dm_build_409(strctDesc.column.colType) 268 269 msg.Dm_build_465(strctDesc.m_sqlName.m_name, strctDesc.getServerEncoding(), strctDesc.m_conn) 270 271 msg.Dm_build_409(int32(strctDesc.m_objId)) 272 273 msg.Dm_build_409(int32(strctDesc.m_objVersion)) 274 275 msg.Dm_build_405(int16(strctDesc.m_size)) 276 277 for i := 0; i < strctDesc.m_size; i++ { 278 err := pack(&strctDesc.m_fieldsObj[i], msg) 279 if err != nil { 280 return err 281 } 282 } 283 return nil 284 } 285 286 func getPackRecordSize(strctDesc *TypeDescriptor) (int, error) { 287 l := 0 288 289 l += ULINT_SIZE 290 291 name := strctDesc.m_sqlName.m_name 292 l += USINT_SIZE 293 294 serverEncoding := strctDesc.getServerEncoding() 295 ret := Dm_build_1.Dm_build_214(name, serverEncoding, strctDesc.m_conn) 296 l += len(ret) 297 298 l += ULINT_SIZE 299 300 l += ULINT_SIZE 301 302 l += USINT_SIZE 303 304 for i := 0; i < strctDesc.m_size; i++ { 305 i, err := getPackSize(&strctDesc.m_fieldsObj[i]) 306 if err != nil { 307 return 0, err 308 } 309 l += i 310 } 311 312 return l, nil 313 } 314 315 func getPackClassSize(strctDesc *TypeDescriptor) (int, error) { 316 l := 0 317 318 l += ULINT_SIZE 319 320 name := strctDesc.m_sqlName.m_name 321 l += USINT_SIZE 322 323 serverEncoding := strctDesc.getServerEncoding() 324 ret := Dm_build_1.Dm_build_214(name, serverEncoding, strctDesc.m_conn) 325 l += len(ret) 326 327 l += ULINT_SIZE 328 329 l += ULINT_SIZE 330 331 if strctDesc.m_objId == 4 { 332 333 l += ULINT_SIZE 334 335 l += ULINT_SIZE 336 337 l += USINT_SIZE 338 } 339 340 return l, nil 341 } 342 343 func packClass(strctDesc *TypeDescriptor, msg *Dm_build_358) error { 344 345 msg.Dm_build_409(strctDesc.column.colType) 346 347 msg.Dm_build_465(strctDesc.m_sqlName.m_name, strctDesc.getServerEncoding(), strctDesc.m_conn) 348 349 msg.Dm_build_409(int32(strctDesc.m_objId)) 350 351 msg.Dm_build_409(int32(strctDesc.m_objVersion)) 352 353 if strctDesc.m_objId == 4 { 354 355 msg.Dm_build_409(int32(strctDesc.m_outerId)) 356 357 msg.Dm_build_409(int32(strctDesc.m_outerVer)) 358 359 msg.Dm_build_409(int32(strctDesc.m_subId)) 360 361 } 362 363 return nil 364 } 365 366 func (typeDescriptor *TypeDescriptor) unpack(buffer *Dm_build_358) error { 367 368 typeDescriptor.column.colType = buffer.Dm_build_483() 369 370 switch typeDescriptor.column.colType { 371 case ARRAY, SARRAY: 372 return typeDescriptor.unpackArray(buffer) 373 case CLASS: 374 return typeDescriptor.unpackClass(buffer) 375 case PLTYPE_RECORD: 376 return typeDescriptor.unpackRecord(buffer) 377 } 378 379 typeDescriptor.column.prec = buffer.Dm_build_483() 380 381 typeDescriptor.column.scale = buffer.Dm_build_483() 382 return nil 383 } 384 385 func (typeDescriptor *TypeDescriptor) unpackArray(buffer *Dm_build_358) error { 386 387 typeDescriptor.m_sqlName.m_name = buffer.Dm_build_533(typeDescriptor.getServerEncoding(), typeDescriptor.m_conn) 388 389 typeDescriptor.m_sqlName.m_schId = int(buffer.Dm_build_483()) 390 391 typeDescriptor.m_sqlName.m_packId = int(buffer.Dm_build_483()) 392 393 typeDescriptor.m_objId = int(buffer.Dm_build_483()) 394 395 typeDescriptor.m_objVersion = int(buffer.Dm_build_483()) 396 397 typeDescriptor.m_length = int(buffer.Dm_build_483()) 398 if typeDescriptor.column.colType == ARRAY { 399 typeDescriptor.m_length = 0 400 } 401 402 typeDescriptor.m_arrObj = newTypeDescriptor(typeDescriptor.m_conn) 403 return typeDescriptor.m_arrObj.unpack(buffer) 404 } 405 406 func (typeDescriptor *TypeDescriptor) unpackRecord(buffer *Dm_build_358) error { 407 408 typeDescriptor.m_sqlName.m_name = buffer.Dm_build_533(typeDescriptor.getServerEncoding(), typeDescriptor.m_conn) 409 410 typeDescriptor.m_sqlName.m_schId = int(buffer.Dm_build_483()) 411 412 typeDescriptor.m_sqlName.m_packId = int(buffer.Dm_build_483()) 413 414 typeDescriptor.m_objId = int(buffer.Dm_build_483()) 415 416 typeDescriptor.m_objVersion = int(buffer.Dm_build_483()) 417 418 typeDescriptor.m_size = int(buffer.Dm_build_498()) 419 420 typeDescriptor.m_fieldsObj = make([]TypeDescriptor, typeDescriptor.m_size) 421 for i := 0; i < typeDescriptor.m_size; i++ { 422 typeDescriptor.m_fieldsObj[i] = *newTypeDescriptor(typeDescriptor.m_conn) 423 typeDescriptor.m_fieldsObj[i].unpack(buffer) 424 } 425 426 return nil 427 } 428 429 func (typeDescriptor *TypeDescriptor) unpackClnt_nestTab(buffer *Dm_build_358) error { 430 431 typeDescriptor.m_maxCnt = int(buffer.Dm_build_483()) 432 433 typeDescriptor.m_arrObj = newTypeDescriptor(typeDescriptor.m_conn) 434 435 typeDescriptor.m_arrObj.unpack(buffer) 436 437 return nil 438 } 439 440 func (typeDescriptor *TypeDescriptor) unpackClnt(buffer *Dm_build_358) error { 441 442 typeDescriptor.m_outerId = int(buffer.Dm_build_483()) 443 444 typeDescriptor.m_outerVer = int(buffer.Dm_build_483()) 445 446 typeDescriptor.m_subId = int(buffer.Dm_build_498()) 447 448 typeDescriptor.m_cltnType = int(buffer.Dm_build_498()) 449 450 switch typeDescriptor.m_cltnType { 451 case CLTN_TYPE_IND_TABLE: 452 return ECGO_UNSUPPORTED_TYPE.throw() 453 454 case CLTN_TYPE_NST_TABLE, CLTN_TYPE_VARRAY: 455 return typeDescriptor.unpackClnt_nestTab(buffer) 456 457 } 458 return nil 459 } 460 461 func (typeDescriptor *TypeDescriptor) unpackClass(buffer *Dm_build_358) error { 462 463 typeDescriptor.m_sqlName.m_name = buffer.Dm_build_533(typeDescriptor.getServerEncoding(), typeDescriptor.m_conn) 464 465 typeDescriptor.m_sqlName.m_schId = int(buffer.Dm_build_483()) 466 467 typeDescriptor.m_sqlName.m_packId = int(buffer.Dm_build_483()) 468 469 typeDescriptor.m_objId = int(buffer.Dm_build_483()) 470 471 typeDescriptor.m_objVersion = int(buffer.Dm_build_483()) 472 473 if typeDescriptor.m_objId == 4 { 474 return typeDescriptor.unpackClnt(buffer) 475 } else { 476 477 typeDescriptor.m_size = int(buffer.Dm_build_498()) 478 479 typeDescriptor.m_fieldsObj = make([]TypeDescriptor, typeDescriptor.m_size) 480 for i := 0; i < typeDescriptor.m_size; i++ { 481 typeDescriptor.m_fieldsObj[i] = *newTypeDescriptor(typeDescriptor.m_conn) 482 err := typeDescriptor.m_fieldsObj[i].unpack(buffer) 483 if err != nil { 484 return err 485 } 486 } 487 return nil 488 } 489 490 } 491 492 func calcChkDescLen_array(desc *TypeDescriptor) (int, error) { 493 offset := 0 494 495 offset += USINT_SIZE 496 497 offset += ULINT_SIZE 498 499 tmp, err := calcChkDescLen(desc) 500 if err != nil { 501 return 0, err 502 } 503 504 offset += tmp 505 506 return offset, nil 507 } 508 509 func calcChkDescLen_record(desc *TypeDescriptor) (int, error) { 510 offset := 0 511 512 offset += USINT_SIZE 513 514 offset += USINT_SIZE 515 516 for i := 0; i < desc.m_size; i++ { 517 tmp, err := calcChkDescLen(&desc.m_fieldsObj[i]) 518 if err != nil { 519 return 0, err 520 } 521 offset += tmp 522 } 523 524 return offset, nil 525 } 526 527 func calcChkDescLen_class_normal(desc *TypeDescriptor) (int, error) { 528 offset := 0 529 530 offset += USINT_SIZE 531 532 for i := 0; i < desc.m_size; i++ { 533 tmp, err := calcChkDescLen(&desc.m_fieldsObj[i]) 534 if err != nil { 535 return 0, err 536 } 537 offset += tmp 538 } 539 540 return offset, nil 541 } 542 543 func calcChkDescLen_class_cnlt(desc *TypeDescriptor) (int, error) { 544 offset := 0 545 546 offset += USINT_SIZE 547 548 offset += ULINT_SIZE 549 550 switch desc.getCltnType() { 551 case CLTN_TYPE_IND_TABLE: 552 return 0, ECGO_UNSUPPORTED_TYPE.throw() 553 554 case CLTN_TYPE_VARRAY, CLTN_TYPE_NST_TABLE: 555 556 i, err := calcChkDescLen(desc.m_arrObj) 557 if err != nil { 558 return 0, err 559 } 560 561 offset += i 562 } 563 564 return offset, nil 565 } 566 567 func calcChkDescLen_class(desc *TypeDescriptor) (int, error) { 568 offset := 0 569 570 offset += USINT_SIZE 571 572 offset += BYTE_SIZE 573 574 if desc.m_objId == 4 { 575 i, err := calcChkDescLen_class_cnlt(desc) 576 if err != nil { 577 return 0, err 578 } 579 offset += i 580 } else { 581 i, err := calcChkDescLen_class_normal(desc) 582 if err != nil { 583 return 0, err 584 } 585 offset += i 586 } 587 588 return offset, nil 589 } 590 591 func calcChkDescLen_buildin() int { 592 offset := 0 593 594 offset += USINT_SIZE 595 596 offset += USINT_SIZE 597 598 offset += USINT_SIZE 599 600 return offset 601 } 602 603 func calcChkDescLen(desc *TypeDescriptor) (int, error) { 604 605 switch desc.getDType() { 606 case ARRAY, SARRAY: 607 return calcChkDescLen_array(desc) 608 609 case PLTYPE_RECORD: 610 return calcChkDescLen_record(desc) 611 612 case CLASS: 613 return calcChkDescLen_class(desc) 614 615 default: 616 return calcChkDescLen_buildin(), nil 617 } 618 619 } 620 621 func (typeDescriptor *TypeDescriptor) makeChkDesc_array(offset int, desc *TypeDescriptor) (int, error) { 622 623 Dm_build_1.Dm_build_12(typeDescriptor.m_descBuf, offset, ARRAY) 624 offset += USINT_SIZE 625 626 Dm_build_1.Dm_build_17(typeDescriptor.m_descBuf, offset, int32(desc.m_length)) 627 offset += ULINT_SIZE 628 629 return typeDescriptor.makeChkDesc(offset, desc) 630 } 631 632 func (typeDescriptor *TypeDescriptor) makeChkDesc_record(offset int, desc *TypeDescriptor) (int, error) { 633 634 Dm_build_1.Dm_build_12(typeDescriptor.m_descBuf, offset, PLTYPE_RECORD) 635 offset += USINT_SIZE 636 637 Dm_build_1.Dm_build_12(typeDescriptor.m_descBuf, offset, int16(desc.m_size)) 638 offset += USINT_SIZE 639 var err error 640 for i := 0; i < desc.m_size; i++ { 641 offset, err = typeDescriptor.makeChkDesc(offset, &desc.m_fieldsObj[i]) 642 if err != nil { 643 return 0, err 644 } 645 } 646 647 return offset, nil 648 } 649 650 func (typeDescriptor *TypeDescriptor) makeChkDesc_buildin(offset int, desc *TypeDescriptor) int { 651 dtype := int16(desc.getDType()) 652 prec := 0 653 scale := 0 654 655 if dtype != BLOB { 656 prec = desc.getPrec() 657 scale = desc.getScale() 658 } 659 660 Dm_build_1.Dm_build_12(typeDescriptor.m_descBuf, offset, dtype) 661 offset += USINT_SIZE 662 663 Dm_build_1.Dm_build_12(typeDescriptor.m_descBuf, offset, int16(prec)) 664 offset += USINT_SIZE 665 666 Dm_build_1.Dm_build_12(typeDescriptor.m_descBuf, offset, int16(scale)) 667 offset += USINT_SIZE 668 669 return offset 670 } 671 672 func (typeDescriptor *TypeDescriptor) makeChkDesc_class_normal(offset int, desc *TypeDescriptor) (int, error) { 673 674 Dm_build_1.Dm_build_12(typeDescriptor.m_descBuf, offset, int16(desc.m_size)) 675 offset += USINT_SIZE 676 var err error 677 678 for i := 0; i < desc.m_size; i++ { 679 offset, err = typeDescriptor.makeChkDesc(offset, &desc.m_fieldsObj[i]) 680 if err != nil { 681 return 0, err 682 } 683 } 684 685 return offset, nil 686 } 687 688 func (typeDescriptor *TypeDescriptor) makeChkDesc_class_clnt(offset int, desc *TypeDescriptor) (int, error) { 689 690 Dm_build_1.Dm_build_12(typeDescriptor.m_descBuf, offset, int16(desc.m_cltnType)) 691 offset += USINT_SIZE 692 693 Dm_build_1.Dm_build_17(typeDescriptor.m_descBuf, offset, int32(desc.getMaxCnt())) 694 offset += ULINT_SIZE 695 696 switch desc.m_cltnType { 697 case CLTN_TYPE_IND_TABLE: 698 return 0, ECGO_UNSUPPORTED_TYPE.throw() 699 700 case CLTN_TYPE_NST_TABLE, CLTN_TYPE_VARRAY: 701 702 return typeDescriptor.makeChkDesc(offset, desc.m_arrObj) 703 } 704 705 return offset, nil 706 } 707 708 func (typeDescriptor *TypeDescriptor) makeChkDesc_class(offset int, desc *TypeDescriptor) (int, error) { 709 710 Dm_build_1.Dm_build_12(typeDescriptor.m_descBuf, offset, CLASS) 711 offset += USINT_SIZE 712 713 isClnt := false 714 if desc.m_objId == 4 { 715 isClnt = true 716 } 717 718 if isClnt { 719 Dm_build_1.Dm_build_2(typeDescriptor.m_descBuf, offset, byte(1)) 720 } else { 721 Dm_build_1.Dm_build_2(typeDescriptor.m_descBuf, offset, byte(0)) 722 } 723 724 offset += BYTE_SIZE 725 726 if isClnt { 727 return typeDescriptor.makeChkDesc_class_clnt(offset, desc) 728 } else { 729 return typeDescriptor.makeChkDesc_class_normal(offset, desc) 730 } 731 } 732 733 func (typeDescriptor *TypeDescriptor) makeChkDesc(offset int, subDesc *TypeDescriptor) (int, error) { 734 switch subDesc.getDType() { 735 case ARRAY, SARRAY: 736 return typeDescriptor.makeChkDesc_array(offset, subDesc) 737 738 case PLTYPE_RECORD: 739 return typeDescriptor.makeChkDesc_record(offset, subDesc) 740 741 case CLASS: 742 return typeDescriptor.makeChkDesc_class(offset, subDesc) 743 744 default: 745 return typeDescriptor.makeChkDesc_buildin(offset, subDesc), nil 746 } 747 748 } 749 750 func (typeDescriptor *TypeDescriptor) getClassDescChkInfo() ([]byte, error) { 751 if typeDescriptor.m_descBuf != nil { 752 return typeDescriptor.m_descBuf, nil 753 } 754 755 l, err := calcChkDescLen(typeDescriptor) 756 if err != nil { 757 return nil, err 758 } 759 typeDescriptor.m_descBuf = make([]byte, l) 760 761 typeDescriptor.makeChkDesc(0, typeDescriptor) 762 return typeDescriptor.m_descBuf, nil 763 }