ggorm

package
v0.0.0-...-b65ca89 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 Add

func Add(tx *gorm.DB, v any, fields ...string) *gorm.DB

Add 封装,fields 是添加的字段

func AddIgnore

func AddIgnore(tx *gorm.DB, v any, fields ...string) *gorm.DB

AddIgnore 封装,fields 是修改的字段

func AddIgnoreOmit

func AddIgnoreOmit(tx *gorm.DB, v any, fields ...string) *gorm.DB

AddIgnoreOmit 封装,fields 是修改的字段

func AddOmit

func AddOmit(tx *gorm.DB, v any, fields ...string) *gorm.DB

AddOmit 封装,fields 是忽略的字段

func AddOrUpdate

func AddOrUpdate(tx *gorm.DB, v any, fields ...string) *gorm.DB

AddOrUpdate 封装,fields 是添加/修改的字段

func All

func All[M any](db *gorm.DB, query any, fields ...string) (ms []M, err error)

All 用于查询全部,query 是条件,fields 是查询的字段

func AllLockForUpdate

func AllLockForUpdate[M any](db *gorm.DB, query any, fields ...string) ([]M, error)

查询锁

func AllOmit

func AllOmit[M any](db *gorm.DB, query any, fields ...string) (ms []M, err error)

AllOmit 用于查询全部,query 是条件,fields 是忽略的字段

func AllOrder

func AllOrder[M any](db *gorm.DB, query any, order string, fields ...string) (ms []M, err error)

AllOrder 用于查询全部,query 是条件,order 是排序sql,fields 是查询的字段

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 Exec

func Exec(tx, clean *gorm.DB, init func(db *gorm.DB) *gorm.DB) (int64, error)

执行sql 在 db 被全局锁表,比如 casbin 的事务使用

func Field

func Field[M any](db *gorm.DB, query any, field string) (ms []M, err error)

Field 用于查询单个字段,query 是条件,field 是字段名

func First

func First(tx *gorm.DB, ptr any, query any, fields ...string) error

First 封装,ptr 是接收数据的结构体指针,query 是条件struct,fields 是查询的字段

func GetLockForUpdate

func GetLockForUpdate(db *gorm.DB, m any) error

查询锁

func IsDataNotFound

func IsDataNotFound(err error) bool

是否没有数据错误

func IsMySqlDuplicateError

func IsMySqlDuplicateError(err error) bool

判断是否唯一性错误,mysql 使用

func LockForUpdate

func LockForUpdate(db *gorm.DB) *gorm.DB

查询锁

func NewLog

func NewLog(lg glog.Logger, traceKey string) logger.Interface

NewLog 用于接收 的日志 traceKey 用于从 context 中获取 traceId

func Open

func Open(dsn string) (*gorm.DB, error)

初始化,自动创建数据库 mysql dsn: mysql://root:1234@tcp(127.0.0.1:3306)/test?charset=utf8mb4&parseTime=True&loc=Local sqlite dsn: ./test.db

func Page

func Page[M any](db *gorm.DB, page *PageQuery, res *PageResult[M], fields ...string) error

Page 用于分页查询

func PageOmit

func PageOmit[M any](db *gorm.DB, page *PageQuery, res *PageResult[M], fields ...string) error

Page 用于分页查询

func SQL

func SQL(tx, clean *gorm.DB, init func(db *gorm.DB) *gorm.DB) (string, []any)

使用一个新 session 来生成sql 在 db 被全局锁表,比如 casbin 的事务使用

func Scan

func Scan(tx, clean *gorm.DB, init func(db *gorm.DB) *gorm.DB, destPtr any) error

执行sql 在 db 被全局锁表,比如 casbin 的事务使用

func ScanValue

func ScanValue(value any, ptr any) error

func Update

func Update(tx *gorm.DB, v any, query any, fields ...string) *gorm.DB

Update 封装,fields 是添加的字段,query 是条件struct,fields 是修改的字段

