v.go 3.4 KB

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