Documentation
¶
Index ¶
- Variables
- func Add(tx *gorm.DB, v any, fields ...string) *gorm.DB
- func AddIgnore(tx *gorm.DB, v any, fields ...string) *gorm.DB
- func AddIgnoreOmit(tx *gorm.DB, v any, fields ...string) *gorm.DB
- func AddOmit(tx *gorm.DB, v any, fields ...string) *gorm.DB
- func AddOrUpdate(tx *gorm.DB, v any, fields ...string) *gorm.DB
- func All[M any](db *gorm.DB, query any, fields ...string) (ms []M, err error)
- func AllLockForUpdate[M any](db *gorm.DB, query any, fields ...string) ([]M, error)
- func AllOmit[M any](db *gorm.DB, query any, fields ...string) (ms []M, err error)
- func AllOrder[M any](db *gorm.DB, query any, order string, fields ...string) (ms []M, err error)
- func AllOrderOmit[M any](db *gorm.DB, query any, order string, fields ...string) (ms []M, err error)
- func Exec(tx, clean *gorm.DB, init func(db *gorm.DB) *gorm.DB) (int64, error)
- func Field[M any](db *gorm.DB, query any, field string) (ms []M, err error)
- func First(tx *gorm.DB, ptr any, query any, fields ...string) error
- func GetLockForUpdate(db *gorm.DB, m any) error
- func IsDataNotFound(err error) bool
- func IsMySqlDuplicateError(err error) bool
- func LockForUpdate(db *gorm.DB) *gorm.DB
- func NewLog(lg glog.Logger, traceKey string) logger.Interface
- func Open(dsn string) (*gorm.DB, error)
- func Page[M any](db *gorm.DB, page *PageQuery, res *PageResult[M], fields ...string) error
- func PageOmit[M any](db *gorm.DB, page *PageQuery, res *PageResult[M], fields ...string) error
- func SQL(tx, clean *gorm.DB, init func(db *gorm.DB) *gorm.DB) (string, []any)
- func Scan(tx, clean *gorm.DB, init func(db *gorm.DB) *gorm.DB, destPtr any) error
- func ScanValue(value any, ptr any) error
- func Update(tx *gorm.DB, v any, query any, fields ...string) *gorm.DB
- func ValueData(ptr any) (driver.Value, error)
- func WhereQuery(db *gorm.DB, q any) *gorm.DB
- func WhereQueryEQ(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
- func WhereQueryGT(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
- func WhereQueryGTE(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
- func WhereQueryIN(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
- func WhereQueryLIKE(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
- func WhereQueryLLIKE(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
- func WhereQueryLT(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
- func WhereQueryLTE(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
- func WhereQueryNEQ(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
- func WhereQueryNIN(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
- func WhereQueryNULL(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
- func WhereQueryNotNULL(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
- func WhereQueryRLIKE(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
- func WhereQuerySelect(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
- func WhereQueryWithTag(db *gorm.DB, q any, tag string) *gorm.DB
- type DB
- func (m *DB[K, V]) Add(ctx context.Context, v V, fields ...string) error
- func (m *DB[K, V]) AddAny(ctx context.Context, v any, fields ...string) error
- func (m *DB[K, V]) AddAnyOmit(ctx context.Context, v any, fields ...string) error
- func (m *DB[K, V]) AddIgnore(ctx context.Context, v V, fields ...string) error
- func (m *DB[K, V]) AddIgnoreOmit(ctx context.Context, v V, fields ...string) error
- func (m *DB[K, V]) AddOmit(ctx context.Context, v V, fields ...string) error
- func (m *DB[K, V]) AddOrUpdate(ctx context.Context, v V, fields ...string) error
- func (m *DB[K, V]) All(ctx context.Context, query any, fields ...string) ([]V, error)
- func (m *DB[K, V]) AllOmit(ctx context.Context, query any, fields ...string) ([]V, error)
- func (m *DB[K, V]) AllOrder(ctx context.Context, query any, order string, fields ...string) ([]V, error)
- func (m *DB[K, V]) AllOrderOmit(ctx context.Context, query any, order string, fields ...string) ([]V, error)
- func (m *DB[K, V]) AutoMigrate(db *gorm.DB, v V, table string, key string) error
- func (m *DB[K, V]) BatchAdd(ctx context.Context, vs []V, fields ...string) error
- func (m *DB[K, V]) BatchAddAny(ctx context.Context, vs []any, fields ...string) error
- func (m *DB[K, V]) BatchDelete(ctx context.Context, ks []K) (int64, error)
- func (m *DB[K, V]) BatchUpdate(ctx context.Context, vs []V, fields ...string) (int64, error)
- func (m *DB[K, V]) Delete(ctx context.Context, k K) (int64, error)
- func (m *DB[K, V]) DeleteWhere(ctx context.Context, query any) (int64, error)
- func (m *DB[K, V]) Field(ctx context.Context, query any, field string, resPtr any) error
- func (m *DB[K, V]) First(ctx context.Context, ptr V, query any, fields ...string) error
- func (m *DB[K, V]) FirstAny(ctx context.Context, ptr any, query any, fields ...string) error
- func (m *DB[K, V]) Get(ctx context.Context, ptr V, fields ...string) error
- func (m *DB[K, V]) Init(db *gorm.DB, table string, key string)
- func (m *DB[K, V]) Page(ctx context.Context, page *PageQuery, query any, res *PageResult[V], ...) error
- func (m *DB[K, V]) PageAny(ctx context.Context, page *PageQuery, query any, res *PageResult[any], ...) error
- func (m *DB[K, V]) PageOmit(ctx context.Context, page *PageQuery, query any, res *PageResult[V], ...) error
- func (m *DB[K, V]) Update(ctx context.Context, v V, fields ...string) (int64, error)
- func (m *DB[K, V]) UpdateAny(ctx context.Context, k K, v any, fields ...string) (int64, error)
- func (m *DB[K, V]) UpdateAnyWhere(ctx context.Context, v any, query any, fields ...string) (int64, error)
- func (m *DB[K, V]) UpdateColumn(ctx context.Context, k K, field string, value any) (int64, error)
- func (m *DB[K, V]) UpdateColumnWhere(ctx context.Context, query any, field string, value any) (int64, error)
- func (m *DB[K, V]) UpdateColumns(ctx context.Context, k K, v map[string]any) (int64, error)
- type DropLogger
- func (g *DropLogger) Error(ctx context.Context, str string, args ...any)
- func (g *DropLogger) Info(ctx context.Context, str string, args ...any)
- func (g *DropLogger) LogMode(logger.LogLevel) logger.Interface
- func (g *DropLogger) Trace(ctx context.Context, begin time.Time, ...)
- func (g *DropLogger) Warn(ctx context.Context, str string, args ...any)
- type Logger
- func (g *Logger) Error(ctx context.Context, str string, args ...any)
- func (g *Logger) Info(ctx context.Context, str string, args ...any)
- func (g *Logger) LogMode(logger.LogLevel) logger.Interface
- func (g *Logger) Trace(ctx context.Context, begin time.Time, ...)
- func (g *Logger) Warn(ctx context.Context, str string, args ...any)
- type PageQuery
- type PageResult
- type TxDB
- func (m *TxDB[K, V]) Add(ctx context.Context, cb func(*gorm.DB) error, v V, fields ...string) error
- func (m *TxDB[K, V]) AddAnyOmit(ctx context.Context, cb func(*gorm.DB) error, v any, fields ...string) error
- func (m *TxDB[K, V]) AddIgnore(ctx context.Context, cb func(*gorm.DB) error, v V, fields ...string) error
- func (m *TxDB[K, V]) AddIgnoreOmit(ctx context.Context, cb func(*gorm.DB) error, v V, fields ...string) error
- func (m *TxDB[K, V]) AddOmit(ctx context.Context, cb func(*gorm.DB) error, v V, fields ...string) error
- func (m *TxDB[K, V]) AddOrUpdate(ctx context.Context, cb func(*gorm.DB) error, v V, fields ...string) error
- func (m *TxDB[K, V]) BatchAdd(ctx context.Context, cb func(*gorm.DB) error, vs []V, fields ...string) error
- func (m *TxDB[K, V]) BatchAddAny(ctx context.Context, cb func(*gorm.DB) error, vs []any, fields ...string) error
- func (m *TxDB[K, V]) BatchDelete(ctx context.Context, cb func(*gorm.DB) error, ks []K) (int64, error)
- func (m *TxDB[K, V]) BatchUpdate(ctx context.Context, cb func(*gorm.DB) error, vs []V, fields ...string) (int64, error)
- func (m *TxDB[K, V]) Delete(ctx context.Context, cb func(*gorm.DB) error, k K) (int64, error)
- func (m *TxDB[K, V]) DeleteWhere(ctx context.Context, cb func(*gorm.DB) error, query any) (int64, error)
- func (m *TxDB[K, V]) Update(ctx context.Context, cb func(*gorm.DB) error, v V, fields ...string) (int64, error)
- func (m *TxDB[K, V]) UpdateAny(ctx context.Context, cb func(*gorm.DB) error, k K, v any, fields ...string) (int64, error)
- func (m *TxDB[K, V]) UpdateAnyWhere(ctx context.Context, cb func(*gorm.DB) error, v any, query any, ...) (int64, error)
- func (m *TxDB[K, V]) UpdateColumn(ctx context.Context, cb func(*gorm.DB) error, k K, field string, value any) (int64, error)
- func (m *TxDB[K, V]) UpdateColumnWhere(ctx context.Context, cb func(*gorm.DB) error, query any, field string, ...) (int64, error)
- func (m *TxDB[K, V]) UpdateColumns(ctx context.Context, cb func(*gorm.DB) error, k K, v map[string]any) (int64, error)
Constants ¶
This section is empty.
Variables ¶
var ( // WhereTag 是 WhereQuery 解析 tag 的名称 WhereTag = "ggorm" // WhereQueryFunc 是 WhereQuery 处理函数映射 // field: 字段名或者tag指定的字段名 // value: 字段值 // kind: 字段类型 // raw: 是否给字段名加上 `field` 符号,true 不加 // 支持的操作符: // - in: field IN (values) // - nin: field NOT IN (values) // - eq: field = value // - neq: field != value // - like: field LIKE %value% // - llike: field LIKE %value // - rlike: field LIKE value% // - gt: field < value // - gte: field <= value // - lt: field > value // - lte: field >= value // - null: field IS NULL/IS NOT NULL // - nnull: field IS NOT NULL/IS NULL // - sel: select WhereQueryFunc = map[string]func(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB{ "in": WhereQueryIN, "nin": WhereQueryNIN, "eq": WhereQueryEQ, "neq": WhereQueryNEQ, "like": WhereQueryLIKE, "llike": WhereQueryLLIKE, "rlike": WhereQueryRLIKE, "gt": WhereQueryGT, "gte": WhereQueryGTE, "lt": WhereQueryLT, "lte": WhereQueryLTE, "null": WhereQueryNULL, "nnull": WhereQueryNotNULL, "sel": WhereQuerySelect, } )
Functions ¶
func AddIgnoreOmit ¶
AddIgnoreOmit 封装,fields 是修改的字段
func AddOrUpdate ¶
AddOrUpdate 封装,fields 是添加/修改的字段
func AllLockForUpdate ¶
查询锁
func AllOrderOmit ¶
func AllOrderOmit[M any](db *gorm.DB, query any, order string, fields ...string) (ms []M, err error)
AllOrderOmit 用于查询全部,query 是条件,order 是排序sql,fields 是忽略的字段
func Open ¶
初始化,自动创建数据库 mysql dsn: mysql://root:1234@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local sqlite dsn: ./test.db
func WhereQuery ¶
WhereQuery 将结构体转换为 WHERE 条件查询 支持通过结构体标签定义查询条件,所有条件都是 AND 连接 零值字段会被忽略,不会生成查询条件
用法示例:
type UserQuery struct {
// 等于查询:WHERE `id` = ?
ID int64 `ggorm:"eq"`
// 自定义字段名:WHERE `user_name` LIKE %value%
Name string `ggorm:"like=user_name"`
// IN 查询:WHERE `status` IN (?)
Status []int `ggorm:"in=status"`
// 大于等于查询:WHERE `age` >= ?
Age int `ggorm:"gte"`
// NULL 查询:如果 IsDeleted 为 true,WHERE `deleted_at` IS NULL
// 如果 IsDeleted 为 false,WHERE `deleted_at` IS NOT NULL
IsDeleted bool `ggorm:"null=deleted_at"`
}
query := UserQuery{
Name: "test",
Status: []int{1, 2, 3},
Age: 18,
}
db = WhereQuery(db, query)
func WhereQueryEQ ¶
func WhereQueryEQ(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
WhereQueryEQ 处理等于查询 field = value
func WhereQueryGT ¶
func WhereQueryGT(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
WhereQueryGT 处理大于查询 field < value
func WhereQueryGTE ¶
func WhereQueryGTE(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
WhereQueryGTE 处理大于等于查询 field <= value
func WhereQueryIN ¶
func WhereQueryIN(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
WhereQueryIN 处理 IN 查询 field IN (values)
func WhereQueryLIKE ¶
func WhereQueryLIKE(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
WhereQueryLIKE 处理模糊查询 field LIKE %value%
func WhereQueryLLIKE ¶
func WhereQueryLLIKE(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
WhereQueryLLIKE 处理左模糊查询 field LIKE %value
func WhereQueryLT ¶
func WhereQueryLT(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
WhereQueryLT 处理小于查询 field > value
func WhereQueryLTE ¶
func WhereQueryLTE(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
WhereQueryLTE 处理小于等于查询 field >= value
func WhereQueryNEQ ¶
func WhereQueryNEQ(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
WhereQueryNEQ 处理不等于查询 field != value
func WhereQueryNIN ¶
func WhereQueryNIN(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
WhereQueryNIN 处理 NOT IN 查询 field NOT IN (values)
func WhereQueryNULL ¶
func WhereQueryNULL(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
WhereQueryNULL 处理 NULL 查询 根据字段值确定是使用 IS NULL 还是 IS NOT NULL 支持的字段类型: - 整数类型:1 表示 IS NOT NULL,0 表示 IS NULL - 布尔类型:true 表示 IS NOT NULL,false 表示 IS NULL - 字符串:'1' 表示 IS NOT NULL,其他值表示 IS NULL
func WhereQueryNotNULL ¶
func WhereQueryNotNULL(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
WhereQueryNotNULL 处理 NOT NULL 查询 与 WhereQueryNULL 相反的逻辑
func WhereQueryRLIKE ¶
func WhereQueryRLIKE(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB
WhereQueryRLIKE 处理右模糊查询 field LIKE value%
Types ¶
type DB ¶
进一步封装,K 是主键命,V 是结构体
func (*DB[K, V]) AddAnyOmit ¶
添加,v 是符合表结构的任意结构体或者map
func (*DB[K, V]) AddIgnoreOmit ¶
添加,如果唯一键存在则放弃
func (*DB[K, V]) AddOrUpdate ¶
添加,如果唯一键存在则更新指定的字段
func (*DB[K, V]) AllOrder ¶
func (m *DB[K, V]) AllOrder(ctx context.Context, query any, order string, fields ...string) ([]V, error)
查询所有,query 是条件,fields 是字段名称
func (*DB[K, V]) AllOrderOmit ¶
func (m *DB[K, V]) AllOrderOmit(ctx context.Context, query any, order string, fields ...string) ([]V, error)
查询所有,query 是条件,fields 是忽略的字段名称
func (*DB[K, V]) AutoMigrate ¶
初始化,自动创建表
func (*DB[K, V]) BatchAddAny ¶
批量添加,vs 是符合表结构的任意结构体列表
func (*DB[K, V]) BatchDelete ¶
主键批量删除,k 是主键
func (*DB[K, V]) BatchUpdate ¶
事务中批量更新
func (*DB[K, V]) DeleteWhere ¶
条件删除,query 是条件struct
func (*DB[K, V]) Page ¶
func (m *DB[K, V]) Page(ctx context.Context, page *PageQuery, query any, res *PageResult[V], fields ...string) error
分页查询,page是分页,query 是条件,res 是结果,fields 是字段名称
func (*DB[K, V]) PageAny ¶
func (m *DB[K, V]) PageAny(ctx context.Context, page *PageQuery, query any, res *PageResult[any], fields ...string) error
分页查询,page是分页,query 是条件,res 是结果,fields 是字段名称
func (*DB[K, V]) PageOmit ¶
func (m *DB[K, V]) PageOmit(ctx context.Context, page *PageQuery, query any, res *PageResult[V], fields ...string) error
分页查询,page是分页,query 是条件,res 是结果,fields 是忽略的字段名称
func (*DB[K, V]) UpdateAnyWhere ¶
func (m *DB[K, V]) UpdateAnyWhere(ctx context.Context, v any, query any, fields ...string) (int64, error)
更新指定字段,v 是符合表结构的任意结构体或者map,query 是条件struct
func (*DB[K, V]) UpdateColumn ¶
更新单个字段
type DropLogger ¶
type DropLogger struct {
}
用于接收 gorm 日志,什么都不做
func (*DropLogger) Error ¶
func (g *DropLogger) Error(ctx context.Context, str string, args ...any)
Error 实现接口
func (*DropLogger) Info ¶
func (g *DropLogger) Info(ctx context.Context, str string, args ...any)
Info 实现接口
func (*DropLogger) LogMode ¶
func (g *DropLogger) LogMode(logger.LogLevel) logger.Interface
LogMode 实现接口
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
用于接收 gorm 日志
type PageQuery ¶
type PageQuery struct {
// 偏移,小于 0 不匹配
Offset int `json:"offset,omitempty" form:"offset" binding:"omitempty,min=0"`
// 条数,小于 1 不匹配
Count int `json:"count,omitempty" form:"count" binding:"omitempty,min=1"`
// 排序,"column1 [desc], column2..."
Order string `json:"order,omitempty" form:"order"`
// 是否需要返回总数
Total string `json:"total,omitempty" form:"total" binding:"omitempty,oneof=0 1"`
}
PageQuery 分页查询参数
type PageResult ¶
PageResult 是 Page 的返回值
type TxDB ¶
type TxDB[K comparable, V any] struct { DB[K, V] }
事务封装
func (*TxDB[K, V]) AddAnyOmit ¶
func (m *TxDB[K, V]) AddAnyOmit(ctx context.Context, cb func(*gorm.DB) error, v any, fields ...string) error
添加
func (*TxDB[K, V]) AddIgnore ¶
func (m *TxDB[K, V]) AddIgnore(ctx context.Context, cb func(*gorm.DB) error, v V, fields ...string) error
添加,如果唯一键存在则放弃
func (*TxDB[K, V]) AddIgnoreOmit ¶
func (m *TxDB[K, V]) AddIgnoreOmit(ctx context.Context, cb func(*gorm.DB) error, v V, fields ...string) error
添加,如果唯一键存在则放弃
func (*TxDB[K, V]) AddOmit ¶
func (m *TxDB[K, V]) AddOmit(ctx context.Context, cb func(*gorm.DB) error, v V, fields ...string) error
添加
func (*TxDB[K, V]) AddOrUpdate ¶
func (m *TxDB[K, V]) AddOrUpdate(ctx context.Context, cb func(*gorm.DB) error, v V, fields ...string) error
添加,如果唯一键存在则更新指定的字段
func (*TxDB[K, V]) BatchAdd ¶
func (m *TxDB[K, V]) BatchAdd(ctx context.Context, cb func(*gorm.DB) error, vs []V, fields ...string) error
批量添加
func (*TxDB[K, V]) BatchAddAny ¶
func (m *TxDB[K, V]) BatchAddAny(ctx context.Context, cb func(*gorm.DB) error, vs []any, fields ...string) error
批量添加
func (*TxDB[K, V]) BatchDelete ¶
func (m *TxDB[K, V]) BatchDelete(ctx context.Context, cb func(*gorm.DB) error, ks []K) (int64, error)
主键批量删除,k 是主键
func (*TxDB[K, V]) BatchUpdate ¶
func (m *TxDB[K, V]) BatchUpdate(ctx context.Context, cb func(*gorm.DB) error, vs []V, fields ...string) (int64, error)
事务中批量更新
func (*TxDB[K, V]) DeleteWhere ¶
func (m *TxDB[K, V]) DeleteWhere(ctx context.Context, cb func(*gorm.DB) error, query any) (int64, error)
条件删除,query 是条件struct
func (*TxDB[K, V]) Update ¶
func (m *TxDB[K, V]) Update(ctx context.Context, cb func(*gorm.DB) error, v V, fields ...string) (int64, error)
主键条件更新,不指定字段更新所有
func (*TxDB[K, V]) UpdateAny ¶
func (m *TxDB[K, V]) UpdateAny(ctx context.Context, cb func(*gorm.DB) error, k K, v any, fields ...string) (int64, error)
更新指定字段,v 是任意类型
func (*TxDB[K, V]) UpdateAnyWhere ¶
func (m *TxDB[K, V]) UpdateAnyWhere(ctx context.Context, cb func(*gorm.DB) error, v any, query any, fields ...string) (int64, error)
更新指定字段,v 是任意类型,query 是条件struct
func (*TxDB[K, V]) UpdateColumn ¶
func (m *TxDB[K, V]) UpdateColumn(ctx context.Context, cb func(*gorm.DB) error, k K, field string, value any) (int64, error)
更新单个字段