func ValueData

func ValueData(ptr any) (driver.Value, error)

func WhereQuery

func WhereQuery(db *gorm.DB, q any) *gorm.DB

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%

func WhereQuerySelect

func WhereQuerySelect(db *gorm.DB, field string, value reflect.Value, kind reflect.Kind, raw bool) *gorm.DB

WhereQuerySelect 处理 Select 查询

func WhereQueryWithTag

func WhereQueryWithTag(db *gorm.DB, q any, tag string) *gorm.DB

WhereQueryWithTag 使用自定义标签名进行查询条件构建 功能与 WhereQuery 相同,但允许指定自定义的标签名

Types

type DB

type DB[K comparable, V any] struct {
	// db
	D *gorm.DB
	// 表名
	T string
	// 主键名
	K string
}

进一步封装,K 是主键命,V 是结构体

func (*DB[K, V]) Add

func (m *DB[K, V]) Add(ctx context.Context, v V, fields ...string) error

添加

func (*DB[K, V]) AddAny

func (m *DB[K, V]) AddAny(ctx context.Context, v any, fields ...string) error

添加,v 是符合表结构的任意结构体或者map

func (*DB[K, V]) AddAnyOmit

func (m *DB[K, V]) AddAnyOmit(ctx context.Context, v any, fields ...string) error

添加,v 是符合表结构的任意结构体或者map

func (*DB[K, V]) AddIgnore

func (m *DB[K, V]) AddIgnore(ctx context.Context, v V, fields ...string) error

添加,如果唯一键存在则放弃

func (*DB[K, V]) AddIgnoreOmit

func (m *DB[K, V]) AddIgnoreOmit(ctx context.Context, v V, fields ...string) error

添加,如果唯一键存在则放弃

func (*DB[K, V]) AddOmit

func (m *DB[K, V]) AddOmit(ctx context.Context, v V, fields ...string) error

添加

func (*DB[K, V]) AddOrUpdate

func (m *DB[K, V]) AddOrUpdate(ctx context.Context, v V, fields ...string) error

添加,如果唯一键存在则更新指定的字段

func (*DB[K, V]) All

func (m *DB[K, V]) All(ctx context.Context, query any, fields ...string) ([]V, error)

查询所有,query 是条件,fields 是字段名称

func (*DB[K, V]) AllOmit

func (m *DB[K, V]) AllOmit(ctx context.Context, query any, fields ...string) ([]V, error)

查询所有,query 是条件,fields 是忽略的字段名称

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 (m *DB[K, V]) AutoMigrate(db *gorm.DB, v V, table string, key string) error

初始化,自动创建表

func (*DB[K, V]) BatchAdd

func (m *DB[K, V]) BatchAdd(ctx context.Context, vs []V, fields ...string) error

批量添加

func (*DB[K, V]) BatchAddAny

func (m *DB[K, V]) BatchAddAny(ctx context.Context, vs []any, fields ...string) error

批量添加,vs 是符合表结构的任意结构体列表

func (*DB[K, V]) BatchDelete

func (m *DB[K, V]) BatchDelete(ctx context.Context, ks []K) (int64, error)

主键批量删除,k 是主键

func (*DB[K, V]) BatchUpdate

func (m *DB[K, V]) BatchUpdate(ctx context.Context, vs []V, fields ...string) (int64, error)

事务中批量更新

func (*DB[K, V]) Delete

func (m *DB[K, V]) Delete(ctx context.Context, k K) (int64, error)

主键删除,k 是主键

func (*DB[K, V]) DeleteWhere

func (m *DB[K, V]) DeleteWhere(ctx context.Context, query any) (int64, error)

条件删除,query 是条件struct

func (*DB[K, V]) Field

func (m *DB[K, V]) Field(ctx context.Context, query any, field string, resPtr any) error

查询单个字段,query 是条件,field 是字段名称,resPtr 是数组指针

func (*DB[K, V]) First

