/
drv.go
261 lines (235 loc) · 6.75 KB
/
drv.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
// 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 <oci.h>
#include <stdlib.h>
*/
import "C"
import (
"database/sql/driver"
"sync"
"sync/atomic"
"time"
)
// DrvCfg represents configuration values for the ora package.
type DrvCfg struct {
StmtCfg
Log LogDrvCfg
}
// NewDrvCfg creates a DrvCfg with default values.
func NewDrvCfg() DrvCfg {
return DrvCfg{StmtCfg: NewStmtCfg(), Log: NewLogDrvCfg()}
}
func (cfg DrvCfg) SetStmtCfg(stmtCfg StmtCfg) DrvCfg {
cfg.StmtCfg = stmtCfg
return cfg
}
func (c DrvCfg) SetPrefetchRowCount(prefetchRowCount uint32) DrvCfg {
c.StmtCfg = c.StmtCfg.SetPrefetchRowCount(prefetchRowCount)
return c
}
func (c DrvCfg) SetPrefetchMemorySize(prefetchMemorySize uint32) DrvCfg {
c.StmtCfg = c.StmtCfg.SetPrefetchMemorySize(prefetchMemorySize)
return c
}
func (c DrvCfg) SetLongBufferSize(size uint32) DrvCfg {
c.StmtCfg = c.StmtCfg.SetLongBufferSize(size)
return c
}
func (c DrvCfg) SetLongRawBufferSize(size uint32) DrvCfg {
c.StmtCfg = c.StmtCfg.SetLongRawBufferSize(size)
return c
}
func (c DrvCfg) SetLobBufferSize(size int) DrvCfg {
c.StmtCfg = c.StmtCfg.SetLobBufferSize(size)
return c
}
func (c DrvCfg) SetStringPtrBufferSize(size int) DrvCfg {
c.StmtCfg = c.StmtCfg.SetStringPtrBufferSize(size)
return c
}
func (c DrvCfg) SetByteSlice(gct GoColumnType) DrvCfg {
c.StmtCfg = c.StmtCfg.SetByteSlice(gct)
return c
}
func (c DrvCfg) SetNumberInt(gct GoColumnType) DrvCfg {
c.StmtCfg = c.StmtCfg.SetNumberInt(gct)
return c
}
func (c DrvCfg) SetNumberBigInt(gct GoColumnType) DrvCfg {
c.StmtCfg = c.StmtCfg.SetNumberBigInt(gct)
return c
}
func (c DrvCfg) SetNumberFloat(gct GoColumnType) DrvCfg {
c.StmtCfg = c.StmtCfg.SetNumberFloat(gct)
return c
}
func (c DrvCfg) SetNumberBigFloat(gct GoColumnType) DrvCfg {
c.StmtCfg = c.StmtCfg.SetNumberBigFloat(gct)
return c
}
func (c DrvCfg) SetBinaryDouble(gct GoColumnType) DrvCfg {
c.StmtCfg = c.StmtCfg.SetBinaryDouble(gct)
return c
}
func (c DrvCfg) SetBinaryFloat(gct GoColumnType) DrvCfg {
c.StmtCfg = c.StmtCfg.SetBinaryFloat(gct)
return c
}
func (c DrvCfg) SetFloat(gct GoColumnType) DrvCfg { c.StmtCfg = c.StmtCfg.SetFloat(gct); return c }
func (c DrvCfg) SetDate(gct GoColumnType) DrvCfg { c.StmtCfg = c.StmtCfg.SetDate(gct); return c }
func (c DrvCfg) SetTimestamp(gct GoColumnType) DrvCfg {
c.StmtCfg = c.StmtCfg.SetTimestamp(gct)
return c
}
func (c DrvCfg) SetTimestampTz(gct GoColumnType) DrvCfg {
c.StmtCfg = c.StmtCfg.SetTimestampTz(gct)
return c
}
func (c DrvCfg) SetTimestampLtz(gct GoColumnType) DrvCfg {
c.StmtCfg = c.StmtCfg.SetTimestampLtz(gct)
return c
}
func (c DrvCfg) SetChar1(gct GoColumnType) DrvCfg { c.StmtCfg = c.StmtCfg.SetChar1(gct); return c }
func (c DrvCfg) SetChar(gct GoColumnType) DrvCfg { c.StmtCfg = c.StmtCfg.SetChar(gct); return c }
func (c DrvCfg) SetVarchar(gct GoColumnType) DrvCfg { c.StmtCfg = c.StmtCfg.SetVarchar(gct); return c }
func (c DrvCfg) SetLong(gct GoColumnType) DrvCfg { c.StmtCfg = c.StmtCfg.SetLong(gct); return c }
func (c DrvCfg) SetClob(gct GoColumnType) DrvCfg { c.StmtCfg = c.StmtCfg.SetClob(gct); return c }
func (c DrvCfg) SetBlob(gct GoColumnType) DrvCfg { c.StmtCfg = c.StmtCfg.SetBlob(gct); return c }
func (c DrvCfg) SetRaw(gct GoColumnType) DrvCfg { c.StmtCfg = c.StmtCfg.SetRaw(gct); return c }
func (c DrvCfg) SetLongRaw(gct GoColumnType) DrvCfg { c.StmtCfg = c.StmtCfg.SetLongRaw(gct); return c }
func (c DrvCfg) SetLogger(lgr Logger) DrvCfg { c.Log.Logger = lgr; return c }
// LogDrvCfg represents package-level logging configuration values.
type LogDrvCfg struct {
// Logger writes log messages.
// Logger can be replaced with any type implementing the Logger interface.
//
// The default implementation uses the standard lib's log package.
//
// For a glog-based implementation, see gopkg.in/rana/ora.v4/glg.
// LogDrvCfg.Logger = glg.Log
//
// For an gopkg.in/inconshreveable/log15.v2-based, see gopkg.in/rana/ora.v4/lg15.
// LogDrvCfg.Logger = lg15.Log
Logger Logger
// OpenEnv determines whether the ora.OpenEnv method is logged.
//
// The default is true.
OpenEnv bool
// Ins determines whether the ora.Ins method is logged.
//
// The default is true.
Ins bool
// Upd determines whether the ora.Upd method is logged.
//
// The default is true.
Upd bool
// Del determines whether the ora.Del method is logged.
//
// The default is true.
Del bool
// Sel determines whether the ora.Sel method is logged.
//
// The default is true.
Sel bool
// AddTbl determines whether the ora.AddTbl method is logged.
//
// The default is true.
AddTbl bool
Env LogEnvCfg
Srv LogSrvCfg
Ses LogSesCfg
Stmt LogStmtCfg
Tx LogTxCfg
Con LogConCfg
Rset LogRsetCfg
}
// NewLogDrvCfg creates a LogDrvCfg with default values.
func NewLogDrvCfg() LogDrvCfg {
c := LogDrvCfg{}
c.Logger = EmpLgr{}
c.OpenEnv = true
c.Ins = true
c.Upd = true
c.Del = true
c.Sel = true
c.AddTbl = true
c.Env = NewLogEnvCfg()
c.Srv = NewLogSrvCfg()
c.Ses = NewLogSesCfg()
c.Stmt = NewLogStmtCfg()
c.Tx = NewLogTxCfg()
c.Con = NewLogConCfg()
c.Rset = NewLogRsetCfg()
return c
}
// IsEnabled returns whether the logger is enabled (and enabled is true).
func (c LogDrvCfg) IsEnabled(enabled bool) bool {
if !enabled || c.Logger == nil {
return false
}
_, ok := c.Logger.(EmpLgr)
return !ok
}
// Drv represents an Oracle database driver.
//
// Drv is not meant to be called by user-code.
//
// Drv implements the driver.Driver interface.
type Drv struct {
sync.RWMutex
cfg atomic.Value
envId Id
srvId Id
conId Id
sesId Id
txId Id
stmtId Id
rsetId Id
listPool *sync.Pool
envPool *sync.Pool
conPool *sync.Pool
srvPool *sync.Pool
sesPool *sync.Pool
stmtPool *sync.Pool
txPool *sync.Pool
rsetPool *sync.Pool
bndPools []*sync.Pool
defPools []*sync.Pool
locationsMu sync.RWMutex
locations map[string]*time.Location
sqlPkgEnv *Env // An environment for use by the database/sql package.
openEnvs *envList
}
func (drv *Drv) Cfg() DrvCfg {
c := drv.cfg.Load()
//fmt.Fprintf(os.Stderr, "%p.Cfg=%#v\n", drv, c)
if c == nil || c.(DrvCfg).IsZero() {
return NewDrvCfg()
}
return c.(DrvCfg)
}
func (drv *Drv) SetCfg(cfg DrvCfg) {
//fmt.Fprintf(os.Stderr, "%p.SetCfg(%#v)\n", drv, cfg)
drv.cfg.Store(cfg)
}
// Open opens a connection to an Oracle server with the database/sql environment.
//
// This is intended to be called by the database/sql package only.
//
// Alternatively, you may call Env.OpenCon to create an *ora.Con.
//
// Open is a member of the driver.Driver interface.
func (drv *Drv) Open(conStr string) (driver.Conn, error) {
log(true)
drv.RLock()
env := drv.sqlPkgEnv
drv.RUnlock()
con, err := env.OpenCon(conStr)
if err != nil {
return nil, maybeBadConn(err)
}
return con, nil
}