forked from go-goracle/goracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
obj.go
435 lines (392 loc) · 12.3 KB
/
obj.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// Copyright 2017 Tamás Gulácsi
//
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package goracle
/*
#include <stdlib.h>
#include "dpiImpl.h"
*/
import "C"
import (
"fmt"
"reflect"
"unsafe"
"github.com/pkg/errors"
)
var _ = fmt.Printf
// Object represents a dpiObject.
type Object struct {
dpiObject *C.dpiObject
ObjectType
scratch Data
}
func (O *Object) getError() error { return O.drv.getError() }
var ErrNoSuchKey = errors.New("no such key")
// GetAttribute gets the i-th attribute into data.
func (O *Object) GetAttribute(data *Data, name string) error {
if O == nil || O.dpiObject == nil {
panic("nil dpiObject")
}
attr, ok := O.Attributes[name]
if !ok {
return errors.Wrap(ErrNoSuchKey, name)
}
data.reset()
data.NativeTypeNum = attr.NativeTypeNum
data.ObjectType = attr.ObjectType
wasNull := data.dpiData == nil
// the maximum length of that buffer must be supplied
// in the value.asBytes.length attribute before calling this function.
if attr.NativeTypeNum == C.DPI_NATIVE_TYPE_BYTES && attr.OracleTypeNum == C.DPI_ORACLE_TYPE_NUMBER {
var a [22]byte
C.dpiData_setBytes(data.dpiData, (*C.char)(unsafe.Pointer(&a[0])), 22)
}
//fmt.Printf("getAttributeValue(%p, %p, %d, %+v)\n", O.dpiObject, attr.dpiObjectAttr, data.NativeTypeNum, data.dpiData)
if C.dpiObject_getAttributeValue(O.dpiObject, attr.dpiObjectAttr, data.NativeTypeNum, data.dpiData) == C.DPI_FAILURE {
if wasNull {
C.free(unsafe.Pointer(data.dpiData))
data.dpiData = nil
}
return errors.Wrapf(O.getError(), "getAttributeValue(obj=%+v, attr=%+v, typ=%d)", O, attr.dpiObjectAttr, data.NativeTypeNum)
}
//fmt.Printf("getAttributeValue(%p, %q=%p, %d, %+v)\n", O.dpiObject, attr.Name, attr.dpiObjectAttr, data.NativeTypeNum, data.dpiData)
return nil
}
// SetAttribute sets the i-th attribute with data.
func (O *Object) SetAttribute(name string, data *Data) error {
attr := O.Attributes[name]
if data.NativeTypeNum == 0 {
data.NativeTypeNum = attr.NativeTypeNum
data.ObjectType = attr.ObjectType
}
if C.dpiObject_setAttributeValue(O.dpiObject, attr.dpiObjectAttr, data.NativeTypeNum, data.dpiData) == C.DPI_FAILURE {
return O.getError()
}
return nil
}
// Get scans the named attribute into dest, and returns it.
func (O *Object) Get(name string) (interface{}, error) {
if err := O.GetAttribute(&O.scratch, name); err != nil {
return nil, err
}
isObject := O.scratch.IsObject()
if isObject {
O.scratch.ObjectType = O.Attributes[name].ObjectType
}
v := O.scratch.Get()
if !isObject {
return v, nil
}
sub := v.(*Object)
if sub != nil && sub.CollectionOf != nil {
return &ObjectCollection{Object: sub}, nil
}
return sub, nil
}
// ObjectCollection represents a Collection of Objects - itself an Object, too.
type ObjectCollection struct {
*Object
}
// ErrNotCollection is returned when the Object is not a collection.
var ErrNotCollection = errors.New("not collection")
// ErrNotExist is returned when the collection's requested element does not exist.
var ErrNotExist = errors.New("not exist")
// AsSlice retrieves the collection into a slice.
func (coll *ObjectCollection) AsSlice(dest interface{}) (interface{}, error) {
var data Data
var dr reflect.Value
needsInit := dest == nil
if !needsInit {
dr = reflect.ValueOf(dest)
}
for i, err := coll.First(); err == nil; i, err = coll.Next(i) {
if coll.CollectionOf.NativeTypeNum == C.DPI_NATIVE_TYPE_OBJECT {
data.ObjectType = *coll.CollectionOf
}
if err = coll.Get(&data, i); err != nil {
return dest, err
}
vr := reflect.ValueOf(data.Get())
if needsInit {
needsInit = false
length, err := coll.Len()
if err != nil {
return dr.Interface(), err
}
dr = reflect.MakeSlice(reflect.SliceOf(vr.Type()), 0, length)
}
if dr = reflect.Append(dr, vr); err != nil {
return dr.Interface(), err
}
}
return dr.Interface(), nil
}
// Append data to the collection.
func (O *ObjectCollection) Append(data *Data) error {
if C.dpiObject_appendElement(O.dpiObject, data.NativeTypeNum, data.dpiData) == C.DPI_FAILURE {
return errors.Wrapf(O.getError(), "append(%d)", data.NativeTypeNum)
}
return nil
}
// Delete i-th element of the collection.
func (O *ObjectCollection) Delete(i int) error {
if C.dpiObject_deleteElementByIndex(O.dpiObject, C.int32_t(i)) == C.DPI_FAILURE {
return errors.Wrapf(O.getError(), "delete(%d)", i)
}
return nil
}
// Get the i-th element of the collection into data.
func (O *ObjectCollection) Get(data *Data, i int) error {
if data == nil {
panic("data cannot be nil")
}
idx := C.int32_t(i)
var exists C.int
if C.dpiObject_getElementExistsByIndex(O.dpiObject, idx, &exists) == C.DPI_FAILURE {
return errors.Wrapf(O.getError(), "exists(%d)", idx)
}
if exists == 0 {
return ErrNotExist
}
data.reset()
data.NativeTypeNum = O.CollectionOf.NativeTypeNum
data.ObjectType = *O.CollectionOf
if C.dpiObject_getElementValueByIndex(O.dpiObject, idx, data.NativeTypeNum, data.dpiData) == C.DPI_FAILURE {
return errors.Wrapf(O.getError(), "get(%d[%d])", idx, data.NativeTypeNum)
}
return nil
}
// Set the i-th element of the collection with data.
func (O *ObjectCollection) Set(i int, data *Data) error {
if C.dpiObject_setElementValueByIndex(O.dpiObject, C.int32_t(i), data.NativeTypeNum, data.dpiData) == C.DPI_FAILURE {
return errors.Wrapf(O.getError(), "set(%d[%d])", i, data.NativeTypeNum)
}
return nil
}
// First returns the first element's index of the collection.
func (O *ObjectCollection) First() (int, error) {
var exists C.int
var idx C.int32_t
if C.dpiObject_getFirstIndex(O.dpiObject, &idx, &exists) == C.DPI_FAILURE {
return 0, errors.Wrap(O.getError(), "first")
}
if exists == 1 {
return int(idx), nil
}
return 0, ErrNotExist
}
// Last returns the index of the last element.
func (O *ObjectCollection) Last() (int, error) {
var exists C.int
var idx C.int32_t
if C.dpiObject_getLastIndex(O.dpiObject, &idx, &exists) == C.DPI_FAILURE {
return 0, errors.Wrap(O.getError(), "last")
}
if exists == 1 {
return int(idx), nil
}
return 0, ErrNotExist
}
// Next returns the succeeding index of i.
func (O *ObjectCollection) Next(i int) (int, error) {
var exists C.int
var idx C.int32_t
if C.dpiObject_getNextIndex(O.dpiObject, C.int32_t(i), &idx, &exists) == C.DPI_FAILURE {
return 0, errors.Wrapf(O.getError(), "next(%d)", i)
}
if exists == 1 {
return int(idx), nil
}
return 0, ErrNotExist
}
// Len returns the length of the collection.
func (O *ObjectCollection) Len() (int, error) {
var size C.int32_t
if C.dpiObject_getSize(O.dpiObject, &size) == C.DPI_FAILURE {
return 0, errors.Wrap(O.getError(), "len")
}
return int(size), nil
}
// Trim the collection to n.
func (O *ObjectCollection) Trim(n int) error {
if C.dpiObject_trim(O.dpiObject, C.uint32_t(n)) == C.DPI_FAILURE {
return O.getError()
}
return nil
}
// ObjectType holds type info of an Object.
type ObjectType struct {
dpiObjectType *C.dpiObjectType
Schema, Name string
CollectionOf *ObjectType
OracleTypeNum C.dpiOracleTypeNum
NativeTypeNum C.dpiNativeTypeNum
DBSize, ClientSizeInBytes, CharSize int
Precision int16
Scale int8
FsPrecision uint8
Attributes map[string]ObjectAttribute
drv *drv
}
func (t ObjectType) getError() error { return t.drv.getError() }
// FullName returns the object's name with the schame prepended.
func (t ObjectType) FullName() string {
if t.Schema == "" {
return t.Name
}
return t.Schema + "." + t.Name
}
// GetObjectType returns the ObjectType of a name.
func (c *conn) GetObjectType(name string) (ObjectType, error) {
cName := C.CString(name)
defer func() { C.free(unsafe.Pointer(cName)) }()
objType := (*C.dpiObjectType)(C.malloc(C.sizeof_void))
if C.dpiConn_getObjectType(c.dpiConn, cName, C.uint32_t(len(name)), &objType) == C.DPI_FAILURE {
C.free(unsafe.Pointer(objType))
return ObjectType{}, errors.Wrapf(c.getError(), "getObjectType(%q) conn=%p", name, c.dpiConn)
}
t := ObjectType{drv: c.drv, dpiObjectType: objType}
return t, t.init()
}
// NewObject returns a new Object with ObjectType type.
func (t ObjectType) NewObject() (*Object, error) {
obj := (*C.dpiObject)(C.malloc(C.sizeof_void))
if C.dpiObjectType_createObject(t.dpiObjectType, &obj) == C.DPI_FAILURE {
C.free(unsafe.Pointer(obj))
return nil, t.getError()
}
return &Object{ObjectType: t, dpiObject: obj}, nil
}
func wrapObject(d *drv, objectType *C.dpiObjectType, object *C.dpiObject) (*Object, error) {
if objectType == nil {
return nil, errors.New("objectType is nil")
}
o := &Object{
ObjectType: ObjectType{dpiObjectType: objectType, drv: d},
dpiObject: object,
}
return o, o.init()
}
func (t *ObjectType) init() error {
if t.drv == nil {
panic("drv is nil")
}
if t.Name != "" && t.Attributes != nil {
return nil
}
if t.dpiObjectType == nil {
return nil
}
var info C.dpiObjectTypeInfo
if C.dpiObjectType_getInfo(t.dpiObjectType, &info) == C.DPI_FAILURE {
return errors.Wrapf(t.getError(), "%v.getInfo", t)
}
t.Schema = C.GoStringN(info.schema, C.int(info.schemaLength))
t.Name = C.GoStringN(info.name, C.int(info.nameLength))
t.CollectionOf = nil
numAttributes := int(info.numAttributes)
if info.isCollection == 1 {
t.CollectionOf = &ObjectType{drv: t.drv}
if err := t.CollectionOf.fromDataTypeInfo(info.elementTypeInfo); err != nil {
return err
}
if t.CollectionOf.Name == "" {
t.CollectionOf.Schema = t.Schema
t.CollectionOf.Name = t.Name
}
}
if numAttributes == 0 {
t.Attributes = map[string]ObjectAttribute{}
return nil
}
t.Attributes = make(map[string]ObjectAttribute, numAttributes)
attrs := make([]*C.dpiObjectAttr, numAttributes)
if C.dpiObjectType_getAttributes(t.dpiObjectType,
C.uint16_t(len(attrs)),
(**C.dpiObjectAttr)(unsafe.Pointer(&attrs[0])),
) == C.DPI_FAILURE {
return errors.Wrapf(t.getError(), "%v.getAttributes", t)
}
for i, attr := range attrs {
var attrInfo C.dpiObjectAttrInfo
if C.dpiObjectAttr_getInfo(attr, &attrInfo) == C.DPI_FAILURE {
return errors.Wrapf(t.getError(), "%v.attr_getInfo", attr)
}
if Log != nil {
Log("i", i, "attrInfo", attrInfo)
}
typ := attrInfo.typeInfo
sub, err := objectTypeFromDataTypeInfo(t.drv, typ)
if err != nil {
return err
}
objAttr := ObjectAttribute{
dpiObjectAttr: attr,
Name: C.GoStringN(attrInfo.name, C.int(attrInfo.nameLength)),
ObjectType: sub,
}
//fmt.Printf("%d=%q. typ=%+v sub=%+v\n", i, objAttr.Name, typ, sub)
t.Attributes[objAttr.Name] = objAttr
}
return nil
}
func (t *ObjectType) fromDataTypeInfo(typ C.dpiDataTypeInfo) error {
t.dpiObjectType = typ.objectType
t.OracleTypeNum = typ.oracleTypeNum
t.NativeTypeNum = typ.defaultNativeTypeNum
t.DBSize = int(typ.dbSizeInBytes)
t.ClientSizeInBytes = int(typ.clientSizeInBytes)
t.CharSize = int(typ.sizeInChars)
t.Precision = int16(typ.precision)
t.Scale = int8(typ.scale)
t.FsPrecision = uint8(typ.fsPrecision)
return t.init()
}
func objectTypeFromDataTypeInfo(drv *drv, typ C.dpiDataTypeInfo) (ObjectType, error) {
if drv == nil {
panic("drv nil")
}
if typ.oracleTypeNum == 0 {
panic("typ is nil")
}
t := ObjectType{drv: drv}
err := t.fromDataTypeInfo(typ)
return t, err
}
// ObjectAttribute is an attribute of an Object.
type ObjectAttribute struct {
dpiObjectAttr *C.dpiObjectAttr
Name string
ObjectType
}
// Close the ObjectAttribute.
func (A ObjectAttribute) Close() error {
attr := A.dpiObjectAttr
if attr == nil {
return nil
}
A.dpiObjectAttr = nil
if C.dpiObjectAttr_release(attr) == C.DPI_FAILURE {
return A.getError()
}
return nil
}
// GetObjectType returns the ObjectType for the name.
func GetObjectType(ex Execer, typeName string) (ObjectType, error) {
c, err := getConn(ex)
if err != nil {
return ObjectType{}, errors.WithMessage(err, "getConn for "+typeName)
}
return c.GetObjectType(typeName)
}