Documentation
¶
Index ¶
- Constants
- func ExecAndLastInsertID(ctx context.Context, conn Tx, query string, args ...any) (id int64, err error)
- func ExecAndRowsAffected(ctx context.Context, conn Tx, query string, args ...any) (id int64, err error)
- func MySQLOpen(dataSourceName string) (*sql.DB, error)
- func Open(driverName, dataSourceName string, maxConn int, ...) (*sql.DB, error)
- func WithTx(ctx context.Context, db BeginTx, f func(Tx) error) (err error)
- type Any
- type BeginTx
- type Tx
Constants ¶
const ( // MaxConn is the maximum number of open connections to the database. MaxConn = 25 // MaxLifetime is the maximum amount of time a connection may be reused. MaxLifetime = 5 * time.Minute // Timeout is the default timeout duration. Timeout = 5 * time.Second )
const MySQLDriver = "mysql"
MySQLDriver is name of MySQL driver.
Variables ¶
This section is empty.
Functions ¶
func ExecAndLastInsertID ¶
func ExecAndLastInsertID(ctx context.Context, conn Tx, query string, args ...any) (id int64, err error)
ExecAndLastInsertID executes a prepared statement with the given arguments and returns the integer generated by the database in response to a command. Typically, this will be from an "auto increment" column when inserting a new row. Not all databases support this feature, and the syntax of such statements varies.
func ExecAndRowsAffected ¶
func ExecAndRowsAffected(ctx context.Context, conn Tx, query string, args ...any) (id int64, err error)
ExecAndRowsAffected executes a prepared statement with the given arguments and returns the number of rows affected by an update, insert, or delete. Not every database or database driver may support this.
func Open ¶
func Open(driverName, dataSourceName string, maxConn int, maxLifetime, pingTimeout time.Duration) (*sql.DB, error)
Open opens a database specified by its database driver name and a driver-specific data source name, usually consisting of at least a database name and connection information. It also validates the data source name by calling a Ping.
Types ¶
type Any ¶
Any is map of string containing any values.
func QueryAny ¶
QueryAny executes a query that is expected to return at most one row, so one Any. The args are for any placeholder parameters in the query. If multiple rows are returned by the query, the Scan method discards all but the first.
func QueryAnyRows ¶
QueryAnyRows executes a query that returns rows as slice of Any, typically a SELECT. The args are for any placeholder parameters in the query.
type BeginTx ¶
type BeginTx interface { // BeginTx starts a transaction. BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error) }
BeginTx represents the database connection who handles transactions.
type Tx ¶
type Tx interface { // ExecContext executes a prepared statement with the given arguments and // returns a Result summarizing the effect of the statement. ExecContext(ctx context.Context, query string, args ...any) (sql.Result, error) // PrepareContext creates a prepared statement for use within a transaction. PrepareContext(ctx context.Context, query string) (*sql.Stmt, error) // QueryContext executes a query that returns rows, typically a SELECT. // The args are for any placeholder parameters in the query. QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error) // QueryRowContext executes a query that is expected to return at most one row. // It always returns a non-nil value. QueryRowContext(ctx context.Context, query string, args ...any) *sql.Row }
Tx lists all the methods used inside a SQL transaction.