rdb

package
v1.5.5 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ASC 正序.
	ASC = "asc"
	// DESC 倒序.
	DESC = "desc"
)
View Source
const (
	Eq    = "="
	Neq   = "!="
	Gte   = ">="
	Gt    = ">"
	Lte   = "<="
	Lt    = "<"
	In    = "in"
	NotIn = "not in"
	Like  = "like"
)

Variables

View Source
var UnsupportedDBType = errors.New("unsupported db type")

Functions

func InitDBWithGorm

func InitDBWithGorm(dbConfig DB, dbType enums.DBType) (*gorm.DB, error)

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 CallbackFunc func(db *gorm.DB)

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

type DBClient struct {
	DBType enums.DBType // 数据库类型
	// contains filtered or unexported fields
}

func NewDBClient

func NewDBClient(db *gorm.DB, dbType enums.DBType, rawConfig DB) (client *DBClient)

func (*DBClient) AddRecord

func (c *DBClient) AddRecord(data interface{}, opts ...OptDBFunc) error

AddRecord 添加记录 data 结构体指针.

func (*DBClient) AddRecords

func (c *DBClient) AddRecords(data interface{}, batchSize int, opts ...OptDBFunc) error

AddRecords 批量添加记录.

func (*DBClient) Check added in v1.2.6

func (c *DBClient) Check(tx *gorm.DB, excludeErr ...error) (exist bool, err error)

func (*DBClient) Close

func (c *DBClient) Close() error

Close 关闭连接池.

func (*DBClient) DelRecordById added in v1.4.0

func (c *DBClient) DelRecordById(modelPtr interface{}, id int64) (int64, error)

func (*DBClient) DelRecords added in v1.4.0

func (c *DBClient) DelRecords(modelPtr interface{}, condition interface{}, extraData map[string]interface{}) (int64, error)

DelRecords 逻辑删除 modelPtr 模型指针 condition 删除条件 extraData 额外更新数据.

func (*DBClient) DelRecordsByIds added in v1.4.0

func (c *DBClient) DelRecordsByIds(modelPtr interface{}, ids []int64) (int64, error)

func (*DBClient) Exist

func (c *DBClient) Exist(condition map[string]interface{}, modelPtr interface{}, dst interface{}, opts ...OptDBFunc) (exist bool, err error)

Exist 记录是否存在.

func (*DBClient) First

func (c *DBClient) First(condition interface{}, pointer interface{}, modelPtr interface{}, opts ...OptDBFunc) (exist bool, err error)

First 查询第一条记录.

func (*DBClient) GetReadDB added in v1.2.6

func (c *DBClient) GetReadDB(optFns ...OptDBFunc) *gorm.DB

GetReadDB 获取读节点DB对象.

func (*DBClient) GetWriteDB added in v1.2.6

func (c *DBClient) GetWriteDB(optFns ...OptDBFunc) *gorm.DB

GetWriteDB 获取写节点DB对象.

func (*DBClient) Heartbeat added in v1.1.4

func (c *DBClient) Heartbeat() error

Heartbeat 检测心跳.

func (*DBClient) Last

func (c *DBClient) Last(condition interface{}, pointer interface{}, modelPtr interface{}, opts ...OptDBFunc) (exist bool, err error)

Last 查询最后一条记录.

func (*DBClient) Migrate

func (c *DBClient) Migrate(pointers ...interface{}) error

Migrate 数据库迁移 models 数据库模型 model: client.Migrate(&Product{}, &Fruit{})

func (*DBClient) Query

func (c *DBClient) Query(modelPtr interface{}, 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{}, modelPtr interface{}, opts ...OptDBFunc) (exist bool, err error)

QueryByMap 通过Map查询.

func (*DBClient) QueryByPrimaryKey

func (c *DBClient) QueryByPrimaryKey(pkColumnName string, pkValue, pointer interface{}, opts ...OptDBFunc) (exist bool, err error)

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(modelPtr interface{}, condition *QueryCondition, opts ...OptDBFunc) (count int64, err error)

QueryCount 查询数量.

func (*DBClient) RawConfig added in v1.1.5

func (c *DBClient) RawConfig() DB

