Documentation ¶
Index ¶
- Constants
- Variables
- func InitDBWithGorm(dbConfig DB, dbType enums.DBType) (*gorm.DB, error)
- type BaseModel
- type BasicDModel
- type BasicModel
- type Callback
- type CallbackFunc
- type DB
- 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) Check(tx *gorm.DB, excludeErr ...error) (exist bool, err error)
- func (c *DBClient) Close() error
- func (c *DBClient) Exist(condition map[string]interface{}, tableName string, dst interface{}, ...) (exist bool, err error)
- func (c *DBClient) First(condition interface{}, pointer interface{}, tableName string, ...) (exist bool, err error)
- func (c *DBClient) GetReadDB(optFns ...OptDBFunc) *gorm.DB
- func (c *DBClient) GetWriteDB(optFns ...OptDBFunc) *gorm.DB
- func (c *DBClient) Heartbeat() error
- func (c *DBClient) Last(condition interface{}, pointer interface{}, tableName string, ...) (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{}, tableName string, ...) (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() 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("unsupported 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 `` /* 177-byte string literal not displayed */ UpdatedAt types.NullTime `` /* 185-byte string literal not displayed */ }
BaseModel 表通用字段
type BasicDModel ¶ added in v1.2.5
type BasicDModel struct { ID int64 `gorm:"column:id;primaryKey;type:bigint(20) unsigned not null auto_increment;comment:主键" json:"id"` CreatedAt time.Time `` /* 177-byte string literal not displayed */ UpdatedAt time.Time `` /* 185-byte string literal not displayed */ DeletedAt gorm.DeletedAt `gorm:"column:deleted_at;index:idx_deleted_at;type:datetime;comment:逻辑删除"` }
BasicDModel 表通用字段
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 `` /* 177-byte string literal not displayed */ UpdatedAt time.Time `` /* 185-byte string literal not displayed */ }
BasicModel 表通用字段
type Callback ¶ added in v1.3.2
type Callback struct { OpType enums.OperateType // 操作类型 Name string // 目的回调名字 When enums.DBCbWhen // 具体时间 RegisterName string // 回调名字 Action CallbackFunc // 回调函数 }
type CallbackFunc ¶ added in v1.3.2
type DB ¶ added in v1.3.2
type DB struct { Url string `yaml:"url" json:"url"` // 写节点Url Replicas []string `yaml:"replicas" json:"replicas"` // 读节点Url TablePrefix string `yaml:"tablePrefix" json:"tablePrefix"` // 表前缀 SingularTable bool `yaml:"singularTable" json:"singularTable"` // 表复数禁用 CreateBatchSize int `yaml:"createBatchSize" json:"createBatchSize"` // 批量创建数量 EnableRawSQL bool `yaml:"enableRawSql" json:"enableRawSql"` // 打印原生SQL MaxFreeConnCount int `yaml:"maxFreeConnCount" json:"maxFreeConnCount"` // 最大闲置连接数量 MaxOpenConnCount int `yaml:"maxOpenConnCount" json:"maxOpenConnCount"` // 最大连接数量 ConnMaxLifetime int `yaml:"connMaxLifetime" json:"connMaxLifetime"` // 连接存活最大时长, 单位: 秒 FreeMaxLifetime int `yaml:"freeMaxLifetime" json:"freeMaxLifetime"` // 闲置连接存活的最大时间, 单位: 秒 CreateCallbacks []Callback UpdateCallbacks []Callback QueryCallbacks []Callback DeleteCallbacks []Callback }
DB 数据库配置 Mysql Url eg: root:my_pass@tcp(127.0.0.1:3306)/my_db?charset=utf8mb4&parseTime=True&loc=UTC Postgres Url eg: `host=127.0.0.1 user=root password=my_pass dbname=my_db port=5432 sslmode=disable TimeZone=UTC`
type DBClient ¶
func NewDBClient ¶
func (*DBClient) AddRecords ¶
AddRecords 批量添加记录
func (*DBClient) Exist ¶
func (c *DBClient) Exist(condition map[string]interface{}, tableName string, dst interface{}, opts ...OptDBFunc) (exist bool, err error)
Exist 记录是否存在
func (*DBClient) First ¶
func (c *DBClient) First(condition interface{}, pointer interface{}, tableName string, opts ...OptDBFunc) (exist bool, err error)
First 查询第一条记录
func (*DBClient) GetWriteDB ¶ added in v1.2.6
GetWriteDB 获取写节点DB对象
func (*DBClient) Last ¶
func (c *DBClient) Last(condition interface{}, pointer interface{}, tableName string, 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{}, tableName string, 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 FindDeleted ¶ added in v1.2.7
func FindDeleted() OptDBFunc
func UseReadNode ¶ added in v1.3.5
func UseReadNode() OptDBFunc
func UseWriteNode ¶ added in v1.1.9
func UseWriteNode() OptDBFunc
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%