github.com/xiyichan/dm8@v0.0.0-20211213021639-be727be3e136/x.go (about) 1 /* 2 * Copyright (c) 2000-2018, 达梦数据库有限公司. 3 * All rights reserved. 4 */ 5 6 package dm 7 8 type DmStruct struct { 9 TypeData 10 m_strctDesc *StructDescriptor // 结构体的描述信息 11 12 m_attribs []TypeData // 各属性值 13 14 m_objCount int // 一个数组项中存在对象类型的个数(class、动态数组) 15 16 m_strCount int // 一个数组项中存在字符串类型的个数 17 18 typeName string 19 20 elements []interface{} 21 } 22 23 func newDmStruct(typeName string, elements []interface{}) *DmStruct { 24 ds := new(DmStruct) 25 ds.typeName = typeName 26 ds.elements = elements 27 return ds 28 } 29 30 func (ds *DmStruct) create(dc *DmConnection) (*DmStruct, error) { 31 desc, err := newStructDescriptor(ds.typeName, dc) 32 if err != nil { 33 return nil, err 34 } 35 return ds.createByStructDescriptor(desc, dc) 36 } 37 38 func newDmStructByTypeData(atData []TypeData, desc *TypeDescriptor) *DmStruct { 39 ds := new(DmStruct) 40 ds.initTypeData() 41 ds.m_strctDesc = newStructDescriptorByTypeDescriptor(desc) 42 ds.m_attribs = atData 43 return ds 44 } 45 46 func (dest *DmStruct) Scan(src interface{}) error { 47 switch src := src.(type) { 48 case nil: 49 *dest = *new(DmStruct) 50 return nil 51 case *DmStruct: 52 *dest = *src 53 return nil 54 default: 55 return UNSUPPORTED_SCAN 56 } 57 } 58 59 func (ds *DmStruct) getAttribsTypeData() []TypeData { 60 return ds.m_attribs 61 } 62 63 func (ds *DmStruct) createByStructDescriptor(desc *StructDescriptor, conn *DmConnection) (*DmStruct, error) { 64 ds.initTypeData() 65 66 if nil == desc { 67 return nil, ECGO_INVALID_PARAMETER_VALUE.throw() 68 } 69 70 ds.m_strctDesc = desc 71 if nil == ds.elements { 72 ds.m_attribs = make([]TypeData, desc.getSize()) 73 } else { 74 if desc.getSize() != len(ds.elements) && desc.getObjId() != 4 { 75 return nil, ECGO_STRUCT_MEM_NOT_MATCH.throw() 76 } 77 var err error 78 ds.m_attribs, err = TypeDataSV.toStruct(ds.elements, ds.m_strctDesc.m_typeDesc) 79 if err != nil { 80 return nil, err 81 } 82 } 83 84 return ds, nil 85 } 86 87 func (ds *DmStruct) getSQLTypeName() (string, error) { 88 return ds.m_strctDesc.m_typeDesc.getFulName() 89 } 90 91 func (ds *DmStruct) getAttributes() ([]interface{}, error) { 92 return TypeDataSV.toJavaArrayByDmStruct(ds) 93 } 94 95 func (ds *DmStruct) checkCol(col int) error { 96 if col < 1 || col > len(ds.m_attribs) { 97 return ECGO_INVALID_SEQUENCE_NUMBER.throw() 98 } 99 return nil 100 } 101 102 // 获取指定索引的成员变量值,以TypeData的形式给出,col 1 based 103 func (ds *DmStruct) getAttrValue(col int) (*TypeData, error) { 104 err := ds.checkCol(col) 105 if err != nil { 106 return nil, err 107 } 108 return &ds.m_attribs[col-1], nil 109 }