func (m *DB[K, V]) First(ctx context.Context, ptr V, query any, fields ...string) error

查询第一个,query 是条件struct,ptr 是接收数据的结构体指针

func (*DB[K, V]) FirstAny

func (m *DB[K, V]) FirstAny(ctx context.Context, ptr any, query any, fields ...string) error

查询第一个,query 是条件struct,ptr 是接收数据的结构体指针

func (*DB[K, V]) Get

func (m *DB[K, V]) Get(ctx context.Context, ptr V, fields ...string) error

主键查询,ptr 是接收数据的结构体指针

func (*DB[K, V]) Init

func (m *DB[K, V]) Init(db *gorm.DB, table string, key string)

初始化

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]) Update

func (m *DB[K, V]) Update(ctx context.Context, v V, fields ...string) (int64, error)

主键条件更新,不指定字段更新所有

func (*DB[K, V]) UpdateAny

func (m *DB[K, V]) UpdateAny(ctx context.Context, k K, v any, fields ...string) (int64, error)

更新指定字段,v 是符合表结构的任意结构体或者map

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

func (m *DB[K, V]) UpdateColumn(ctx context.Context, k K, field string, value any) (int64, error)

更新单个字段

func (*DB[K, V]) UpdateColumnWhere

func (m *DB[K, V]) UpdateColumnWhere(ctx context.Context, query any, field string, value any) (int64, error)

更新单个字段

func (*DB[K, V]) UpdateColumns

func (m *DB[K, V]) UpdateColumns(ctx context.Context, k K, v map[string]any) (int64, error)

更新多个字段

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

LogMode 实现接口

func (*DropLogger) Trace

func (g *DropLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error)

Trace 实现接口

func (*DropLogger) Warn

func (g *DropLogger) Warn(ctx context.Context, str string, args ...any)

Warn 实现接口

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

用于接收 gorm 日志

func (*Logger) Error

func (g *Logger) Error(ctx context.Context, str string, args ...any)

Error 实现接口

func (*Logger) Info

func (g *Logger) Info(ctx context.Context, str string, args ...any)

Info 实现接口

func (*Logger) LogMode

func (g *Logger) LogMode(logger.LogLevel) logger.Interface

LogMode 实现接口

func (*Logger) Trace

func (g *Logger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error)

Trace 实现接口

func (*Logger) Warn

func (g *Logger) Warn(ctx context.Context, str string, args ...any)

Warn 实现接口

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 分页查询参数

func (*PageQuery) InitDB

func (m *PageQuery) InitDB(db *gorm.DB) *gorm.DB

InitDB 根据分页参数初始化数据库查询 设置 offset、limit 和排序规则 参数:

  • db: GORM 数据库连接

返回:

  • 设置了分页参数的数据库连接

type PageResult

type PageResult[M any] struct {
	// 总数
	Total int64 `json:"total"`
	// 列表
	Data []M `json:"data"`
}

PageResult 是 Page 的返回值

type TxDB

type TxDB[K comparable, V any] struct {
	DB[K, V]
}

事务封装

func (*TxDB[K, V]) Add

func (m *TxDB[K, V]) Add(ctx context.Context, cb func(*gorm.DB) error, v V, fields ...string) error

添加

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]) Delete

func (m *TxDB[K, V]) Delete(ctx context.Context, cb func(*gorm.DB) error, k K) (int64, error)

主键删除,k 是主键

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)

更新单个字段

func (*TxDB[K, V]) UpdateColumnWhere

func (m *TxDB[K, V]) UpdateColumnWhere(ctx context.Context, cb func(*gorm.DB) error, query any, field string, value any) (int64, error)

更新单个字段

func (*TxDB[K, V]) UpdateColumns

func (m *TxDB[K, V]) UpdateColumns(ctx context.Context, cb func(*gorm.DB) error, k K, v map[string]any) (int64, error)

更新多个字段

Jump to

Keyboard shortcuts

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