mysql

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultMaxOpenConns = 50
	DefaultMaxIdleConns = 10
	DefaultMaxLifetime  = 30 * time.Second
	DefaultDialTimeout  = 2 * time.Second
	DefaultReadTimeout  = 0 * time.Second
	DefaultWriteTimeout = 0 * time.Second
)

Variables

View Source
var (
	ErrorNotImplemented       = errors.New("not implemented")
	ErrorResultNoColumnsFound = errors.New("no columns found")
)
View Source
var (
	ErrorClientInvalidReplica = errors.New("invalid replica")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

func NewClient

func NewClient(primary *Config) (*Client, error)

NewClient returns a new client.

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) Close

func (c *Client) Close()

Close stop the client.

func (*Client) Exec

func (c *Client) 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 (*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

func (c *Client) GetPrimary() *Database

GetPrimary returns the primary database.

func (*Client) GetReplica

func (c *Client) GetReplica() *Database

GetReplica returns a replica database.

func (*Client) Query

func (c *Client) 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 (*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

func (c *Client) SetReplica(replica *Config) error

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

func NewDefaultConfig(addr, dbname, user, passwd string, ping bool) *Config

New a default config.

func (*Config) UniqId

func (c *Config) UniqId() string

Generate an unique ID.

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

func NewDatabaseWithConfig(conf *Config) (*Database, error)

NewDatabaseWithConfig returns a new database connection.

func (*Database) ActiveConns

func (d *Database) ActiveConns() int

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

func (d *Database) Close() error

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

func (d *Database) 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 (*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) IdleConns

func (d *Database) IdleConns() int

Get the number of idle connections.

func (*Database) Ping

func (d *Database) Ping(ctx context.Context) error

Ping verifies a connection to the database is still alive, establishing a connection if necessary.

func (*Database) Prepare

func (d *Database) 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 (*Database) PrepareContext

func (d *Database) PrepareContext(ctx context.Context, query string) (*Statement, error)

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 (*Database) Query

func (d *Database) 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 (*Database) QueryContext

func (d *Database) 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.

type DatabaseOption

type DatabaseOption func(conf *Config)

func WithDialTimeout

func WithDialTimeout(timeout time.Duration) DatabaseOption

DialTimeout

func WithMaxConnLifetime

func WithMaxConnLifetime(lifetime time.Duration) DatabaseOption

MaxConnLifetime

func WithMaxIdleConns

func WithMaxIdleConns(limit int) DatabaseOption

MaxIdleConns

func WithMaxOpenConns

func WithMaxOpenConns(limit int) DatabaseOption

MaxOpenConns

func WithPingTest

func WithPingTest(b bool) DatabaseOption

PingTest

func WithReadTimeout

func WithReadTimeout(timeout time.Duration) DatabaseOption

ReadTimeout

func WithWriteTimeout

func WithWriteTimeout(timeout time.Duration) DatabaseOption

WriteTimeout

type Result

type Result struct {
	// contains filtered or unexported fields
}

func (Result) Hit

func (r Result) Hit() string

Hit returns the data source.

func (Result) LastInsertId

func (r Result) LastInsertId() (int64, error)

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) Row

func (r Result) Row() (row map[string]string, err error)

Get first row from the set.

func (Result) Rows

func (r Result) Rows() (rows []map[string]string, err error)

Get all rows from the set.

func (Result) RowsAffected

func (r Result) RowsAffected() (int64, error)

RowsAffected returns the number of rows affected by an update, insert, or delete. Not every database or database driver may support this.

func (Result) Unmarshal

func (r Result) Unmarshal(rows interface{}) error

Unmarshal all rows to a declared variable.

type Statement

type Statement struct {
	// contains filtered or unexported fields
}

func (*Statement) Close

func (s *Statement) Close() error

Close closes the statement.

func (*Statement) Exec

func (s *Statement) Exec(args ...interface{}) (Result, error)

Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*Statement) ExecContext

func (s *Statement) ExecContext(ctx context.Context, args ...interface{}) (Result, error)

ExecContext executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*Statement) Query

func (s *Statement) Query(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 (*Statement) QueryContext

func (s *Statement) QueryContext(ctx context.Context, args ...interface{}) (Result, error)

QueryContext executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

type Transaction

type Transaction struct {
	// contains filtered or unexported fields
}

func (*Transaction) Commit

func (t *Transaction) Commit() error

Commit commits the transaction.

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

func (t *Transaction) PrepareContext(ctx context.Context, query string) (*Statement, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL