Skip to content
An Oracle database driver for the Go programming language.
Go Other
  1. Go 99.3%
  2. Other 0.7%
Branch: master
Clone or download

Latest commit

Latest commit 15da1e2 Aug 9, 2018

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.idea Added statement configuration parameter forceMaxFetchLen Oct 25, 2017
contrib megacheck Dec 31, 2017
date replace all remaining ora.v3 to ora.v4 Mar 3, 2017
examples megacheck Dec 31, 2017
glg revised var name May 24, 2015
lg Move associative array calls to ExeP and PrepAndExeP Apr 29, 2016
lg15 Change lg15 Info level to Debug May 27, 2015
num megacheck Dec 31, 2017
tstlg go vet Jun 19, 2015
.gitattributes 💥🐫 Added .gitattributes & .gitignore files Oct 29, 2014
.gitignore test-one-by-one to segment some (10) tests in one run Nov 24, 2016
CHANGELOG.md Add [Un]Marshal{JSON,Text} to OCINum Dec 31, 2017
LICENSE Initial commit Oct 29, 2014
README.md Update README.md Aug 9, 2018
appveyor.yml small fix in README Dec 29, 2016
bndBfile.go begin implementing named parameters Dec 5, 2016
bndBin.go begin implementing named parameters Dec 5, 2016
bndBinSlice.go bndBinSlice: allow zero-length slice Feb 16, 2017
bndBool.go begin implementing named parameters Dec 5, 2016
bndBoolPtr.go begin implementing named parameters Dec 5, 2016
bndBoolSlice.go begin implementing named parameters Dec 5, 2016
bndDate.go begin implementing named parameters Dec 5, 2016
bndDatePtr.go begin implementing named parameters Dec 5, 2016
bndDateSlice.go nullInds must be reset to 0 Oct 19, 2017
bndFloat32.go begin implementing named parameters Dec 5, 2016
bndFloat32Ptr.go Nullable output parameters for String, Int*, Float* May 23, 2017
bndFloat32Slice.go nullInds must be reset to 0 Oct 19, 2017
bndFloat64.go begin implementing named parameters Dec 5, 2016
bndFloat64Ptr.go Nullable output parameters for String, Int*, Float* May 23, 2017
bndFloat64Slice.go nullInds must be reset to 0 Oct 19, 2017
bndInt16.go begin implementing named parameters Dec 5, 2016
bndInt16Ptr.go Nullable output parameters for String, Int*, Float* May 23, 2017
bndInt16Slice.go nullInds must be reset to 0 Oct 19, 2017
bndInt32.go begin implementing named parameters Dec 5, 2016
bndInt32Ptr.go Nullable output parameters for String, Int*, Float* May 23, 2017
bndInt32Slice.go nullInds must be reset to 0 Oct 19, 2017
bndInt64.go begin implementing named parameters Dec 5, 2016
bndInt64Ptr.go Nullable output parameters for String, Int*, Float* May 23, 2017
bndInt64Slice.go nullInds must be reset to 0 Oct 19, 2017
bndInt8.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
bndInt8Ptr.go Nullable output parameters for String, Int*, Float* May 23, 2017
bndInt8Slice.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
bndIntervalDS.go begin implementing named parameters Dec 5, 2016
bndIntervalDSSlice.go begin implementing named parameters Dec 5, 2016
bndIntervalYM.go begin implementing named parameters Dec 5, 2016
bndIntervalYMSlice.go begin implementing named parameters Dec 5, 2016
bndLob.go megacheck Dec 31, 2017
bndLobPtr.go Use AL32UTF8 for NCLOBs, too Jun 22, 2017
bndLobSlice.go begin implementing named parameters Dec 5, 2016
bndNil.go begin implementing named parameters Dec 5, 2016
bndNumString.go megacheck Dec 31, 2017
bndNumStringPtr.go Alleviate side effect of TM9 number format Apr 18, 2017
bndNumStringSlice.go begin implementing named parameters Dec 5, 2016
bndOCINum.go Allow OCINum as bind parameter Apr 11, 2017
bndOCINumPtr.go Allow OCINum as bind parameter Apr 11, 2017
bndOCINumSlice.go Allow OCINum as bind parameter Apr 11, 2017
bndRset.go vet Apr 19, 2017
bndString.go NCLOB may be returned as UTF-16 Mar 2, 2017
bndStringPtr.go Nullable output parameters for String, Int*, Float* May 23, 2017
bndStringSlice.go nullInds must be reset to 0 Oct 19, 2017
bndTime.go begin implementing named parameters Dec 5, 2016
bndTimePtr.go begin implementing named parameters Dec 5, 2016
bndTimeSlice.go megacheck Dec 31, 2017
bndUint16.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
bndUint16Ptr.go document need of -lnnz11 for Instant Client 11.2 Jul 23, 2017
bndUint16Slice.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
bndUint32.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
bndUint32Ptr.go document need of -lnnz11 for Instant Client 11.2 Jul 23, 2017
bndUint32Slice.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
bndUint64.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
bndUint64Ptr.go document need of -lnnz11 for Instant Client 11.2 Jul 23, 2017
bndUint64Slice.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
bndUint8.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
bndUint8Ptr.go document need of -lnnz11 for Instant Client 11.2 Jul 23, 2017
bndUint8Slice.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
bnd_hlp.go megacheck Dec 31, 2017
conn.go add ORA-28547 to maybeBadConn - fixes #217 Sep 20, 2017
conn_go1_7.go Make PrepareContext return driver.ErrBadConn if needed Mar 4, 2017
conn_go1_8.go fix race condition when context is cancelled after function returns Nov 21, 2017
const.go Allow OCINum as bind parameter Apr 11, 2017
ctx.go add WithStmtCfg - tackles #135 Dec 1, 2016
defBfile.go move def.allocated into arrHlp and use it in all defXxx.go Nov 13, 2017
defBool.go use Pool in arrHlp Apr 29, 2017
defDate.go use Pool in arrHlp Apr 29, 2017
defFloat32.go use Pool in arrHlp Apr 29, 2017
defFloat64.go use Pool in arrHlp Apr 29, 2017
defInt16.go nullInds must be reset to 0 Oct 19, 2017
defInt32.go nullInds must be reset to 0 Oct 19, 2017
defInt64.go nullInds must be reset to 0 Oct 19, 2017
defInt8.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
defIntervalDS.go move def.allocated into arrHlp and use it in all defXxx.go Nov 13, 2017
defIntervalYM.go move def.allocated into arrHlp and use it in all defXxx.go Nov 13, 2017
defLob.go megacheck Dec 31, 2017
defLongRaw.go use Pool in arrHlp Apr 29, 2017
defOCINum.go nullInds must be reset to 0 Oct 19, 2017
defRaw.go use Pool in arrHlp Apr 29, 2017
defRowid.go use Pool in arrHlp Apr 29, 2017
defRset.go Revert "autoClose, but not enough for #233" Nov 16, 2017
defString.go Add test for issue #192 May 16, 2017
defTime.go move def.allocated into arrHlp and use it in all defXxx.go Nov 13, 2017
defUint16.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
defUint32.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
defUint64.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
defUint8.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
doc.go fix documentation about -lnnz11. Fixes #242 Jan 18, 2018
drv.go Remove pool from behind database/sql connections (that's double buffe… Dec 22, 2016
drvExecResult.go RETURNING .. INTO does not allow column alias Dec 19, 2015
drvQueryResult.go make DrvQueryResult.Close close the query - as suggested in #238 Dec 7, 2017
drvStmt.go atomic Cfg/SetCfg Nov 20, 2016
drvStmt_go1_8.go fix race condition when context is cancelled after function returns Nov 21, 2017
env.go Changing errant fmt.Printf to env.log Aug 5, 2017
gen.go Add *[]{,u}int{16,32,64} and *[]float32, *[]floa64 slices Oct 4, 2016
list.go megacheck Dec 31, 2017
logger.go removed ora.Log, Made EmpLgr exportable in case user would like to in… Jun 29, 2015
ora.go bump version Nov 7, 2017
orm.go megacheck Dec 31, 2017
pool.go megacheck Dec 31, 2017
rset.go add locks to arrHlp, but this is not enough for #244 Jan 25, 2018
rsetCfg.go vet Apr 19, 2017
rsetCfg_test.go Add NumString, a GoString-OCINumber bnd/def Feb 20, 2016
ses.go Break should not panic on nil session Nov 20, 2017
srv.go DRCPool requires Min/Max/Incr Mar 30, 2017
stmt.go Revert "autoClose, but not enough for #233" Nov 16, 2017
stmtCfg.go allow specifying statemenet-specific fetching lengths - both for "nor… Oct 25, 2017
stmt_go1.go nameAndValue to separate function Dec 5, 2016
stmt_go1_8.go delete debugging Println from NumInput Dec 7, 2016
test-one-by-one.sh test-one-by-one: time runs Nov 24, 2016
test.sh replace all remaining ora.v3 to ora.v4 Mar 3, 2017
tx.go fix data race in Tx caused by typo May 16, 2017
type.go megacheck Dec 31, 2017
util.go megacheck Dec 31, 2017
util_arr.go Revert "add locks to arrHlp, but this is not enough for #244" Jan 25, 2018
util_test.go Use bytesArena (sync.Pool-based arena) for []byte allocations Oct 8, 2016
version.c cannot pass unsafe.Pointer to Go function Dec 5, 2016
version.h RowsAffected: may be 4 bytes, not 8, depending on Oracle version Dec 19, 2016
z_benchmem_test.go DRCPool requires Min/Max/Incr Mar 30, 2017
z_bfile_session_test.go Bump version to v4.0.0, generate README.md with godocdown Nov 23, 2016
z_bool_session_test.go simplify pool insteadClose and no global testSes Nov 13, 2017
z_bytes_session_test.go LOB tests cannot be run with Parallel()! Nov 24, 2016
z_db_go1_8_test.go context: simplify cancel logic May 19, 2017
z_db_test.go add test case for #244 Jan 25, 2018
z_environment_test.go free ocisrv in OpenSrv on error Aug 2, 2017
z_example_test.go megacheck Dec 31, 2017
z_interval_session_test.go Bump version to v4.0.0, generate README.md with godocdown Nov 23, 2016
z_issue131.go add pprof to z_issue131.go Dec 8, 2016
z_issue197.go simplify z_issue197 Jun 22, 2017
z_lob_test.go TestLobIssue237 for #237 Nov 27, 2017
z_numeric_session_test.go simplify pool insteadClose and no global testSes Nov 13, 2017
z_oracle_test.go megacheck Dec 31, 2017
z_plsarr_session_test.go simplify pool insteadClose and no global testSes Nov 13, 2017
z_race.go megacheck Dec 31, 2017
z_reconn.go vet Apr 19, 2017
z_resultSet_session_test.go simplify pool insteadClose and no global testSes Nov 13, 2017
z_rowid_session_test.go simplify pool insteadClose and no global testSes Nov 13, 2017
z_server_session_test.go Bump version to v4.0.0, generate README.md with godocdown Nov 23, 2016
z_session_test.go Revert "Rset: autoClose in closeWithRemove" Nov 16, 2017
z_statement_session_test.go megacheck Dec 31, 2017
z_string_session_test.go simplify pool insteadClose and no global testSes Nov 13, 2017
z_time_session_test.go tests: don't reuse the same value (slice) Nov 24, 2016

README.md

ora

-- import "gopkg.in/rana/ora.v4"

Package ora implements an Oracle database driver.

Golang Oracle Database Driver

TL;DR; just use it

import (
	"database/sql"

	_ "gopkg.in/rana/ora.v4"
)

func main() {
	db, err := sql.Open("ora", "user/passw@host:port/sid")
	defer db.Close()

	// Set timeout (Go 1.8)
	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	// Set prefetch count (Go 1.8)
	ctx = ora.WithStmtCfg(ctx, ora.Cfg().StmtCfg.SetPrefetchCount(50000))
	rows, err := db.QueryContext(ctx, "SELECT * FROM user_objects")
	defer rows.Close()
}

Call stored procedure with OUT parameters:

import (
	"gopkg.in/rana/ora.v4"
)

func main() {
	env, srv, ses, err := ora.NewEnvSrvSes("user/passw@host:port/sid")
	if err != nil {
		log.Fatal(err)
	}
	defer env.Close()
	defer srv.Close()
	defer ses.Close()

	var user string
	if _, err = ses.PrepAndExe("BEGIN :1 := SYS_CONTEXT('USERENV', :2); END;", &res, "SESSION_USER"); err != nil {
		log.Fatal(err)
	}
	log.Printf("user: %q", user)
}

Background

An Oracle database may be accessed through the database/sql package or through the ora package directly. database/sql offers connection pooling, thread safety, a consistent API to multiple database technologies and a common set of Go types. The ora package offers additional features including pointers, slices, nullable types, numerics of various sizes, Oracle-specific types, Go return type configuration, and Oracle abstractions such as environment, server and session.

The ora package is written with the Oracle Call Interface (OCI) C-language libraries provided by Oracle. The OCI libraries are a standard for client application communication and driver communication with Oracle databases.

The ora package has been verified to work with:

  • Oracle Standard 11g (11.2.0.4.0), Linux x86_64 (RHEL6)

  • Oracle Enterprise 12c (12.1.0.1.0), Windows 8.1 and AMD64.

---

---

Installation

Minimum requirements are Go 1.3 with CGO enabled, a GCC C compiler, and Oracle 11g (11.2.0.4.0) or Oracle Instant Client (11.2.0.4.0).

Install Oracle or Oracle Instant Client.

Copy the oci8.pc from the contrib folder (or the one for your system, maybe tailored to your specific locations) to a folder in $PKG_CONFIG_PATH or a system folder, such as

cp -aL contrib/oci8.pc /usr/local/lib/pkgconfig/oci8.pc

The ora package has no external Go dependencies and is available on GitHub and gopkg.in:

go get gopkg.in/rana/ora.v4

WARNING: If you have Oracle Instant Client 11.2, you'll need to add "-lnnz11" to the list of linked libs! Otherwise, you may encounter "undefined reference to `nzosSCSP_SetCertSelectionParams' " errors. Oracle Instant Client 12.1 does not need this.

Data Types

The ora package supports all built-in Oracle data types. The supported Oracle built-in data types are NUMBER, BINARY_DOUBLE, BINARY_FLOAT, FLOAT, DATE, TIMESTAMP, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH LOCAL TIME ZONE, INTERVAL YEAR TO MONTH, INTERVAL DAY TO SECOND, CHAR, NCHAR, VARCHAR, VARCHAR2, NVARCHAR2, LONG, CLOB, NCLOB, BLOB, LONG RAW, RAW, ROWID and BFILE. SYS_REFCURSOR is also supported.

Oracle does not provide a built-in boolean type. Oracle provides a single-byte character type. A common practice is to define two single-byte characters which represent true and false. The ora package adopts this approach. The oracle package associates a Go bool value to a Go rune and sends and receives the rune to a CHAR(1 BYTE) column or CHAR(1 CHAR) column.

The default false rune is zero '0'. The default true rune is one '1'. The bool rune association may be configured or disabled when directly using the ora package but not with the database/sql package.

SQL Placeholder Syntax

Within a SQL string a placeholder may be specified to indicate where a Go variable is placed. The SQL placeholder is an Oracle identifier, from 1 to 30 characters, prefixed with a colon (:). For example:

// example Oracle placeholder uses a colon
INSERT INTO T1 (C1) VALUES (:C1)

Placeholders within a SQL statement are bound by position. The actual name is not used by the ora package driver e.g., placeholder names :c1, :1, or :xyz are treated equally.

LastInsertId

The database/sql package provides a LastInsertId method to return the last inserted row's id. Oracle does not provide such functionality, but if you append ... RETURNING col /*LastInsertId*/ to your SQL, then it will be presented as LastInsertId. Note that you have to mark with a /*LastInsertId*/ (case insensitive) your RETURNING part, to allow ora to return the last column as LastInsertId(). That column must fit in int64, though!

Working With The Sql Package

You may access an Oracle database through the database/sql package. The database/sql package offers a consistent API across different databases, connection pooling, thread safety and a set of common Go types. database/sql makes working with Oracle straight-forward.

The ora package implements interfaces in the database/sql/driver package enabling database/sql to communicate with an Oracle database. Using database/sql ensures you never have to call the ora package directly.

When using database/sql, the mapping between Go types and Oracle types may be changed slightly. The database/sql package has strict expectations on Go return types. The Go-to-Oracle type mapping for database/sql is:

Go type		Oracle type

int64		NUMBER°, BINARY_DOUBLE, BINARY_FLOAT, FLOAT

float64		NUMBER¹, BINARY_DOUBLE, BINARY_FLOAT, FLOAT

time.Time	TIMESTAMP, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH LOCAL TIME ZONE, DATE

string		CHAR², NCHAR, VARCHAR, VARCHAR2, NVARCHAR2, LONG, CLOB, NCLOB

bool		CHAR(1 BYTE)³, CHAR(1 CHAR)³

[]byte		BLOB, LONG RAW, RAW

° A select-list column defined as an Oracle NUMBER with zero scale e.g.,
NUMBER(10,0) is returned as an int64. Either int64 or float64 may be inserted
into a NUMBER column with zero scale. float64 insertion will have its fractional
part truncated.

¹ A select-list column defined as an Oracle NUMBER with a scale greater than
zero e.g., NUMBER(10,4) is returned as a float64. Either int64 or float64 may
be inserted into a NUMBER column with a scale greater than zero.

² A select-list column defined as an Oracle CHAR with a length greater than 1
e.g., CHAR(2 BYTE) or CHAR(2 CHAR) is returned as a string. A Go string of any
length up to the column max length may be inserted into the CHAR column.

³ The Go bool value false is mapped to the zero rune '0'. The Go bool value
true is mapped to the one rune '1'.

The "ora" driver is automatically registered for use with sql.Open, but you can call ora.SetCfg to set the used configuration options including statement configuration and Rset configuration.

    func init() {
		drvCfg := ora.Cfg()
		drvCfg.FalseRune = 'N'
		drvCfg.TrueRune = 'Y'
		drvCfg.TrueRune = 'Y'
		ora.SetCfg(drvCfg)
	}

When configuring the driver for use with database/sql, keep in mind that database/sql has strict Go type-to-Oracle type mapping expectations.

Working With The Oracle Package Directly

The ora package allows programming with pointers, slices, nullable types, numerics of various sizes, Oracle-specific types, Go return type configuration, and Oracle abstractions such as environment, server and session. When working with the ora package directly, the API is slightly different than database/sql.

When using the ora package directly, the mapping between Go types and Oracle types may be changed. The Go-to-Oracle type mapping for the ora package is:

Go type				Oracle type

int64, int32, int16, int8	NUMBER°, BINARY_DOUBLE, BINARY_FLOAT, FLOAT
uint64, uint32, uint16, uint8
Int64, Int32, Int16, Int8
Uint64, Uint32, Uint16, Uint8
*int64, *int32, *int16, *int8
*uint64, *uint32, *uint16, *uint8
[]int64, []int32, []int16, []int8
[]uint64, []uint32, []uint16, []uint8
[]Int64, []Int32, []Int16, []Int8
[]Uint64, []Uint32, []Uint16, []Uint8

float64, float32		NUMBER¹, BINARY_DOUBLE, BINARY_FLOAT, FLOAT
Float64, Float32
*float64, *float32
[]float64, []float32
[]Float64, []Float32

time.Time			TIMESTAMP, TIMESTAMP WITH TIME ZONE,
Time				TIMESTAMP WITH LOCAL TIME ZONE, DATE
*time.Time
[]time.Time
[]Time

string				CHAR², NCHAR, VARCHAR, VARCHAR2,
String				NVARCHAR2, LONG, CLOB, NCLOB, ROWID
*string
[]string
[]String

bool				CHAR(1 BYTE)³, CHAR(1 CHAR)³
Bool
*bool
[]bool
[]Bool

[]byte, [][]byte	BLOB

Lob, []Lob, *Lob	BLOB, CLOB

Raw, []Raw			RAW, LONG RAW

IntervalYM			INTERVAL MONTH TO YEAR
[]IntervalYM

IntervalDS			INTERVAL DAY TO SECOND
[]IntervalDS

Bfile				BFILE

° A select-list column defined as an Oracle NUMBER with zero scale e.g.,
NUMBER(10,0) is returned as an int64 by default. Integer and floating point
numerics may be inserted into a NUMBER column with zero scale. Inserting a
floating point numeric will have its fractional part truncated.

¹ A select-list column defined as an Oracle NUMBER with a scale greater than
zero e.g., NUMBER(10,4) is returned as a float64 by default. Integer and
floating point numerics may be inserted into a NUMBER column with a scale
greater than zero.

² A select-list column defined as an Oracle CHAR with a length greater than 1
e.g., CHAR(2 BYTE) or CHAR(2 CHAR) is returned as a string. A Go string of any
length up to the column max length may be inserted into the CHAR column.

³ The Go bool value false is mapped to the zero rune '0'. The Go bool value
true is mapped to the one rune '1'.

An example of using the ora package directly:

package main

import (
	"fmt"
	"gopkg.in/rana/ora.v4"
)

func main() {
	// example usage of the ora package driver
	// connect to a server and open a session
	env, err := ora.OpenEnv()
	defer env.Close()
	if err != nil {
		panic(err)
	}
	srvCfg := ora.SrvCfg{Dblink: "orcl"}
	srv, err := env.OpenSrv(&srvCfg)
	defer srv.Close()
	if err != nil {
		panic(err)
	}
	sesCfg := ora.SesCfg{
		Username: "test",
		Password: "test",
	}
	ses, err := srv.OpenSes(sesCfg)
	defer ses.Close()
	if err != nil {
		panic(err)
	}

	// create table
	tableName := "t1"
	stmtTbl, err := ses.Prep(fmt.Sprintf("CREATE TABLE %v "+
		"(C1 NUMBER(19,0) GENERATED ALWAYS AS IDENTITY "+
		"(START WITH 1 INCREMENT BY 1), C2 VARCHAR2(48 CHAR))", tableName))
	defer stmtTbl.Close()
	if err != nil {
		panic(err)
	}
	rowsAffected, err := stmtTbl.Exe()
	if err != nil {
		panic(err)
	}
	fmt.Println(rowsAffected)

	// begin first transaction
	tx1, err := ses.StartTx()
	if err != nil {
		panic(err)
	}

	// insert record
	var id uint64
	str := "Go is expressive, concise, clean, and efficient."
	stmtIns, err := ses.Prep(fmt.Sprintf(
		"INSERT INTO %v (C2) VALUES (:C2) RETURNING C1 INTO :C1", tableName))
	defer stmtIns.Close()
	rowsAffected, err = stmtIns.Exe(str, &id)
	if err != nil {
		panic(err)
	}
	fmt.Println(rowsAffected)

	// insert nullable String slice
	a := make([]ora.String, 4)
	a[0] = ora.String{Value: "Its concurrency mechanisms make it easy to"}
	a[1] = ora.String{IsNull: true}
	a[2] = ora.String{Value: "It's a fast, statically typed, compiled"}
	a[3] = ora.String{Value: "One of Go's key design goals is code"}
	stmtSliceIns, err := ses.Prep(fmt.Sprintf(
		"INSERT INTO %v (C2) VALUES (:C2)", tableName))
	defer stmtSliceIns.Close()
	if err != nil {
		panic(err)
	}
	rowsAffected, err = stmtSliceIns.Exe(a)
	if err != nil {
		panic(err)
	}
	fmt.Println(rowsAffected)

	// fetch records
	stmtQry, err := ses.Prep(fmt.Sprintf(
		"SELECT C1, C2 FROM %v", tableName))
	defer stmtQry.Close()
	if err != nil {
		panic(err)
	}
	rset, err := stmtQry.Qry()
	if err != nil {
		panic(err)
	}
	for rset.Next() {
		fmt.Println(rset.Row[0], rset.Row[1])
	}
	if err := rset.Err(); err != nil {
		panic(err)
	}

	// commit first transaction
	err = tx1.Commit()
	if err != nil {
		panic(err)
	}

	// begin second transaction
	tx2, err := ses.StartTx()
	if err != nil {
		panic(err)
	}
	// insert null String
	nullableStr := ora.String{IsNull: true}
	stmtTrans, err := ses.Prep(fmt.Sprintf(
		"INSERT INTO %v (C2) VALUES (:C2)", tableName))
	defer stmtTrans.Close()
	if err != nil {
		panic(err)
	}
	rowsAffected, err = stmtTrans.Exe(nullableStr)
	if err != nil {
		panic(err)
	}
	fmt.Println(rowsAffected)
	// rollback second transaction
	err = tx2.Rollback()
	if err != nil {
		panic(err)
	}

	// fetch and specify return type
	stmtCount, err := ses.Prep(fmt.Sprintf(
		"SELECT COUNT(C1) FROM %v WHERE C2 IS NULL", tableName), ora.U8)
	defer stmtCount.Close()
	if err != nil {
		panic(err)
	}
	rset, err = stmtCount.Qry()
	if err != nil {
		panic(err)
	}
	row := rset.NextRow()
	if row != nil {
		fmt.Println(row[0])
	}
	if err := rset.Err(); err != nil {
		panic(err)
	}

	// create stored procedure with sys_refcursor
	stmtProcCreate, err := ses.Prep(fmt.Sprintf(
		"CREATE OR REPLACE PROCEDURE PROC1(P1 OUT SYS_REFCURSOR) AS BEGIN "+
			"OPEN P1 FOR SELECT C1, C2 FROM %v WHERE C1 > 2 ORDER BY C1; "+
			"END PROC1;",
		tableName))
	defer stmtProcCreate.Close()
	rowsAffected, err = stmtProcCreate.Exe()
	if err != nil {
		panic(err)
	}

	// call stored procedure
	// pass *Rset to Exe to receive the results of a sys_refcursor
	stmtProcCall, err := ses.Prep("CALL PROC1(:1)")
	defer stmtProcCall.Close()
	if err != nil {
		panic(err)
	}
	procRset := &ora.Rset{}
	rowsAffected, err = stmtProcCall.Exe(procRset)
	if err != nil {
		panic(err)
	}
	if procRset.IsOpen() {
		for procRset.Next() {
			fmt.Println(procRset.Row[0], procRset.Row[1])
		}
		if err := procRset.Err(); err != nil {
			panic(err)
		}
		fmt.Println(procRset.Len())
	}

	// Output:
	// 0
	// 1
	// 4
	// 1 Go is expressive, concise, clean, and efficient.
	// 2 Its concurrency mechanisms make it easy to
	// 3
	// 4 It's a fast, statically typed, compiled
	// 5 One of Go's key design goals is code
	// 1
	// 1
	// 3
	// 4 It's a fast, statically typed, compiled
	// 5 One of Go's key design goals is code
	// 3
}

Pointers may be used to capture out-bound values from a SQL statement such as an insert or stored procedure call. For example, a numeric pointer captures an identity value:

// given:
// CREATE TABLE T1 (
// C1 NUMBER(19,0) GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1),
// C2 VARCHAR2(48 CHAR))
var id int64
stmt, err = ses.Prep("INSERT INTO T1 (C2) VALUES ('GO') RETURNING C1 INTO :C1")
stmt.Exe(&id)

A string pointer captures an out parameter from a stored procedure:

// given:
// CREATE OR REPLACE PROCEDURE PROC1 (P1 OUT VARCHAR2) AS BEGIN P1 := 'GO'; END PROC1;
var str string
stmt, err = ses.Prep("CALL PROC1(:1)")
stmt.Exe(&str)

Slices may be used to insert multiple records with a single insert statement:

// insert one million rows with single insert statement
// given: CREATE TABLE T1 (C1 NUMBER)
values := make([]int64, 1000000)
for n, _ := range values {
	values[n] = int64(n)
}
rowsAffected, err := ses.PrepAndExe("INSERT INTO T1 (C1) VALUES (:C1)", values)

The ora package provides nullable Go types to support DML operations such as insert and select. The nullable Go types provided by the ora package are Int64, Int32, Int16, Int8, Uint64, Uint32, Uint16, Uint8, Float64, Float32, Time, IntervalYM, IntervalDS, String, Bool, Binary and Bfile. For example, you may insert nullable Strings and select nullable Strings:

// insert String slice
// given: CREATE TABLE T1 (C1 VARCHAR2(48 CHAR))
a := make([]ora.String, 5)
a[0] = ora.String{Value: "Go is expressive, concise, clean, and efficient."}
a[1] = ora.String{Value: "Its concurrency mechanisms make it easy to"}
a[2] = ora.String{IsNull: true}
a[3] = ora.String{Value: "It's a fast, statically typed, compiled"}
a[4] = ora.String{Value: "One of Go's key design goals is code"}
stmt, err = ses.Prep("INSERT INTO T1 (C1) VALUES (:C1)")
stmt.Exe(a)

// Specify OraS to Prep method to return ora.String values
// fetch records
stmt, err = ses.Prep("SELECT C1 FROM T1", OraS)
rset, err := stmt.Qry()
for rset.Next() {
	fmt.Println(rset.Row[0])
}

The Stmt.Prep method is variadic accepting zero or more GoColumnType which define a Go return type for a select-list column. For example, a Prep call can be configured to return an int64 and a nullable Int64 from the same column:

// given: create table t1 (c1 number)
stmt, err = ses.Prep("SELECT C1, C1 FROM T1", ora.I64, ora.OraI64)
rset, err := stmt.Qry()
for rset.Next() {
	fmt.Println(rset.Row[0], rset.Row[1])
}

Go numerics of various sizes are supported in DML operations. The ora package supports int64, int32, int16, int8, uint64, uint32, uint16, uint8, float64 and float32. For example, you may insert a uint16 and select numerics of various sizes:

// insert uint16
// given: create table t1 (c1 number)
value := uint16(9)
stmt, err = ses.Prep("INSERT INTO T1 (C1) VALUES (:C1)")
stmt.Exe(value)

// select numerics of various sizes from the same column
stmt, err = ses.Prep(
	"SELECT C1, C1, C1, C1, C1, C1, C1, C1, C1, C1, FROM T1",
	ora.I64, ora.I32, ora.I16, ora.I8, ora.U64, ora.U32, ora.U16, ora.U8,
	ora.F64, ora.F32)
rset, err := stmt.Qry()
row := rset.NextRow()

If a non-nullable type is defined for a nullable column returning null, the Go type's zero value is returned.

GoColumnTypes defined by the ora package are:

Go type		GoColumnType

int64		I64

int32		I32

int16		I16

int8		I8

uint64		U64

uint32		U32

uint16		U16

uint8		U8

float64		F64

Int64		OraI64

Int32		OraI32

Int16		OraI16

Int8		OraI8

Uint64		OraU64

Uint32		OraU32

Uint16		OraU16

Uint8		OraU8

Float64		OraF64

Float32		OraF32

time.Time	T

Time		OraT

string		S

String		OraS

bool		B

Bool		OraB

[]byte		Bin

Raw			Bin

Lob°		Bin or S

default¹	D

° Lob will return binary data if the Oracle column is a BLOB; otherwise, Lob
  will return a string if the Oracle column is a CLOB.

¹ D represents a default mapping between a select-list column and a Go type.
The default mapping is defined in RsetCfg.

When Stmt.Prep doesn't receive a GoColumnType, or receives an incorrect GoColumnType, the default value defined in RsetCfg is used.

EnvCfg, SrvCfg, SesCfg, StmtCfg and RsetCfg are the main configuration structs. EnvCfg configures aspects of an Env. SrvCfg configures aspects of a Srv. SesCfg configures aspects of a Ses. StmtCfg configures aspects of a Stmt. RsetCfg configures aspects of Rset. StmtCfg and RsetCfg have the most options to configure. RsetCfg defines the default mapping between an Oracle select-list column and a Go type. StmtCfg may be set in an EnvCfg, SrvCfg, SesCfg and StmtCfg. RsetCfg may be set in a Stmt.

EnvCfg.StmtCfg, SrvCfg.StmtCfg, SesCfg.StmtCfg may optionally be specified to configure a statement. If StmtCfg isn't specified default values are applied. EnvCfg.StmtCfg, SrvCfg.StmtCfg, SesCfg.StmtCfg cascade to new descendent structs. When ora.OpenEnv() is called a specified EnvCfg is used or a default EnvCfg is created. Creating a Srv with env.OpenSrv() will use SrvCfg.StmtCfg if it is specified; otherwise, EnvCfg.StmtCfg is copied by value to SrvCfg.StmtCfg. Creating a Ses with srv.OpenSes() will use SesCfg.StmtCfg if it is specified; otherwise, SrvCfg.StmtCfg is copied by value to SesCfg.StmtCfg. Creating a Stmt with ses.Prep() will use SesCfg.StmtCfg if it is specified; otherwise, a new StmtCfg with default values is set on the Stmt. Call Stmt.Cfg() to change a Stmt's configuration.

An Env may contain multiple Srv. A Srv may contain multiple Ses. A Ses may contain multiple Stmt. A Stmt may contain multiple Rset.

// StmtCfg cascades to descendent structs
// EnvCfg -> SrvCfg -> SesCfg -> StmtCfg -> RsetCfg

Setting a RsetCfg on a StmtCfg does not cascade through descendent structs. Configuration of Stmt.Cfg takes effect prior to calls to Stmt.Exe and Stmt.Qry; consequently, any updates to Stmt.Cfg after a call to Stmt.Exe or Stmt.Qry are not observed.

One configuration scenario may be to set a server's select statements to return nullable Go types by default:

sc := &ora.SrvCfg{Dblink: "orcl"}
sc.Dblink = "orcl"
cfg := NewStmtCfg().
	SetNumberInt(ora.OraI64).
	SetNumberFloat(ora.OraF64).
	SetBinaryDouble(ora.OraF64).
	SetBinaryFloat(ora.OraF64).
	SetFloat(ora.OraF64).
	SetDate(ora.OraT).
	SetTimestamp(ora.OraT).
	SetTimestampTz(ora.OraT).
	SetTimestampLtz(ora.OraT).
	SetChar1(ora.OraS).
	SetVarchar(ora.OraS).
	SetLong(ora.OraS).
	SetClob(ora.OraS).
	SetBlob(ora.OraBin).
	SetRaw(ora.OraBin).
	SetLongRaw(ora.OraBin).
	SetFetchLen(100).
	SetLOBFetchLen(100)
sc.StmtCfg = cfg
srv, err := env.OpenSrv(sc)
// any new SesCfg.StmtCfg, StmtCfg.Cfg will receive this StmtCfg
// any new Rset will receive the StmtCfg.Rset configuration

Another scenario may be to configure the runes mapped to bool values:

// update StmtCfg to change the FalseRune and TrueRune inserted into the database
// given: CREATE TABLE T1 (C1 CHAR(1 BYTE))

stmt.Cfg().Char1(ora.OraB)

// insert 'false' record
var falseValue bool = false
stmt, err = ses.Prep("INSERT INTO T1 (C1) VALUES (:C1)")
stmt.Cfg().FalseRune = 'N'
stmt.Exe(falseValue)

// insert 'true' record
var trueValue bool = true
stmt, err = ses.Prep("INSERT INTO T1 (C1) VALUES (:C1)")
stmt.Cfg().TrueRune = 'Y'
stmt.Exe(trueValue)

// update RsetCfg to change the TrueRune
// used to translate an Oracle char to a Go bool
// fetch inserted records
stmt, err = ses.Prep("SELECT C1 FROM T1")
stmt.Cfg().Rset.TrueRune = 'Y'
rset, err := stmt.Qry()
for rset.Next() {
	fmt.Println(rset.Row[0])
}

Oracle-specific types offered by the ora package are ora.Rset, ora.IntervalYM, ora.IntervalDS, ora.Raw, ora.Lob and ora.Bfile. ora.Rset represents an Oracle SYS_REFCURSOR. ora.IntervalYM represents an Oracle INTERVAL YEAR TO MONTH. ora.IntervalDS represents an Oracle INTERVAL DAY TO SECOND. ora.Raw represents an Oracle RAW or LONG RAW. ora.Lob may represent an Oracle BLOB or Oracle CLOB. And ora.Bfile represents an Oracle BFILE. ROWID columns are returned as strings and don't have a unique Go type.

LOBs

The default for SELECTing [BC]LOB columns is a safe Bin or S, which means all the contents of the LOB is slurped into memory and returned as a []byte or string.

The DefaultLOBFetchLen says LOBs are prefetched only a minimal way, to minimize extra memory usage - you can override this using stmt.SetCfg(stmt.Cfg().SetLOBFetchLen(100)).

If you want more control, you can use ora.L in Prep, Qry or ses.SetCfg(ses.Cfg().SetBlob(ora.L)). But keep in mind that Oracle restricts the use of LOBs: it is forbidden to do ANYTHING while reading the LOB! No another query, no exec, no close of the Rset - even advance to the next record in the result set is forbidden!

Failing to adhere these rules results in "Invalid handle" and ORA-03127 errors.

You cannot start reading another LOB till you haven't finished reading the previous LOB, not even in the same row! Failing this results in ORA-24804!

For examples, see z_lob_test.go.

Rset

Rset is used to obtain Go values from a SQL select statement. Methods Rset.Next, Rset.NextRow, and Rset.Len are available. Fields Rset.Row, Rset.Err, Rset.Index, and Rset.ColumnNames are also available. The Next method attempts to load data from an Oracle buffer into Row, returning true when successful. When no data is available, or if an error occurs, Next returns false setting Row to nil. Any error in Next is assigned to Err. Calling Next increments Index and method Len returns the total number of rows processed. The NextRow method is convenient for returning a single row. NextRow calls Next and returns Row. ColumnNames returns the names of columns defined by the SQL select statement.

Rset has two usages. Rset may be returned from Stmt.Qry when prepared with a SQL select statement:

// given: CREATE TABLE T1 (C1 NUMBER, C2, CHAR(1 BYTE), C3 VARCHAR2(48 CHAR))
stmt, err = ses.Prep("SELECT C1, C2, C3 FROM T1")
rset, err := stmt.Qry()
for rset.Next() {
	fmt.Println(rset.Index, rset.Row[0], rset.Row[1], rset.Row[2])
}

Or, *Rset may be passed to Stmt.Exe when prepared with a stored procedure accepting an OUT SYS_REFCURSOR parameter:

// given:
// CREATE TABLE T1 (C1 NUMBER, C2 VARCHAR2(48 CHAR))
// CREATE OR REPLACE PROCEDURE PROC1(P1 OUT SYS_REFCURSOR) AS
// BEGIN OPEN P1 FOR SELECT C1, C2 FROM T1 ORDER BY C1; END PROC1;
stmt, err = ses.Prep("CALL PROC1(:1)")
rset := &ora.Rset{}
stmt.Exe(rset)
if rset.IsOpen() {
	for rset.Next() {
		fmt.Println(rset.Row[0], rset.Row[1])
	}
}

Stored procedures with multiple OUT SYS_REFCURSOR parameters enable a single Exe call to obtain multiple Rsets:

// given:
// CREATE TABLE T1 (C1 NUMBER, C2 VARCHAR2(48 CHAR))
// CREATE OR REPLACE PROCEDURE PROC1(P1 OUT SYS_REFCURSOR, P2 OUT SYS_REFCURSOR) AS
// BEGIN OPEN P1 FOR SELECT C1 FROM T1 ORDER BY C1; OPEN P2 FOR SELECT C2 FROM T1 ORDER BY C2;
// END PROC1;
stmt, err = ses.Prep("CALL PROC1(:1, :2)")
rset1 := &ora.Rset{}
rset2 := &ora.Rset{}
stmt.Exe(rset1, rset2)
// read from first cursor
if rset1.IsOpen() {
	for rset1.Next() {
		fmt.Println(rset1.Row[0])
	}
}
// read from second cursor
if rset2.IsOpen() {
	for rset2.Next() {
		fmt.Println(rset2.Row[0])
	}
}

The types of values assigned to Row may be configured in StmtCfg.Rset. For configuration to take effect, assign StmtCfg.Rset prior to calling Stmt.Qry or Stmt.Exe.

Rset prefetching may be controlled by StmtCfg.PrefetchRowCount and StmtCfg.PrefetchMemorySize. PrefetchRowCount works in coordination with PrefetchMemorySize. When PrefetchRowCount is set to zero only PrefetchMemorySize is used; otherwise, the minimum of PrefetchRowCount and PrefetchMemorySize is used. The default uses a PrefetchMemorySize of 134MB.

Opening and closing Rsets is managed internally. Rset does not have an Open method or Close method.

IntervalYM may be be inserted and selected:

// insert IntervalYM slice
// given: CREATE TABLE T1 (C1 INTERVAL YEAR TO MONTH)
a := make([]ora.IntervalYM, 5)
a[0] = ora.IntervalYM{Year: 1, Month: 1}
a[1] = ora.IntervalYM{Year: 99, Month: 9}
a[2] = ora.IntervalYM{IsNull: true}
a[3] = ora.IntervalYM{Year: -1, Month: -1}
a[4] = ora.IntervalYM{Year: -99, Month: -9}
stmt, err = ses.Prep("INSERT INTO T1 (C1) VALUES (:C1)")
stmt.Exe(a)

// query IntervalYM
stmt, err = ses.Prep("SELECT C1 FROM T1")
rset, err := stmt.Qry()
for rset.Next() {
	fmt.Println(rset.Row[0])
}

IntervalDS may be be inserted and selected:

// insert IntervalDS slice
// given: CREATE TABLE T1 (C1 INTERVAL DAY TO SECOND)
a := make([]ora.IntervalDS, 5)
a[0] = ora.IntervalDS{Day: 1, Hour: 1, Minute: 1, Second: 1, Nanosecond: 123456789}
a[1] = ora.IntervalDS{Day: 59, Hour: 59, Minute: 59, Second: 59, Nanosecond: 123456789}
a[2] = ora.IntervalDS{IsNull: true}
a[3] = ora.IntervalDS{Day: -1, Hour: -1, Minute: -1, Second: -1, Nanosecond: -123456789}
a[4] = ora.IntervalDS{Day: -59, Hour: -59, Minute: -59, Second: -59, Nanosecond: -123456789}
stmt, err = ses.Prep("INSERT INTO T1 (C1) VALUES (:C1)")
stmt.Exe(a)

// query IntervalDS
stmt, err = ses.Prep("SELECT C1 FROM T1")
rset, err := stmt.Qry()
for rset.Next() {
	fmt.Println(rset.Row[0])
}

Transactions on an Oracle server are supported. DML statements auto-commit unless a transaction has started:

// given: CREATE TABLE T1 (C1 NUMBER)

// rollback
tx, err := ses.StartTx()
stmt, err = ses.Prep("INSERT INTO T1 (C1) VALUES (3)")
stmt.Exe()
stmt, err = ses.Prep("INSERT INTO T1 (C1) VALUES (5)")
stmt.Exe()
tx.Rollback()

// commit
tx, err = ses.StartTx()
stmt, err = ses.Prep("INSERT INTO T1 (C1) VALUES (7)")
stmt.Exe()
stmt, err = ses.Prep("INSERT INTO T1 (C1) VALUES (9)")
stmt.Exe()
tx.Commit()

// fetch records
stmt, err = ses.Prep("SELECT C1 FROM T1")
rset, err := stmt.Qry()
for rset.Next() {
	fmt.Println(rset.Row[0])
}

Ses.PrepAndExe, Ses.PrepAndQry, Ses.Ins, Ses.Upd, and Ses.Sel are convenient one-line methods.

Ses.PrepAndExe offers a convenient one-line call to Ses.Prep and Stmt.Exe.

rowsAffected, err := ses.PrepAndExe("CREATE TABLE T1 (C1 NUMBER)")

Ses.PrepAndQry offers a convenient one-line call to Ses.Prep and Stmt.Qry.

rset, err := ses.PrepAndQry("SELECT CURRENT_TIMESTAMP FROM DUAL")

Ses.Ins composes, prepares and executes a sql INSERT statement. Ses.Ins is useful when you have to create and maintain a simple INSERT statement with a long list of columns. As table columns are added and dropped over the lifetime of a table Ses.Ins is easy to read and revise.

err = ses.Ins("T1",
	"C2", e.C2,
	"C3", e.C3,
	"C4", e.C4,
	"C5", e.C5,
	"C6", e.C6,
	"C7", e.C7,
	"C8", e.C8,
	"C9", e.C9,
	"C10", e.C10,
	"C11", e.C11,
	"C12", e.C12,
	"C13", e.C13,
	"C14", e.C14,
	"C15", e.C15,
	"C16", e.C16,
	"C17", e.C17,
	"C18", e.C18,
	"C19", e.C19,
	"C20", e.C20,
	"C21", e.C21,
	"C1", &e.C1)

Ses.Upd composes, prepares and executes a sql UPDATE statement. Ses.Upd is useful when you have to create and maintain a simple UPDATE statement with a long list of columns. As table columns are added and dropped over the lifetime of a table Ses.Upd is easy to read and revise.

err = ses.Upd("T1",
	"C2", e.C2*2,
	"C3", e.C3*2,
	"C4", e.C4*2,
	"C5", e.C5*2,
	"C6", e.C6*2,
	"C7", e.C7*2,
	"C8", e.C8*2,
	"C9", e.C9*2,
	"C10", e.C10*2,
	"C11", e.C11*2,
	"C12", e.C12*2,
	"C13", e.C13*2,
	"C14", e.C14*2,
	"C15", e.C15*2,
	"C16", e.C16*2,
	"C17", e.C17*2,
	"C18", e.C18*2,
	"C19", e.C19*2,
	"C20", e.C20*2,
	"C21", e.C21*2,
	"C1", e.C1)

Ses.Sel composes, prepares and queries a sql SELECT statement. Ses.Sel is useful when you have to create and maintain a simple SELECT statement with a long list of columns that have non-default GoColumnTypes. As table columns are added and dropped over the lifetime of a table Ses.Sel is easy to read and revise.

rset, err := ses.Sel("T1",
	"C1", ora.U64,
	"C2", ora.F64,
	"C3", ora.I8,
	"C4", ora.I16,
	"C5", ora.I32,
	"C6", ora.I64,
	"C7", ora.U8,
	"C8", ora.U16,
	"C9", ora.U32,
	"C10", ora.U64,
	"C11", ora.F32,
	"C12", ora.F64,
	"C13", ora.I8,
	"C14", ora.I16,
	"C15", ora.I32,
	"C16", ora.I64,
	"C17", ora.U8,
	"C18", ora.U16,
	"C19", ora.U32,
	"C20", ora.U64,
	"C21", ora.F32)

The Ses.Ping method checks whether the client's connection to an Oracle server is valid. A call to Ping requires an open Ses. Ping will return a nil error when the connection is fine:

// open a session before calling Ping
ses, _ := srv.OpenSes("username", "password")
err := ses.Ping()
if err == nil {
	fmt.Println("Ping successful")
}

The Srv.Version method is available to obtain the Oracle server version. A call to Version requires an open Ses:

// open a session before calling Version
ses, err := srv.OpenSes("username", "password")
version, err := srv.Version()
if version != "" && err == nil {
	fmt.Println("Received version from server")
}

Further code examples are available in the example file, test files and samples folder.

Logging

The ora package provides a simple ora.Logger interface for logging. Logging is disabled by default. Specify one of three optional built-in logging packages to enable logging; or, use your own logging package.

ora.Cfg().Log offers various options to enable or disable logging of specific ora driver methods. For example:

// enable logging of the Rset.Next method
ora.Cfg().Log.Rset.Next = true

To use the standard Go log package:

import (
	"gopkg.in/rana/ora.v4"
	"gopkg.in/rana/ora.v4/lg"
)

func main() {
	// use an optional log package for ora logging
	ora.Cfg().Log.Logger = lg.Log
}

which produces a sample log of:

ORA I 2015/05/23 16:54:44.615462 drv.go:411: OpenEnv 1
ORA I 2015/05/23 16:54:44.626443 drv.go:411: OpenEnv 2
ORA I 2015/05/23 16:54:44.627465 env.go:115: E2] OpenSrv (dbname orcl)
ORA I 2015/05/23 16:54:44.643449 env.go:150: E2] OpenSrv (srvId 1)
ORA I 2015/05/23 16:54:44.643449 srv.go:113: E2S1] OpenSes (username test)
ORA I 2015/05/23 16:54:44.665451 ses.go:163: E2S1S1] Prep: SELECT CURRENT_TIMESTAMP FROM DUAL
ORA I 2015/05/23 16:54:44.666451 rset.go:205: E2S1S1S1R0] open
ORA I 2015/05/23 16:54:44.666451 ses.go:74: E2S1S1] Close
ORA I 2015/05/23 16:54:44.666451 stmt.go:78: E2S1S1S1] Close
ORA I 2015/05/23 16:54:44.666451 rset.go:57: E2S1S1S1R0] close
ORA I 2015/05/23 16:54:44.666451 srv.go:63: E2S1] Close
ORA I 2015/05/23 16:54:44.667451 env.go:68: E2] Close

