mysql

package
v1.8.7 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2021 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	BETWEEN    = "BETWEEN"
	NOTBETWEEN = "NOT BETWEEN"
	IN         = "IN"
	NOTIN      = "NOT IN"
	AND        = "AND"
	OR         = "OR"
	ISNULL     = "IS NULL"
	ISNOTNULL  = "IS NOT NULL"
	EQUAL      = "="
	NOTEQUAL   = "!="
	LIKE       = "LIKE"
	JOIN       = "JOIN"
	INNERJOIN  = "INNER JOIN"
	LEFTJOIN   = "LEFT JOIN"
	RIGHTJOIN  = "RIGHT JOIN"
	UNION      = "UNION"
	UNIONALL   = "UNION ALL"
	DESC       = "DESC"
	ASC        = "ASC"
)

Variables

This section is empty.

Functions

func SetTrace

func SetTrace(t bool)

Types

type Config

type Config struct {
	Username        string        //账号 root
	Password        string        //密码
	Host            string        //host localhost
	Port            string        //端口 3306
	Charset         string        //字符编码 utf8mb4
	Database        string        //默认连接数据库
	ConnMaxLifetime time.Duration //设置一个连接的最长生命周期,因为数据库本身对连接有一个超时时间的设置,如果超时时间到了数据库会单方面断掉连接,此时再用连接池内的连接进行访问就会出错, 因此这个值往往要小于数据库本身的连接超时时间
	ConnMaxIdleTime time.Duration //设置连接的生命周期的最大
	MaxIdleConns    int           //设置闲置的连接数,连接池里面允许Idel的最大连接数, 这些Idel的连接 就是并发时可以同时获取的连接,也是用完后放回池里面的互用的连接, 从而提升性能
	MaxOpenConns    int           //设置最大打开的连接数,默认值为0表示不限制。控制应用于数据库建立连接的数量,避免过多连接压垮数据库。
}

Config 数据库配置

func (*Config) URI

func (config *Config) URI() string

URI 构造数据库连接

type Connection

type Connection interface {
	Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)
	Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
	NewQuery(ctx context.Context) *QueryBuilder
	GetLastSql() Sql
	LastSql(query string, args ...interface{})
}

Connection 链接

type DbError

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

func NewDBError

func NewDBError(msg string, sql Sql) DbError

func (DbError) Error

func (e DbError) Error() string

type Epr

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

func NewEpr

func NewEpr(value string) Epr

func (Epr) ToString

func (e Epr) ToString() string

type Grammar

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

Grammar sql 语法

func (Grammar) Delete

func (g Grammar) Delete() string

func (Grammar) Insert

func (g Grammar) Insert() string

func (Grammar) InsertUpdate

func (g Grammar) InsertUpdate() string

func (Grammar) Replace

func (g Grammar) Replace() string

func (Grammar) Select

func (g Grammar) Select() string

Select 构造select

func (Grammar) ToSql

func (g Grammar) ToSql() string

func (Grammar) Update

func (g Grammar) Update() string

type NullBool added in v1.4.12

type NullBool struct{ sql.NullBool }

func NewBool added in v1.4.12

func NewBool(b bool) NullBool

func NewNullBool added in v1.4.12

func NewNullBool() NullBool

func (NullBool) Convert added in v1.4.12

func (n NullBool) Convert() string

func (NullBool) IsEmpty added in v1.4.12

func (n NullBool) IsEmpty() bool

func (NullBool) IsValid added in v1.4.12

func (n NullBool) IsValid() bool

func (NullBool) MarshalJSON added in v1.4.12

func (nb NullBool) MarshalJSON() ([]byte, error)

func (NullBool) Result added in v1.4.12

func (ns NullBool) Result() interface{}

func (*NullBool) UnmarshalJSON added in v1.4.12

func (nb *NullBool) UnmarshalJSON(b []byte) error

type NullDate added in v1.4.12

type NullDate struct {
	sql.NullString
}

NullDate 空字符串

func NewDate added in v1.4.12

func NewDate(s interface{}) NullDate

func NewNullDate added in v1.4.12

func NewNullDate() NullDate

func (NullDate) IsEmpty added in v1.4.12

func (ns NullDate) IsEmpty() bool

func (NullDate) IsValid added in v1.4.12

func (ns NullDate) IsValid() bool

func (NullDate) MarshalJSON added in v1.4.12

func (ns NullDate) MarshalJSON() ([]byte, error)

func (NullDate) Result added in v1.4.12

func (ns NullDate) Result() interface{}

func (*NullDate) Scan added in v1.4.12

