Documentation
¶
Index ¶
- Constants
- Variables
- type Client
- func (c *Client) BeginTransaction() (*Transaction, error)
- func (c *Client) BeginTransactionContext(ctx context.Context) (*Transaction, error)
- func (c *Client) Close()
- func (c *Client) Exec(query string, args ...interface{}) (Result, error)
- func (c *Client) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)
- func (c *Client) GetPrimary() *Database
- func (c *Client) GetReplica() *Database
- func (c *Client) Query(query string, args ...interface{}) (Result, error)
- func (c *Client) QueryContext(ctx context.Context, query string, args ...interface{}) (Result, error)
- func (c *Client) SetReplica(replica *Config) error
- type Config
- type Database
- func (d *Database) ActiveConns() int
- func (d *Database) BeginTransaction() (*Transaction, error)
- func (d *Database) BeginTransactionContext(ctx context.Context) (*Transaction, error)
- func (d *Database) Close() error
- func (d *Database) Exec(query string, args ...interface{}) (Result, error)
- func (d *Database) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)
- func (d *Database) IdleConns() int
- func (d *Database) Ping(ctx context.Context) error
- func (d *Database) Prepare(query string) (*Statement, error)
- func (d *Database) PrepareContext(ctx context.Context, query string) (*Statement, error)
- func (d *Database) Query(query string, args ...interface{}) (Result, error)
- func (d *Database) QueryContext(ctx context.Context, query string, args ...interface{}) (Result, error)
- type DatabaseOption
- func WithDialTimeout(timeout time.Duration) DatabaseOption
- func WithMaxConnLifetime(lifetime time.Duration) DatabaseOption
- func WithMaxIdleConns(limit int) DatabaseOption
- func WithMaxOpenConns(limit int) DatabaseOption
- func WithPingTest(b bool) DatabaseOption
- func WithReadTimeout(timeout time.Duration) DatabaseOption
- func WithWriteTimeout(timeout time.Duration) DatabaseOption
- type Result
- type Statement
- func (s *Statement) Close() error
- func (s *Statement) Exec(args ...interface{}) (Result, error)
- func (s *Statement) ExecContext(ctx context.Context, args ...interface{}) (Result, error)
- func (s *Statement) Query(args ...interface{}) (Result, error)
- func (s *Statement) QueryContext(ctx context.Context, args ...interface{}) (Result, error)
- type Transaction
- func (t *Transaction) Commit() error
- func (t *Transaction) Exec(query string, args ...interface{}) (Result, error)
- func (t *Transaction) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)
- func (t *Transaction) Prepare(query string) (*Statement, error)
- func (t *Transaction) PrepareContext(ctx context.Context, query string) (*Statement, error)
- func (t *Transaction) Query(query string, args ...interface{}) (Result, error)
- func (t *Transaction) QueryContext(ctx context.Context, query string, args ...interface{}) (Result, error)
- func (t *Transaction) Rollback() error
Constants ¶
Variables ¶
var ( ErrorNotImplemented = errors.New("not implemented") ErrorResultNoColumnsFound = errors.New("no columns found") )
var (
ErrorClientInvalidReplica = errors.New("invalid replica")
)
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
func (*Client) BeginTransaction ¶
func (c *Client) BeginTransaction() (*Transaction, error)
BeginTransaction starts a transaction. The default isolation level is dependent on the driver.
func (*Client) BeginTransactionContext ¶
func (c *Client) BeginTransactionContext(ctx context.Context) (*Transaction, error)
BeginTransactionContext starts a transaction.
The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginTx is canceled.
The provided TxOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.
func (*Client) Exec ¶
Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*Client) ExecContext ¶
func (c *Client) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)
ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*Client) GetPrimary ¶
GetPrimary returns the primary database.
func (*Client) GetReplica ¶
GetReplica returns a replica database.
func (*Client) Query ¶
Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
func (*Client) QueryContext ¶
func (c *Client) QueryContext(ctx context.Context, query string, args ...interface{}) (Result, error)
QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
func (*Client) SetReplica ¶
SetReplica sets a new replica database.
type Config ¶
type Config struct { *driver.Config // Extension MaxOpenConns int MaxIdleConns int MaxLifetime time.Duration PingTest bool }
func NewDefaultConfig ¶
New a default config.
type Database ¶
type Database struct {
// contains filtered or unexported fields
}
func NewDatabase ¶
func NewDatabase(addr, dbname, user, passwd string, opts ...DatabaseOption) (*Database, error)
NewDatabase returns a new database connection.
func NewDatabaseWithConfig ¶
NewDatabaseWithConfig returns a new database connection.
func (*Database) ActiveConns ¶
Get the number of connections currently in use.
func (*Database) BeginTransaction ¶
func (d *Database) BeginTransaction() (*Transaction, error)
BeginTransaction starts a transaction. The default isolation level is dependent on the driver.
func (*Database) BeginTransactionContext ¶
func (d *Database) BeginTransactionContext(ctx context.Context) (*Transaction, error)
BeginTransactionContext starts a transaction.
The provided context is used until the transaction is committed or rolled back. If the context is canceled, the sql package will roll back the transaction. Tx.Commit will return an error if the context provided to BeginTx is canceled.
The provided TxOptions is optional and may be nil if defaults should be used. If a non-default isolation level is used that the driver doesn't support, an error will be returned.
func (*Database) Close ¶
Close closes the database and prevents new queries from starting. Close then waits for all queries that have started processing on the server to finish.
It is rare to Close a DB, as the DB handle is meant to be long-lived and shared between many goroutines.
func (*Database) Exec ¶
Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*Database) ExecContext ¶
func (d *Database) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)
ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*Database) Ping ¶
Ping verifies a connection to the database is still alive, establishing a connection if necessary.
func (*Database) Prepare ¶
Prepare creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's Close method when the statement is no longer needed.
func (*Database) PrepareContext ¶
PrepareContext creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's Close method when the statement is no longer needed.
The provided context is used for the preparation of the statement, not for the execution of the statement.
type DatabaseOption ¶
type DatabaseOption func(conf *Config)
func WithMaxConnLifetime ¶
func WithMaxConnLifetime(lifetime time.Duration) DatabaseOption
MaxConnLifetime
type Result ¶
type Result struct {
// contains filtered or unexported fields
}
func (Result) LastInsertId ¶
LastInsertId 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 (Result) RowsAffected ¶
RowsAffected returns the number of rows affected by an update, insert, or delete. Not every database or database driver may support this.
type Statement ¶
type Statement struct {
// contains filtered or unexported fields
}
func (*Statement) Exec ¶
Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*Statement) ExecContext ¶
ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.
type Transaction ¶
type Transaction struct {
// contains filtered or unexported fields
}
func (*Transaction) Exec ¶
func (t *Transaction) Exec(query string, args ...interface{}) (Result, error)
Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*Transaction) ExecContext ¶
func (t *Transaction) ExecContext(ctx context.Context, query string, args ...interface{}) (Result, error)
ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*Transaction) Prepare ¶
func (t *Transaction) Prepare(query string) (*Statement, error)
Prepare creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's Close method when the statement is no longer needed.
func (*Transaction) PrepareContext ¶
PrepareContext creates a prepared statement for later queries or executions. Multiple queries or executions may be run concurrently from the returned statement. The caller must call the statement's Close method when the statement is no longer needed.
The provided context is used for the preparation of the statement, not for the execution of the statement.
func (*Transaction) Query ¶
func (t *Transaction) Query(query string, args ...interface{}) (Result, error)
Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
func (*Transaction) QueryContext ¶
func (t *Transaction) QueryContext(ctx context.Context, query string, args ...interface{}) (Result, error)
QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
func (*Transaction) Rollback ¶
func (t *Transaction) Rollback() error
Rollback aborts the transaction.