forked from mattn/go-oci8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
globals.go
187 lines (160 loc) · 4.14 KB
/
globals.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
package oci8
/*
#cgo !noPkgConfig pkg-config: oci8
#include "oci8.go.h"
*/
import "C"
// noPkgConfig is a Go tag for disabling using pkg-config and using environmental settings like CGO_CFLAGS and CGO_LDFLAGS instead
import (
"database/sql"
"database/sql/driver"
"errors"
"io/ioutil"
"log"
"regexp"
"time"
"unsafe"
)
const (
lobBufferSize = 4000
useOCISessionBegin = true
sizeOfNilPointer = unsafe.Sizeof(unsafe.Pointer(nil))
)
type (
// DSN is Oracle Data Source Name
DSN struct {
Connect string
Username string
Password string
prefetchRows C.ub4
prefetchMemory C.ub4
Location *time.Location
transactionMode C.ub4
enableQMPlaceholders bool
operationMode C.ub4
externalauthentication bool
}
// OCI8DriverStruct is Oracle driver struct
OCI8DriverStruct struct {
// Logger is used to log connection ping errors, defaults to discard
// To log set it to something like: log.New(os.Stderr, "oci8 ", log.Ldate|log.Ltime|log.LUTC|log.Llongfile)
Logger *log.Logger
}
// OCI8Connector is the sql driver connector
OCI8Connector struct {
// Logger is used to log connection ping errors
Logger *log.Logger
}
// OCI8Conn is Oracle connection
OCI8Conn struct {
svc *C.OCISvcCtx
srv *C.OCIServer
env *C.OCIEnv
errHandle *C.OCIError
usrSession *C.OCISession
prefetchRows C.ub4
prefetchMemory C.ub4
location *time.Location
transactionMode C.ub4
operationMode C.ub4
inTransaction bool
enableQMPlaceholders bool
closed bool
logger *log.Logger
}
// OCI8Tx is Oracle transaction
OCI8Tx struct {
conn *OCI8Conn
}
namedValue struct {
Name string
Ordinal int
Value driver.Value
}
outValue struct {
Dest interface{}
In bool
}
// OCI8Stmt is Oracle statement
OCI8Stmt struct {
conn *OCI8Conn
stmt *C.OCIStmt
closed bool
pbind []oci8Bind // bind params
}
// OCI8Result is Oracle result
OCI8Result struct {
rowsAffected int64
rowsAffectedErr error
rowid string
rowidErr error
stmt *OCI8Stmt
}
oci8Define struct {
name string
dataType C.ub2
pbuf unsafe.Pointer
maxSize C.sb4
length *C.ub2
indicator *C.sb2
defineHandle *C.OCIDefine
}
oci8Bind struct {
dataType C.ub2
pbuf unsafe.Pointer
maxSize C.sb4
length *C.ub2
indicator *C.sb2
bindHandle *C.OCIBind
out sql.Out
}
// OCI8Rows is Oracle rows
OCI8Rows struct {
stmt *OCI8Stmt
defines []oci8Define
e bool
closed bool
done chan struct{}
cls bool
}
)
var (
// ErrOCISuccessWithInfo is OCI_SUCCESS_WITH_INFO
ErrOCISuccessWithInfo = errors.New("OCI_SUCCESS_WITH_INFO")
// ErrNoRowid is result has no rowid
ErrNoRowid = errors.New("result has no rowid")
phre = regexp.MustCompile(`\?`)
defaultCharset = C.ub2(0)
// OCI8Driver is the sql driver
OCI8Driver = &OCI8DriverStruct{
Logger: log.New(ioutil.Discard, "", 0),
}
)
func init() {
sql.Register("oci8", OCI8Driver)
// set defaultCharset to AL32UTF8
var envP *C.OCIEnv
envPP := &envP
var result C.sword
result = C.OCIEnvCreate(envPP, C.OCI_DEFAULT, nil, nil, nil, nil, 0, nil)
if result != C.OCI_SUCCESS {
panic("OCIEnvCreate error")
}
nlsLang := cString("AL32UTF8")
defaultCharset = C.OCINlsCharSetNameToId(unsafe.Pointer(*envPP), (*C.oratext)(nlsLang))
C.free(unsafe.Pointer(nlsLang))
C.OCIHandleFree(unsafe.Pointer(*envPP), C.OCI_HTYPE_ENV)
}
/*
OCI Documentation Notes
Datatypes:
https://docs.oracle.com/en/database/oracle/oracle-database/12.2/lnoci/data-types.html
Handle and Descriptor Attributes:
https://docs.oracle.com/en/database/oracle/oracle-database/12.2/lnoci/handle-and-descriptor-attributes.html
OCI Function Server Round Trips:
https://docs.oracle.com/en/database/oracle/oracle-database/12.2/lnoci/oci-function-server-round-trips.html
OCI examples:
https://github.com/alexeyvo/oracle_oci_examples
Oracle datatypes:
https://ss64.com/ora/syntax-datatypes.html
*/