func (nt *NullDate) Scan(value interface{}) error

func (*NullDate) UnmarshalJSON added in v1.4.12

func (ns *NullDate) UnmarshalJSON(b []byte) error

type NullDateTime added in v1.4.12

type NullDateTime struct {
	sql.NullString
}

NullDate 空字符串

func NewDateTime added in v1.4.12

func NewDateTime(s interface{}) NullDateTime

func NewNullDateTime added in v1.4.12

func NewNullDateTime() NullDateTime

func (NullDateTime) IsEmpty added in v1.4.12

func (ns NullDateTime) IsEmpty() bool

func (NullDateTime) IsValid added in v1.4.12

func (ns NullDateTime) IsValid() bool

func (NullDateTime) MarshalJSON added in v1.4.12

func (ns NullDateTime) MarshalJSON() ([]byte, error)

func (NullDateTime) Result added in v1.4.12

func (ns NullDateTime) Result() interface{}

func (*NullDateTime) Scan added in v1.4.13

func (nt *NullDateTime) Scan(value interface{}) error

func (*NullDateTime) UnmarshalJSON added in v1.4.12

func (ns *NullDateTime) UnmarshalJSON(b []byte) error

type NullFloat64 added in v1.4.12

type NullFloat64 struct{ sql.NullFloat64 }

func NewFloat64 added in v1.4.12

func NewFloat64(f float64) NullFloat64

func NewNullFloat64 added in v1.4.12

func NewNullFloat64() NullFloat64

func (NullFloat64) Convert added in v1.4.12

func (n NullFloat64) Convert() string

func (NullFloat64) IsEmpty added in v1.4.12

func (n NullFloat64) IsEmpty() bool

func (NullFloat64) IsValid added in v1.4.12

func (n NullFloat64) IsValid() bool

func (NullFloat64) MarshalJSON added in v1.4.12

func (nf NullFloat64) MarshalJSON() ([]byte, error)

func (NullFloat64) Result added in v1.4.12

func (ns NullFloat64) Result() interface{}

func (*NullFloat64) UnmarshalJSON added in v1.4.12

func (nf *NullFloat64) UnmarshalJSON(b []byte) error

type NullInt32 added in v1.4.12

type NullInt32 struct{ sql.NullInt32 }

func NewInt32 added in v1.4.12

func NewInt32(i int32) NullInt32

func NewNullInt32 added in v1.4.12

func NewNullInt32() NullInt32

func (NullInt32) Convert added in v1.4.12

func (n NullInt32) Convert() string

func (NullInt32) IsEmpty added in v1.4.12

func (n NullInt32) IsEmpty() bool

func (NullInt32) IsValid added in v1.4.12

func (n NullInt32) IsValid() bool

func (NullInt32) MarshalJSON added in v1.4.12

func (ni NullInt32) MarshalJSON() ([]byte, error)

func (NullInt32) Result added in v1.4.12

func (ns NullInt32) Result() interface{}

func (*NullInt32) UnmarshalJSON added in v1.4.12

func (ni *NullInt32) UnmarshalJSON(b []byte) error

type NullInt64 added in v1.4.12

type NullInt64 struct{ sql.NullInt64 }

func NewInt64 added in v1.4.12

func NewInt64(i int64) NullInt64

func NewNullInt64 added in v1.4.12

func NewNullInt64() NullInt64

func (NullInt64) Convert added in v1.4.12

func (n NullInt64) Convert() string

func (NullInt64) IsEmpty added in v1.4.12

func (n NullInt64) IsEmpty() bool

func (NullInt64) IsValid added in v1.4.12

func (n NullInt64) IsValid() bool

func (NullInt64) MarshalJSON added in v1.4.12

func (ni NullInt64) MarshalJSON() ([]byte, error)

func (NullInt64) Result added in v1.4.12

func (ns NullInt64) Result() interface{}

func (*NullInt64) UnmarshalJSON added in v1.4.12

func (ni *NullInt64) UnmarshalJSON(b []byte) error

type NullString added in v1.4.12

type NullString struct {
	sql.NullString
}

NullString 空字符串

func NewNullString added in v1.4.12

func NewNullString() NullString

func NewString added in v1.4.12

func NewString(s string) NullString

func (NullString) Convert added in v1.4.12

func (n NullString) Convert() string

func (NullString) IsEmpty added in v1.4.12

func (ns NullString) IsEmpty() bool

func (NullString) IsValid added in v1.4.12

func (ns NullString) IsValid() bool

func (NullString) MarshalJSON added in v1.4.12

func (ns NullString) MarshalJSON() ([]byte, error)

