gorm_ext

package
v0.0.0-...-389fc93 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MySql     = "mysql"
	Postgres  = "postgres"
	Sqlite    = "sqlite"
	Sqlserver = "sqlserver"
	Dm        = "dm"
)

定义数据库类型

View Source
const (
	Eq        = "="
	NotEq     = "<>"
	Gt        = ">"
	GtAndEq   = ">="
	Less      = "<"
	LessAndEq = "<="
	In        = "IN"
	NotIn     = "NOT IN"
	Like      = "LIKE"
	NotLike   = "NOT LIKE"
	StartWith = "STARTWITH"
	EndWith   = "ENDWITH"
	IsNull    = "IS NULL"
	NotNull   = "IS NOT NULL"
)

定义运算符

View Source
const (
	Max   = "Max"   //最大值
	Min   = "Min"   //最小值
	Avg   = "Avg"   //平均值
	Sum   = "Sum"   //求和
	Count = "Count" //统计行
)

聚合函数

View Source
const (
	Abs   = "Abs"   //求绝对值
	Sqrt  = "Sqrt"  //开平方根
	Ceil  = "Ceil"  //向上取整
	Floor = "Floor" //向下取整
	Round = "Round" //四舍五入
)

数值型函数

View Source
const (
	Upper = "Upper" //转大写
	Lower = "Lower" //转小写
)

字符串函数

Variables

This section is empty.

Functions

func GetTableColumn

func GetTableColumn(column any) string

GetTableColumn 通过模型字段获取数据库字段

func Init

func Init(db *gorm.DB, dbType string)

Init 初始化塞入db

func ResolveTableColumnName

func ResolveTableColumnName(column any, dbType string) string

ResolveTableColumnName 从缓存获取数据库字段名称:如果不能匹配,则返回 string 值

Types

type Condition

type Condition struct {
	TableAlias     string      //表别名
	Column         any         //字段名
	CompareSymbols string      //比较符号
	Arg            interface{} //sql 参数
	Func           string      //数据库函数,max、min 等,给当前字段套上函数
	// contains filtered or unexported fields
}

Condition 表与值比较条件

func (*Condition) BuildSql

func (c *Condition) BuildSql(dbType string, extend ...interface{}) (string, []interface{}, error)

type ConditionBuilder

type ConditionBuilder struct {
	Or      bool                //and、or
	Items   []*ConditionBuilder //条件集合
	Current WhereCondition      //当前条件
	// contains filtered or unexported fields
}

ConditionBuilder 条件构建器

func NewAndConditionBuilder

func NewAndConditionBuilder(conditions ...WhereCondition) *ConditionBuilder

NewAndConditionBuilder 创建 Builder,当条件个数为1,则加在builder本身,大于1,则加在Items;内部关系是 And

func NewAndEmptyConditionBuilder

func NewAndEmptyConditionBuilder() *ConditionBuilder

func NewOrConditionBuilder

func NewOrConditionBuilder(conditions ...WhereCondition) *ConditionBuilder

NewOrConditionBuilder 创建 Builder,当条件个数为1,则加在builder本身,大于1,则加在Items;内部关系是 Or

func NewOrEmptyConditionBuilder

func NewOrEmptyConditionBuilder() *ConditionBuilder

func (*ConditionBuilder) AddChildrenBuilder

func (c *ConditionBuilder) AddChildrenBuilder(builders ...*ConditionBuilder) *ConditionBuilder

AddChildrenBuilder builder 添加子条件

func (*ConditionBuilder) AddChildrenCondition

func (c *ConditionBuilder) AddChildrenCondition(conditions ...WhereCondition) *ConditionBuilder

AddChildrenCondition builder 添加子条件

func (*ConditionBuilder) BuildSql

func (c *ConditionBuilder) BuildSql(dbType string, extend ...interface{}) (string, []interface{}, error)

BuildSql 生成sql

func (*ConditionBuilder) Error

func (c *ConditionBuilder) Error(error string) *ConditionBuilder

func (*ConditionBuilder) SetCondition

func (c *ConditionBuilder) SetCondition(condition WhereCondition) *ConditionBuilder

SetCondition builder 设置本级条件

type ExistsCondition

type ExistsCondition struct {
	Table            schema.Tabler     //指定内表
	ConditionBuilder *ConditionBuilder //条件
	IsNotExists      bool              //默认false:exists;true :not exists
	Func             string            //数据库函数,max、min 等,给当前字段套上函数
	// contains filtered or unexported fields
}

ExistsCondition Exists 和 Not Exists

func (*ExistsCondition) BuildSql

func (c *ExistsCondition) BuildSql(dbType string, extend ...interface{}) (string, []interface{}, error)

type GormTableResult

type GormTableResult[T interface{}] struct {
	Table *TableInfo[T]
	Error error
}

GormTableResult 创建表缓存结果

func BuildGormTable