Messages are prefixed with 'ORA I' for information or 'ORA E' for an error. The log package is configured to write to os.Stderr by default. Use the ora/lg.Std type to configure an alternative io.Writer.

To use the glog package:

import (
	"flag"
	"gopkg.in/rana/ora.v4"
	"gopkg.in/rana/ora.v4/glg"
)

func main() {

	// parse flags for glog (required)
	// consider specifying cmd line arg -alsologtostderr=true
	flag.Parse()

	// use the optional glog package for ora logging
	cfg := ora.Cfg()
	cfg.Log.Logger = glg.Log
	ora.SetCfg(cfg)
}

which produces a sample log of:

I0523 17:31:41.702365   97708 drv.go:411] OpenEnv 1
I0523 17:31:41.728377   97708 drv.go:411] OpenEnv 2
I0523 17:31:41.728377   97708 env.go:115] E2] OpenSrv (dbname orcl)
I0523 17:31:41.741390   97708 env.go:150] E2] OpenSrv (srvId 1)
I0523 17:31:41.741390   97708 srv.go:113] E2S1] OpenSes (username test)
I0523 17:31:41.762366   97708 ses.go:163] E2S1S1] Prep: SELECT CURRENT_TIMESTAMP FROM DUAL
I0523 17:31:41.762366   97708 rset.go:205] E2S1S1S1R0] open
I0523 17:31:41.762366   97708 ses.go:74] E2S1S1] Close
I0523 17:31:41.762366   97708 stmt.go:78] E2S1S1S1] Close
I0523 17:31:41.762366   97708 rset.go:57] E2S1S1S1R0] close
I0523 17:31:41.763365   97708 srv.go:63] E2S1] Close
I0523 17:31:41.763365   97708 env.go:68] E2] Close

