forked from rana/ora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defIntervalYM.go
108 lines (99 loc) · 2.93 KB
/
defIntervalYM.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
// Copyright 2014 Rana Ian. All rights reserved.
// Use of this source code is governed by The MIT License
// found in the accompanying LICENSE file.
package ora
/*
#include <stdlib.h>
#include <oci.h>
#include "version.h"
*/
import "C"
import (
"unsafe"
)
type defIntervalYM struct {
ociDef
intervals []*C.OCIInterval
}
func (def *defIntervalYM) define(position int, rset *Rset) error {
def.rset = rset
if def.intervals != nil {
C.free(unsafe.Pointer(&def.intervals[0]))
}
def.intervals = (*((*[MaxFetchLen]*C.OCIInterval)(C.malloc(C.size_t(rset.fetchLen) * C.sof_Intervalp))))[:rset.fetchLen]
def.ensureAllocatedLength(len(def.intervals))
return def.ociDef.defineByPos(position, unsafe.Pointer(&def.intervals[0]), int(C.sof_Intervalp), C.SQLT_INTERVAL_YM)
}
func (def *defIntervalYM) value(offset int) (value interface{}, err error) {
intervalYM := IntervalYM{IsNull: def.nullInds[offset] < 0}
if !intervalYM.IsNull {
var year C.sb4
var month C.sb4
r := C.OCIIntervalGetYearMonth(
unsafe.Pointer(def.rset.stmt.ses.srv.env.ocienv), //void *hndl,
def.rset.stmt.ses.srv.env.ocierr, //OCIError *err,
&year, //sb4 *yr,
&month, //sb4 *mnth,
def.intervals[offset]) //const OCIInterval *interval );
if r == C.OCI_ERROR {
err = def.rset.stmt.ses.srv.env.ociError()
}
intervalYM.Year = int32(year)
intervalYM.Month = int32(month)
}
return intervalYM, err
}
func (def *defIntervalYM) alloc() error {
for i, p := range def.intervals {
if p != nil {
def.intervals[i] = nil
//C.OCIDescriptorFree(unsafe.Pointer(p), C.OCI_DTYPE_INTERVAL_YM)
}
def.allocated[i] = false
r := C.OCIDescriptorAlloc(
unsafe.Pointer(def.rset.stmt.ses.srv.env.ocienv), //CONST dvoid *parenth,
(*unsafe.Pointer)(unsafe.Pointer(&def.intervals[i])), //dvoid **descpp,
C.OCI_DTYPE_INTERVAL_YM, //ub4 type,
0, //size_t xtramem_sz,
nil) //dvoid **usrmempp);
if r == C.OCI_ERROR {
return def.rset.stmt.ses.srv.env.ociError()
} else if r == C.OCI_INVALID_HANDLE {
return errNew("unable to allocate oci interval handle during define")
}
def.allocated[i] = true
}
return nil
}
func (def *defIntervalYM) free() {
for i, p := range def.intervals {
if p == nil {
continue
}
def.intervals[i] = nil
if !def.allocated[i] {
continue
}
C.OCIDescriptorFree(
unsafe.Pointer(p), //void *descp,
C.OCI_DTYPE_INTERVAL_YM) //timeDefine.descTypeCode) //ub4 type );
}
def.arrHlp.close()
}
func (def *defIntervalYM) close() (err error) {
defer func() {
if value := recover(); value != nil {
err = errR(value)
}
}()
def.free()
if def.intervals != nil {
C.free(unsafe.Pointer(&def.intervals[0]))
def.intervals = nil
}
rset := def.rset
def.rset = nil
def.ocidef = nil
rset.putDef(defIdxIntervalYM, def)
return nil
}