func BuildGormTable[T interface{}]() *GormTableResult[T]

BuildGormTable 获取

type OriginalCondition

type OriginalCondition struct {
	Sql string      //原始sql,不经过任何处理
	Arg interface{} //sql 参数
	// contains filtered or unexported fields
}

OriginalCondition gorm 原始where条件,不作任何处理

func (*OriginalCondition) BuildSql

func (c *OriginalCondition) BuildSql(dbType string, extend ...interface{}) (string, []interface{}, error)

type OrmWrapper

type OrmWrapper[T any] struct {
	Error error
	Model *T
	// contains filtered or unexported fields
}

OrmWrapper gorm包装器

func BuildOrmWrapper

func BuildOrmWrapper[T any](ctx context.Context, db ...*gorm.DB) *OrmWrapper[T]

BuildOrmWrapper 创建gorm包装器

func (*OrmWrapper[T]) Build

func (a *OrmWrapper[T]) Build() *gorm.DB

Build 创建 gorm sql

func (*OrmWrapper[T]) BuildForQuery

func (a *OrmWrapper[T]) BuildForQuery() *gorm.DB

BuildForQuery 创建 gorm sql

func (*OrmWrapper[T]) Count

func (a *OrmWrapper[T]) Count() (int64, error)

Count 查询总条数

func (*OrmWrapper[T]) FirstOrDefault

func (a *OrmWrapper[T]) FirstOrDefault(scan ...func(db *gorm.DB) error) (*T, error)

FirstOrDefault 返回第一条,没命中返回nil,可以传入自定义scan,自定义接收数据

func (*OrmWrapper[T]) GroupBy

func (a *OrmWrapper[T]) GroupBy(column any, tableAlias ...string) *OrmWrapper[T]

GroupBy 可多次调用,按照调用顺序排列字段

func (*OrmWrapper[T]) LeftJoin

func (a *OrmWrapper[T]) LeftJoin(table schema.Tabler, alias string, leftColumn any, rightColumn any) *OrmWrapper[T]

LeftJoin 左连表

func (*OrmWrapper[T]) LeftJoinIf

func (a *OrmWrapper[T]) LeftJoinIf(do bool, table schema.Tabler, alias string, leftColumn any, rightColumn any) *OrmWrapper[T]

LeftJoinIf 左连表

func (*OrmWrapper[T]) OrderBy

func (a *OrmWrapper[T]) OrderBy(column any, tableAlias ...string) *OrmWrapper[T]

OrderBy 可多次调用,按照调用顺序排列字段

func (*OrmWrapper[T]) OrderByDesc

func (a *OrmWrapper[T]) OrderByDesc(column any, tableAlias ...string) *OrmWrapper[T]

OrderByDesc 可多次调用,按照调用顺序排列字段

func (*OrmWrapper[T]) Select

func (a *OrmWrapper[T]) Select(selectColumns ...interface{}) *OrmWrapper[T]

Select 查询主表字段

func (*OrmWrapper[T]) SelectColumn

func (a *OrmWrapper[T]) SelectColumn(selectColumn interface{}, columnAlias string, tableAlias ...string) *OrmWrapper[T]

SelectColumn 单次查询一个字段,可传入 字段别名,表名;可多次调用

func (*OrmWrapper[T]) SelectColumnOriginal

func (a *OrmWrapper[T]) SelectColumnOriginal(selectColumn string, columnAlias string, tableAlias ...string) *OrmWrapper[T]

SelectColumnOriginal 单次查询一个字段,可传入 字段别名,表名;可多次调用;不处理字段名

func (*OrmWrapper[T]) SelectWithFunc

func (a *OrmWrapper[T]) SelectWithFunc(selectColumn string, columnAlias string, f string, tableAlias ...string) *OrmWrapper[T]

SelectWithFunc 传入表别名,查询此表下的字段

func (*OrmWrapper[T]) SelectWithTableAlias

func (a *OrmWrapper[T]) SelectWithTableAlias(tableAlias string, selectColumns ...interface{}) *OrmWrapper[T]

SelectWithTableAlias 传入表别名,查询此表下的多个字段

func (*OrmWrapper[T]) SetDb

func (a *OrmWrapper[T]) SetDb(ctx context.Context, db ...*gorm.DB) *OrmWrapper[T]

SetDb 外部传入db,适用于外部开事务的场景

func (*OrmWrapper[T]) SetTableAlias

func (a *OrmWrapper[T]) SetTableAlias(alias string) *OrmWrapper[T]

SetTableAlias 指定主表表别名,如果不指定,当有 left join 或者 exists时,默认是表名称

func (*OrmWrapper[T]) ToList

func (a *OrmWrapper[T]) ToList(scan ...func(db *gorm.DB) error) ([]*T, error)

ToList 返回列表,可以传入自定义scan,自定义接收数据

func (*OrmWrapper[T]) ToPagerList

