-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
c_helpers.go
136 lines (124 loc) · 3.36 KB
/
c_helpers.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
package oci8
// #include "oci8.go.h"
import "C"
import (
"unsafe"
)
// getInt64 gets int64 from pointer
func getInt64(p unsafe.Pointer) int64 {
return int64(*(*C.sb8)(p))
}
// getUint64 gets uint64 from pointer
func getUint64(p unsafe.Pointer) uint64 {
return uint64(*(*C.sb8)(p))
}
// cByte converts byte slice to OraText.
// must be freed
func cByte(b []byte) *C.OraText {
p := C.malloc(C.size_t(len(b)))
pp := (*[1 << 30]byte)(p)
copy(pp[:], b)
return (*C.OraText)(p)
}
// cByteN converts byte slice to C OraText with size.
// must be freed
func cByteN(b []byte, size int) *C.OraText {
p := C.malloc(C.size_t(size))
pp := (*[1 << 30]byte)(p)
copy(pp[:], b)
return (*C.OraText)(p)
}
// cString coverts string to C OraText.
// must be freed
func cString(s string) *C.OraText {
p := C.malloc(C.size_t(len(s) + 1))
pp := (*[1 << 30]byte)(p)
copy(pp[:], s)
pp[len(s)] = 0
return (*C.OraText)(p)
}
// cStringN coverts string to C OraText with size.
// must be freed
func cStringN(s string, size int) *C.OraText {
p := C.malloc(C.size_t(size))
pp := (*[1 << 30]byte)(p)
copy(pp[:], s)
if len(s) < size {
pp[len(s)] = 0
} else {
pp[size-1] = 0
}
return (*C.OraText)(p)
}
// CGoStringN coverts C OraText to Go string
func cGoStringN(s *C.OraText, size int) string {
if size == 0 {
return ""
}
p := (*[1 << 30]byte)(unsafe.Pointer(s))
buf := make([]byte, size)
copy(buf, p[:])
return *(*string)(unsafe.Pointer(&buf))
}
// freeDefines frees defines
func freeDefines(defines []defineStruct) {
for i := 0; i < len(defines); i++ {
if len(defines[i].subDefines) > 0 {
freeDefines(defines[i].subDefines)
}
defines[i].subDefines = nil
if defines[i].pbuf != nil {
freeBuffer(defines[i].pbuf, defines[i].dataType)
defines[i].pbuf = nil
}
if defines[i].length != nil {
C.free(unsafe.Pointer(defines[i].length))
defines[i].length = nil
}
if defines[i].indicator != nil {
C.free(unsafe.Pointer(defines[i].indicator))
defines[i].indicator = nil
}
defines[i].defineHandle = nil // should be freed by oci statement close
}
}
// freeBinds frees binds
func freeBinds(binds []bindStruct) {
for _, bind := range binds {
if bind.pbuf != nil {
freeBuffer(bind.pbuf, bind.dataType)
bind.pbuf = nil
}
if bind.length != nil {
C.free(unsafe.Pointer(bind.length))
bind.length = nil
}
if bind.indicator != nil {
C.free(unsafe.Pointer(bind.indicator))
bind.indicator = nil
}
bind.bindHandle = nil // freed by oci statement close
}
}
// freeBuffer calles OCIDescriptorFree to free double pointer to buffer
// or calles C free to free pointer to buffer
func freeBuffer(buffer unsafe.Pointer, dataType C.ub2) {
switch dataType {
case C.SQLT_CLOB, C.SQLT_BLOB:
C.OCIDescriptorFree(*(*unsafe.Pointer)(buffer), C.OCI_DTYPE_LOB)
case C.SQLT_TIMESTAMP:
C.OCIDescriptorFree(*(*unsafe.Pointer)(buffer), C.OCI_DTYPE_TIMESTAMP)
case C.SQLT_TIMESTAMP_TZ:
C.OCIDescriptorFree(*(*unsafe.Pointer)(buffer), C.OCI_DTYPE_TIMESTAMP_TZ)
case C.SQLT_TIMESTAMP_LTZ:
C.OCIDescriptorFree(*(*unsafe.Pointer)(buffer), C.OCI_DTYPE_TIMESTAMP_LTZ)
case C.SQLT_INTERVAL_DS:
C.OCIDescriptorFree(*(*unsafe.Pointer)(buffer), C.OCI_DTYPE_INTERVAL_DS)
case C.SQLT_INTERVAL_YM:
C.OCIDescriptorFree(*(*unsafe.Pointer)(buffer), C.OCI_DTYPE_INTERVAL_YM)
case C.SQLT_RSET:
C.OCIDescriptorFree(*(*unsafe.Pointer)(buffer), C.OCI_HTYPE_STMT)
default:
C.free(buffer)
}
}