rdb

package
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2022 License: Apache-2.0 Imports: 17 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("invalid db type")

Functions

func InitDBWithGorm

func InitDBWithGorm(dbConfig settings.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 `` /* 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

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

func NewDBClient

func NewDBClient(db *gorm.DB, dbType enums.DBType, rawConfig settings.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) Close

func (c *DBClient) Close() error

Close 关闭连接池

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

func (c *DBClient) GetDB() *gorm.DB

GetDB 获取原生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{}, 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(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 (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(tableName string, condition *QueryCondition, opts ...OptDBFunc) (count int64, err error)

QueryCount 查询数量

func (*DBClient) RawConfig added in v1.1.5

func (c *DBClient) RawConfig() settings.DB

func (*DBClient) Save

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

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

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

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

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条件

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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