func (*DBClient) RecoverRecords added in v1.4.0

func (c *DBClient) RecoverRecords(modelPtr interface{}, condition interface{}, extraData map[string]interface{}) (int64, error)

RecoverRecords 恢复记录.

func (*DBClient) Save

func (c *DBClient) Save(ptr interface{}, opts ...OptDBFunc) error

Save 保存记录, 会保存所有的字段,即使字段是零值 ptr 必须是struct指针.

func (*DBClient) UpdateById

func (c *DBClient) UpdateById(modelPtr interface{}, id int64, data interface{}, opts ...OptDBFunc) error

UpdateById 根据主键更新 data为结构体指针时, 结构体零值字段不会被更新 data为`map`时, 更具`map`更新属性.

func (*DBClient) UpdateRecord

func (c *DBClient) UpdateRecord(modelPtr interface{}, condition interface{}, dstValue interface{}, opts ...OptDBFunc) error

UpdateRecord 更新记录, condition必须包含条件, 否则会返回错误ErrMissingWhereClause, 如果想无条件更新, 请使用updateRecordWithoutCond modelPtr 表名 dstValue struct时, 只会更新非零字段; map 时, 根据 `map` 更新属性 condition struct时, 只会把非零字段当做条件; map 时, 根据 `map` 设置条件.

func (*DBClient) UpdateRecordNoCond

func (c *DBClient) UpdateRecordNoCond(modelPtr interface{}, dstValue interface{}, opts ...OptDBFunc) error

UpdateRecordNoCond 无条件更新记录 modelPtr 模型 dstValue, struct时, 只会更新非零字段; map 时, 根据 `map` 更新属性.

func (*DBClient) WatchHeartbeat added in v1.1.4

func (c *DBClient) WatchHeartbeat()

WatchHeartbeat 监测心跳和重连.

type OptDBFunc added in v1.1.5

type OptDBFunc func(db *gorm.DB) *gorm.DB

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

func WithContext(ctx context.Context) OptDBFunc

type Order

type Order struct {
	//Column 字段名
	Column string
	//Sort 排序, 用 ASC, DESC 常量
	Sort string
}

func NewDescOrder

func NewDescOrder(column string) (order *Order)

NewDescOrder 逆排序 order by desc.

func NewOrder

func NewOrder(column string) (order *Order)

NewOrder 正排序 order by asc.

func (Order) String

func (o Order) String() string

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

func (qc *QueryCondition) GetLimit() (limit int)

GetLimit 获取偏移量.

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 NewEqWhere

func NewEqWhere(column string, value interface{}) *Where

NewEqWhere =.

func NewGtWhere

func NewGtWhere(column string, value interface{}) *Where

NewGtWhere >.

func NewGteWhere

func NewGteWhere(column string, value interface{}) *Where

NewGteWhere >=.

func NewInWhere

func NewInWhere(column string, value interface{}) *Where

NewInWhere in.

func NewLeftLikeWhere

func NewLeftLikeWhere(column string, value string) *Where

NewLeftLikeWhere 模糊查询 %demo.

func NewLikeWhere

func NewLikeWhere(column string, value string) *Where

NewLikeWhere 模糊查询 %demo%.

func NewLtWhere

func NewLtWhere(column string, value interface{}) *Where

NewLtWhere <.

func NewLteWhere

func NewLteWhere(column string, value interface{}) *Where

NewLteWhere <=.

func NewNotEqWhere

func NewNotEqWhere(column string, value interface{}) *Where

NewNotEqWhere !=.

func NewNotInWhere

func NewNotInWhere(column string, value interface{}) *Where

NewNotInWhere not in.

func NewRightLikeWhere

func NewRightLikeWhere(column string, value string) *Where

NewRightLikeWhere 模糊查询 demo%.

func NewWhere

func NewWhere(column string, value interface{}, sign string) *Where

NewWhere 设置查询条件.

func (*Where) Or

func (where *Where) Or(w *Where) *Where

Or 添加Or条件.

func (*Where) ToSQL added in v1.2.8

func (where *Where) ToSQL() (pattern string, value interface{})

ToSQL 获取SQL.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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