Documentation ¶
Overview ¶
Package db defines a simple Statement interface for querying Oracle databases, abstracting some boilerplate (beginning transactions, wraping bind values in data structures, etc.).
Usage:
// Creating a database connection is straightforward. db, err := New(&Config{ User: "user", Pass: "pass", Host: "host", }) // You can also ping the connection, useful for building auto-reconnect // functionality. err := db.Ping() // Begin a new transaction and return a Statement type. Statements use named // query parameters. stmt, err := db.Prepare("...") // Bind values to the query. This wraps creating NamedArg data structures // via sql.Named() calls. stmt.Bind("foo", 1) stmt.Bind("bar", "baz") // When a result cursor is required, use Query() to execute the statement. // This wraps the sql.Stmt.Query() call to compile the bound data (and any // additional data passed in the exec call). Query generates a cursor that // can be used to iterate through the results. stmt.Query() // To iterate through the cursor, a Next() method is provided. This wraps // the sql.Rows.Next() and sql.Rows.Scan() methods into a single call. The // input arguments are destination targets for the query results. // See https://golang.org/pkg/database/sql/#Rows.Scan for details. var foo int var bar string for stmt.Next(&foo, &bar) { doThings(foo, bar) } // When a result cursor is not required, use Exec() to execute the statement. // This wraps the sql.Stmt.Exec() call to compile the bound data (and any // additional data passed in the exec call). Exec returns a sql.Result // interface to summarize the executed SQL command. result, err := stmt.Exec() fmt.Println(result.RowsAffected()) // All statements are transactions, commit the transaction to save data // changes and close the transaction. err = stmt.Commit() // Close the database connection. This returns an error if no connection // exists. err := db.Close()
Index ¶
- Variables
- func InstrumentSQLConnector(cfg *Config) driver.Connector
- func InstrumentSQLDriver(cfg *Config) driver.Driver
- type Config
- type DB
- func (db *DB) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
- func (db *DB) Close() error
- func (db *DB) Config() *Config
- func (db *DB) Connect() error
- func (db *DB) Exec(query string, args ...interface{}) (sql.Result, error)
- func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
- func (db *DB) Ping() error
- func (db *DB) Prepare(query string) (*Statement, error)
- func (db *DB) PrepareContext(ctx context.Context, query string) (*Statement, error)
- func (db *DB) Query(query string, args ...interface{}) (*sql.Rows, error)
- func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
- func (db *DB) QueryRow(query string, args ...interface{}) *sql.Row
- func (db *DB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
- type Statement
- func (statement *Statement) Bind(key string, value interface{}) *Statement
- func (statement *Statement) Close() error
- func (statement *Statement) Commit() error
- func (statement *Statement) Err() error
- func (statement *Statement) Exec(args ...interface{}) (sql.Result, error)
- func (statement *Statement) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error)
- func (statement *Statement) LastErr() error
- func (statement *Statement) MapNext(dest map[string]interface{}) bool
- func (statement *Statement) MapScan(dest map[string]interface{}) error
- func (statement *Statement) Next(dest ...interface{}) bool
- func (statement *Statement) Query(args ...interface{}) (*sql.Rows, error)
- func (statement *Statement) QueryContext(ctx context.Context, args ...interface{}) (*sql.Rows, error)
- func (statement *Statement) QueryRow(args ...interface{}) *sql.Row
- func (statement *Statement) QueryRowContext(ctx context.Context, args ...interface{}) *sql.Row
- func (statement *Statement) Result() sql.Result
- func (statement *Statement) Rollback() error
- func (statement *Statement) Rows() *sql.Rows
- func (statement *Statement) Scan(dest ...interface{}) error
Constants ¶
This section is empty.
Variables ¶
var ( // ErrDSNStringEmpty defines the empty DSN string error. ErrDSNStringEmpty = fmt.Errorf("DSNString is empty") // ErrInvalidTLSConfig defines the invalid TLS config error. ErrInvalidTLSConfig = fmt.Errorf("invalid value / unknown config name") )
var (
// RFC3339Milli is RFC3339 with miliseconds
RFC3339Milli = "2006-01-02T15:04:05.000Z07:00"
)
Functions ¶
func InstrumentSQLConnector ¶
InstrumentSQLConnector returns a wrapped driver.Driver send statistics to NewRelic agent. The returned driver must be registered and used when opening a connection.
func InstrumentSQLDriver ¶
InstrumentSQLDriver returns a wrapped driver.Driver send statistics to NewRelic agent. The returned driver must be registered and used when opening a connection.
Types ¶
type Config ¶
type Config struct { // Recommended, a database connector or driver instance is required to instrument // database queries with NewRelic. Connector driver.Connector // cancel provides the context cancellation function used internally to manage graceful shutdown. Cancel context.CancelFunc // Recommended, application context. Ctx context.Context // Required, DatabaseName is name of database instance being connected to. Used for tracking // metrics. i.e. "CPROD1" DatabaseName string // Recommended, a database connector or driver instance is required to instrument // database queries with NewRelic. Driver driver.Driver // database/sql/driver.Driver instance // Required, the driver name used when initiating a database connection. DriverName string // i.e. "goracle", "godror", "postgres", "snowflake" // Optional, used when generating a DSN string if a DSNString or DSNFn are not provided. // Automatic DSN generation using DSNData is supported for several database drivers DriverType string // i.e. "oracle", "postgres", "snowflake" // Optional, any data needed to generate the DSN string. DSNData map[string]string // Optional, function to generate the DSN string. *Config.DSNData will be passed in. // // Something like: // func(cfg &db.Config) string { // return fmt.Sprintf("%s/%s@%s", cfg.DSNData["user"], cfg.DSNData["pass"], cfg.DSNData["host"]) // } DSNFn func(*Config) string // Optional, function to parse the DSNString and populate database configuration // properties. DSNParser func(*Config) error // Optional, DSN string used to connect to the database. DSNString string // Location data storage for DSNParser or DSNFn. Loc *time.Location // NewRelic application instance NewRelic *nr.Application // Additional connection parameter storage for DSNParser or DSNFn. Params map[string]string // TLS configuration value storage for DSNParser or DSNFn. TLS *tls.Config }
Config represents a database client configuration, used to create DSN strings or store values parsed out of a DSN string.
type DB ¶
type DB struct { // Database configuration Cfg *Config // Database connection Conn *sql.DB Ctx context.Context }
DB defines an Oracle database connection structure.
func New ¶
New returns a new database connection instance.
- Validate required configuration parameters. - Init config values as necessary. - Begin a NewRelic transaction if applicable. - Instrument the database driver. - Initialize the database client and connect. - Start a shutdown handler.
func (*DB) BeginTx ¶
BeginTx is the constructor for Transaction instances.
Transaction instances handle multiple statements and can be committed or rolled back. If a New Relic application has been provided, transaction metrics will be written there. https://golang.org/pkg/database/sql/#Conn.BeginTx
func (*DB) Close ¶
Close closes the database, releasing any open resources. It is rare to Close a DB, as the DB handle is meant to be long-lived and shared between many goroutines. https://golang.org/pkg/database/sql/#DB.Close
func (*DB) Connect ¶
Connect opens a connection to the database with the provided credentials. If a database connection exists it will be disconnected before trying to reconnect.
func (*DB) ExecContext ¶
func (db *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
ExecContext implements database/sql.ExecContext
func (*DB) Ping ¶
Ping verifies a connection to the database is still alive, establishing a connection if necessary.
func (*DB) Prepare ¶
Prepare is the constructor for Statement instances.
Statement instances handle all transaction logic.
func (*DB) PrepareContext ¶
PrepareContext is the constructor for Statement instances.
Statement instances handle all transaction logic.
func (*DB) Query ¶
Query implements Tx.Query. Query executes a query that returns rows, typically a SELECT. https://golang.org/pkg/database/sql/#Tx.Query
func (*DB) QueryContext ¶
func (db *DB) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
QueryContext implemtnts Tx.Query. Query executes a query that returns rows, typically a SELECT. https://golang.org/pkg/database/sql/#Tx.Query
func (*DB) QueryRowContext ¶
QueryContext implemtnts Tx.Query. QueryRow executes a query that returns row, typically a SELECT. https://golang.org/pkg/database/sql/#Tx.Query
type Statement ¶
type Statement struct {
// contains filtered or unexported fields
}
Statement defines the prepared statement structure and API.
func (*Statement) Err ¶
Err returns the error, if any, that was encountered during iteration. Err may be called after an explicit or implicit Close. https://golang.org/pkg/database/sql/#Rows.Err
func (*Statement) Exec ¶
Exec executes the prepared statement with any arguments that have been added using Bind() calls.
func (*Statement) ExecContext ¶
func (statement *Statement) ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error)
ExecContext executes the prepared statement with any arguments that have been added using Bind() calls.
func (*Statement) MapNext ¶
MapNext prepares the next result row for reading with the Scan method(). It returns true on success, or false if there is no next result row or an error happened while preparing it. Statement.Err should be consulted to distinguish between the two cases. https://golang.org/pkg/database/sql/#Rows.Next
This also performs a Scan operation. Scan copies the columns in the current row into the values pointed at by dest. The number of values in dest must be the same as the number of columns in Rows. https://golang.org/pkg/database/sql/#Rows.Scan
func (*Statement) MapScan ¶
MapScan copies the columns in the current row into the values pointed at by dest. The number of values in dest must be the same as the number of columns in Rows. https://golang.org/pkg/database/sql/#Rows.Scan
func (*Statement) Next ¶
Next prepares the next result row for reading with the Scan method(). It returns true on success, or false if there is no next result row or an error happened while preparing it. Statement.Err should be consulted to distinguish between the two cases. https://golang.org/pkg/database/sql/#Rows.Next
This also performs a Scan operation. Scan copies the columns in the current row into the values pointed at by dest. The number of values in dest must be the same as the number of columns in Rows. https://golang.org/pkg/database/sql/#Rows.Scan
func (*Statement) Query ¶
Query executes the prepared statement with any arguments that have been added using Bind() calls. Query stores a cursor to the result of the SQL query.
func (*Statement) QueryContext ¶
func (statement *Statement) QueryContext(ctx context.Context, args ...interface{}) (*sql.Rows, error)
QueryContext executes the prepared statement with any arguments that have been added using Bind() calls. Query stores a cursor to the result of the SQL query.
func (*Statement) QueryRow ¶
QueryRow executes the prepared statement with any arguments that have been added using Bind() calls. Query stores a cursor to the result of the SQL query.
func (*Statement) QueryRowContext ¶
QueryRowContext executes the prepared statement with any arguments that have been added using Bind() calls. Query stores a cursor to the result of the SQL query.
func (*Statement) Scan ¶
Scan copies the columns in the current row into the values pointed at by dest. The number of values in dest must be the same as the number of columns in Rows. https://golang.org/pkg/database/sql/#Rows.Scan