Skip to content

Commit 16089af

Browse files
authored
Merge pull request #384 from MichaelS11/rename
Remove OCI8 from names & added examples
2 parents 6d59bec + 347aa65 commit 16089af

11 files changed

+488
-107
lines changed

c_helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func cGoStringN(s *C.OraText, size int) string {
7171
}
7272

7373
// freeDefines frees defines
74-
func freeDefines(defines []oci8Define) {
74+
func freeDefines(defines []defineStruct) {
7575
for i := 0; i < len(defines); i++ {
7676
if len(defines[i].subDefines) > 0 {
7777
freeDefines(defines[i].subDefines)
@@ -94,7 +94,7 @@ func freeDefines(defines []oci8Define) {
9494
}
9595

9696
// freeBinds frees binds
97-
func freeBinds(binds []oci8Bind) {
97+
func freeBinds(binds []bindStruct) {
9898
for _, bind := range binds {
9999
if bind.pbuf != nil {
100100
freeBuffer(bind.pbuf, bind.dataType)

connection.go

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
// Ping database connection
17-
func (conn *OCI8Conn) Ping(ctx context.Context) error {
17+
func (conn *Conn) Ping(ctx context.Context) error {
1818
done := make(chan struct{})
1919
go conn.ociBreakDone(ctx, done)
2020
result := C.OCIPing(conn.svc, conn.errHandle, C.OCI_DEFAULT)
@@ -35,7 +35,7 @@ func (conn *OCI8Conn) Ping(ctx context.Context) error {
3535
}
3636

3737
// Close a connection
38-
func (conn *OCI8Conn) Close() error {
38+
func (conn *Conn) Close() error {
3939
if conn.closed {
4040
return nil
4141
}
@@ -82,12 +82,12 @@ func (conn *OCI8Conn) Close() error {
8282
}
8383

8484
// Prepare prepares a query
85-
func (conn *OCI8Conn) Prepare(query string) (driver.Stmt, error) {
85+
func (conn *Conn) Prepare(query string) (driver.Stmt, error) {
8686
return conn.PrepareContext(context.Background(), query)
8787
}
8888

8989
// PrepareContext prepares a query with context
90-
func (conn *OCI8Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
90+
func (conn *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
9191
if conn.enableQMPlaceholders {
9292
query = placeholders(query)
9393
}
@@ -112,16 +112,16 @@ func (conn *OCI8Conn) PrepareContext(ctx context.Context, query string) (driver.
112112
return nil, conn.getError(rv)
113113
}
114114

115-
return &OCI8Stmt{conn: conn, stmt: *stmt}, nil
115+
return &Stmt{conn: conn, stmt: *stmt}, nil
116116
}
117117

118118
// Begin starts a transaction
119-
func (conn *OCI8Conn) Begin() (driver.Tx, error) {
119+
func (conn *Conn) Begin() (driver.Tx, error) {
120120
return conn.BeginTx(context.Background(), driver.TxOptions{})
121121
}
122122

123123
// BeginTx starts a transaction
124-
func (conn *OCI8Conn) BeginTx(ctx context.Context, txOptions driver.TxOptions) (driver.Tx, error) {
124+
func (conn *Conn) BeginTx(ctx context.Context, txOptions driver.TxOptions) (driver.Tx, error) {
125125
if conn.transactionMode != C.OCI_TRANS_READWRITE {
126126
// transaction handle
127127
trans, _, err := conn.ociHandleAlloc(C.OCI_HTYPE_TRANS, 0)
@@ -153,11 +153,11 @@ func (conn *OCI8Conn) BeginTx(ctx context.Context, txOptions driver.TxOptions) (
153153

154154
conn.inTransaction = true
155155

156-
return &OCI8Tx{conn}, nil
156+
return &Tx{conn: conn}, nil
157157
}
158158

159159
// getError gets error from return result (sword) or OCIError
160-
func (conn *OCI8Conn) getError(result C.sword) error {
160+
func (conn *Conn) getError(result C.sword) error {
161161
switch result {
162162
case C.OCI_SUCCESS:
163163
return nil
@@ -198,7 +198,7 @@ func (conn *OCI8Conn) getError(result C.sword) error {
198198
}
199199

200200
// ociGetError calls OCIErrorGet then returs error code and text
201-
func (conn *OCI8Conn) ociGetError() (int, error) {
201+
func (conn *Conn) ociGetError() (int, error) {
202202
var errorCode C.sb4
203203
errorText := make([]byte, 1024)
204204

@@ -222,7 +222,7 @@ func (conn *OCI8Conn) ociGetError() (int, error) {
222222

223223
// ociAttrGet calls OCIAttrGet with OCIParam then returns attribute size and error.
224224
// The attribute value is stored into passed value.
225-
func (conn *OCI8Conn) ociAttrGet(paramHandle *C.OCIParam, value unsafe.Pointer, attributeType C.ub4) (C.ub4, error) {
225+
func (conn *Conn) ociAttrGet(paramHandle *C.OCIParam, value unsafe.Pointer, attributeType C.ub4) (C.ub4, error) {
226226
var size C.ub4
227227

228228
result := C.OCIAttrGet(
@@ -239,7 +239,7 @@ func (conn *OCI8Conn) ociAttrGet(paramHandle *C.OCIParam, value unsafe.Pointer,
239239

240240
// ociAttrSet calls OCIAttrSet.
241241
// Only uses errHandle from conn, so can be called in conn setup after errHandle has been set.
242-
func (conn *OCI8Conn) ociAttrSet(
242+
func (conn *Conn) ociAttrSet(
243243
handle unsafe.Pointer,
244244
handleType C.ub4,
245245
value unsafe.Pointer,
@@ -260,7 +260,7 @@ func (conn *OCI8Conn) ociAttrSet(
260260

261261
// ociHandleAlloc calls OCIHandleAlloc then returns
262262
// handle pointer to pointer, buffer pointer to pointer, and error
263-
func (conn *OCI8Conn) ociHandleAlloc(handleType C.ub4, size C.size_t) (*unsafe.Pointer, *unsafe.Pointer, error) {
263+
func (conn *Conn) ociHandleAlloc(handleType C.ub4, size C.size_t) (*unsafe.Pointer, *unsafe.Pointer, error) {
264264
var handleTemp unsafe.Pointer
265265
handle := &handleTemp
266266
var bufferTemp unsafe.Pointer
@@ -291,7 +291,7 @@ func (conn *OCI8Conn) ociHandleAlloc(handleType C.ub4, size C.size_t) (*unsafe.P
291291

292292
// ociDescriptorAlloc calls OCIDescriptorAlloc then returns
293293
// descriptor pointer to pointer, buffer pointer to pointer, and error
294-
func (conn *OCI8Conn) ociDescriptorAlloc(descriptorType C.ub4, size C.size_t) (*unsafe.Pointer, *unsafe.Pointer, error) {
294+
func (conn *Conn) ociDescriptorAlloc(descriptorType C.ub4, size C.size_t) (*unsafe.Pointer, *unsafe.Pointer, error) {
295295
var descriptorTemp unsafe.Pointer
296296
descriptor := &descriptorTemp
297297
var bufferTemp unsafe.Pointer
@@ -321,7 +321,7 @@ func (conn *OCI8Conn) ociDescriptorAlloc(descriptorType C.ub4, size C.size_t) (*
321321
}
322322

323323
// ociLobCreateTemporary calls OCILobCreateTemporary then returns error
324-
func (conn *OCI8Conn) ociLobCreateTemporary(lobLocator *C.OCILobLocator, form C.ub1, lobType C.ub1) error {
324+
func (conn *Conn) ociLobCreateTemporary(lobLocator *C.OCILobLocator, form C.ub1, lobType C.ub1) error {
325325

326326
result := C.OCILobCreateTemporary(
327327
conn.svc, // service context handle
@@ -338,7 +338,7 @@ func (conn *OCI8Conn) ociLobCreateTemporary(lobLocator *C.OCILobLocator, form C.
338338
}
339339

340340
// ociLobRead calls OCILobRead then returns lob bytes and error.
341-
func (conn *OCI8Conn) ociLobRead(lobLocator *C.OCILobLocator, form C.ub1) ([]byte, error) {
341+
func (conn *Conn) ociLobRead(lobLocator *C.OCILobLocator, form C.ub1) ([]byte, error) {
342342
buffer := make([]byte, 0)
343343

344344
// set character set form
@@ -389,7 +389,7 @@ func (conn *OCI8Conn) ociLobRead(lobLocator *C.OCILobLocator, form C.ub1) ([]byt
389389
}
390390

391391
// ociLobWrite calls OCILobWrite then returns error.
392-
func (conn *OCI8Conn) ociLobWrite(lobLocator *C.OCILobLocator, form C.ub1, data []byte) error {
392+
func (conn *Conn) ociLobWrite(lobLocator *C.OCILobLocator, form C.ub1, data []byte) error {
393393
start := 0
394394
writeBuffer := byteBufferPool.Get().([]byte)
395395
piece := (C.ub1)(C.OCI_FIRST_PIECE)
@@ -443,7 +443,7 @@ func (conn *OCI8Conn) ociLobWrite(lobLocator *C.OCILobLocator, form C.ub1, data
443443
}
444444

445445
// ociDateTimeToTime coverts OCIDateTime to Go Time
446-
func (conn *OCI8Conn) ociDateTimeToTime(dateTime *C.OCIDateTime, ociDateTimeHasTimeZone bool) (*time.Time, error) {
446+
func (conn *Conn) ociDateTimeToTime(dateTime *C.OCIDateTime, ociDateTimeHasTimeZone bool) (*time.Time, error) {
447447
// get date
448448
var year C.sb2
449449
var month C.ub1
@@ -507,7 +507,7 @@ func (conn *OCI8Conn) ociDateTimeToTime(dateTime *C.OCIDateTime, ociDateTimeHasT
507507
}
508508

509509
// timeToOCIDateTime coverts Go Time to OCIDateTime
510-
func (conn *OCI8Conn) timeToOCIDateTime(aTime *time.Time) (*unsafe.Pointer, error) {
510+
func (conn *Conn) timeToOCIDateTime(aTime *time.Time) (*unsafe.Pointer, error) {
511511
var err error
512512
var dateTimePP *unsafe.Pointer
513513
dateTimePP, _, err = conn.ociDescriptorAlloc(C.OCI_DTYPE_TIMESTAMP_TZ, 0)
@@ -567,7 +567,7 @@ func appendSmallInt(slice []byte, num int) []byte {
567567
}
568568

569569
// ociBreakDone calls OCIBreak if ctx.Done is finished before done chan is closed
570-
func (conn *OCI8Conn) ociBreakDone(ctx context.Context, done chan struct{}) {
570+
func (conn *Conn) ociBreakDone(ctx context.Context, done chan struct{}) {
571571
select {
572572
case <-done:
573573
case <-ctx.Done():
@@ -581,7 +581,7 @@ func (conn *OCI8Conn) ociBreakDone(ctx context.Context, done chan struct{}) {
581581
}
582582

583583
// ociBreak calls OCIBreak
584-
func (conn *OCI8Conn) ociBreak() {
584+
func (conn *Conn) ociBreak() {
585585
result := C.OCIBreak(
586586
unsafe.Pointer(conn.svc), // service or server context handle
587587
conn.errHandle, // error handle

connector.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ import (
1111

1212
// NewConnector returns a new database connector
1313
func NewConnector(hosts ...string) driver.Connector {
14-
return &OCI8Connector{
14+
return &Connector{
1515
Logger: log.New(ioutil.Discard, "", 0),
1616
}
1717
}
1818

1919
// Driver returns the OCI8 driver
20-
func (oci8Connector *OCI8Connector) Driver() driver.Driver {
21-
return OCI8Driver
20+
func (connector *Connector) Driver() driver.Driver {
21+
return Driver
2222
}
2323

2424
// Connect returns a new database connection
25-
func (oci8Connector *OCI8Connector) Connect(ctx context.Context) (driver.Conn, error) {
26-
oci8Conn := &OCI8Conn{
27-
logger: oci8Connector.Logger,
25+
func (connector *Connector) Connect(ctx context.Context) (driver.Conn, error) {
26+
conn := &Conn{
27+
logger: connector.Logger,
2828
}
29-
if oci8Conn.logger == nil {
30-
oci8Conn.logger = log.New(ioutil.Discard, "", 0)
29+
if conn.logger == nil {
30+
conn.logger = log.New(ioutil.Discard, "", 0)
3131
}
3232

33-
return oci8Conn, nil
33+
return conn, nil
3434
}

example_sql_go112_test.go

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// +build go1.12
2+
3+
package oci8_test
4+
5+
import (
6+
"context"
7+
"database/sql"
8+
"fmt"
9+
"log"
10+
"os"
11+
"time"
12+
13+
"github.com/mattn/go-oci8"
14+
)
15+
16+
func Example_sqlCursor() {
17+
// Example shows how to do a cursor select
18+
19+
// For testing, check if database tests are disabled
20+
if oci8.TestDisableDatabase || oci8.TestDisableDestructive {
21+
fmt.Println(3)
22+
return
23+
}
24+
25+
oci8.Driver.Logger = log.New(os.Stderr, "oci8 ", log.Ldate|log.Ltime|log.LUTC|log.Lshortfile)
26+
27+
var openString string
28+
// [username/[password]@]host[:port][/service_name][?param1=value1&...&paramN=valueN]
29+
if len(oci8.TestUsername) > 0 {
30+
if len(oci8.TestPassword) > 0 {
31+
openString = oci8.TestUsername + "/" + oci8.TestPassword + "@"
32+
} else {
33+
openString = oci8.TestUsername + "@"
34+
}
35+
}
36+
openString += oci8.TestHostValid
37+
38+
// A normal simple Open to localhost would look like:
39+
// db, err := sql.Open("oci8", "127.0.0.1")
40+
// For testing, need to use additional variables
41+
db, err := sql.Open("oci8", openString)
42+
if err != nil {
43+
fmt.Printf("Open error is not nil: %v", err)
44+
return
45+
}
46+
if db == nil {
47+
fmt.Println("db is nil")
48+
return
49+
}
50+
51+
// defer close database
52+
defer func() {
53+
err = db.Close()
54+
if err != nil {
55+
fmt.Println("Close error is not nil:", err)
56+
}
57+
}()
58+
59+
ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
60+
err = db.PingContext(ctx)
61+
cancel()
62+
if err != nil {
63+
fmt.Println("PingContext error is not nil:", err)
64+
return
65+
}
66+
67+
var rows *sql.Rows
68+
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second)
69+
defer cancel()
70+
rows, err = db.QueryContext(ctx, "select 1, cursor(select 2 from dual union select 3 from dual) from dual")
71+
if err != nil {
72+
fmt.Println("QueryContext error is not nil:", err)
73+
return
74+
}
75+
76+
// defer close rows
77+
defer func() {
78+
err = rows.Close()
79+
if err != nil {
80+
fmt.Println("Close error is not nil:", err)
81+
}
82+
}()
83+
84+
if !rows.Next() {
85+
fmt.Println("no Next rows")
86+
return
87+
}
88+
89+
var aInt int64
90+
var subRows *sql.Rows
91+
err = rows.Scan(&aInt, &subRows)
92+
if err != nil {
93+
fmt.Println("Scan error is not nil:", err)
94+
return
95+
}
96+
97+
if aInt != 1 {
98+
fmt.Println("aInt != 1")
99+
return
100+
}
101+
if subRows == nil {
102+
fmt.Println("subRows is nil")
103+
return
104+
}
105+
106+
if !subRows.Next() {
107+
fmt.Println("no Next subRows")
108+
return
109+
}
110+
111+
err = subRows.Scan(&aInt)
112+
if err != nil {
113+
fmt.Println("Scan error is not nil:", err)
114+
return
115+
}
116+
117+
if aInt != 2 {
118+
fmt.Println("aInt != 2")
119+
return
120+
}
121+
122+
if !subRows.Next() {
123+
fmt.Println("no Next subRows")
124+
return
125+
}
126+
127+
err = subRows.Scan(&aInt)
128+
if err != nil {
129+
fmt.Println("Scan error is not nil:", err)
130+
return
131+
}
132+
133+
if aInt != 3 {
134+
fmt.Println("aInt != 3")
135+
return
136+
}
137+
138+
if subRows.Next() {
139+
fmt.Println("has Next rows")
140+
return
141+
}
142+
143+
err = subRows.Err()
144+
if err != nil {
145+
fmt.Println("Err error is not nil:", err)
146+
return
147+
}
148+
149+
if rows.Next() {
150+
fmt.Println("has Next rows")
151+
return
152+
}
153+
154+
err = rows.Err()
155+
if err != nil {
156+
fmt.Println("Err error is not nil:", err)
157+
return
158+
}
159+
160+
fmt.Println(aInt)
161+
162+
// output: 3
163+
}

0 commit comments

Comments
 (0)