tidb

package
v0.0.0-...-3ecd8c4 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2020 License: MIT Imports: 17 Imported by: 0

README

database/tidb

项目简介

TiDB数据库驱动 对mysql驱动进行封装

功能

  1. 支持discovery服务发现 多节点直连
  2. 支持通过lvs单一地址连接
  3. 支持prepare绑定多个节点
  4. 支持动态增减节点负载均衡
  5. 日志区分运行节点

依赖包

1.Go-MySQL-Driver

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrStmtNil prepared stmt error
	ErrStmtNil = errors.New("sql: prepare failed and stmt nil")
	// ErrNoRows is returned by Scan when QueryRow doesn't return a row.
	// In such a case, QueryRow returns a placeholder *Row value that defers
	// this error until a Scan.
	ErrNoRows = sql.ErrNoRows
	// ErrTxDone transaction done.
	ErrTxDone = sql.ErrTxDone
)

Functions

This section is empty.

Types

type Config

type Config struct {
	DSN          string          // dsn
	Active       int             // pool
	Idle         int             // pool
	IdleTimeout  time.Duration   // connect max life time.
	QueryTimeout time.Duration   // query sql timeout
	ExecTimeout  time.Duration   // execute sql timeout
	TranTimeout  time.Duration   // transaction sql timeout
	Breaker      *breaker.Config // breaker
}

Config mysql config.

type DB

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

DB database.

func NewTiDB

func NewTiDB(c *Config) (db *DB)

NewTiDB new db and retry connection when has error.

func Open

func Open(c *Config) (db *DB, err 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.

func (*DB) Begin

func (db *DB) Begin(c context.Context) (tx *Tx, err error)

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

func (*DB) Close

func (db *DB) Close() (err error)

Close closes the databases, releasing any open resources.

func (*DB) Exec

func (db *DB) Exec(c context.Context, query string, args ...interface{}) (res sql.Result, err error)

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

func (*DB) Ping

func (db *DB) Ping(c context.Context) (err error)

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

func (*DB) Prepare

func (db *DB) Prepare(query string) (*Stmt, 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 (*DB) Prepared

func (db *DB) Prepared(query string) (s *Stmts)

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

func (db *DB) Query(c context.Context, query string, args ...interface{}) (rows *Rows, err error)

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

func (*DB) QueryRow

func (db *DB) QueryRow(c context.Context, query string, args ...interface{}) *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.

type Row

type Row struct {
	*sql.Row
	// contains filtered or unexported fields
}

Row row.

func (*Row) Scan

func (r *Row) Scan(dest ...interface{}) (err error)

Scan copies the columns from the matched row into the values pointed at by dest.

type Rows

type Rows struct {
	*sql.Rows
	// contains filtered or unexported fields
}

Rows rows.

func (*Rows) Close

func (rs *Rows) Close() (err error)

Close closes the Rows, preventing further enumeration. If Next is called and returns false and there are no further result sets, the Rows are closed automatically and it will suffice to check the result of Err. Close is idempotent and does not affect the result of Err.

type Stmt

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

Stmt prepared stmt.

func (*Stmt) Close

func (s *Stmt) Close() (err error)

Close closes the statement.

func (*Stmt) Exec

func (s *Stmt) Exec(c context.Context, args ...interface{}) (res sql.Result, err error)

Exec executes a prepared statement with the given arguments and returns a Result summarizing the effect of the statement.

func (*Stmt) Query

func (s *Stmt) Query(c context.Context, args ...interface{}) (rows *Rows, err error)

Query executes a prepared query statement with the given arguments and returns the query results as a *Rows.

func (*Stmt) QueryRow

func (s *Stmt) QueryRow(c context.Context, args ...interface{}) (row *Row)

QueryRow executes a prepared query statement with the given arguments. If an error occurs during the execution of the statement, that error will be returned by a call to Scan on the returned *Row, which is always non-nil. 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.

type Stmts

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

Stmts random prepared stmt.

func (*Stmts) Close

func (s *Stmts) Close() (err error)

Close closes the statement.

func (*Stmts) Exec

func (s *Stmts) Exec(c context.Context, args ...interface{}) (res sql.Result, err error)

Exec executes a prepared statement with the given arguments and returns a Result summarizing the effect of the statement.

func (*Stmts) Query

func (s *Stmts) Query(c context.Context, args ...interface{}) (rows *Rows, err error)

Query executes a prepared query statement with the given arguments and returns the query results as a *Rows.

func (*Stmts) QueryRow

func (s *Stmts) QueryRow(c context.Context, args ...interface{}) (row *Row)

QueryRow executes a prepared query statement with the given arguments. If an error occurs during the execution of the statement, that error will be returned by a call to Scan on the returned *Row, which is always non-nil. 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.

type Tx

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

Tx transaction.

func (*Tx) Commit

func (tx *Tx) Commit() (err error)

Commit commits the transaction.

func (*Tx) Exec

func (tx *Tx) Exec(query string, args ...interface{}) (res sql.Result, err error)

Exec executes a query that doesn't return rows. For example: an INSERT and UPDATE.

func (*Tx) Prepare

func (tx *Tx) Prepare(query string) (*Stmt, error)

Prepare creates a prepared statement for use within a transaction. The returned statement operates within the transaction and can no longer be used once the transaction has been committed or rolled back. To use an existing prepared statement on this transaction, see Tx.Stmt.

func (*Tx) Query

func (tx *Tx) Query(query string, args ...interface{}) (rows *Rows, err error)

Query executes a query that returns rows, typically a SELECT.

func (*Tx) QueryRow

func (tx *Tx) QueryRow(query string, args ...interface{}) *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.

func (*Tx) Rollback

func (tx *Tx) Rollback() (err error)

Rollback aborts the transaction.

func (*Tx) Stmt

func (tx *Tx) Stmt(stmt *Stmt) *Stmt

Stmt returns a transaction-specific prepared statement from an existing statement.

func (*Tx) Stmts

func (tx *Tx) Stmts(stmt *Stmts) *Stmt

Stmts returns a transaction-specific prepared statement from an existing statement.

Jump to

Keyboard shortcuts

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