To use the log15 package:

import (
	"gopkg.in/rana/ora.v4"
	"gopkg.in/rana/ora.v4/lg15"
)
func main() {
	// use the optional log15 package for ora logging
	cfg := ora.Cfg()
	cfg.Log.Logger = lg15.Log
	ora.SetCfg(cfg)
}

which produces a sample log of:

t=2015-05-23T17:08:32-0700 lvl=info msg="OpenEnv 1" lib=ora
t=2015-05-23T17:08:32-0700 lvl=info msg="OpenEnv 2" lib=ora
t=2015-05-23T17:08:32-0700 lvl=info msg="E2] OpenSrv (dbname orcl)" lib=ora
t=2015-05-23T17:08:32-0700 lvl=info msg="E2] OpenSrv (srvId 1)" lib=ora
t=2015-05-23T17:08:32-0700 lvl=info msg="E2S1] OpenSes (username test)" lib=ora
t=2015-05-23T17:08:32-0700 lvl=info msg="E2S1S1] Prep: SELECT CURRENT_TIMESTAMP FROM DUAL" lib=ora
t=2015-05-23T17:08:32-0700 lvl=info msg="E2S1S1S1R0] open" lib=ora
t=2015-05-23T17:08:32-0700 lvl=info msg="E2S1S1] Close" lib=ora
t=2015-05-23T17:08:32-0700 lvl=info msg="E2S1S1S1] Close" lib=ora
t=2015-05-23T17:08:32-0700 lvl=info msg="E2S1S1S1R0] close" lib=ora
t=2015-05-23T17:08:32-0700 lvl=info msg="E2S1] Close" lib=ora
t=2015-05-23T17:08:32-0700 lvl=info msg="E2] Close" lib=ora