func (NullString) Result added in v1.4.12

func (ns NullString) Result() interface{}

func (*NullString) UnmarshalJSON added in v1.4.12

func (ns *NullString) UnmarshalJSON(b []byte) error

type Nullable added in v1.4.12

type Nullable interface {
	IsEmpty() bool
	IsValid() bool
}

type Proxy

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

Proxy 代理

func NewProxy added in v1.1.13

func NewProxy() *Proxy

NewProxy 代理池

func (*Proxy) Read

func (proxy *Proxy) Read() *QueryDb

Read 获取读库

func (*Proxy) SetRead

func (proxy *Proxy) SetRead(query *QueryDb)

SetRead 设置读库

func (*Proxy) SetWrite

func (proxy *Proxy) SetWrite(query *QueryDb)

SetWrite 设置写库

func (*Proxy) Write

func (proxy *Proxy) Write() *QueryDb

Write 获取写库

type ProxyPool

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

ProxyPool 代理池

func NewProxyPool

func NewProxyPool() *ProxyPool

NewProxyPool 代理池

func (*ProxyPool) NameSpace

func (proxypool *ProxyPool) NameSpace(group string) *Proxy

NameSpace 获取分组

func (*ProxyPool) SetNameSpace

func (proxypool *ProxyPool) SetNameSpace(group string, proxy *Proxy)

SetNameSpace 设置组

type QueryBuilder

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

QueryBuilder 查询构造器

func (*QueryBuilder) Between

func (query *QueryBuilder) Between(column string, value1 interface{}, value2 interface{}) *QueryBuilder

Between 构造Between

func (*QueryBuilder) Count

func (query *QueryBuilder) Count() (int64, error)

Count ...

func (*QueryBuilder) Delete

func (query *QueryBuilder) Delete() (int64, error)

Delete .

func (*QueryBuilder) DeleteSQL

func (query *QueryBuilder) DeleteSQL() string

DeleteSQL .

func (*QueryBuilder) Distinct

func (query *QueryBuilder) Distinct() *QueryBuilder

Distinct .

func (*QueryBuilder) Equal

func (query *QueryBuilder) Equal(column string, value interface{}) *QueryBuilder

Equal 构造等于

func (*QueryBuilder) Exec

func (query *QueryBuilder) Exec(sql string, args ...interface{}) (int64, error)

Exec 原始SQl语句执行

func (*QueryBuilder) ExecSQL

func (query *QueryBuilder) ExecSQL(sql string, args ...interface{}) string

ExecSQL 原始SQl语句执行

func (*QueryBuilder) GroupBy

func (query *QueryBuilder) GroupBy(groups ...string) *QueryBuilder

GroupBy .

func (*QueryBuilder) In

func (query *QueryBuilder) In(column string, value ...interface{}) *QueryBuilder

In 构造 in语句

func (*QueryBuilder) InnerJoin

func (query *QueryBuilder) InnerJoin(tablename string, on string) *QueryBuilder

func (*QueryBuilder) Insert

func (query *QueryBuilder) Insert(data interface{}) (int64, error)

Insert 插入数据

func (*QueryBuilder) InsertSQL

func (query *QueryBuilder) InsertSQL(data interface{}) string

InsertSQL 获取SQL语句

func (*QueryBuilder) InsertUpdate

func (query *QueryBuilder) InsertUpdate(insert interface{}, update interface{}) (int64, error)

InsertUpdate ...

func (*QueryBuilder) InsertUpdateSQL

func (query *QueryBuilder) InsertUpdateSQL(insert interface{}, update interface{}) string

InsertUpdateSQL ...

func (*QueryBuilder) IsNULL

func (query *QueryBuilder) IsNULL(column string) *QueryBuilder

IsNULL .

func (*QueryBuilder) IsNotNULL

func (query *QueryBuilder) IsNotNULL(column string) *QueryBuilder

IsNotNULL .

func (*QueryBuilder) IsZero added in v1.7.30

func (query *QueryBuilder) IsZero(v reflect.Value) bool

func (*QueryBuilder) Join

func (query *QueryBuilder) Join(tablename string, on string) *QueryBuilder

Join .

func (*QueryBuilder) LeftJoin

func (query *QueryBuilder) LeftJoin(tablename string, on string) *QueryBuilder

LeftJoin .

func (*QueryBuilder) Like

func (query *QueryBuilder) Like(column string, value interface{}) *QueryBuilder

Like .

func (*QueryBuilder) Limit

func (query *QueryBuilder) Limit(limit int64) *QueryBuilder

Limit .

