gitee.com/curryzheng/dm@v0.0.1/w.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  func newDmStruct(typeName string, elements []interface{}) *DmStruct {
    29  	ds := new(DmStruct)
    30  	ds.typeName = typeName
    31  	ds.elements = elements
    32  	ds.Valid = true
    33  	return ds
    34  }
    35  
    36  func (ds *DmStruct) create(dc *DmConnection) (*DmStruct, error) {
    37  	desc, err := newStructDescriptor(ds.typeName, dc)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	return ds.createByStructDescriptor(desc, dc)
    42  }
    43  
    44  func newDmStructByTypeData(atData []TypeData, desc *TypeDescriptor) *DmStruct {
    45  	ds := new(DmStruct)
    46  	ds.Valid = true
    47  	ds.initTypeData()
    48  	ds.m_strctDesc = newStructDescriptorByTypeDescriptor(desc)
    49  	ds.m_attribs = atData
    50  	return ds
    51  }
    52  
    53  func (dest *DmStruct) Scan(src interface{}) error {
    54  	switch src := src.(type) {
    55  	case nil:
    56  		*dest = *new(DmStruct)
    57  		// 将Valid标志置false表示数据库中该列为NULL
    58  		(*dest).Valid = false
    59  		return nil
    60  	case *DmStruct:
    61  		*dest = *src
    62  		return nil
    63  	default:
    64  		return UNSUPPORTED_SCAN
    65  	}
    66  }
    67  
    68  func (dt DmStruct) Value() (driver.Value, error) {
    69  	if !dt.Valid {
    70  		return nil, nil
    71  	}
    72  	return dt, nil
    73  }
    74  
    75  func (ds *DmStruct) getAttribsTypeData() []TypeData {
    76  	return ds.m_attribs
    77  }
    78  
    79  func (ds *DmStruct) createByStructDescriptor(desc *StructDescriptor, conn *DmConnection) (*DmStruct, error) {
    80  	ds.initTypeData()
    81  
    82  	if nil == desc {
    83  		return nil, ECGO_INVALID_PARAMETER_VALUE.throw()
    84  	}
    85  
    86  	ds.m_strctDesc = desc
    87  	if nil == ds.elements {
    88  		ds.m_attribs = make([]TypeData, desc.getSize())
    89  	} else {
    90  		if desc.getSize() != len(ds.elements) && desc.getObjId() != 4 {
    91  			return nil, ECGO_STRUCT_MEM_NOT_MATCH.throw()
    92  		}
    93  		var err error
    94  		ds.m_attribs, err = TypeDataSV.toStruct(ds.elements, ds.m_strctDesc.m_typeDesc)
    95  		if err != nil {
    96  			return nil, err
    97  		}
    98  	}
    99  
   100  	return ds, nil
   101  }
   102  
   103  func (ds *DmStruct) getSQLTypeName() (string, error) {
   104  	return ds.m_strctDesc.m_typeDesc.getFulName()
   105  }
   106  
   107  func (ds *DmStruct) getAttributes() ([]interface{}, error) {
   108  	return TypeDataSV.toJavaArrayByDmStruct(ds)
   109  }
   110  
   111  func (ds *DmStruct) checkCol(col int) error {
   112  	if col < 1 || col > len(ds.m_attribs) {
   113  		return ECGO_INVALID_SEQUENCE_NUMBER.throw()
   114  	}
   115  	return nil
   116  }
   117  
   118  // 获取指定索引的成员变量值,以TypeData的形式给出,col 1 based
   119  func (ds *DmStruct) getAttrValue(col int) (*TypeData, error) {
   120  	err := ds.checkCol(col)
   121  	if err != nil {
   122  		return nil, err
   123  	}
   124  	return &ds.m_attribs[col-1], nil
   125  }
   126  
   127  func (ds *DmStruct) checkValid() error {
   128  	if !ds.Valid {
   129  		return ECGO_IS_NULL.throw()
   130  	}
   131  	return nil
   132  }