func (a *OrmWrapper[T]) ToPagerList(pager *Pager, scan ...func(db *gorm.DB) error) (*PagerList[T], error)

ToPagerList 分页查询,可以自定义scan,否则返回当前实体分页结果

func (*OrmWrapper[T]) ToSql

func (a *OrmWrapper[T]) ToSql() (string, error)

ToSql 转换成 Sql

func (*OrmWrapper[T]) Unscoped

func (a *OrmWrapper[T]) Unscoped() *OrmWrapper[T]

Unscoped 和gorm一样,忽略软删除字段

func (*OrmWrapper[T]) Update

func (a *OrmWrapper[T]) Update(item *T, updateColumns ...interface{}) (int64, error)

Update 更新,传了字段只更新出入字段,否则更新全部

func (*OrmWrapper[T]) UpdateList

func (a *OrmWrapper[T]) UpdateList(items []*T, updateColumns ...interface{}) (int64, error)

UpdateList 更新,传了字段只更新出入字段,否则更新全部

func (*OrmWrapper[T]) Where

func (a *OrmWrapper[T]) Where(query interface{}, args ...interface{}) *OrmWrapper[T]

Where gorm 原生查询

func (*OrmWrapper[T]) WhereByColumn

func (a *OrmWrapper[T]) WhereByColumn(column any, compareSymbols string, arg interface{}, tableAlias ...string) *OrmWrapper[T]

WhereByColumn 通过字段查询,连表时支持传入表别名

func (*OrmWrapper[T]) WhereByColumnIf

func (a *OrmWrapper[T]) WhereByColumnIf(do bool, column any, compareSymbols string, arg interface{}, tableAlias ...string) *OrmWrapper[T]

WhereByColumnIf 通过字段查询,连表时支持传入表别名

func (*OrmWrapper[T]) WhereCondition

func (a *OrmWrapper[T]) WhereCondition(query WhereCondition) *OrmWrapper[T]

WhereCondition 通过条件查询

func (*OrmWrapper[T]) WhereConditionIf

func (a *OrmWrapper[T]) WhereConditionIf(do bool, query WhereCondition) *OrmWrapper[T]

WhereConditionIf 通过条件查询,加入 bool 条件控制

func (*OrmWrapper[T]) WhereIf

func (a *OrmWrapper[T]) WhereIf(do bool, query interface{}, args ...interface{}) *OrmWrapper[T]

WhereIf gorm 原生查询,加入 bool 条件控制

func (*OrmWrapper[T]) WhereIfNotNil

func (a *OrmWrapper[T]) WhereIfNotNil(query interface{}, arg interface{}) *OrmWrapper[T]

WhereIfNotNil gorm 原生查询,值为 nil 时跳过

type Pager

type Pager struct {
	Page     int32  `json:"page" form:"page"`           //页码
	PageSize int32  `json:"page_size" form:"page_size"` //分页条数
	Order    string `json:"order" form:"order"`         //排序字段
	Keyword  string `json:"keyword" form:"keyword"`     //关键词
}

Pager 分页数据请求模型

type PagerList

type PagerList[T interface{}] struct {
	Page       int32  `json:"page" form:"page"`               //页码
	PageSize   int32  `json:"page_size" form:"page_size"`     //分页条数
	TotalCount int32  `json:"total_count" form:"total_count"` //总条数
	Order      string `json:"order" form:"order"`             //排序字段
	Data       []*T   `json:"data" form:"data"`               //数据项
}

PagerList 分页数据结果模型

type TableCondition

type TableCondition struct {
	InnerAlias     string //内部表表别名(exists);左边表别名
	InnerColumn    any    //内部表字段名(exists);左边表字段名
	OuterAlias     string //外部表表别名(exists);右边表别名
	OuterColumn    any    //外部表字段名(exists);右边表字段名
	CompareSymbols string //比较运算符号
	InnerFunc      string //数据库函数,max、min 等,给当前字段套上函数 左表
	OuterFunc      string //数据库函数,max、min 等,给当前字段套上函数 右表
	// contains filtered or unexported fields
}

TableCondition 表与表之间比较条件

func (*TableCondition) BuildSql

func (c *TableCondition) BuildSql(dbType string, extend ...interface{}) (string, []interface{}, error)

type TableInfo

type TableInfo[T interface{}] struct {
	T *T
	*TableSchema
}

TableInfo 表信息

type TableSchema

type TableSchema struct {
	Name              string //表名
	DeletedColumnName string //软删除字段名
}

func GetTableSchema

func GetTableSchema(table schema.Tabler) *TableSchema

GetTableSchema 获取表元数据

type WhereCondition

type WhereCondition interface {
	BuildSql(dbType string, ext ...interface{}) (string, []interface{}, error) //生成 sql
}

WhereCondition 定义查询条件

Jump to

Keyboard shortcuts

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