Skip to content

Commit

Permalink
Merge pull request #384 from MichaelS11/rename
Browse files Browse the repository at this point in the history
Remove OCI8 from names & added examples
  • Loading branch information
mattn authored Mar 26, 2020
2 parents 6d59bec + 347aa65 commit 16089af
Show file tree
Hide file tree
Showing 11 changed files with 488 additions and 107 deletions.
4 changes: 2 additions & 2 deletions c_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func cGoStringN(s *C.OraText, size int) string {
}

// freeDefines frees defines
func freeDefines(defines []oci8Define) {
func freeDefines(defines []defineStruct) {
for i := 0; i < len(defines); i++ {
if len(defines[i].subDefines) > 0 {
freeDefines(defines[i].subDefines)
Expand All @@ -94,7 +94,7 @@ func freeDefines(defines []oci8Define) {
}

// freeBinds frees binds
func freeBinds(binds []oci8Bind) {
func freeBinds(binds []bindStruct) {
for _, bind := range binds {
if bind.pbuf != nil {
freeBuffer(bind.pbuf, bind.dataType)
Expand Down
42 changes: 21 additions & 21 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
)

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

// Close a connection
func (conn *OCI8Conn) Close() error {
func (conn *Conn) Close() error {
if conn.closed {
return nil
}
Expand Down Expand Up @@ -82,12 +82,12 @@ func (conn *OCI8Conn) Close() error {
}

// Prepare prepares a query
func (conn *OCI8Conn) Prepare(query string) (driver.Stmt, error) {
func (conn *Conn) Prepare(query string) (driver.Stmt, error) {
return conn.PrepareContext(context.Background(), query)
}

// PrepareContext prepares a query with context
func (conn *OCI8Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
func (conn *Conn) PrepareContext(ctx context.Context, query string) (driver.Stmt, error) {
if conn.enableQMPlaceholders {
query = placeholders(query)
}
Expand All @@ -112,16 +112,16 @@ func (conn *OCI8Conn) PrepareContext(ctx context.Context, query string) (driver.
return nil, conn.getError(rv)
}

return &OCI8Stmt{conn: conn, stmt: *stmt}, nil
return &Stmt{conn: conn, stmt: *stmt}, nil
}

// Begin starts a transaction
func (conn *OCI8Conn) Begin() (driver.Tx, error) {
func (conn *Conn) Begin() (driver.Tx, error) {
return conn.BeginTx(context.Background(), driver.TxOptions{})
}

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

conn.inTransaction = true

return &OCI8Tx{conn}, nil
return &Tx{conn: conn}, nil
}

// getError gets error from return result (sword) or OCIError
func (conn *OCI8Conn) getError(result C.sword) error {
func (conn *Conn) getError(result C.sword) error {
switch result {
case C.OCI_SUCCESS:
return nil
Expand Down Expand Up @@ -198,7 +198,7 @@ func (conn *OCI8Conn) getError(result C.sword) error {
}

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

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

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

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

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

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

// ociDescriptorAlloc calls OCIDescriptorAlloc then returns
// descriptor pointer to pointer, buffer pointer to pointer, and error
func (conn *OCI8Conn) ociDescriptorAlloc(descriptorType C.ub4, size C.size_t) (*unsafe.Pointer, *unsafe.Pointer, error) {
func (conn *Conn) ociDescriptorAlloc(descriptorType C.ub4, size C.size_t) (*unsafe.Pointer, *unsafe.Pointer, error) {
var descriptorTemp unsafe.Pointer
descriptor := &descriptorTemp
var bufferTemp unsafe.Pointer
Expand Down Expand Up @@ -321,7 +321,7 @@ func (conn *OCI8Conn) ociDescriptorAlloc(descriptorType C.ub4, size C.size_t) (*
}

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

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

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

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

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

// ociDateTimeToTime coverts OCIDateTime to Go Time
func (conn *OCI8Conn) ociDateTimeToTime(dateTime *C.OCIDateTime, ociDateTimeHasTimeZone bool) (*time.Time, error) {
func (conn *Conn) ociDateTimeToTime(dateTime *C.OCIDateTime, ociDateTimeHasTimeZone bool) (*time.Time, error) {
// get date
var year C.sb2
var month C.ub1
Expand Down Expand Up @@ -507,7 +507,7 @@ func (conn *OCI8Conn) ociDateTimeToTime(dateTime *C.OCIDateTime, ociDateTimeHasT
}

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

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

// ociBreak calls OCIBreak
func (conn *OCI8Conn) ociBreak() {
func (conn *Conn) ociBreak() {
result := C.OCIBreak(
unsafe.Pointer(conn.svc), // service or server context handle
conn.errHandle, // error handle
Expand Down
18 changes: 9 additions & 9 deletions connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,24 @@ import (

// NewConnector returns a new database connector
func NewConnector(hosts ...string) driver.Connector {
return &OCI8Connector{
return &Connector{
Logger: log.New(ioutil.Discard, "", 0),
}
}

// Driver returns the OCI8 driver
func (oci8Connector *OCI8Connector) Driver() driver.Driver {
return OCI8Driver
func (connector *Connector) Driver() driver.Driver {
return Driver
}

// Connect returns a new database connection
func (oci8Connector *OCI8Connector) Connect(ctx context.Context) (driver.Conn, error) {
oci8Conn := &OCI8Conn{
logger: oci8Connector.Logger,
func (connector *Connector) Connect(ctx context.Context) (driver.Conn, error) {
conn := &Conn{
logger: connector.Logger,
}
if oci8Conn.logger == nil {
oci8Conn.logger = log.New(ioutil.Discard, "", 0)
if conn.logger == nil {
conn.logger = log.New(ioutil.Discard, "", 0)
}

return oci8Conn, nil
return conn, nil
}
163 changes: 163 additions & 0 deletions example_sql_go112_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
// +build go1.12

package oci8_test

import (
"context"
"database/sql"
"fmt"
"log"
"os"
"time"

"github.com/mattn/go-oci8"
)

func Example_sqlCursor() {
// Example shows how to do a cursor select

// For testing, check if database tests are disabled
if oci8.TestDisableDatabase || oci8.TestDisableDestructive {
fmt.Println(3)
return
}

oci8.Driver.Logger = log.New(os.Stderr, "oci8 ", log.Ldate|log.Ltime|log.LUTC|log.Lshortfile)

var openString string
// [username/[password]@]host[:port][/service_name][?param1=value1&...&paramN=valueN]
if len(oci8.TestUsername) > 0 {
if len(oci8.TestPassword) > 0 {
openString = oci8.TestUsername + "/" + oci8.TestPassword + "@"
} else {
openString = oci8.TestUsername + "@"
}
}
openString += oci8.TestHostValid

// A normal simple Open to localhost would look like:
// db, err := sql.Open("oci8", "127.0.0.1")
// For testing, need to use additional variables
db, err := sql.Open("oci8", openString)
if err != nil {
fmt.Printf("Open error is not nil: %v", err)
return
}
if db == nil {
fmt.Println("db is nil")
return
}

// defer close database
defer func() {
err = db.Close()
if err != nil {
fmt.Println("Close error is not nil:", err)
}
}()

ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second)
err = db.PingContext(ctx)
cancel()
if err != nil {
fmt.Println("PingContext error is not nil:", err)
return
}

var rows *sql.Rows
ctx, cancel = context.WithTimeout(context.Background(), 55*time.Second)
defer cancel()
rows, err = db.QueryContext(ctx, "select 1, cursor(select 2 from dual union select 3 from dual) from dual")
if err != nil {
fmt.Println("QueryContext error is not nil:", err)
return
}

// defer close rows
defer func() {
err = rows.Close()
if err != nil {
fmt.Println("Close error is not nil:", err)
}
}()

if !rows.Next() {
fmt.Println("no Next rows")
return
}

var aInt int64
var subRows *sql.Rows
err = rows.Scan(&aInt, &subRows)
if err != nil {
fmt.Println("Scan error is not nil:", err)
return
}

if aInt != 1 {
fmt.Println("aInt != 1")
return
}
if subRows == nil {
fmt.Println("subRows is nil")
return
}

if !subRows.Next() {
fmt.Println("no Next subRows")
return
}

err = subRows.Scan(&aInt)
if err != nil {
fmt.Println("Scan error is not nil:", err)
return
}

if aInt != 2 {
fmt.Println("aInt != 2")
return
}

if !subRows.Next() {
fmt.Println("no Next subRows")
return
}

err = subRows.Scan(&aInt)
if err != nil {
fmt.Println("Scan error is not nil:", err)
return
}

if aInt != 3 {
fmt.Println("aInt != 3")
return
}

if subRows.Next() {
fmt.Println("has Next rows")
return
}

err = subRows.Err()
if err != nil {
fmt.Println("Err error is not nil:", err)
return
}

if rows.Next() {
fmt.Println("has Next rows")
return
}

err = rows.Err()
if err != nil {
fmt.Println("Err error is not nil:", err)
return
}

fmt.Println(aInt)

// output: 3
}
Loading

0 comments on commit 16089af

Please sign in to comment.