Documentation ¶
Index ¶
- Constants
- Variables
- func InitDBWithGorm(dbConfig settings.DB, dbType enums.DBType) (*gorm.DB, error)
- type BaseModel
- type BasicModel
- type DBClient
- func (c *DBClient) AddRecord(data interface{}, opts ...OptDBFunc) error
- func (c *DBClient) AddRecords(data interface{}, batchSize int, opts ...OptDBFunc) error
- func (c *DBClient) Close() error
- func (c *DBClient) Exist(condition map[string]interface{}, dst interface{}, opts ...OptDBFunc) (exist bool, err error)
- func (c *DBClient) First(condition interface{}, pointer interface{}, opts ...OptDBFunc) (exist bool, err error)
- func (c *DBClient) GetDB() *gorm.DB
- func (c *DBClient) Heartbeat() error
- func (c *DBClient) Last(condition interface{}, pointer interface{}, opts ...OptDBFunc) (exist bool, err error)
- func (c *DBClient) Migrate(pointers ...interface{}) error
- func (c *DBClient) Query(tableName string, condition *QueryCondition, dst interface{}, ...) (totalCount int64, err error)
- func (c *DBClient) QueryById(id int64, pointer interface{}, opts ...OptDBFunc) (exist bool, err error)
- func (c *DBClient) QueryByIds(ids []int64, pointers interface{}, opts ...OptDBFunc) (exist bool, err error)
- func (c *DBClient) QueryByMap(condition map[string]interface{}, dst interface{}, opts ...OptDBFunc) (exist bool, err error)
- func (c *DBClient) QueryByPrimaryKey(pkColumnName string, pkValue, pointer interface{}, opts ...OptDBFunc) (exist bool, err error)
- func (c *DBClient) QueryByStruct(condition interface{}, dst interface{}, opts ...OptDBFunc) (exist bool, err error)
- func (c *DBClient) QueryCount(tableName string, condition *QueryCondition, opts ...OptDBFunc) (count int64, err error)
- func (c *DBClient) RawConfig() settings.DB
- func (c *DBClient) Save(ptr interface{}, opts ...OptDBFunc) error
- func (c *DBClient) UpdateById(tableName string, id int64, data interface{}, opts ...OptDBFunc) error
- func (c *DBClient) UpdateRecord(tableName string, condition interface{}, dstValue interface{}, ...) error
- func (c *DBClient) UpdateRecordNoCond(tableName string, dstValue interface{}, opts ...OptDBFunc) error
- func (c *DBClient) WatchHeartbeat()
- type OptDBFunc
- type Order
- type QueryCondition
- func (qc *QueryCondition) AddSort(sort *Order) *QueryCondition
- func (qc *QueryCondition) AddWhere(where *Where) *QueryCondition
- func (qc *QueryCondition) GetLimit() (limit int)
- func (qc *QueryCondition) GetOffset() (offset int)
- func (qc *QueryCondition) SetPage(page int) *QueryCondition
- func (qc *QueryCondition) SetPageSize(pageSize int) *QueryCondition
- func (qc *QueryCondition) SetTotalCount(query bool) *QueryCondition
- type Where
- func NewEqWhere(column string, value interface{}) *Where
- func NewGtWhere(column string, value interface{}) *Where
- func NewGteWhere(column string, value interface{}) *Where
- func NewInWhere(column string, value interface{}) *Where
- func NewLeftLikeWhere(column string, value string) *Where
- func NewLikeWhere(column string, value string) *Where
- func NewLtWhere(column string, value interface{}) *Where
- func NewLteWhere(column string, value interface{}) *Where
- func NewNotEqWhere(column string, value interface{}) *Where
- func NewNotInWhere(column string, value interface{}) *Where
- func NewRightLikeWhere(column string, value string) *Where
- func NewWhere(column string, value interface{}, sign string) *Where
Constants ¶
const ( //ASC 正序 ASC = "asc" //DESC 倒序 DESC = "desc" )
const ( Eq = "=" Neq = "!=" Gte = ">=" Gt = ">" Lte = "<=" Lt = "<" In = "in" NotIn = "not in" Like = "like" )
Variables ¶
var UnsupportedDBType = errors.New("invalid db type")
Functions ¶
Types ¶
type BaseModel ¶
type BaseModel struct { ID int64 `gorm:"column:id;primaryKey;type:bigint(20) unsigned not null auto_increment;comment:主键" json:"id"` CreatedAt types.NullTime `` /* 157-byte string literal not displayed */ UpdatedAt types.NullTime `` /* 185-byte string literal not displayed */ }
BaseModel 表通用字段
type BasicModel ¶
type BasicModel struct { ID int64 `gorm:"column:id;primaryKey;type:bigint(20) unsigned not null auto_increment;comment:主键" json:"id"` CreatedAt time.Time `` /* 157-byte string literal not displayed */ UpdatedAt time.Time `` /* 185-byte string literal not displayed */ }
BasicModel 表通用字段
type DBClient ¶
func NewDBClient ¶
func (*DBClient) AddRecords ¶
AddRecords 批量添加记录
func (*DBClient) Exist ¶
func (c *DBClient) Exist(condition map[string]interface{}, dst interface{}, opts ...OptDBFunc) (exist bool, err error)
Exist 记录是否存在
func (*DBClient) First ¶
func (c *DBClient) First(condition interface{}, pointer interface{}, opts ...OptDBFunc) (exist bool, err error)
First 查询第一条记录
func (*DBClient) Last ¶
func (c *DBClient) Last(condition interface{}, pointer interface{}, opts ...OptDBFunc) (exist bool, err error)
Last 查询最后一条记录
func (*DBClient) Query ¶
func (c *DBClient) Query(tableName string, condition *QueryCondition, dst interface{}, opts ...OptDBFunc) (totalCount int64, err error)
Query 查询
func (*DBClient) QueryById ¶
func (c *DBClient) QueryById(id int64, pointer interface{}, opts ...OptDBFunc) (exist bool, err error)
QueryById 通过主键查询 exist 记录是否存在 err 发生的错误
func (*DBClient) QueryByIds ¶
func (c *DBClient) QueryByIds(ids []int64, pointers interface{}, opts ...OptDBFunc) (exist bool, err error)
QueryByIds 通过主键查询
func (*DBClient) QueryByMap ¶
func (c *DBClient) QueryByMap(condition map[string]interface{}, dst interface{}, opts ...OptDBFunc) (exist bool, err error)
QueryByMap 通过Map查询
func (*DBClient) QueryByPrimaryKey ¶
func (*DBClient) QueryByStruct ¶
func (c *DBClient) QueryByStruct(condition interface{}, dst interface{}, opts ...OptDBFunc) (exist bool, err error)
QueryByStruct 通过结构体查询, 结构体字段为零值的字段, 不会作为条件
func (*DBClient) QueryCount ¶
func (c *DBClient) QueryCount(tableName string, condition *QueryCondition, opts ...OptDBFunc) (count int64, err error)
QueryCount 查询数量
func (*DBClient) UpdateById ¶
func (c *DBClient) UpdateById(tableName string, id int64, data interface{}, opts ...OptDBFunc) error
UpdateById 根据主键更新 data为结构体指针时, 结构体零值字段不会被更新 data为`map`时, 更具`map`更新属性
func (*DBClient) UpdateRecord ¶
func (c *DBClient) UpdateRecord(tableName string, condition interface{}, dstValue interface{}, opts ...OptDBFunc) error
UpdateRecord 更新记录, condition必须包含条件, 否则会返回错误ErrMissingWhereClause, 如果想无条件更新, 请使用updateRecordWithoutCond tableName 表名 dstValue struct时, 只会更新非零字段; map 时, 根据 `map` 更新属性 condition struct时, 只会把非零字段当做条件; map 时, 根据 `map` 设置条件
func (*DBClient) UpdateRecordNoCond ¶
func (c *DBClient) UpdateRecordNoCond(tableName string, dstValue interface{}, opts ...OptDBFunc) error
UpdateRecordNoCond 无条件更新记录 tableName 表名 dstValue, struct时, 只会更新非零字段; map 时, 根据 `map` 更新属性
func (*DBClient) WatchHeartbeat ¶ added in v1.1.4
func (c *DBClient) WatchHeartbeat()
WatchHeartbeat 监测心跳和重连
type OptDBFunc ¶ added in v1.1.5
func WithContext ¶ added in v1.1.5
type QueryCondition ¶
type QueryCondition struct { //Where 查询条件 Where []*Where //Page 页数, 从1开始 Page int //PageSize 每页数量, 必须大于0 PageSize int //Sort 排序 Sort []*Order //TotalCount 是否查询总数量 TotalCount bool //Offset 偏移量 Offset int //Limit 查询数量 Limit int }
QueryCondition 查询条件
func NewQueryCondition ¶
func NewQueryCondition() *QueryCondition
func (*QueryCondition) AddSort ¶
func (qc *QueryCondition) AddSort(sort *Order) *QueryCondition
AddSort 添加排序
func (*QueryCondition) AddWhere ¶
func (qc *QueryCondition) AddWhere(where *Where) *QueryCondition
AddWhere 添加条件
func (*QueryCondition) GetOffset ¶
func (qc *QueryCondition) GetOffset() (offset int)
GetOffset 获取偏移量
func (*QueryCondition) SetPage ¶
func (qc *QueryCondition) SetPage(page int) *QueryCondition
SetPage 设置页数
func (*QueryCondition) SetPageSize ¶
func (qc *QueryCondition) SetPageSize(pageSize int) *QueryCondition
SetPageSize 设置每页数量
func (*QueryCondition) SetTotalCount ¶
func (qc *QueryCondition) SetTotalCount(query bool) *QueryCondition
SetTotalCount 设置是否查询总数
type Where ¶
type Where struct { Column string // 字段名 Value interface{} // 值 Sign string // 符号 Ors []*Where //或条件 }
func NewLeftLikeWhere ¶
NewLeftLikeWhere 模糊查询 %demo
func NewNotInWhere ¶
NewNotInWhere not in
func NewRightLikeWhere ¶
NewRightLikeWhere 模糊查询 demo%