db

package
v1.0.7 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsZero added in v1.0.5

func IsZero(value any) bool

isNilOrEmpty 判断 Value 是否为 nil 或空值(使用 IsZero 优化)

Types

type Backup

type Backup struct {
	Db *sql.DB
	// contains filtered or unexported fields
}

func NewDbBackup

func NewDbBackup(db *gorm.DB, path string) (*Backup, error)

创建一个数据库备份对象

func (*Backup) Backup

func (db *Backup) Backup() error

备份数据库

func (*Backup) GetDbName

func (db *Backup) GetDbName() (string, error)

func (*Backup) Restore

func (db *Backup) Restore() error

恢复数据库

type DeleteParam

type DeleteParam struct {
	IDS []int32 `json:"id" validate:"required:主键值必须"`
}

type FirstParam

type FirstParam struct {
	ID int32 `form:"id" validate:"required:主键值必须"`
}

type ListData

type ListData struct {
	Data      any   `json:"data"`
	Total     int32 `json:"total"`      // 总数量
	TotalPage int32 `json:"total_page"` // 总页数
	Page      int32 `json:"page"`
	PageSize  int32 `json:"page_size"`
}

type ListParamBase

type ListParamBase struct {
	Page     int32  `form:"page"`
	PageSize int32  `form:"page_size"`
	Order    string `form:"order"`
}

type Model

type Model struct {
	Db     *gorm.DB
	Error  error
	Pk     int32
	PkName string
	Model  any
}

func NewModel

func NewModel(db *gorm.DB, model any) *Model

func (*Model) Code

func (m *Model) Code(n int, field string) (string, error)

生成唯一随机码

func (*Model) Copy

func (m *Model) Copy(param any) *Model

func (*Model) Delete

func (m *Model) Delete(id any) *Model

删除

func (*Model) FindChildren

func (m *Model) FindChildren(pid any, idName, pidName, childrenName, order string) *Model

func (*Model) FindChildrenID

func (m *Model) FindChildrenID(ids *[]int32, pidName string) *Model

func (*Model) List

func (m *Model) List(data *ListData) *Model

查询列表

func (*Model) NotSame

func (m *Model) NotSame(sames *[]Same) *Model

唯一性判断

func (*Model) Save

func (m *Model) Save() *Model

func (*Model) SetModel

func (m *Model) SetModel(db *gorm.DB) *Model

func (*Model) SetPk

func (m *Model) SetPk(pk int32) *Model

func (*Model) SetPkName

func (m *Model) SetPkName(pkName string) *Model

func (*Model) Update

func (m *Model) Update(field string, value any, containsas []string) *Model

更新

func (*Model) Where

func (m *Model) Where(data []Where) *Model

将 Where 结构体转换为安全的 SQL where 子句和参数

type ModelChildren added in v1.0.4

type ModelChildren[T any] struct {
	PID      int32 `gorm:"column:pid;type:bigint(20);default:0;comment:父级id" json:"pid"`
	Level    int32 `gorm:"column:level;type:tinyint(1);default:1;comment:层级" json:"level"`
	Children []T   `gorm:"-:migration;foreignKey:pid;references:id" json:"children"`
}

type ModelCreatedAt added in v1.0.4

type ModelCreatedAt struct {
	CreatedAt Time `gorm:"column:created_at;type:datetime(3);autoCreateTime:milli;comment:创建时间" json:"created_at"`
}

type ModelDeleteAt added in v1.0.4

type ModelDeleteAt struct {
	DeleteAt Time `gorm:"column:delete_at;type:datetime(3);comment:删除时间" json:"delete_at"`
}

type ModelHasChildren added in v1.0.4

type ModelHasChildren struct {
	HasChildren bool `gorm:"-:all;default:false" json:"hasChildren"`
}

type ModelID added in v1.0.4

type ModelID struct {
	ID int32 `gorm:"column:id;primaryKey;autoIncrement;comment:ID" json:"id"`
}

type ModelSort added in v1.0.4

type ModelSort struct {
	Sort int32 `gorm:"column:sort;type:bigint(11);default:100;comment:排序" json:"sort"`
}

type ModelState added in v1.0.4

type ModelState struct {
	State int32 `gorm:"column:state;type:tinyint(1);default:1;comment:状态:1-开启,2-关闭" json:"state"`
}

type ModelUpdatedAt added in v1.0.4

type ModelUpdatedAt struct {
	UpdatedAt Time `gorm:"column:updated_at;type:datetime(3);autoUpdateTime:milli;comment:更新时间" json:"updated_at"`
}

type Same

type Same struct {
	Db      *gorm.DB
	Message string
}

type ShardingMeta

type ShardingMeta struct {
	ID          int32     `gorm:"primaryKey;autoIncrement"`         // 自增主键
	BaseTable   string    `gorm:"type:varchar(50);not null;index"`  // 基础表名(如 access_log)
	ActualTable string    `gorm:"type:varchar(50);not null;unique"` // 实际分表名(如 access_log_1)
	Num         int64     `gorm:"not null"`                         // 分表序号(1、2、3...)
	CreatedAt   time.Time `gorm:"type:datetime;"`                   // 分表创建时间
}

ShardingMeta 分表元数据模型:记录基础表对应的所有分表

func (*ShardingMeta) TableName

func (m *ShardingMeta) TableName() string

type ShardingService

type ShardingService struct {
	MaxNum int64
	Table  string
	Num    int64
	Error  error
	// contains filtered or unexported fields
}

func NewShardingService

func NewShardingService(gormDB *gorm.DB, baseName string, maxNum int64) *ShardingService

func (*ShardingService) GetTable

func (s *ShardingService) GetTable(model any) *ShardingService

func (*ShardingService) SetTable

func (s *ShardingService) SetTable(model any) *ShardingService

type Time

type Time time.Time

func (Time) MarshalJSON

func (t Time) MarshalJSON() ([]byte, error)

func (*Time) Scan

func (t *Time) Scan(v any) error

func (Time) ToDateString

func (t Time) ToDateString() string

func (Time) ToString

func (t Time) ToString() string

func (Time) Value

func (t Time) Value() (driver.Value, error)

type UpdateParam

type UpdateParam struct {
	ID    int32  `json:"id" validate:"required:主键值必须"`
	Field string `json:"field" validate:"required:字段名必须"`
	Value any    `json:"value"`
}

更新参数

type Where

type Where struct {
	Name     string // 字段名
	Op       string // 操作符
	Value    any    // 条件值
	Nullable bool   // 是否允许为空值
}

Jump to

Keyboard shortcuts

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