sqlx

package
v0.1.22 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

README

database/sql

项目简介

MySQL数据库驱动,进行封装加入了链路追踪和统计。

如果需要SQL级别的超时管理 可以在业务代码里面使用context.WithDeadline实现 推荐超时配置放到application.toml里面 方便热加载

依赖包

  1. Go-MySQL-Driver

Documentation

Index

Constants

View Source
const (
	UNKNOWN = iota
	QUESTION
	DOLLAR
	NAMED
	AT
)

Bindvar types supported by Rebind, BindMap and BindStruct.

Variables

View Source
var (
	DeletedNo  int64 = 0
	DeletedYes int64 = 1
)

soft delete flag

View Source
var (
	//	// ErrStmtNil prepared stmt error
	//	ErrStmtNil = errors.New("sql: prepare failed and stmt nil")
	// ErrNoMaster is returned by Master when call master multiple times.
	ErrNoMaster = errors.New("sql: no master instance")
)
View Source
var (
	ErrNotFound = sql.ErrNoRows
)
View Source
var NameMapper = strings.ToLower

NameMapper is used to map column names to struct field names. By default, it uses strings.ToLower to lowercase struct field names. It can be set to whatever you want, but it is encouraged to be set before sqlx is used as name-to-field mappings are cached after first use on a type.

Functions

func BindDriver

func BindDriver(driverName string, bindType int)

BindDriver sets the BindType for driverName to bindType.

func BindNamed

func BindNamed(bindType int, query string, arg interface{}) (string, []interface{}, error)

