rdb

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2024 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

* @Author: reel * @Date: 2023-10-15 07:48:02 * @LastEditors: reel * @LastEditTime: 2024-01-20 14:13:22 * @Description: 回掉函数

* @Author: reel * @Date: 2023-06-15 06:55:41 * @LastEditors: reel * @LastEditTime: 2024-03-27 04:48:55 * @Description: 根据条件结构体, 自动构建查询语句, 并返回gorm.DB, 用于扩展

* @Author: reel * @Date: 2023-05-16 22:16:53 * @LastEditors: reel * @LastEditTime: 2024-03-27 04:50:07 * @Description: 关系数据库配置

* @Author: reel * @Date: 2023-10-15 22:49:03 * @LastEditors: reel * @LastEditTime: 2024-01-14 14:38:54 * @Description: 分区相关

* @Author: reel * @Date: 2023-10-15 20:25:56 * @LastEditors: reel * @LastEditTime: 2023-10-15 22:11:07 * @Description: 定义删除的类型

Index

Constants

View Source
const (
	DATA_PERMISSION_ONESELF       int8 = iota + 1 //本人可见
	DATA_PERMISSION_ALL                           //全部可见
	DATA_PERMISSION_ONLY_DEPT                     //所在部门可见
	DATA_PERMISSION_ONLY_DEPT_ALL                 //所在部门及子级可见
	DATA_PERMISSION_ONLY_CUSTOM                   //选择的部门可见
)
View Source
const (
	// 数据分区模式
	// 默认值, 不分区
	SHADING_MODEL_NOT int8 = iota

	// 数据都在一张表中
	SHADING_MODEL_ONE

	// 按表分区, 根据分区字段值, 设置表后缀, 如果表设置了分区
	SHADING_MODEL_TABLE

	// 按库(不按schema)分区, 根据分区字段值, 设置不同的库名后缀
	//
	// 请注意, 要确保当前用户有创建数据库以及数据表的权限
	SHADING_MODEL_DB

	TABLE_INIT_ALL = "all"

	// 分区列名
	SHADING_KEY             = "sk"
	TX_SHADING_MODEL_KEY    = "tx_sharding_model"
	TX_SHADING_TABLE_KEY    = "tx_sharding_table"
	TX_CONDITION_BUILD_KEY  = "tx_condition_build"
	TX_SUB_QUERY_COLUMN_KEY = "tx_sub_query_column"
	TX_DATA_PERMISSION_KEY  = "tx_data_permission"
)

Variables

View Source
var (
	FlagDeleted = 1
	FlagActived = 0
)

Functions

func CopyCtx added in v1.1.0

func CopyCtx(oldTx, newTx *gorm.DB)

func GetShardingKey added in v1.1.0

func GetShardingKey(tx *gorm.DB) (sk string)

func IsUniqueError

func IsUniqueError(err error) (ok bool)

Types

type Condition

type Condition struct {
	PageSize    int
	PageNumber  int
	Columns     string
	TableName   string
	Orders      string
	Where       map[string]interface{}
	QryDelete   bool
	IsSharding  bool
	ShardingKey string
}

func GenConditionWithParams

func GenConditionWithParams(params reflect.Value) *Condition

根据请求参数构建查询条件

其中表名和返回值需要手动添加

仅适用单表的简单where-and条件查询, 不适用于复杂关联查询

复杂业务查询须手动处理或构建查询视图

func NewCondition

func NewCondition() *Condition

type DataPermissionIntCtx added in v1.1.0

type DataPermissionIntCtx struct {
	DataPermissionType  int8 //
	DataPermission      int64
	DataPermissionScope []int64
}

type DataPermissionIntModel added in v1.1.0

type DataPermissionIntModel struct {
	DataPermission int64 `json:"-" gorm:"column:dp;index;comment:数据权限"`
}

定义数据权限模型 用于有数据权限需求的查询, 创建等, 和分区表的原理类似, 但不涉及到分表操作 适用于int类型的数据

type DataPermissionStringCtx added in v1.1.0

