Documentation
¶
Overview ¶
Package tagsql implements a tagged wrapper for databases.
This package also handles hides context cancellation from database drivers that don't support it.
Index ¶
Constants ¶
const ( // CockroachName is the name when tagsql wraps a Cockroach DB connection. CockroachName string = "cockroach" // PostgresName is the name when tagsql wraps a Cockroach DB connection. PostgresName string = "postgres" // SpannerName is the name when tagsql wraps a Cockroach DB connection. SpannerName string = "spanner" // SqliteName is the name when tagsql wraps a SQLite3 connection. SqliteName string = "sqlite" )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Conn ¶
type Conn interface {
BeginTx(ctx context.Context, txOptions *sql.TxOptions) (Tx, error)
Close() error
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
PingContext(ctx context.Context) error
PrepareContext(ctx context.Context, query string) (Stmt, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
Raw(ctx context.Context, f func(driverConn interface{}) error) (err error)
}
Conn is an interface for *sql.Conn-like connections.
type ContextSupport ¶
type ContextSupport byte
ContextSupport returns the level of context support a driver has.
const ( SupportBasic ContextSupport = 1 << 0 SupportTransactions ContextSupport = 1 << 1 SupportNone ContextSupport = 0 SupportAll ContextSupport = SupportBasic | SupportTransactions )
Constants for defining context level support.
func DetectContextSupport ¶
func DetectContextSupport(db *sql.DB) (ContextSupport, error)
DetectContextSupport detects *sql.DB driver without importing the specific packages.
func (ContextSupport) Basic ¶
func (v ContextSupport) Basic() bool
Basic returns true when driver supports basic contexts.
func (ContextSupport) Transactions ¶
func (v ContextSupport) Transactions() bool
Transactions returns true when driver supports contexts inside transactions.
type DB ¶
type DB interface {
Name() string
BeginTx(ctx context.Context, txOptions *sql.TxOptions) (Tx, error)
Conn(ctx context.Context) (Conn, error)
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
PingContext(ctx context.Context) error
PrepareContext(ctx context.Context, query string) (Stmt, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
Close() error
SetConnMaxLifetime(d time.Duration)
SetMaxIdleConns(n int)
SetMaxOpenConns(n int)
Stats() sql.DBStats
}
DB implements a wrapper for *sql.DB-like database.
The wrapper adds tracing to all calls. It also adds context handling compatibility for different databases.
func AllowContext ¶
AllowContext turns a *sql.DB into a DB which uses context calls.
func Open ¶
func Open(ctx context.Context, driverName, dataSourceName string, recorder *flightrecorder.Box) (DB, error)
Open opens *sql.DB and wraps the implementation with tagging and flight recorder.
func WithoutContext ¶
WithoutContext turns a *sql.DB into a DB-matching that redirects context calls to regular calls.
func WrapWithRecorder ¶ added in v1.129.2
func WrapWithRecorder(db *sql.DB, recorder *flightrecorder.Box) DB
WrapWithRecorder turns a *sql.DB into a DB-matching interface including flight recorder.
type ExecQueryer ¶ added in v1.134.1
type ExecQueryer interface {
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
}
ExecQueryer contains methods for executing queries.
type Rows ¶
type Rows interface {
Close() error
ColumnTypes() ([]*sql.ColumnType, error)
Columns() ([]string, error)
Err() error
Next() bool
NextResultSet() bool
Scan(dest ...interface{}) error
}
Rows implements a wrapper for *sql.Rows.
type Stmt ¶
type Stmt interface {
// ExecContext and other Context methods take a context for tracing and also
// pass the context to the underlying database, if this tagsql instance is
// configured to do so. (By default, lib/pq does not ever, and
// mattn/go-sqlite3 does not for transactions).
ExecContext(ctx context.Context, args ...interface{}) (sql.Result, error)
QueryContext(ctx context.Context, args ...interface{}) (Rows, error)
QueryRowContext(ctx context.Context, args ...interface{}) *sql.Row
Close() error
}
Stmt is an interface for *sql.Stmt.
type Tx ¶
type Tx interface {
// ExecContext and other Context methods take a context for tracing and also
// pass the context to the underlying database, if this tagsql instance is
// configured to do so. (By default, lib/pq does not ever, and
// mattn/go-sqlite3 does not for transactions).
ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
PrepareContext(ctx context.Context, query string) (Stmt, error)
QueryContext(ctx context.Context, query string, args ...interface{}) (Rows, error)
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
Commit() error
Rollback() error
}
Tx is an interface for *sql.Tx-like transactions.