BindNamed binds a struct or a map to a query with named parameters. DEPRECATED: use sqlx.Named` instead of this, it may be removed in future.

func BindType

func BindType(driverName string) int

BindType returns the bindtype for a given database given a drivername.

func CheckExists

func CheckExists(ctx context.Context, db *DB, table string, params map[string]interface{}) (bool, error)

CheckExists 检查是否存在

func In

func In(query string, args ...interface{}) (string, []interface{}, error)

In expands slice values in args, returning the modified query string and a new arg list that can be executed by a database. The `query` should use the `?` bindVar. The return value uses the `?` bindVar.

func InInt32List

func InInt32List(elems []int32) string

func InInt64List

func InInt64List(elems []int64) string

func InStringList

func InStringList(elems []string) string

func InUint32List

func InUint32List(elems []uint32) string

func InUint64List

func InUint64List(elems []uint64) string

func IsDuplicate

func IsDuplicate(err error) bool

IsDuplicate Check if MySQL error is a Error Code: 1062. Duplicate entry ... for key ...

func IsMissingDb

func IsMissingDb(err error) bool

IsMissingDb Check if MySQL error is a Error Code: 1049. Unknown database ...

func Must

func Must(sc StoreChannel) interface{}

func Named

func Named(query string, arg interface{}) (string, []interface{}, error)

Named takes a query using named parameters and an argument and returns a new query with a list of args that can be executed by a database. The return value uses the `?` bindvar.

func Rebind

func Rebind(bindType int, query string) string

Rebind a query from the default bindtype (QUESTION) to the target bindtype.

Types

type CommonDAO

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

func NewCommonDAO

func NewCommonDAO(db *DB) *CommonDAO

func (*CommonDAO) CalcSize

func (dao *CommonDAO) CalcSize(ctx context.Context, table string, params map[string]interface{}) int

func (*CommonDAO) CalcSizeByWhere

func (dao *CommonDAO) CalcSizeByWhere(ctx context.Context, table, where string) int

func (*CommonDAO) CheckExists

func (dao *CommonDAO) CheckExists(ctx context.Context, table string, params map[string]interface{}) bool

CheckExists 检查是否存在

func (*CommonDAO) DB

func (dao *CommonDAO) DB() *DB

type Config

type Config struct {
	DSN            string   // write data source name.
	ReadDSN        []string `json:",optional"` // read data source name.
	Active         int      `json:",optional"` // pool
	Idle           int      `json:",optional"` // pool
	IdleTimeout    string   `json:",optional"` // connect max life time.
	DisableStmtLog bool     `json:",default=false"`
	SlowThreshold  int64    `json:",default=500"`
}

Config mysql config.

type DB

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

DB database.

func NewMySQL

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

NewMySQL new db and retry connection when has error.

func Open

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

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

func (db *DB) Master() *DB

Master return *DB instance direct use master conn use this *DB instance only when you have some reason need to get result without any delay.

func (*DB) NamedExec

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

NamedExec using this DB. Any named placeholder parameters are replaced with fields from arg.

func (*DB) NamedQueryRow

func (db *DB) NamedQueryRow(c context.Context, v interface{}, query string, arg interface{}) error

NamedQueryRow using this DB. Any named placeholder parameters are replaced with fields from arg.

func (*DB) NamedQueryRowPartial

func (db *DB) NamedQueryRowPartial(ctx context.Context, v interface{}, query string, arg interface{}) error

func (*DB) NamedQueryRows

func (db *DB) NamedQueryRows(c context.Context, v interface{}, query string, arg interface{}) error

func (*DB) NamedQueryRowsPartial

func (db *DB) NamedQueryRowsPartial(c context.Context, v interface{}, query string, arg interface{}) (err error)

func (*DB) Prepare

func (db *DB) Prepare(c context.Context, query string) (sqlx.StmtSession, 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) QueryRow

func (db *DB) QueryRow(c context.Context, v interface{}, query string, args ...interface{}) (err error)

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

func (db *DB) QueryRowPartial(c context.Context, v interface{}, query string, args ...interface{}) error

func (*DB) QueryRows

func (db *DB) QueryRows(c context.Context, v interface{}, query string, args ...interface{}) (err error)

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

func (*DB) QueryRowsPartial

func (db *DB) QueryRowsPartial(c context.Context, v interface{}, query string, args ...interface{}) (err error)

func (*DB) Transact

func (db *DB) Transact(ctx context.Context, fn func(*Tx) error) error

type ExecResult

type ExecResult struct {
	LastInsertId int64
	RowsAffected int64
}

type StoreChannel

type StoreChannel chan StoreResult

func Do

func Do(f func(result *StoreResult)) StoreChannel

type StoreResult

type StoreResult struct {
	Data interface{}
	Cost int
	Err  error
}

func TxWrapper

func TxWrapper(ctx context.Context, db *DB, txF func(*Tx, *StoreResult)) *StoreResult

TxWrapper TxWrapper

type Tx

type Tx struct {
	*reflectx.Mapper
	// contains filtered or unexported fields
}

func (*Tx) Context

func (tx *Tx) Context() context.Context

func (*Tx) Exec

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

func (*Tx) NamedExec

func (tx *Tx) NamedExec(query string, arg interface{}) (sql.Result, error)

NamedExec using this DB. Any named placeholder parameters are replaced with fields from arg.

func (*Tx) NamedQueryRow

func (tx *Tx) NamedQueryRow(v interface{}, query string, arg interface{}) error

NamedQueryRow using this DB. Any named placeholder parameters are replaced with fields from arg.

func (*Tx) NamedQueryRowPartial

func (tx *Tx) NamedQueryRowPartial(v interface{}, query string, arg interface{}) error

func (*Tx) NamedQueryRows

func (tx *Tx) NamedQueryRows(v interface{}, query string, arg interface{}) error

func (*Tx) NamedQueryRowsPartial

func (tx *Tx) NamedQueryRowsPartial(v interface{}, query string, arg interface{}) error

func (*Tx) Prepare

func (tx *Tx) Prepare(query string) (sqlx.StmtSession, error)

func (*Tx) QueryRow

func (tx *Tx) QueryRow(v interface{}, query string, args ...interface{}) error

func (*Tx) QueryRowPartial

func (tx *Tx) QueryRowPartial(v interface{}, query string, args ...interface{}) error

func (*Tx) QueryRows

func (tx *Tx) QueryRows(v interface{}, query string, args ...interface{}) error

func (*Tx) QueryRowsPartial

func (tx *Tx) QueryRowsPartial(v interface{}, query string, args ...interface{}) error

Directories

Path Synopsis
Package reflectx implements extensions to the standard reflect lib suitable for implementing marshalling and unmarshalling packages.
Package reflectx implements extensions to the standard reflect lib suitable for implementing marshalling and unmarshalling packages.

Jump to

Keyboard shortcuts

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