type DataPermissionStringCtx struct {
	DataPermissionType  int8 //
	DataPermission      string
	DataPermissionScope []string
}

type DataPermissionStringModel added in v1.1.0

type DataPermissionStringModel struct {
	DataPermission string `json:"-" gorm:"column:dp;index;comment:数据权限"`
}

定义数据权限模型 用于有数据权限需求的查询, 创建等, 和分区表的原理类似, 但不涉及到分表操作 适用于string类型的数据

type DeleteParams added in v1.1.0

type DeleteParams struct {
	ID []uint `json:"id" binding:"required" conditions:"-"`
}

type DeletedAt added in v1.1.0

type DeletedAt uint

func (DeletedAt) DeleteClauses added in v1.1.0

func (DeletedAt) DeleteClauses(f *schema.Field) []clause.Interface

func (DeletedAt) QueryClauses added in v1.1.0

func (DeletedAt) QueryClauses(f *schema.Field) []clause.Interface

func (DeletedAt) UpdateClauses added in v1.1.0

func (DeletedAt) UpdateClauses(f *schema.Field) []clause.Interface

type ModeListJson

type ModeListJson []interface{}

func (*ModeListJson) Scan

func (j *ModeListJson) Scan(value interface{}) error

func (ModeListJson) Value

func (j ModeListJson) Value() (driver.Value, error)

实现 driver.Valuer 接口,Value 返回 json value

type ModeMapJson

type ModeMapJson map[string]interface{}

func (*ModeMapJson) Scan

func (j *ModeMapJson) Scan(value interface{}) error

func (ModeMapJson) Value

func (j ModeMapJson) Value() (driver.Value, error)

实现 driver.Valuer 接口,Value 返回 json value

type Model

type Model struct {
	ID        uint      `json:"id" gorm:"primaryKey;"`                    // id
	CreatedAT uint      `json:"created_at" gorm:"autoCreateTime:milli"`   // 创建时间
	CreatedBy string    `json:"created_by"`                               // 创建人
	UpdatedAT uint      `json:"updated_at" gorm:"autoUpdateTime:milli"`   // 修改时间
	UpdatedBy string    `json:"updated_by"`                               // 修改人
	DeletedAT DeletedAt `json:"deleted_at" gorm:"index;softDelete:milli"` // 删除时间
	DeletedBy string    `json:"deleted_by"`                               // 删除人
	Status    int8      `json:"status" gorm:"index;comment:状态;default:1"` // 是否启用 1 表示启用 -1表示失效
}

通用的字段处理

func (*Model) BeforeCreate

func (m *Model) BeforeCreate(tx *gorm.DB) error

func (*Model) BeforeUpdate

func (m *Model) BeforeUpdate(tx *gorm.DB) error

type ModelQuery

type ModelQuery struct {
	ID     uint `json:"id"`
	Status int8 `json:"status"`
}

通用查询查询字段处理

type RegisterFunc

type RegisterFunc func() error

type ShardingModel added in v1.1.0

type ShardingModel struct {
	ShadingKey string `json:"-" gorm:"column:sk;index;comment:分区"`
}

定义分区字段 用于表分区使用

func (*ShardingModel) BeforeCreate added in v1.1.0

func (m *ShardingModel) BeforeCreate(tx *gorm.DB) error

func (*ShardingModel) BeforeDelete added in v1.1.0

func (m *ShardingModel) BeforeDelete(tx *gorm.DB) error

func (*ShardingModel) BeforeUpdate added in v1.1.0

func (m *ShardingModel) BeforeUpdate(tx *gorm.DB) error

func (*ShardingModel) TableName added in v1.1.0

func (m *ShardingModel) TableName(table string) string

type SoftDeleteDeleteClause added in v1.1.0

type SoftDeleteDeleteClause struct {
	Field         *schema.Field
	Flag          bool
	TimeType      schema.TimeType
	DeleteAtField *schema.Field
}

func (SoftDeleteDeleteClause) Build added in v1.1.0

func (SoftDeleteDeleteClause) MergeClause added in v1.1.0

