mysql

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2020 License: Apache-2.0 Imports: 6 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidConfig invalid mysql config
	ErrInvalidConfig = errors.New("mysql config invalid")
)

Error define

Functions

This section is empty.

Types

type Config

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

Config mysql config master = "127.0.0.1" slaves = ["127.0.0.1", "127.0.0.2"] port = 3306 username = "root" password = "root" dbname = "test" max_open_conn_count = 100 (设置最大打开的连接数,默认值为0表示不限制) max_idle_conn_count = 50 (设置连接池数量) conn_wait_time_ms = 200 conn_idle_time_ms = 21600000 conn_timeout_ms = 5000 (dsn parameter) read_timeout_ms = 3000 (dsn parameter) write_timeout_ms = 3000 (dsn parameter)

func (*Config) SetConnIdleTimeMs

func (c *Config) SetConnIdleTimeMs(connIdleTimeMs int)

SetConnIdleTimeMs set max idle time ms

func (*Config) SetConnTimeoutMs

func (c *Config) SetConnTimeoutMs(connTimeoutMs int)

SetConnTimeoutMs set connection timeout ms

func (*Config) SetConnWaitTimeMs

func (c *Config) SetConnWaitTimeMs(connWaitTimeMs int)

SetConnWaitTimeMs set max wait time ms

func (*Config) SetDBName

func (c *Config) SetDBName(dbName string)

SetDBName set mysql database

func (*Config) SetMaster

func (c *Config) SetMaster(master string)

SetMaster set mysql master server

func (*Config) SetMaxIdleConnCount

func (c *Config) SetMaxIdleConnCount(maxIdleConnCount int)

SetMaxIdleConnCount set mysql max idle connections 设置连接池数量,通常设置小于最大的连接数

func (*Config) SetMaxOpenConnCount

func (c *Config) SetMaxOpenConnCount(maxOpenConnCount int)

SetMaxOpenConnCount set mysql max open connections 设置最大的连接数, 可以避免并发太高导致连接mysql出现too many connections的错误。

func (*Config) SetPassword

func (c *Config) SetPassword(password string)

SetPassword set mysql password

func (*Config) SetPort

func (c *Config) SetPort(port int)

SetPort set mysql server port

func (*Config) SetReadTimeoutMs

func (c *Config) SetReadTimeoutMs(readTimeoutMs int)

SetReadTimeoutMs set read timeout ms

func (*Config) SetSlaves

func (c *Config) SetSlaves(slaves []string)

SetSlaves set mysql slaves

func (*Config) SetUserName

func (c *Config) SetUserName(userName string)

SetUserName set mysql user name

func (*Config) SetWriteTimeoutMs

func (c *Config) SetWriteTimeoutMs(writeTimeoutMs int)

SetWriteTimeoutMs set write timeout ms

type Mysql

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

Mysql define

func NewMysql

func NewMysql(conf *Config) (*Mysql, error)

NewMysql new mysql

func (*Mysql) Begin

func (m *Mysql) Begin() (*sql.Tx, error)

Begin starts a transaction. The default isolation level is dependent on the driver.

func (*Mysql) BeginTx

func (m *Mysql) BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)

BeginTx 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 (*Mysql) Close

func (m *Mysql) Close() error

Close closes the database, releasing any open resources. It is rare to Close a DB, as the DB handle is meant to be long-lived and shared between many goroutines.

func (*Mysql) Conn

func (m *Mysql) Conn(ctx context.Context) (*sql.Conn, error)

Conn returns a single connection by either opening a new connection or returning an existing connection from the connection pool. Conn will block until either a connection is returned or ctx is canceled. Queries run on the same Conn will be run in the same database session.

Every Conn must be returned to the database pool after use by calling Conn.Close.

func (*Mysql) Exec

func (m *Mysql) Exec(query string, args ...interface{}) (sql.Result, error)

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

func (*Mysql) ExecContext

func (m *Mysql) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

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

func (*Mysql) Master

func (m *Mysql) Master() *sql.DB

Master return master DB

func (*Mysql) Query

func (m *Mysql) Query(query string, args ...interface{}) (*sql.Rows, error)

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

func (*Mysql) QueryContext

func (m *Mysql) QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

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

func (*Mysql) QueryRow

func (m *Mysql) QueryRow(query string, args ...interface{}) *sql.Row

QueryRow executes a query that is expected to return at most one row. QueryRow always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

func (*Mysql) QueryRowContext

func (m *Mysql) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row

QueryRowContext executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called. If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.

Jump to

Keyboard shortcuts

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