See https://github.com/rana/ora/tree/master/samples/lg15/main.go for sample code which uses the log15 package.

Test Database Setup

Tests are available and require some setup. Setup varies depending on whether the Oracle server is configured as a container database or non-container database. It's simpler to setup a non-container database. An example for each setup is explained.

Non-container test database setup steps:

// 1. login to an Oracle server with SqlPlus as sysdba:
SQLPLUS / AS SYSDBA

// 2. create a file for the test database use
CREATE TABLESPACE test_ts NOLOGGING DATAFILE 'test.dat' SIZE 100M AUTOEXTEND ON;

// 3. create a test database
CREATE USER test IDENTIFIED BY test DEFAULT TABLESPACE test_ts;

// 4. grant permissions to the database
GRANT CREATE SESSION, CREATE TABLE, CREATE SEQUENCE,
CREATE PROCEDURE, UNLIMITED TABLESPACE TO test;

// 5. increase the number allowable open cursors
ALTER SYSTEM SET OPEN_CURSORS = 400 SCOPE=BOTH;

// 6. create OS environment variables
// specify your_database_name; varies based on installation; may be 'orcl'
GO_ORA_DRV_TEST_DB = your_database_name
GO_ORA_DRV_TEST_USERNAME = test
GO_ORA_DRV_TEST_PASSWORD = test

Container test database setup steps:

// 1. login to an Oracle server with SqlPlus as sysdba:
SQLPLUS / AS SYSDBA

// 2. create a test pluggable database and permissions
// you will need to change the FILE_NAME_CONVERT file paths for your database installation
CREATE PLUGGABLE DATABASE go_driver_test
ADMIN USER test IDENTIFIED BY test
ROLES = (DBA)
FILE_NAME_CONVERT = ('d:\oracle\data\orcl\pdbseed\', 'd:\oracle\data\go_driver_test\');

// 3. modify the pluggable database settings
ALTER PLUGGABLE DATABASE go_driver_test OPEN;
ALTER SESSION SET CONTAINER = go_driver_test;
GRANT DBA TO test;

// 4. add new database service to the tnsnames.ora file:
// located on your client machine in $ORACLE_HOME\network\admin\tnsnames.ora
GO_DRIVER_TEST =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = go_driver_test)
    )
  )

// 5. create OS environment variables
GO_ORA_DRIVER_TEST_DB = go_driver_test
GO_ORA_DRIVER_TEST_USERNAME = test
GO_ORA_DRIVER_TEST_PASSWORD = test

Some helpful SQL maintenance statements:

// delete all tables in a non-container database
BEGIN
FOR c IN (SELECT table_name FROM user_tables) LOOP
EXECUTE IMMEDIATE ('DROP TABLE "' || c.table_name || '" CASCADE CONSTRAINTS');
END LOOP;
END;

// delete the non-container test database; use SqlPlus as sysdba
DROP USER test CASCADE;

Run the tests.

Limitations

database/sql method Stmt.QueryRow is not supported.

Go 1.6 introduced stricter cgo (call C from Go) rules, and introduced runtime checks. This is good, as the possibility of C code corrupting Go code is almost completely eliminated, but it also means a severe call overhead grow. Sometimes this can be 22x the go 1.5.3 call time!

So if you need performance more than correctness, start your programs with "GODEBUG=cgocheck=0" environment setting.

License

Copyright 2017 Rana Ian, Tamás Gulácsi. All rights reserved. Use of this source code is governed by The MIT License found in the accompanying LICENSE file.

Usage

const (
	// The driver name registered with the database/sql package.
	Name string = "ora"

	// The driver version sent to an Oracle server and visible in
	// V$SESSION_CONNECT_INFO or GV$SESSION_CONNECT_INFO.
	Version string = "v4.1.13"
)
const (
	NoPool  = PoolType(0)
	DRCPool = PoolType(1)
	SPool   = PoolType(2)
	CPool   = PoolType(3)
)
const (
	DefaultPoolSize      = 4
	DefaultEvictDuration = time.Minute
)
const (
	MaxFetchLen        = 1024
	DefaultFetchLen    = 128
	DefaultLOBFetchLen = 8
)
const (
	// SysDefault is the default, normal session mode.
	SysDefault = SessionMode(iota)
	// SysDba is for connecting as SYSDBA.
	SysDba
	// SysOper is for connectiong as SYSOPER.
	SysOper
)
var Schema string

Schema may optionally be specified to prefix a table name in the sql generated by the ora.Ins, ora.Upd, ora.Del, and ora.Sel methods.

func AddTbl

func AddTbl(v interface{}, tblName string) (err error)

AddTbl maps a table name to a struct type when a struct type name is not identitcal to an Oracle table name.

AddTbl is optional and used by the orm-like methods ora.Ins, ora.Upd, ora.Del, and ora.Sel.

AddTbl may be called once during the lifetime of the driver.

func Del

func Del(v interface{}, ses *Ses) (err error)

Del deletes a struct from an Oracle table returning a possible error.

Specify a struct, or struct pointer to parameter 'v' and an open Ses to parameter 'ses'.

Del requires one struct field tagged with db:"pk". The field tagged with db:"pk" is used in a sql WHERE clause.

By default, Del generates and executes a sql DELETE statement based on the struct name and one exported field name tagged with db:"pk". A struct name is used for the table name and a field name is used for a column name. Prior to calling Del, you may specify an alternative table name to ora.AddTbl. An alternative column name may be specified to the field tag db:"column_name".

Set ora.Schema to specify an optional table name prefix.

func DescribeQuery

func DescribeQuery(db *sql.DB, qry string) ([]DescribedColumn, error)

DescribeQuery parses the query and returns the column types, as DBMS_SQL.describe_column does.

func GctName

func GctName(gct GoColumnType) string

func GetCompileErrors

func GetCompileErrors(ses *Ses, all bool) ([]CompileError, error)

GetCompileErrors returns the slice of the errors in user_errors.

If all is false, only errors are returned; otherwise, warnings, too.

func Ins

func Ins(v interface{}, ses *Ses) (err error)

Ins inserts a struct into an Oracle table returning a possible error.

Specify a struct, or struct pointer to parameter 'v' and an open Ses to parameter 'ses'.

Optional struct field tags db:"column_name,id,-" may be specified to control how the sql INSERT statement is generated.

