Documentation
¶
Overview ¶
Package driver defines interfaces to be implemented by database drivers as used by package sql.
Most code should use package sql.
The driver interface has evolved over time. Drivers should implement Connector and DriverContext interfaces. The Connector.Connect and Driver.Open methods should never return ErrBadConn. ErrBadConn should only be returned from Validator, SessionResetter, or a query method if the connection is already in an invalid (e.g. closed) state.
All Conn implementations should implement the following interfaces: Pinger, SessionResetter, and Validator.
If named parameters or context are supported, the driver's Conn should implement: ExecerContext, QueryerContext, ConnPrepareContext, and ConnBeginTx.
To support custom data types, implement NamedValueChecker. NamedValueChecker also allows queries to accept per-query options as a parameter by returning ErrRemoveArgument from CheckNamedValue.
If multiple result sets are supported, Rows should implement RowsNextResultSet. If the driver knows how to describe the types present in the returned result it should implement the following interfaces: RowsColumnTypeScanType, RowsColumnTypeDatabaseTypeName, RowsColumnTypeLength, RowsColumnTypeNullable, and RowsColumnTypePrecisionScale. A given row value may also return a Rows type, which may represent a database cursor value.
Before a connection is returned to the connection pool after use, IsValid is called if implemented. Before a connection is reused for another query, ResetSession is called if implemented. If a connection is never returned to the connection pool but immediately reused, then ResetSession is called prior to reuse but IsValid is not called.
Index ¶
- Variables
- func IsScanValue(v any) bool
- func IsValue(v any) bool
- type ColumnConverterdeprecated
- type Conn
- type ConnBeginTx
- type ConnPrepareContext
- type Connector
- type Driver
- type DriverContext
- type Execerdeprecated
- type ExecerContext
- type IsolationLevel
- type NamedValue
- type NamedValueChecker
- type NotNull
- type Null
- type Pinger
- type Queryerdeprecated
- type QueryerContext
- type Result
- type Rows
- type RowsAffected
- type RowsColumnTypeDatabaseTypeName
- type RowsColumnTypeLength
- type RowsColumnTypeNullable
- type RowsColumnTypePrecisionScale
- type RowsColumnTypeScanType
- type RowsNextResultSet
- type SessionResetter
- type Stmt
- type StmtExecContext
- type StmtQueryContext
- type Tx
- type TxOptions
- type Validator
- type Value
- type ValueConverter
- type Valuer
Constants ¶
This section is empty.
Variables ¶
var Bool boolType
Bool is a ValueConverter that converts input values to bools.
The conversion rules are:
- booleans are returned unchanged
- for integer types, 1 is true 0 is false, other integers are an error
- for strings and []byte, same rules as strconv.ParseBool
- all other types are an error
var DefaultParameterConverter defaultConverter
DefaultParameterConverter is the default implementation of ValueConverter that's used when a Stmt doesn't implement ColumnConverter.
DefaultParameterConverter returns its argument directly if IsValue(arg). Otherwise, if the argument implements Valuer, its Value method is used to return a Value. As a fallback, the provided argument's underlying type is used to convert it to a Value: underlying integer types are converted to int64, floats to float64, bool, string, and []byte to themselves. If the argument is a nil pointer, ConvertValue returns a nil Value. If the argument is a non-nil pointer, it is dereferenced and ConvertValue is called recursively. Other types are an error.
var ErrBadConn = errors.New("driver: bad connection")
ErrBadConn should be returned by a driver to signal to the sql package that a driver.Conn is in a bad state (such as the server having earlier closed the connection) and the sql package should retry on a new connection.
To prevent duplicate operations, ErrBadConn should NOT be returned if there's a possibility that the database server might have performed the operation. Even if the server sends back an error, you shouldn't return ErrBadConn.
Errors will be checked using errors.Is. An error may wrap ErrBadConn or implement the Is(error) bool method.
var ErrRemoveArgument = errors.New("driver: remove argument from query")
ErrRemoveArgument may be returned from NamedValueChecker to instruct the sql package to not pass the argument to the driver query interface. Return when accepting query specific options or structures that aren't SQL query arguments.
var ErrSkip = errors.New("driver: skip fast-path; continue as if unimplemented")
ErrSkip may be returned by some optional interfaces' methods to indicate at runtime that the fast path is unavailable and the sql package should continue as if the optional interface was not implemented. ErrSkip is only supported where explicitly documented.
var Int32 int32Type
Int32 is a ValueConverter that converts input values to int64, respecting the limits of an int32 value.
var ResultNoRows noRows
ResultNoRows is a pre-defined Result for drivers to return when a DDL command (such as a CREATE TABLE) succeeds. It returns an error for both LastInsertId and RowsAffected.
var String stringType
String is a ValueConverter that converts its input to a string. If the value is already a string or []byte, it's unchanged. If the value is of another type, conversion to string is done with fmt.Sprintf("%v", v).
Functions ¶
func IsScanValue ¶
IsScanValue is equivalent to IsValue. It exists for compatibility.