/
util_arr.go
198 lines (188 loc) · 4.91 KB
/
util_arr.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
package ora
/*
#include <oci.h>
#include "version.h"
*/
import "C"
import (
"sync"
"unsafe"
)
type arrHlp struct {
sync.Mutex
curlen C.ub4
nullInds []C.sb2
alen []C.ACTUAL_LENGTH_TYPE
rcode []C.ub2
isAssocArr bool
}
type ociDef struct {
ocidef *C.OCIDefine
rset *Rset
arrHlp
}
func (d *ociDef) defineByPos(position int, valuep unsafe.Pointer, valueSize int, dty int) error {
d.ensureFetchLength(d.rset.stmt.Cfg().FetchLen())
// If you omit the rlenp parameter of OCIDefineByPos(), returned values are blank-padded to the buffer length, and NULLs are returned as a string of blank characters. If rlenp is included, returned values are not blank-padded. Instead, their actual lengths are returned in the rlenp parameter.
if r := C.OCIDEFINEBYPOS(
d.rset.ocistmt, //OCIStmt *stmtp,
&d.ocidef, //OCIDefine **defnpp,
d.rset.env.ocierr, //OCIError *errhp,
C.ub4(position), //ub4 position,
valuep, //void *valuep,
C.LENGTH_TYPE(valueSize), //sb8 value_sz,
C.ub2(dty), //ub2 dty,
unsafe.Pointer(&d.nullInds[0]), //void *indp,
&d.alen[0], //ub4 *rlenp,
&d.rcode[0], //ub2 *rcodep,
C.OCI_DEFAULT, //ub4 mode );
); r == C.OCI_ERROR {
return d.rset.stmt.ses.srv.env.ociError()
}
if r := C.OCIDefineArrayOfStruct(
d.ocidef, //OCIDefine *defnp
d.rset.env.ocierr, //OCIError *errhp,
C.ub4(valueSize), //ub4 pvskip,
2, //ub4 indskip,
C.ACTUAL_LENGTH_LENGTH, //ub4 rlskip,
2, //ub4 rcskip
); r == C.OCI_ERROR {
return d.rset.env.ociError()
}
return nil
}
var (
sb2Pool = sync.Pool{New: func() interface{} { return []C.sb2{} }}
ub2Pool = sync.Pool{New: func() interface{} { return []C.ub2{} }}
alenPool = sync.Pool{New: func() interface{} { return []C.ACTUAL_LENGTH_TYPE{} }}
)
func (a *arrHlp) ensureFetchLength(length int) {
if length <= 0 || length >= MaxFetchLen {
length = MaxFetchLen
}
a.Lock()
defer a.Unlock()
if cap(a.nullInds) >= length {
a.nullInds = a.nullInds[:length]
} else {
if a.nullInds = sb2Pool.Get().([]C.sb2); cap(a.nullInds) < length {
a.nullInds = make([]C.sb2, length)
} else {
a.nullInds = a.nullInds[:length]
}
}
if cap(a.alen) >= length {
a.alen = a.alen[:length]
} else {
if a.alen = alenPool.Get().([]C.ACTUAL_LENGTH_TYPE); cap(a.alen) < length {
a.alen = make([]C.ACTUAL_LENGTH_TYPE, length)
} else {
a.alen = a.alen[:length]
}
}
if cap(a.rcode) >= length {
a.rcode = a.rcode[:length]
} else {
if a.rcode = ub2Pool.Get().([]C.ub2); cap(a.rcode) < length {
a.rcode = make([]C.ub2, length)
} else {
a.rcode = a.rcode[:length]
}
}
}
// ensureBindArrLength calculates the needed length and capacity,
// and sets up the helper arrays for binding PL/SQL Table.
//
// Returns whether and element is needed to be appended to the value slice.
func (a *arrHlp) ensureBindArrLength(
length, capacity *int,
isAssocArray bool,
) (iterations uint32, curlenp *C.ub4, needsAppend bool) {
a.Lock()
defer a.Unlock()
a.curlen = C.ub4(*length) // the real length, not L!
if isAssocArray {
// for PL/SQL associative arrays
curlenp = &a.curlen
iterations = 1
a.isAssocArr = true
if *length == 0 {
*length = 1
if *capacity == 0 {
needsAppend = true
*capacity = 1
}
}
} else {
curlenp = nil
iterations = uint32(*length)
a.isAssocArr = false
}
L, C := *length, *capacity
if cap(a.nullInds) >= C {
a.nullInds = (a.nullInds)[:L]
} else {
if a.nullInds = sb2Pool.Get().([]C.sb2); cap(a.nullInds) < C {
a.nullInds = make([]C.sb2, L, C)
} else {
a.nullInds = (a.nullInds)[:L]
}
}
if cap(a.alen) >= C {
a.alen = a.alen[:L]
} else {
if a.alen = alenPool.Get().([]C.ACTUAL_LENGTH_TYPE); cap(a.alen) < C {
a.alen = make([]C.ACTUAL_LENGTH_TYPE, L, C)
} else {
a.alen = a.alen[:L]
}
}
if cap(a.rcode) >= C {
a.rcode = a.rcode[:L]
} else {
if a.rcode = ub2Pool.Get().([]C.ub2); cap(a.rcode) < C {
a.rcode = make([]C.ub2, L, C)
} else {
a.rcode = a.rcode[:L]
}
}
return iterations, curlenp, needsAppend
}
// IsAssocArr returns true if the bind uses PL/SQL Table.
func (a arrHlp) IsAssocArr() bool {
return a.isAssocArr
}
// close nils the slices, except when this is a PL/SQL Table.
//
// The reason for this is that for PL/SQL Tables, after exe returns,
// the bound slices can be reused; otherwise, they are still in use for
// the subsequent iterations!
func (a *arrHlp) close() error {
if a == nil {
return nil
}
a.Lock()
defer a.Unlock()
if a.isAssocArr {
return nil
}
if a.nullInds != nil {
sb2Pool.Put(a.nullInds)
a.nullInds = nil
}
if a.alen != nil {
alenPool.Put(a.alen)
a.alen = nil
}
if a.rcode != nil {
ub2Pool.Put(a.rcode)
a.rcode = nil
}
return nil
}
func getMaxarrLen(C int, isAssocArray bool) C.ub4 {
if !isAssocArray {
return 0
}
return C.ub4(C)
}