By default, Ins generates and executes a sql INSERT statement based on the struct name and all exported field names. A struct name is used for the table name and a field name is used for a column name. Prior to calling Ins, you may specify an alternative table name to ora.AddTbl. An alternative column name may be specified to the field tag db:"column_name". Specifying the db:"-" tag will remove a field from the INSERT statement.

The optional db:"id" field tag may combined with the db:"pk" tag. A field tagged with db:"pk,id" indicates a field is a primary key backed by an Oracle identity sequence. db:"pk,id" may be tagged to one field per struct. When db:"pk,id" is tagged to a field Ins generates a RETURNING clause to recevie a db generated identity value. The db:"id" tag is not required and Ins will insert a struct to a table without returning an identity value.

Set ora.Schema to specify an optional table name prefix.

func NumEnv

func NumEnv() int

NumEnv returns the number of open Oracle environments.

func Register

func Register(cfg DrvCfg)

Register used to register the ora database driver with the database/sql package, but this is automatic now - so this function is deprecated, has the same effect as SetCfg.

func Sel

func Sel(v interface{}, rt ResType, ses *Ses, where string, whereParams ...interface{}) (result interface{}, err error)

Sel selects structs from an Oracle table returning a specified container of structs and a possible error.

Specify a struct, or struct pointer to parameter 'v' to indicate the struct return type. Specify a ResType to parameter 'rt' to indicate the container return type. Possible container return types include a slice of structs, slice of struct pointers, map of structs, and map of struct pointers. Specify an open Ses to parameter 'ses'. Optionally specify a where clause to parameter 'where' and where parameters to variadic parameter 'whereParams'.

Optional struct field tags db:"column_name,omit" may be specified to control how the sql SELECT statement is generated. Optional struct field tags db:"pk,fk1,fk2,fk3,fk4" control how a map return type is generated.

A slice may be returned by specifying one of the 'SliceOf' ResTypes to parameter 'rt'. Specify a SliceOfPtr to return a slice of struct pointers. Specify a SliceOfVal to return a slice of structs.

A map may be returned by specifying one of the 'MapOf' ResTypes to parameter 'rt'. The map key type is based on a struct field type tagged with one of db:"pk", db:"fk1", db:"fk2", db:"fk3", or db:"fk4" matching the specified ResType suffix Pk, Fk1, Fk2, Fk3, or Fk4. The map value type is a struct pointer when a 'MapOfPtr' ResType is specified. The map value type is a struct when a 'MapOfVal' ResType is specified. For example, tagging a uint64 struct field with db:"pk" and specifying a MapOfPtrPk generates a map with a key type of uint64 and a value type of struct pointer.

ResTypes available to specify to parameter 'rt' are MapOfPtrPk, MapOfPtrFk1, MapOfPtrFk2, MapOfPtrFk3, MapOfPtrFk4, MapOfValPk, MapOfValFk1, MapOfValFk2, MapOfValFk3, and MapOfValFk4.

Set ora.Schema to specify an optional table name prefix.

func SetCfg

func SetCfg(cfg DrvCfg)

SetCfg applies the specified cfg to the ora database driver.

func SplitDSN

func SplitDSN(dsn string) (username, password, sid string)

SplitDSN splits the user/password@dblink string to username, password and dblink, to be used as SesCfg.Username, SesCfg.Password, SrvCfg.Dblink.

func Upd

func Upd(v interface{}, ses *Ses) (err error)

Upd updates a struct to an Oracle table returning a possible error.

Specify a struct, or struct pointer to parameter 'v' and an open Ses to parameter 'ses'.

Upd requires one struct field tagged with db:"pk". The field tagged with db:"pk" is used in a sql WHERE clause. Optional struct field tags db:"column_name,-" may be specified to control how the sql UPDATE statement is generated.

By default, Upd generates and executes a sql UPDATE statement based on the struct name and all exported field names. A struct name is used for the table name and a field name is used for a column name. Prior to calling Upd, you may specify an alternative table name to ora.AddTbl. An alternative column name may be specified to the field tag db:"column_name". Specifying the db:"-" tag will remove a field from the UPDATE statement.

Set ora.Schema to specify an optional table name prefix.

func WithStmtCfg

func WithStmtCfg(ctx context.Context, cfg StmtCfg) context.Context

WithStmtCfg returns a new context, with the given cfg that can be used to configure several parameters.

WARNING: the StmtCfg must be derived from Cfg(), or NewStmtCfg(), as an empty StmtCfg is not usable!

type Bfile

type Bfile struct {
	IsNull         bool
	DirectoryAlias string
	Filename       string
}

Bfile represents a nullable BFILE Oracle value.

func (Bfile) Equals

func (this Bfile) Equals(other Bfile) bool

Equals returns true when the receiver and specified Bfile are both null, or when the receiver and specified Bfile are both not null, DirectoryAlias are equal and Filename are equal.

type Bool

type Bool struct {
	IsNull bool
	Value  bool
}

Bool is a nullable bool.

func (Bool) Equals

func (this Bool) Equals(other Bool) bool

Equals returns true when the receiver and specified Bool are both null, or when the receiver and specified Bool are both not null and Values are equal.

func (Bool) MarshalJSON

func (this Bool) MarshalJSON() ([]byte, error)

func (*Bool) UnmarshalJSON

func (this *Bool) UnmarshalJSON(p []byte) error

type Column

type Column struct {
	Name      string
	Type      C.ub2
	Length    uint32
	Precision C.sb2
	Scale     C.sb1
}

type CompileError

type CompileError struct {
	Owner, Name, Type    string
	Line, Position, Code int64
	Text                 string
	Warning              bool
}

CompileError represents a compile-time error as in user_errors view.

func (CompileError) Error

func (ce CompileError) Error() string

type Con

type Con struct {
}

Con is an Oracle connection associated with a server and session.

Implements the driver.Conn interface.

func (*Con) Begin

func (con *Con) Begin() (driver.Tx, error)

Begin starts a transaction.

Begin is a member of the driver.Conn interface.

func (*Con) BeginTx

func (con *Con) BeginTx(ctx context.Context, opts driver.TxOptions) (driver.Tx, error)

BeginTx starts and returns a new transaction. The provided context should be used to roll the transaction back if it is cancelled.

If the driver does not support setting the isolation level and one is set or if there is a set isolation level but the set level is not supported, an error must be returned.

If the read-only value is true to either set the read-only transaction property if supported or return an error if it is not supported.

func (*Con) Close

func (con *Con) Close() (err error)

Close ends a session and disconnects from an Oracle server.

Close is a member of the driver.Conn interface.

func (*Con) IsOpen

func (con *Con) IsOpen() bool

IsOpen returns true when the connection to the Oracle server is open; otherwise, false.

Calling Close will cause IsOpen to return false. Once closed, a connection cannot be re-opened. To open a new connection call Open on a driver.

func (*Con) Name

func (s *Con) Name(calc func() string) string

Name sets the name to the result of calc once, then returns that result forever. (Effectively caches the result of calc().)

func (*Con) Ping

func (con *Con) Ping(ctx context.Context) error

Ping makes a round-trip call to an Oracle server to confirm that the connection is active.

func (*Con) Prepare

func (con *Con) Prepare(query string) (driver.Stmt, error)

Prepare readies a sql string for use.

Prepare is a member of the driver.Conn interface.

func (*Con) PrepareContext

func (con *Con) PrepareContext(ctx context.Context, query string) (driver.Stmt, error)

PrepareContext returns a prepared statement, bound to this connection. context is for the preparation of the statement, it must not store the context within the statement itself.

type Date

type Date struct {
	date.Date
}

Date is a nullable date, for low (second) precisions (OCIDate)

type DescribedColumn

type DescribedColumn struct {
	Column

	Schema                 string
	Nullable               bool
	CharsetID, CharsetForm int
}

DescribedColumn type for describing a column (see DescribeQuery).

type Drv

type Drv struct {
	sync.RWMutex
}

Drv represents an Oracle database driver.

Drv is not meant to be called by user-code.

Drv implements the driver.Driver interface.

func (*Drv) Cfg

func (drv *Drv) Cfg() DrvCfg

func (*Drv) Open

func (drv *Drv) Open(conStr string) (driver.Conn, error)

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) SetCfg

func (drv *Drv) SetCfg(cfg DrvCfg)

type DrvCfg

type DrvCfg struct {
	StmtCfg
	Log LogDrvCfg
}

DrvCfg represents configuration values for the ora package.

func Cfg

func Cfg() DrvCfg

Cfg returns the ora database driver's cfg.

func NewDrvCfg

func NewDrvCfg() DrvCfg

NewDrvCfg creates a DrvCfg with default values.

func (DrvCfg) SetBinaryDouble

func (c DrvCfg) SetBinaryDouble(gct GoColumnType) DrvCfg

func (DrvCfg) SetBinaryFloat

func (c DrvCfg) SetBinaryFloat(gct GoColumnType) DrvCfg

func (DrvCfg) SetBlob

func (c DrvCfg) SetBlob(gct GoColumnType) DrvCfg

func (DrvCfg) SetByteSlice

func (c DrvCfg) SetByteSlice(gct GoColumnType) DrvCfg

func (DrvCfg) SetChar

func (c DrvCfg) SetChar(gct GoColumnType) DrvCfg

func (DrvCfg) SetChar1

func (c DrvCfg) SetChar1(gct GoColumnType) DrvCfg

func (DrvCfg) SetClob

func (c DrvCfg) SetClob(gct GoColumnType) DrvCfg

func (DrvCfg) SetDate

func (c DrvCfg) SetDate(gct GoColumnType) DrvCfg

func (DrvCfg) SetFloat

func (c DrvCfg) SetFloat(gct GoColumnType) DrvCfg

func (DrvCfg) SetLobBufferSize

func (c DrvCfg) SetLobBufferSize(size int) DrvCfg

func (DrvCfg) SetLogger

func (c DrvCfg) SetLogger(lgr Logger) DrvCfg

func (DrvCfg) SetLong

func (c DrvCfg) SetLong(gct GoColumnType) DrvCfg

func (DrvCfg) SetLongBufferSize

func (c DrvCfg) SetLongBufferSize(size uint32) DrvCfg

func (DrvCfg) SetLongRaw

func (c DrvCfg) SetLongRaw(gct GoColumnType) DrvCfg

func (DrvCfg) SetLongRawBufferSize

func (c DrvCfg) SetLongRawBufferSize(size uint32) DrvCfg

func (DrvCfg) SetNumberBigFloat

func (c DrvCfg) SetNumberBigFloat(gct GoColumnType) DrvCfg

func (DrvCfg) SetNumberBigInt

func (c DrvCfg) SetNumberBigInt(gct GoColumnType) DrvCfg

func (DrvCfg) SetNumberFloat

func (c DrvCfg) SetNumberFloat(gct GoColumnType) DrvCfg

func (DrvCfg) SetNumberInt

func (c DrvCfg) SetNumberInt(gct GoColumnType) DrvCfg

func (DrvCfg) SetPrefetchMemorySize

func (c DrvCfg) SetPrefetchMemorySize(prefetchMemorySize uint32) DrvCfg

func (DrvCfg) SetPrefetchRowCount

func (c DrvCfg) SetPrefetchRowCount(prefetchRowCount uint32) DrvCfg

func (DrvCfg) SetRaw

func (c DrvCfg) SetRaw(gct GoColumnType) DrvCfg

func (DrvCfg) SetStmtCfg

func (cfg DrvCfg) SetStmtCfg(stmtCfg StmtCfg) DrvCfg

func (DrvCfg) SetStringPtrBufferSize

func (c DrvCfg) SetStringPtrBufferSize(size int) DrvCfg

func (DrvCfg) SetTimestamp

func (c DrvCfg) SetTimestamp(gct GoColumnType) DrvCfg

func (DrvCfg) SetTimestampLtz

func (c DrvCfg) SetTimestampLtz(gct GoColumnType) DrvCfg

func (DrvCfg) SetTimestampTz

func (c DrvCfg) SetTimestampTz(gct GoColumnType) DrvCfg