func (*QueryBuilder) MultiInsert

func (query *QueryBuilder) MultiInsert(datas ...interface{}) (int64, error)

MultiInsert 批量插入

func (*QueryBuilder) MultiInsertSQL

func (query *QueryBuilder) MultiInsertSQL(datas ...interface{}) string

MultiInsertSQL 批量插入

func (*QueryBuilder) NotBetween

func (query *QueryBuilder) NotBetween(column string, value1 interface{}, value2 interface{}) *QueryBuilder

NotBetween 构造不Not Between

func (*QueryBuilder) NotEqual

func (query *QueryBuilder) NotEqual(column string, value interface{}) *QueryBuilder

NotEqual 构造不等于

func (*QueryBuilder) NotIn

func (query *QueryBuilder) NotIn(column string, value ...interface{}) *QueryBuilder

NotIn .

func (*QueryBuilder) NotOrBetween

func (query *QueryBuilder) NotOrBetween(column string, value1 interface{}, value2 interface{}) *QueryBuilder

NotOrBetween 构造 Not Between OR Not Between

func (*QueryBuilder) Offset

func (query *QueryBuilder) Offset(offset int64) *QueryBuilder

Offset .

func (*QueryBuilder) OrBetween

func (query *QueryBuilder) OrBetween(column string, value1 interface{}, value2 interface{}) *QueryBuilder

OrBetween 构造 或者 Between

func (*QueryBuilder) OrEqual

func (query *QueryBuilder) OrEqual(column string, value interface{}) *QueryBuilder

OrEqual 构造或者等于

func (*QueryBuilder) OrIn

func (query *QueryBuilder) OrIn(column string, value ...interface{}) *QueryBuilder

OrIn orin语句

func (*QueryBuilder) OrIsNULL

func (query *QueryBuilder) OrIsNULL(column string) *QueryBuilder

OrIsNULL .

func (*QueryBuilder) OrIsNotNULL

func (query *QueryBuilder) OrIsNotNULL(column string) *QueryBuilder

OrIsNotNULL .

func (*QueryBuilder) OrLike

func (query *QueryBuilder) OrLike(column string, value interface{}) *QueryBuilder

OrLike .

func (*QueryBuilder) OrNotEqual

func (query *QueryBuilder) OrNotEqual(column string, value interface{}) *QueryBuilder

OrNotEqual 构造或者不等于

func (*QueryBuilder) OrNotIn

func (query *QueryBuilder) OrNotIn(column string, value ...interface{}) *QueryBuilder

OrNotIn .

func (*QueryBuilder) OrWhere

func (query *QueryBuilder) OrWhere(column string, value ...interface{}) *QueryBuilder

OrWhere 构造OR条件

func (*QueryBuilder) OrderBy

func (query *QueryBuilder) OrderBy(column string, direction string) *QueryBuilder

OrderBy .

func (*QueryBuilder) QueryRow

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

QueryRow ...

func (*QueryBuilder) QueryRowSQL

func (query *QueryBuilder) QueryRowSQL(sql string, args ...interface{}) string

QueryRowSQL ...

func (*QueryBuilder) QueryRows

func (query *QueryBuilder) QueryRows(sql string, args ...interface{}) *Rows

QueryRows ...

func (*QueryBuilder) QueryRowsSQL

func (query *QueryBuilder) QueryRowsSQL(sql string, args ...interface{}) string

QueryRowsSQL ...

func (*QueryBuilder) Replace

func (query *QueryBuilder) Replace(datas ...interface{}) (int64, error)

Replace 替换

func (*QueryBuilder) ReplaceSQL

func (query *QueryBuilder) ReplaceSQL(datas ...interface{}) string

ReplaceSQL 替换

func (*QueryBuilder) RightJoin

func (query *QueryBuilder) RightJoin(tablename string, on string) *QueryBuilder

RightJoin .

func (*QueryBuilder) Row

func (query *QueryBuilder) Row() *Row

Row 获取一条记录

func (*QueryBuilder) RowSQL

func (query *QueryBuilder) RowSQL() string

RowSQL ...

func (*QueryBuilder) Rows

func (query *QueryBuilder) Rows() *Rows

Rows 获取多条记录

func (*QueryBuilder) RowsSQL

func (query *QueryBuilder) RowsSQL() string

RowsSQL ...

func (*QueryBuilder) Select

func (query *QueryBuilder) Select(columns ...string) *QueryBuilder

Select 查询字段

func (*QueryBuilder) Skip

func (query *QueryBuilder) Skip(offset int64) *QueryBuilder

Skip .

