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