func (DrvCfg) SetVarchar

func (c DrvCfg) SetVarchar(gct GoColumnType) DrvCfg

type DrvExecResult

type DrvExecResult struct {
}

DrvExecResult is an Oracle execution result.

DrvExecResult implements the driver.Result interface.

func (*DrvExecResult) LastInsertId

func (er *DrvExecResult) LastInsertId() (int64, error)

LastInsertId returns the identity value from an insert statement.

There are two setup steps required to reteive the LastInsertId. One, specify a 'returning into' clause in the SQL insert statement. And, two, specify a nil parameter to DB.Exec or DrvStmt.Exec.

For example:

db, err := sql.Open("ora", "scott/tiger@orcl")

db.Exec("CREATE TABLE T1 (C1 NUMBER(19,0) GENERATED ALWAYS AS IDENTITY (START WITH 1 INCREMENT BY 1), C2 VARCHAR2(48 CHAR))")

result, err := db.Exec("INSERT INTO T1 (C2) VALUES ('GO') RETURNING C1 /*lastInsertId*/ INTO :C1", nil)

id, err := result.LastInsertId()

func (*DrvExecResult) RowsAffected

func (er *DrvExecResult) RowsAffected() (int64, error)

RowsAffected returns the number of rows affected by the exec statement.

type DrvQueryResult

type DrvQueryResult struct {
}

DrvQueryResult contains methods to retrieve the results of a SQL select statement.

DrvQueryResult implements the driver.Rows interface.

func (*DrvQueryResult) Close

func (qr *DrvQueryResult) Close() error

Close performs no operations.

Close is a member of the driver.Rows interface.

func (*DrvQueryResult) ColumnTypeDatabaseTypeName

func (qr *DrvQueryResult) ColumnTypeDatabaseTypeName(index int) string

ColumnTypeDatabaseTypeName returns the database system type name without the length, in uppercase.

func (*DrvQueryResult) ColumnTypeLength

func (qr *DrvQueryResult) ColumnTypeLength(index int) (length int64, ok bool)

ColumnTypeLength returns the length of the column type if the column is a variable length type. If the column is not a variable length type ok should return false. If length is not limited other than system limits, it should return math.MaxInt64.

func (*DrvQueryResult) ColumnTypeNullable

func (qr *DrvQueryResult) ColumnTypeNullable(index int) (nullable, ok bool)

ColumnTypeNullable returns true if it is known the column may be null, or false if the column is known to be not nullable. If the column nullability is unknown, ok should be false.

func (*DrvQueryResult) ColumnTypePrecisionScale

func (qr *DrvQueryResult) ColumnTypePrecisionScale(index int) (precision, scale int64, ok bool)

ColumnTypePrecisionScale return the precision and scale for decimal types. If not applicable, ok should be false.

func (*DrvQueryResult) ColumnTypeScanType

func (qr *DrvQueryResult) ColumnTypeScanType(index int) reflect.Type

func (*DrvQueryResult) Columns

func (qr *DrvQueryResult) Columns() []string

Columns returns query column names.

Columns is a member of the driver.Rows interface.

func (*DrvQueryResult) HasNextResultSet

func (qr *DrvQueryResult) HasNextResultSet() bool

HasNextResultSet reports whether there is another result set after the current one.

func (*DrvQueryResult) Next

func (qr *DrvQueryResult) Next(dest []driver.Value) (err error)

Next populates the specified slice with the next row of data.

Returns io.EOF when there are no more rows.

Next is a member of the driver.Rows interface.

func (*DrvQueryResult) NextResultSet

func (qr *DrvQueryResult) NextResultSet() error

NextResultSet advances the driver to the next result set even if there are remaining rows in the current result set.

type DrvStmt

type DrvStmt struct {
}

DrvStmt is an Oracle statement associated with a session.

DrvStmt wraps Stmt and is intended for use by the database/sql/driver package.

DrvStmt implements the driver.Stmt interface.

func (*DrvStmt) Close

func (ds *DrvStmt) Close() error

Close closes the SQL statement.

Close is a member of the driver.Stmt interface.

func (*DrvStmt) Exec

func (ds *DrvStmt) Exec(values []driver.Value) (driver.Result, error)

Exec executes an Oracle SQL statement on a server. Exec returns a driver.Result and a possible error.

Exec is a member of the driver.Stmt interface.

func (*DrvStmt) ExecContext

func (ds *DrvStmt) ExecContext(ctx context.Context, values []driver.NamedValue) (driver.Result, error)

ExecContext enhances the Stmt interface by providing Exec with context. ExecContext must honor the context timeout and return when it is cancelled.

func (*DrvStmt) NumInput

func (ds *DrvStmt) NumInput() int

NumInput returns the number of placeholders in a sql statement.

NumInput is a member of the driver.Stmt interface.

func (*DrvStmt) Query

func (ds *DrvStmt) Query(values []driver.Value) (driver.Rows, error)

Query runs a SQL query on an Oracle server. Query returns driver.Rows and a possible error.

Query is a member of the driver.Stmt interface.

func (*DrvStmt) QueryContext

func (ds *DrvStmt) QueryContext(ctx context.Context, values []driver.NamedValue) (driver.Rows, error)

QueryContext enhances the Stmt interface by providing Query with context. QueryContext must honor the context timeout and return when it is cancelled.

type EmpLgr

type EmpLgr struct{}

func (EmpLgr) Errorf

func (e EmpLgr) Errorf(format string, v ...interface{})

func (EmpLgr) Errorln

func (e EmpLgr) Errorln(v ...interface{})

func (EmpLgr) Infof

func (e EmpLgr) Infof(format string, v ...interface{})

func (EmpLgr) Infoln

func (e EmpLgr) Infoln(v ...interface{})

type Env

type Env struct {
	sync.RWMutex
}

Env represents an Oracle environment.

func NewEnvSrvSes

func NewEnvSrvSes(dsn string) (*Env, *Srv, *Ses, error)

NewEnvSrvSes is a comfort function which opens the environment, creates a connection (Srv) to the server, and opens a session (Ses), in one call.

Ideal for simple use cases.

func OpenEnv

func OpenEnv() (env *Env, err error)

OpenEnv opens an Oracle environment.

Optionally specify a cfg parameter. If cfg is nil, default cfg values are applied.

func (*Env) Cfg

func (env *Env) Cfg() StmtCfg

func (*Env) Close

func (env *Env) Close() (err error)

Close disconnects from servers and resets optional fields.

func (*Env) IsOpen

func (env *Env) IsOpen() bool

IsOpen returns true when the environment is open; otherwise, false.

Calling Close will cause IsOpen to return false. Once closed, the environment may be re-opened by calling Open.

func (*Env) Name

func (s *Env) Name(calc func() string) string

Name sets the name to the result of calc once, then returns that result forever. (Effectively caches the result of calc().)

func (*Env) NewPool

func (env *Env) NewPool(srvCfg SrvCfg, sesCfg SesCfg, size int) *Pool

NewPool returns an idle session pool, which evicts the idle sessions every minute, and automatically manages the required new connections (Srv).

This is done by maintaining a 1-1 pairing between the Srv and its Ses.

This pool does NOT limit the number of active connections, just helps reuse already established connections and sessions, lowering the resource usage on the server.

If size <= 0, then DefaultPoolSize is used.

func (*Env) NewSrvPool

func (env *Env) NewSrvPool(srvCfg SrvCfg, size int) *SrvPool

NewSrvPool returns a connection pool, which evicts the idle connections in every minute. The pool holds at most size idle Srv. If size is zero, DefaultPoolSize will be used.

func (*Env) NumCon

func (env *Env) NumCon() int

NumCon returns the number of open Oracle connections.

func (*Env) NumSrv

func (env *Env) NumSrv() int

NumSrv returns the number of open Oracle servers.

func (*Env) OCINumberFromFloat

func (env *Env) OCINumberFromFloat(dest *C.OCINumber, value float64, byteLen int) error

func (*Env) OCINumberFromInt

func (env *Env) OCINumberFromInt(dest *C.OCINumber, value int64, byteLen int) error

func (*Env) OCINumberFromUint

func (env *Env) OCINumberFromUint(dest *C.OCINumber, value uint64, byteLen int) error

func (*Env) OCINumberToFloat

func (env *Env) OCINumberToFloat(src *C.OCINumber, byteLen int) (float64, error)

func (*Env) OCINumberToInt

func (env *Env) OCINumberToInt(src *C.OCINumber, byteLen int) (int64, error)

func (*Env) OCINumberToUint

func (env *Env) OCINumberToUint(src *C.OCINumber, byteLen int) (uint64, error)

func (*Env) OpenCon

func (env *Env) OpenCon(dsn string) (con *Con, err error)

OpenCon starts an Oracle session on a server returning a *Con and possible error.

The connection string has the form username/password@dblink e.g., scott/tiger@orcl For connecting as SYSDBA or SYSOPER, append " AS SYSDBA" to the end of the connection string: "sys/sys as sysdba".

dblink is a connection identifier such as a net service name, full connection identifier, or a simple connection identifier. The dblink may be defined in the client machine's tnsnames.ora file.

func (*Env) OpenSrv

func (env *Env) OpenSrv(cfg SrvCfg) (srv *Srv, err error)

OpenSrv connects to an Oracle server returning a *Srv and possible error.

func (*Env) SetCfg

func (env *Env) SetCfg(cfg StmtCfg)

type Float32

type Float32 struct {
	IsNull bool
	Value  float32
}

Float32 is a nullable float32.

func (Float32) Equals

func (this Float32) Equals(other Float32) bool

Equals returns true when the receiver and specified Float32 are both null, or when the receiver and specified Float32 are both not null and Values are equal.

func (Float32) MarshalJSON

func (this Float32) MarshalJSON() ([]byte, error)

func (*Float32) UnmarshalJSON

func (this *Float32) UnmarshalJSON(p []byte) error

type Float64

type Float64 struct {
	IsNull bool
	Value  float64
}

Float64 is a nullable float64.

func (Float64) Equals

func (this Float64) Equals(other Float64) bool

Equals returns true when the receiver and specified Float64 are both null, or when the receiver and specified Float64 are both not null and Values are equal.

func (Float64) MarshalJSON

func (this Float64) MarshalJSON() ([]byte, error)

func (*Float64) UnmarshalJSON

func (this *Float64) UnmarshalJSON(p []byte) error

type GoColumnType

type GoColumnType uint

GoColumnType defines the Go type returned from a sql select column.

const (
	// D defines a sql select column based on its default mapping.
	D GoColumnType = iota + 1
	// I64 defines a sql select column as a Go int64.
	I64
	// I32 defines a sql select column as a Go int32.
	I32
	// I16 defines a sql select column as a Go int16.
	I16
	// I8 defines a sql select column as a Go int8.
	I8
	// U64 defines a sql select column as a Go uint64.
	U64
	// U32 defines a sql select column as a Go uint32.
	U32
	// U16 defines a sql select column as a Go uint16.
	U16
	// U8 defines a sql select column as a Go uint8.
	U8
	// F64 defines a sql select column as a Go float64.
	F64
	// F32 defines a sql select column as a Go float32.
	F32
	// OraI64 defines a sql select column as a nullable Go ora.Int64.
	OraI64
	// OraI32 defines a sql select column as a nullable Go ora.Int32.
	OraI32
	// OraI16 defines a sql select column as a nullable Go ora.Int16.
	OraI16
	// OraI8 defines a sql select column as a nullable Go ora.Int8.
	OraI8
	// OraU64 defines a sql select column as a nullable Go ora.Uint64.
	OraU64
	// OraU32 defines a sql select column as a nullable Go ora.Uint32.
	OraU32
	// OraU16 defines a sql select column as a nullable Go ora.Uint16.
	OraU16
	// OraU8 defines a sql select column as a nullable Go ora.Uint8.
	OraU8
	// OraF64 defines a sql select column as a nullable Go ora.Float64.
	OraF64
	// OraF32 defines a sql select column as a nullable Go ora.Float32.
	OraF32
	// T defines a sql select column as a Go time.Time.
	T
	// OraT defines a sql select column as a nullable Go ora.Time.
	OraT
	// S defines a sql select column as a Go string.
	S
	// OraS defines a sql select column as a nullable Go ora.String.
	OraS
	// B defines a sql select column as a Go bool.
	B
	// OraB defines a sql select column as a nullable Go ora.Bool.
	OraB
	// Bin defines a sql select column or bind parmeter as a Go byte slice.
	Bin
	// OraBin defines a sql select column as a nullable Go ora.Binary.
	OraBin
	// N defines a sql select column as a Go string for number.
	N
	// OraN defines a sql select column as a nullable Go string for number.
	OraN
	// L defins an sql select column as an ora.Lob.
	L
)