func (*QueryBuilder) Table

func (query *QueryBuilder) Table(tablename ...string) *QueryBuilder

Table 设置操作的表名称

func (*QueryBuilder) ToSql

func (query *QueryBuilder) ToSql(method string) string

ToSql 输出SQL语句

func (*QueryBuilder) Union

func (query *QueryBuilder) Union(unions ...QueryBuilder) *QueryBuilder

Union .

func (*QueryBuilder) UnionAll

func (query *QueryBuilder) UnionAll(unions ...QueryBuilder) *QueryBuilder

UnionAll .

func (*QueryBuilder) UnionLimit

func (query *QueryBuilder) UnionLimit(limit int64) *QueryBuilder

UnionLimit .

func (*QueryBuilder) UnionOffset

func (query *QueryBuilder) UnionOffset(offset int64) *QueryBuilder

UnionOffset .

func (*QueryBuilder) UnionOrderBy

func (query *QueryBuilder) UnionOrderBy(column string, direction string) *QueryBuilder

UnionOrderBy .

func (*QueryBuilder) Update

func (query *QueryBuilder) Update(data interface{}) (int64, error)

Update 更新

func (*QueryBuilder) UpdateSQL

func (query *QueryBuilder) UpdateSQL(data interface{}) string

UpdateSQL 更新

func (*QueryBuilder) Where

func (query *QueryBuilder) Where(column string, value ...interface{}) *QueryBuilder

Where 构造条件语句

type QueryDb

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

QueryDb mysql 配置

func New

func New(config *Config) *QueryDb

New 新连接

func (*QueryDb) Begin

func (querydb *QueryDb) Begin() (*QueryTx, error)

Begin 开启一个事务

func (*QueryDb) Exec

func (querydb *QueryDb) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

Exec 复用执行语句

func (*QueryDb) GetLastSql

func (querydb *QueryDb) GetLastSql() Sql

GetLastSql 获取sql语句

func (*QueryDb) LastSql

func (querydb *QueryDb) LastSql(query string, args ...interface{})

func (*QueryDb) NewQuery

func (querydb *QueryDb) NewQuery(ctx context.Context) *QueryBuilder

NewQuery 生成一个新的查询构造器

func (*QueryDb) Query

func (querydb *QueryDb) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

Query 复用查询语句

type QueryTx

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

QueryTx 事务

func (*QueryTx) Commit

func (querytx *QueryTx) Commit() error

Commit 事务提交

func (*QueryTx) Exec

func (querytx *QueryTx) Exec(ctx context.Context, query string, args ...interface{}) (sql.Result, error)

Exec 复用执行语句

func (*QueryTx) GetLastSql

func (querytx *QueryTx) GetLastSql() Sql

GetLastSql 获取sql语句

func (*QueryTx) LastSql

func (querytx *QueryTx) LastSql(query string, args ...interface{})

func (*QueryTx) NewQuery

func (querytx *QueryTx) NewQuery(ctx context.Context) *QueryBuilder

NewQuery 生成一个新的查询构造器

func (*QueryTx) Query

func (querytx *QueryTx) Query(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)

Query 复用查询语句

func (*QueryTx) Rollback

func (querytx *QueryTx) Rollback() error

Rollback 事务回滚

type Row

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

Row 获取记录

func (*Row) ToArray

func (r *Row) ToArray() (result []string, err error)

ToArray get Array

func (*Row) ToInterface

func (r *Row) ToInterface() (result map[string]interface{}, err error)

func (*Row) ToMap

func (r *Row) ToMap() (result map[string]string, err error)

ToMap get Map

func (*Row) ToStruct

func (r *Row) ToStruct(st interface{}) error

ToStruct get Struct

type Rows

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

Rows get data

func (*Rows) ToArray

func (r *Rows) ToArray() (data [][]string, err error)

ToArray get Array

func (*Rows) ToInterface

func (r *Rows) ToInterface() (data []map[string]interface{}, err error)

ToInterface []map[interface{}]interface{}

func (*Rows) ToMap

func (r *Rows) ToMap() (data []map[string]string, err error)

ToMap get Map

func (*Rows) ToStruct

func (r *Rows) ToStruct(st interface{}) error

ToStruct get Struct

type Sql

type Sql struct {
	Sql      string
	Args     []interface{}
	CostTime time.Duration
}

Sql sql语句

func (Sql) ToJson

func (sql Sql) ToJson() string

ToJson sql语句转出json

func (Sql) ToString

func (sqlRaw Sql) ToString() string

ToString sql语句转出string

Jump to

Keyboard shortcuts

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