func (sd SoftDeleteDeleteClause) MergeClause(*clause.Clause)

func (SoftDeleteDeleteClause) ModifyStatement added in v1.1.0

func (sd SoftDeleteDeleteClause) ModifyStatement(stmt *gorm.Statement)

func (SoftDeleteDeleteClause) Name added in v1.1.0

func (sd SoftDeleteDeleteClause) Name() string

type SoftDeleteQueryClause added in v1.1.0

type SoftDeleteQueryClause struct {
	Field *schema.Field
}

func (SoftDeleteQueryClause) Build added in v1.1.0

func (SoftDeleteQueryClause) MergeClause added in v1.1.0

func (sd SoftDeleteQueryClause) MergeClause(*clause.Clause)

func (SoftDeleteQueryClause) ModifyStatement added in v1.1.0

func (sd SoftDeleteQueryClause) ModifyStatement(stmt *gorm.Statement)

func (SoftDeleteQueryClause) Name added in v1.1.0

func (sd SoftDeleteQueryClause) Name() string

type SoftDeleteUpdateClause added in v1.1.0

type SoftDeleteUpdateClause struct {
	Field *schema.Field
}

func (SoftDeleteUpdateClause) Build added in v1.1.0

func (SoftDeleteUpdateClause) MergeClause added in v1.1.0

func (sd SoftDeleteUpdateClause) MergeClause(*clause.Clause)

func (SoftDeleteUpdateClause) ModifyStatement added in v1.1.0

func (sd SoftDeleteUpdateClause) ModifyStatement(stmt *gorm.Statement)

func (SoftDeleteUpdateClause) Name added in v1.1.0

func (sd SoftDeleteUpdateClause) Name() string

type Store

type Store interface {
	DB() *gorm.DB

	// service.Service接口
	Name() string
	Stop() error
	Start() error
	Status() int8

	// DB 相关
	SetConfig(fs ...dsn.DsnFunc) error

	// 注册表结构, 同时允许注册时写入函数, 如初始化创建部分数据等
	Register(t Tabler, fs ...RegisterFunc) Store

	// 根据条件结构体生成查询tx
	BuildQuery(cb *Condition) (tx *gorm.DB)

	// 通过参数反射值构建普通查询tx用于应用端使用
	// 大多数查询通过使用该方法即可
	BuildQueryWithParams(params reflect.Value) *gorm.DB

	// 通过参数反射值构建普通查询tx用于应用端使用
	//
	// 此方法适用于表中有ID(主键)的字段, 优化了翻页查询性能
	BuildQueryWihtSubQryID(cb *Condition) (tx *gorm.DB)

	// 设置分区模式
	//
	// SHADING_MODEL_NOT : 不分区, 默认值, 数据都在一张表中,
	//
	// SHADING_MODEL_TABLE: 按表分区, 根据分区字段值, 设置表后缀
	//
	// TODO:SHADING_MODEL_DB: 按库(schema)分区, 根据分区字段值, 设置不同的库名(schema)后缀
	//
	// 该模式适用于使用cores 上下文ctx.TX()方式生成的 gorm.DB, 且在上下文中传入了分区字段, 会自动构建查询条件, 配合 ShardingModel使用,可以自动写入分区字段
	//
	// 如果直接使用gorm.DB, 该设置并不会生效
	SetShardingModel(model int8, suffix []interface{})

	// 获取分区模式
	ShardingModel() (model int8)

	// 获取可分区表
	ShardingTable() map[string][]string

	// // SHADING_MODEL_TABLE(按表分区) 模式下, 自定义添加可用于分区的表
	AddShardingTable(table string)

	// 添加启动前的前置执行程序
	AddMigrateList(fs ...func() error)

	//添加分区后缀, 同时会重置表分区,自动迁移新增表分区的结构
	AddShardingSuffixs(suffixs string) (err error)
	// contains filtered or unexported methods
}

func New

func New() (s Store)

type Tabler

type Tabler interface {
	TableName() string
}

Jump to

Keyboard shortcuts

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