Documentation
¶
Index ¶
- Constants
- Variables
- func FormatRows(rows *sql.Rows, handler RowFormat)
- func SqlDelete(args *[]interface{}, table string, where Where) (sql string)
- func SqlDeleteByObj(args *[]interface{}, obj interface{}) (sqlStr string, err error)
- func SqlFindOneObj(args *[]interface{}, where Where, obj interface{}) (sql string, err error)
- func SqlInsert(args *[]interface{}, table string, rows ...Row) (sql string)
- func SqlInsertObjs(args *[]interface{}, rows interface{}) (sql string, err error)
- func SqlUpdate(args *[]interface{}, table string, set Row, where Where) (sql string)
- func SqlUpdateByObj(args *[]interface{}, obj interface{}) (sqlStr string, err error)
- func ToMap(rows *sql.Rows) ([]map[string]string, error)
- func ToObj(rows *sql.Rows, obj interface{}) error
- func ToObjList(rows *sql.Rows, obj interface{}) ([]interface{}, error)
- type Column
- type Condition
- type FieldMap
- type Group
- type GroupOption
- type Pool
- type PoolOption
- type Query
- type Row
- type RowFormat
- type Table
- type Transaction
- type Where
Constants ¶
View Source
const ( Or = `OR` And = `AND` )
View Source
const ( Bit = "bit" TinyInt = "tinyint" SmallInt = "smallint" MediumInt = "mediumint" Int = "int" BigInt = "bigint" Float = "float" Double = "double" Decimal = "decimal" TinyText = "tinytext" MediumText = "mediumtext" Text = "text" LongText = "longtext" Json = "json" Varchar = "varchar" Char = "char" Timestamp = "timestamp" Date = "date" Datetime = "datetime" TinyBlob = "tinyblob" MediumBlob = "mediumblob" Blob = "blob" LongBlob = "longblob" Set = "set" Enum = "enum" )
Variables ¶
View Source
var ( ErrInvalidRowsTypes = errors.New(`only *struct or []*struct types are supported`) ErrNotFoundField = errors.New(`failed to match the field from the struct to the database. Please configure the borm tag correctly`) ErrNotFoundPrimaryField = errors.New(`failed to found primary field. Please configure the primary on borm tag correctly`) ErrInvalidTypes = errors.New(`only *struct types are supported`) ErrInvalidFieldTypes = errors.New(`only bool(1 is true, other is false),string、float64、float32、int、uint、int8、uint8、int16、uint16、int32、uint32、int64 and uint64 types are supported`) )
View Source
var ( ErrNoMasterConn = errors.New("mysql group: no master connection available") ErrNoSlaveConn = errors.New("mysql group: no slave connection available") )
Functions ¶
func SqlDeleteByObj ¶
SqlDeleteByObj ---
func SqlFindOneObj ¶
SqlFindOneObj ---
func SqlInsertObjs ¶
SqlInsertObjs ---
func SqlUpdateByObj ¶
SqlUpdateByObj ---
Types ¶
type Column ¶ added in v1.1.0
type Column struct {
Field string `json:"field"`
Type string `json:"type"`
Length int `json:"length"`
Point int `json:"point"`
Unsigned bool `json:"unsigned"`
Collation string `json:"collation"`
Null bool `json:"null"`
Key string `json:"key"`
Default string `json:"default"`
Extra string `json:"extra"`
Privileges string `json:"privileges"`
Comment string `json:"comment"`
}
func (*Column) ToProperty ¶ added in v1.1.0
type Group ¶
type Group interface {
// BadPool 获取BadPool列表
BadPool(isMaster bool) (list []int)
// Query 查询
Query(useMaster bool, sqlStr string, args ...interface{}) (rows []map[string]string, err error)
// QueryContext with context 查询
QueryContext(ctx context.Context, useMaster bool, sqlStr string, args ...interface{}) (rows []map[string]string, err error)
// Exec 执行
Exec(sqlStr string, args ...interface{}) (result sql.Result, err error)
// ExecContext with context 执行
ExecContext(ctx context.Context, sqlStr string, args ...interface{}) (result sql.Result, err error)
// Tables 获取表列表
Tables(pattern string, useMaster bool) (tableList []string, err error)
Table(table string, useMaster bool) (t *Table, err error)
// InsertObj 插入对象
InsertObj(obj interface{}) (result sql.Result, err error)
// InsertObjContext with context 插入对象
InsertObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
// DeleteObj 删除对象
DeleteObj(obj interface{}) (result sql.Result, err error)
// DeleteObjContext with context 删除对象
DeleteObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
// UpdateObj 更新对象
UpdateObj(obj interface{}) (result sql.Result, err error)
// UpdateObjContext with context 更新对象
UpdateObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
// Find 根据Query查询
Find(query Query, useMaster bool) (rows []map[string]string, err error)
// FindContext with context 根据Query查询
FindContext(ctx context.Context, query Query, useMaster bool) (rows []map[string]string, err error)
// FindAll 根据Query查询,返回对象列表
FindAll(query Query, obj interface{}, useMaster bool) (objList []interface{}, err error)
// FindAllContext with context 根据Query查询,返回对象列表
FindAllContext(ctx context.Context, query Query, obj interface{}, useMaster bool) (objList []interface{}, err error)
// FindOne 查询一个
FindOne(table string, where Where, useMaster bool) (row map[string]string, err error)
// FindOneContext with context 查询一个
FindOneContext(ctx context.Context, table string, where Where, useMaster bool) (row map[string]string, err error)
// FindOneObj 查询一个对象
FindOneObj(where Where, obj interface{}, useMaster bool) (err error)
// FindOneObjContext with context 查询一个对象
FindOneObjContext(ctx context.Context, where Where, obj interface{}, useMaster bool) (err error)
// Insert 插入
Insert(table string, rows ...Row) (result sql.Result, err error)
// InsertContext with context 插入
InsertContext(ctx context.Context, table string, rows ...Row) (result sql.Result, err error)
// DeleteAll 删除
DeleteAll(table string, where Where) (result sql.Result, err error)
// DeleteAllContext with context 删除
DeleteAllContext(ctx context.Context, table string, where Where) (result sql.Result, err error)
// UpdateAll 更新
UpdateAll(table string, set Row, where Where) (result sql.Result, err error)
// UpdateAllContext with context 更新
UpdateAllContext(ctx context.Context, table string, set Row, where Where) (result sql.Result, err error)
// Begin 开启事务
Begin() (Transaction, error)
// BeginTx with context 开启事务
BeginTx(ctx context.Context, opts *sql.TxOptions) (Transaction, error)
}
func NewMysqlGroup ¶
func NewMysqlGroup(groupOption *GroupOption) (Group, error)
type GroupOption ¶
type GroupOption struct {
Masters []PoolOption `yaml:"masters" json:"masters"`
Slaves []PoolOption `yaml:"slaves" json:"slaves"`
//单位s
RetryInterval int64 `yaml:"retryInterval" json:"retryInterval"`
}
type Pool ¶
type Pool interface {
// Query 查询
Query(sqlStr string, args ...interface{}) (rows *sql.Rows, err error)
// QueryContext with context 查询
QueryContext(ctx context.Context, sqlStr string, args ...interface{}) (rows *sql.Rows, err error)
// Exec 执行
Exec(sqlStr string, args ...interface{}) (result sql.Result, err error)
// ExecContext with context 执行
ExecContext(ctx context.Context, sqlStr string, args ...interface{}) (result sql.Result, err error)
// Begin 开启事务
Begin() (Transaction, error)
// BeginTx with context 开启事务
BeginTx(ctx context.Context, opts *sql.TxOptions) (Transaction, error)
}
type PoolOption ¶
type PoolOption struct {
//格式:"userName:password@schema(host:port)/dbName",如:root:123456@tcp(127.0.0.1:3306)/test
Dsn string `yaml:"dsn" json:"dsn"`
//单位s
MaxConnLifetime int `yaml:"maxConnLifetime" json:"maxConnLifetime"`
MaxOpenConns int `yaml:"maxOpenConns" json:"maxOpenConns"`
MaxIdleConns int `yaml:"maxIdleConns" json:"maxIdleConns"`
}
type Query ¶
type Query interface {
// Select Select表达式
Select(columns ...string) Query
// From From表达式
From(table string) Query
// Where Where表达式
Where(where Where) Query
// And 附加And where
And(condition Condition) Query
// Or 附加Or where
Or(condition Condition) Query
// Group Group表达式
Group(fields ...string) Query
// Having Having表达式
Having(having string) Query
// Order Order表达式
Order(orders ...string) Query
// Offset Offset表达式
Offset(offset int64) Query
// Limit Limit表达式
Limit(limit int64) Query
// Sql 生成sql
Sql(arguments *[]interface{}) (sql string)
// Close 释放Query
Close()
}
Query Query对象
func AcquireQuery4Mysql ¶ added in v1.1.0
func AcquireQuery4Mysql() Query
AcquireQuery4Mysql 获取mysqlQuery对象
type Transaction ¶ added in v1.0.1
type Transaction interface {
// Commit 提交事务
Commit() (err error)
// Rollback 回滚事务
Rollback() (err error)
// Query 查询
Query(sqlStr string, args ...interface{}) (rows []map[string]string, err error)
// QueryContext with context 查询
QueryContext(ctx context.Context, sqlStr string, args ...interface{}) (rows []map[string]string, err error)
// Exec 执行
Exec(sqlStr string, args ...interface{}) (result sql.Result, err error)
// ExecContext with context 执行
ExecContext(ctx context.Context, sqlStr string, args ...interface{}) (result sql.Result, err error)
// InsertObj 插入对象
InsertObj(obj interface{}) (result sql.Result, err error)
// InsertObjContext with context 插入对象
InsertObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
// DeleteObj 删除对象
DeleteObj(obj interface{}) (result sql.Result, err error)
// DeleteObjContext with context 删除对象
DeleteObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
// UpdateObj 更新对象
UpdateObj(obj interface{}) (result sql.Result, err error)
// UpdateObjContext with context 更新对象
UpdateObjContext(ctx context.Context, obj interface{}) (result sql.Result, err error)
// Find 根据Query查询
Find(query Query) (rows []map[string]string, err error)
// FindContext with context 根据Query查询
FindContext(ctx context.Context, query Query) (rows []map[string]string, err error)
// FindAll 根据Query查询,返回对象列表
FindAll(query Query, obj interface{}) (objList []interface{}, err error)
// FindAllContext with context 根据Query查询,返回对象列表
FindAllContext(ctx context.Context, query Query, obj interface{}) (objList []interface{}, err error)
// FindOne 查询一个
FindOne(table string, where Where) (row map[string]string, err error)
// FindOneContext with context 查询一个
FindOneContext(ctx context.Context, table string, where Where) (row map[string]string, err error)
// FindOneObj 查询一个对象
FindOneObj(where Where, obj interface{}) (err error)
// FindOneObjContext with context 查询一个对象
FindOneObjContext(ctx context.Context, where Where, obj interface{}) (err error)
// Insert 插入
Insert(table string, rows ...Row) (result sql.Result, err error)
// InsertContext with context 插入
InsertContext(ctx context.Context, table string, rows ...Row) (result sql.Result, err error)
// DeleteAll 删除
DeleteAll(table string, where Where) (result sql.Result, err error)
// DeleteAllContext with context 删除
DeleteAllContext(ctx context.Context, table string, where Where) (result sql.Result, err error)
// UpdateAll 更新
UpdateAll(table string, set Row, where Where) (result sql.Result, err error)
// UpdateAllContext with context 更新
UpdateAllContext(ctx context.Context, table string, set Row, where Where) (result sql.Result, err error)
}
Source Files
¶
Click to show internal directories.
Click to hide internal directories.