go column types

func (GoColumnType) String

func (gct GoColumnType) String() string

type Id

type Id struct {
}

type Int16

type Int16 struct {
	IsNull bool
	Value  int16
}

Int16 is a nullable int16.

func (Int16) Equals

func (this Int16) Equals(other Int16) bool

Equals returns true when the receiver and specified Int16 are both null, or when the receiver and specified Int16 are both not null and Values are equal.

func (Int16) MarshalJSON

func (this Int16) MarshalJSON() ([]byte, error)

func (*Int16) UnmarshalJSON

func (this *Int16) UnmarshalJSON(p []byte) error

type Int32

type Int32 struct {
	IsNull bool
	Value  int32
}

Int32 is a nullable int32.

func (Int32) Equals

func (this Int32) Equals(other Int32) bool

Equals returns true when the receiver and specified Int32 are both null, or when the receiver and specified Int32 are both not null and Values are equal.

func (Int32) MarshalJSON

func (this Int32) MarshalJSON() ([]byte, error)

func (*Int32) UnmarshalJSON

func (this *Int32) UnmarshalJSON(p []byte) error

type Int64

type Int64 struct {
	IsNull bool
	Value  int64
}

Int64 is a nullable int64.

func (Int64) Equals

func (this Int64) Equals(other Int64) bool

Equals returns true when the receiver and specified Int64 are both null, or when the receiver and specified Int64 are both not null and Values are equal.

func (Int64) MarshalJSON

func (this Int64) MarshalJSON() ([]byte, error)

func (*Int64) UnmarshalJSON

func (this *Int64) UnmarshalJSON(p []byte) error

type Int8

type Int8 struct {
	IsNull bool
	Value  int8
}

Int8 is a nullable int8.

func (Int8) Equals

func (this Int8) Equals(other Int8) bool

Equals returns true when the receiver and specified Int8 are both null, or when the receiver and specified Int8 are both not null and Values are equal.

func (Int8) MarshalJSON

func (this Int8) MarshalJSON() ([]byte, error)

func (*Int8) UnmarshalJSON

func (this *Int8) UnmarshalJSON(p []byte) error

type IntervalDS

type IntervalDS struct {
	IsNull     bool
	Day        int32
	Hour       int32
	Minute     int32
	Second     int32
	Nanosecond int32
}

IntervalDS represents a nullable INTERVAL DAY TO SECOND Oracle value.

func (IntervalDS) Equals

func (this IntervalDS) Equals(other IntervalDS) bool

Equals returns true when the receiver and specified IntervalDS are both null, or when the receiver and specified IntervalDS are both not null, and all other fields are equal.

func (IntervalDS) ShiftTime

func (this IntervalDS) ShiftTime(t time.Time) time.Time

ShiftTime returns a new Time with IntervalDS applied.

func (IntervalDS) String

func (this IntervalDS) String() string

type IntervalYM

type IntervalYM struct {
	IsNull bool
	Year   int32
	Month  int32
}

IntervalYM represents a nullable INTERVAL YEAR TO MONTH Oracle value.

func (IntervalYM) Equals

func (this IntervalYM) Equals(other IntervalYM) bool

Equals returns true when the receiver and specified IntervalYM are both null, or when the receiver and specified IntervalYM are both not null, Year are equal and Month are equal.

func (IntervalYM) ShiftTime

func (this IntervalYM) ShiftTime(t time.Time) time.Time

ShiftTime returns a new Time with IntervalYM applied.

func (IntervalYM) String

func (this IntervalYM) String() string

type Lob

type Lob struct {
	io.Reader
	io.Closer
	C bool
}

Lob Reader is sent to the DB on bind, if not nil. The Reader can read the LOB if we bind a *Lob, Closer will close the LOB. Set Lob.C = true to make this a CLOB reader!

func (*Lob) Bytes

func (this *Lob) Bytes() ([]byte, error)

Bytes will read the contents of the Lob.Reader, and will keep that for future.

func (*Lob) Close

func (this *Lob) Close() error

func (*Lob) Equals

func (this *Lob) Equals(other Lob) bool

Equals returns true when the receiver and specified Lob are both null, or when they both not null and share the same Reader.

func (*Lob) MarshalJSON

func (this *Lob) MarshalJSON() ([]byte, error)

func (*Lob) Read

func (this *Lob) Read(p []byte) (int, error)

func (*Lob) Scan

func (this *Lob) Scan(src interface{}) error

func (*Lob) String

func (this *Lob) String() string

func (*Lob) UnmarshalJSON

func (this *Lob) UnmarshalJSON(p []byte) error

func (*Lob) Value

func (this *Lob) Value() (driver.Value, error)

Value returns what Lob.Bytes returns.

type LogConCfg

type LogConCfg struct {
	// Close determines whether the Con.Close method is logged.
	//
	// The default is true.
	Close bool

	// Prepare determines whether the Con.Prepare method is logged.
	//
	// The default is true.
	Prepare bool

	// Begin determines whether the Con.Begin method is logged.
	//
	// The default is true.
	Begin bool

	// Ping determines whether the Con.Ping method is logged.
	//
	// The default is true.
	Ping bool
}

LogConCfg represents Con logging configuration values.

func NewLogConCfg

func NewLogConCfg() LogConCfg

NewLogConCfg creates a LogTxCfg with default values.

type LogDrvCfg

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
}

LogDrvCfg represents package-level logging configuration values.

func NewLogDrvCfg

func NewLogDrvCfg() LogDrvCfg

NewLogDrvCfg creates a LogDrvCfg with default values.

func (LogDrvCfg) IsEnabled

func (c LogDrvCfg) IsEnabled(enabled bool) bool

IsEnabled returns whether the logger is enabled (and enabled is true).

type LogEnvCfg

type LogEnvCfg struct {
	// Close determines whether the Env.Close method is logged.
	//
	// The default is true.
	Close bool

	// OpenSrv determines whether the Env.OpenSrv method is logged.
	//
	// The default is true.
	OpenSrv bool

	// OpenCon determines whether the Env.OpenCon method is logged.
	//
	// The default is true.
	OpenCon bool
}

LogEnvCfg represents Env logging configuration values.

func NewLogEnvCfg

func NewLogEnvCfg() LogEnvCfg

NewLogEnvCfg creates a LogEnvCfg with default values.

type LogRsetCfg

type LogRsetCfg struct {
	// Close determines whether the Rset.close method is logged.
	//
	// The default is true.
	Close bool

	// BeginRow determines whether the Rset.beginRow method is logged.
	//
	// The default is false.
	BeginRow bool

	// EndRow determines whether the Rset.endRow method is logged.
	//
	// The default is false.
	EndRow bool

	// Next determines whether the Rset.Next method is logged.
	//
	// The default is false.
	Next bool

	// Open determines whether the Rset.open method is logged.
	//
	// The default is true.
	Open bool

	// OpenDefs determines whether Select-list definitions with the Rset.open method are logged.
	//
	// The default is true.
	OpenDefs bool
}

LogRsetCfg represents Rset logging configuration values.

func NewLogRsetCfg

func NewLogRsetCfg() LogRsetCfg

NewLogTxCfg creates a LogRsetCfg with default values.

type LogSesCfg

type LogSesCfg struct {
	// Close determines whether the Ses.Close method is logged.
	//
	// The default is true.
	Close bool

	// PrepAndExe determines whether the Ses.PrepAndExe method is logged.
	//
	// The default is true.
	PrepAndExe bool

	// PrepAndQry determines whether the Ses.PrepAndQry method is logged.
	//
	// The default is true.
	PrepAndQry bool

	// Prep determines whether the Ses.Prep method is logged.
	//
	// The default is true.
	Prep bool

	// Ins determines whether the Ses.Ins method is logged.
	//
	// The default is true.
	Ins bool

	// Upd determines whether the Ses.Upd method is logged.
	//
	// The default is true.
	Upd bool

	// Sel determines whether the Ses.Sel method is logged.
	//
	// The default is true.
	Sel bool

	// StartTx determines whether the Ses.StartTx method is logged.
	//
	// The default is true.
	StartTx bool

	// Ping determines whether the Ses.Ping method is logged.
	//
	// The default is true.
	Ping bool

	// Break determines whether the Ses.Break method is logged.
	//
	// The default is true.
	Break bool
}

LogSesCfg represents Ses logging configuration values.

func NewLogSesCfg

func NewLogSesCfg() LogSesCfg

NewLogSesCfg creates a LogSesCfg with default values.

type LogSrvCfg

type LogSrvCfg struct {
	// Close determines whether the Srv.Close method is logged.
	//
	// The default is true.
	Close bool

	// OpenSes determines whether the Srv.OpenSes method is logged.
	//
	// The default is true.
	OpenSes bool

	// Version determines whether the Srv.Version method is logged.
	//
	// The default is true.
	Version bool
}

LogSrvCfg represents Srv logging configuration values.

func NewLogSrvCfg

func NewLogSrvCfg() LogSrvCfg

NewLogSrvCfg creates a LogSrvCfg with default values.

type LogStmtCfg

type LogStmtCfg struct {
	// Close determines whether the Stmt.Close method is logged.
	//
	// The default is true.
	Close bool

	// Exe determines whether the Stmt.Exe method is logged.
	//
	// The default is true.
	Exe bool

	// Qry determines whether the Stmt.Qry method is logged.
	//
	// The default is true.
	Qry bool

	// Bind determines whether the Stmt.bind method is logged.
	//
	// The default is true.
	Bind bool
}

LogStmtCfg represents Stmt logging configuration values.

func NewLogStmtCfg

func NewLogStmtCfg() LogStmtCfg

NewLogStmtCfg creates a LogStmtCfg with default values.

type LogTxCfg

type LogTxCfg struct {
	// Commit determines whether the Tx.Commit method is logged.
	//
	// The default is true.
	Commit bool

	// Rollback determines whether the Tx.Rollback method is logged.
	//
	// The default is true.
	Rollback bool
}

LogTxCfg represents Tx logging configuration values.

func NewLogTxCfg

func NewLogTxCfg() LogTxCfg

NewLogTxCfg creates a LogTxCfg with default values.

type Logger

type Logger interface {
	Infof(format string, args ...interface{})
	Infoln(args ...interface{})
	Errorf(format string, args ...interface{})
	Errorln(args ...interface{})
}

Logger interface is for logging.

type MultiErr

type MultiErr struct {
}

MultiErr holds multiple errors in a single string.

func (MultiErr) Error

func (m MultiErr) Error() string

Error returns one or more errors.

Error is a member of the 'error' interface.

type Num

type Num string

type OCINum

type OCINum struct {
	num.OCINum
}

func (*OCINum) FromC

func (num *OCINum) FromC(x C.OCINumber)

FromC converts from the given C.OCINumber.

func (OCINum) String

func (n OCINum) String() string

func (OCINum) ToC

func (num OCINum) ToC(x *C.OCINumber)

ToC converts the OCINum into the given *C.OCINumber.

func (OCINum) Value

func (n OCINum) Value() (driver.Value, error)

Value returns the driver.Value as required by database/sql. So OCINum is allowed as a parameter to Scan.