-
Notifications
You must be signed in to change notification settings - Fork 0
/
j.go
270 lines (222 loc) · 6.32 KB
/
j.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Copyright (c) 2000-2018, 达梦数据库有限公司.
* All rights reserved.
*/
package dm
import "database/sql/driver"
type DmArray struct {
TypeData
m_arrDesc *ArrayDescriptor // 数组的描述信息
m_arrData []TypeData // 数组中各行数据值
m_objArray interface{} // 从服务端获取的
m_itemCount int // 本次获取的行数
m_itemSize int // 数组中一个数组项的大小,单位bytes
m_objCount int // 一个数组项中存在对象类型的个数(class、动态数组)
m_strCount int // 一个数组项中存在字符串类型的个数
m_objStrOffs []int // 对象在前,字符串在后
typeName string
elements []interface{}
// Valid为false代表DmArray数据在数据库中为NULL
Valid bool
}
func (da *DmArray) init() *DmArray {
da.initTypeData()
da.m_itemCount = 0
da.m_itemSize = 0
da.m_objCount = 0
da.m_strCount = 0
da.m_objStrOffs = nil
da.m_dumyData = nil
da.m_offset = 0
da.m_objArray = nil
da.Valid = true
return da
}
// 数据库自定义数组Array构造函数,typeName为库中定义的数组类型名称,elements为该数组类型的每个值
//
// 例如,自定义数组类型语句为:create or replace type myArray is array int[];
//
// 则绑入绑出的go对象为: val := dm.NewDmArray("myArray", []interface{} {123, 456})
func NewDmArray(typeName string, elements []interface{}) *DmArray {
da := new(DmArray)
da.typeName = typeName
da.elements = elements
da.Valid = true
return da
}
func (da *DmArray) create(dc *DmConnection) (*DmArray, error) {
desc, err := newArrayDescriptor(da.typeName, dc)
if err != nil {
return nil, err
}
return da.createByArrayDescriptor(desc, dc)
}
func (da *DmArray) createByArrayDescriptor(arrDesc *ArrayDescriptor, conn *DmConnection) (*DmArray, error) {
if nil == arrDesc {
return nil, ECGO_INVALID_PARAMETER_VALUE.throw()
}
da.init()
da.m_arrDesc = arrDesc
if nil == da.elements {
da.m_arrData = make([]TypeData, 0)
} else {
// 若为静态数组,判断给定数组长度是否超过静态数组的上限
if arrDesc.getMDesc() == nil || (arrDesc.getMDesc().getDType() == SARRAY && len(da.elements) > arrDesc.getMDesc().getStaticArrayLength()) {
return nil, ECGO_INVALID_ARRAY_LEN.throw()
}
var err error
da.m_arrData, err = TypeDataSV.toArray(da.elements, da.m_arrDesc.getMDesc())
if err != nil {
return nil, err
}
}
da.m_itemCount = len(da.m_arrData)
return da, nil
}
func newDmArrayByTypeData(atData []TypeData, desc *TypeDescriptor) *DmArray {
da := new(DmArray)
da.init()
da.m_arrDesc = newArrayDescriptorByTypeDescriptor(desc)
da.m_arrData = atData
return da
}
func (da *DmArray) checkIndex(index int64) error {
if index < 0 || index > int64(len(da.m_arrData)-1) {
return ECGO_INVALID_LENGTH_OR_OFFSET.throw()
}
return nil
}
func (da *DmArray) checkIndexAndCount(index int64, count int) error {
err := da.checkIndex(index)
if err != nil {
return err
}
if count <= 0 || index+int64(count) > int64(len(da.m_arrData)) {
return ECGO_INVALID_LENGTH_OR_OFFSET.throw()
}
return nil
}
// 获取Array对象在数据库中的类型名称
func (da *DmArray) GetBaseTypeName() (string, error) {
if err := da.checkValid(); err != nil {
return "", err
}
return da.m_arrDesc.m_typeDesc.getFulName()
}
// 获取Array对象的go数组对象
func (da *DmArray) GetArray() (interface{}, error) {
if da.m_arrData == nil || len(da.m_arrData) <= 0 {
return nil, nil;
}
return TypeDataSV.toJavaArray(da, 0, len(da.m_arrData), da.m_arrDesc.getItemDesc().getDType())
}
// 获取Array对象的指定偏移和执行长度go数据对象 index从0开始
func (da *DmArray) GetObjArray(index int64, count int) (interface{}, error) {
var err error
if err = da.checkValid(); err != nil {
return nil, err
}
if err = da.checkIndexAndCount(index, count); err != nil {
return nil, err
}
return TypeDataSV.toJavaArray(da, index, count, da.m_arrDesc.getItemDesc().getDType())
}
func (da *DmArray) GetIntArray(index int64, count int) ([]int, error) {
var err error
if err = da.checkValid(); err != nil {
return nil, err
}
if err = da.checkIndexAndCount(index, count); err != nil {
return nil, err
}
tmp, err := TypeDataSV.toNumericArray(da, index, count, ARRAY_TYPE_INTEGER)
if err != nil {
return nil, err
}
return tmp.([]int), nil
}
func (da *DmArray) GetInt16Array(index int64, count int) ([]int16, error) {
var err error
if err = da.checkValid(); err != nil {
return nil, err
}
if err = da.checkIndexAndCount(index, count); err != nil {
return nil, err
}
tmp, err := TypeDataSV.toNumericArray(da, index, count, ARRAY_TYPE_SHORT)
if err != nil {
return nil, err
}
return tmp.([]int16), nil
}
func (da *DmArray) GetInt64Array(index int64, count int) ([]int64, error) {
var err error
if err = da.checkValid(); err != nil {
return nil, err
}
if err = da.checkIndexAndCount(index, count); err != nil {
return nil, err
}
tmp, err := TypeDataSV.toNumericArray(da, index, count, ARRAY_TYPE_LONG)
if err != nil {
return nil, err
}
return tmp.([]int64), nil
}
func (da *DmArray) GetFloatArray(index int64, count int) ([]float32, error) {
var err error
if err = da.checkValid(); err != nil {
return nil, err
}
if err = da.checkIndexAndCount(index, count); err != nil {
return nil, err
}
tmp, err := TypeDataSV.toNumericArray(da, index, count, ARRAY_TYPE_FLOAT)
if err != nil {
return nil, err
}
return tmp.([]float32), nil
}
func (da *DmArray) GetDoubleArray(index int64, count int) ([]float64, error) {
var err error
if err = da.checkValid(); err != nil {
return nil, err
}
if err = da.checkIndexAndCount(index, count); err != nil {
return nil, err
}
tmp, err := TypeDataSV.toNumericArray(da, index, count, ARRAY_TYPE_DOUBLE)
if err != nil {
return nil, err
}
return tmp.([]float64), nil
}
func (dest *DmArray) Scan(src interface{}) error {
if dest == nil {
return ECGO_STORE_IN_NIL_POINTER.throw()
}
switch src := src.(type) {
case nil:
*dest = *new(DmArray)
// 将Valid标志置false表示数据库中该列为NULL
(*dest).Valid = false
return nil
case *DmArray:
*dest = *src
return nil
default:
return UNSUPPORTED_SCAN.throw()
}
}
func (array DmArray) Value() (driver.Value, error) {
if !array.Valid {
return nil, nil
}
return array, nil
}
func (array *DmArray) checkValid() error {
if !array.Valid {
return ECGO_IS_NULL.throw()
}
return nil
}