gitee.com/chunanyong/dm@v1.8.12/v.go (about)

     1  /*
     2   * Copyright (c) 2000-2018, 达梦数据库有限公司.
     3   * All rights reserved.
     4   */
     5  
     6  package dm
     7  
     8  import "database/sql/driver"
     9  
    10  type DmStruct struct {
    11  	TypeData
    12  	m_strctDesc *StructDescriptor // 结构体的描述信息
    13  
    14  	m_attribs []TypeData // 各属性值
    15  
    16  	m_objCount int // 一个数组项中存在对象类型的个数(class、动态数组)
    17  
    18  	m_strCount int // 一个数组项中存在字符串类型的个数
    19  
    20  	typeName string
    21  
    22  	elements []interface{}
    23  
    24  	// Valid为false代表DmArray数据在数据库中为NULL
    25  	Valid bool
    26  }
    27  
    28  // 数据库自定义类型Struct构造函数,typeName为库中定义的类型名称,elements为该类型每个字段的值
    29  //
    30  // 例如,自定义类型语句为:create or replace type myType as object (a1 int, a2 varchar);
    31  //
    32  // 则绑入绑出的go对象为: val := dm.NewDmStruct("myType", []interface{} {123, "abc"})
    33  func NewDmStruct(typeName string, elements []interface{}) *DmStruct {
    34  	ds := new(DmStruct)
    35  	ds.typeName = typeName
    36  	ds.elements = elements
    37  	ds.Valid = true
    38  	return ds
    39  }
    40  
    41  func (ds *DmStruct) create(dc *DmConnection) (*DmStruct, error) {
    42  	desc, err := newStructDescriptor(ds.typeName, dc)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	return ds.createByStructDescriptor(desc, dc)
    47  }
    48  
    49  func newDmStructByTypeData(atData []TypeData, desc *TypeDescriptor) *DmStruct {
    50  	ds := new(DmStruct)
    51  	ds.Valid = true
    52  	ds.initTypeData()
    53  	ds.m_strctDesc = newStructDescriptorByTypeDescriptor(desc)
    54  	ds.m_attribs = atData
    55  	return ds
    56  }
    57  
    58  func (dest *DmStruct) Scan(src interface{}) error {
    59  	if dest == nil {
    60  		return ECGO_STORE_IN_NIL_POINTER.throw()
    61  	}
    62  	switch src := src.(type) {
    63  	case nil:
    64  		*dest = *new(DmStruct)
    65  		// 将Valid标志置false表示数据库中该列为NULL
    66  		(*dest).Valid = false
    67  		return nil
    68  	case *DmStruct:
    69  		*dest = *src
    70  		return nil
    71  	default:
    72  		return UNSUPPORTED_SCAN.throw()
    73  	}
    74  }
    75  
    76  func (dt DmStruct) Value() (driver.Value, error) {
    77  	if !dt.Valid {
    78  		return nil, nil
    79  	}
    80  	return dt, nil
    81  }
    82  
    83  func (ds *DmStruct) getAttribsTypeData() []TypeData {
    84  	return ds.m_attribs
    85  }
    86  
    87  func (ds *DmStruct) createByStructDescriptor(desc *StructDescriptor, conn *DmConnection) (*DmStruct, error) {
    88  	ds.initTypeData()
    89  
    90  	if nil == desc {
    91  		return nil, ECGO_INVALID_PARAMETER_VALUE.throw()
    92  	}
    93  
    94  	ds.m_strctDesc = desc
    95  	if nil == ds.elements {
    96  		ds.m_attribs = make([]TypeData, desc.getSize())
    97  	} else {
    98  		if desc.getSize() != len(ds.elements) && desc.getObjId() != 4 {
    99  			return nil, ECGO_STRUCT_MEM_NOT_MATCH.throw()
   100  		}
   101  		var err error
   102  		ds.m_attribs, err = TypeDataSV.toStruct(ds.elements, ds.m_strctDesc.m_typeDesc)
   103  		if err != nil {
   104  			return nil, err
   105  		}
   106  	}
   107  
   108  	return ds, nil
   109  }
   110  
   111  // 获取Struct对象在数据库中的类型名称
   112  func (ds *DmStruct) GetSQLTypeName() (string, error) {
   113  	return ds.m_strctDesc.m_typeDesc.getFulName()
   114  }
   115  
   116  // 获取Struct对象中的各个字段的值
   117  func (ds *DmStruct) GetAttributes() ([]interface{}, error) {
   118  	return TypeDataSV.toJavaArrayByDmStruct(ds)
   119  }
   120  
   121  func (ds *DmStruct) checkCol(col int) error {
   122  	if col < 1 || col > len(ds.m_attribs) {
   123  		return ECGO_INVALID_SEQUENCE_NUMBER.throw()
   124  	}
   125  	return nil
   126  }
   127  
   128  // 获取指定索引的成员变量值,以TypeData的形式给出,col 1 based
   129  func (ds *DmStruct) getAttrValue(col int) (*TypeData, error) {
   130  	err := ds.checkCol(col)
   131  	if err != nil {
   132  		return nil, err
   133  	}
   134  	return &ds.m_attribs[col-1], nil
   135  }
   136  
   137  func (ds *DmStruct) checkValid() error {
   138  	if !ds.Valid {
   139  		return ECGO_IS_NULL.throw()
   140  	}
   141  	return nil
   142  }