orm

package
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2024 License: MIT Imports: 10 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"
	IsNotNull = "IS NOT NULL"
)

定义运算符

View Source
const (
	LeftJoin  = "Left Join"
	InnerJoin = "Inner Join"
	RightJoin = "Right Join"
	OuterJoin = "Outer Join"
)

连表

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 FirstOrDefault

func FirstOrDefault[T interface{}](slice []T) T

func GetString added in v0.0.7

func GetString(column any) string

func GetTableColumn

func GetTableColumn(column any) string

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

func Init

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

Init 初始化塞入db,可以指定数据库类型,不指定时从db解析

func ResolveTableColumnName

func ResolveTableColumnName(column any, dbType string) string

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

Types

type ColumnCondition added in v0.0.7

type ColumnCondition 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
}

ColumnCondition 表与表之间比较条件

func (*ColumnCondition) BuildSql added in v0.0.7

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

type Condition

type Condition struct {
	TableAlias     string      //表别名
	Column         any         //字段名
	CompareSymbols string      //比较符号
	Arg            interface{} //sql 参数,也可以是子表 *gorm.DB
	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 []OrmCondition //条件集合
	// contains filtered or unexported fields
}

ConditionBuilder 条件构建器

func NewAnd added in v0.0.6

func NewAnd(conditions ...OrmCondition) *ConditionBuilder

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

func NewOr added in v0.0.6

func NewOr(conditions ...OrmCondition) *ConditionBuilder

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

func (*ConditionBuilder) Add added in v0.0.6

func (c *ConditionBuilder) Add(conditions ...OrmCondition) *ConditionBuilder

Add builder 添加子条件

func (*ConditionBuilder) BuildSql

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

BuildSql 生成sql

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 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 OrmCondition added in v0.0.6

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

OrmCondition 定义查询条件

type OrmTableResult

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

OrmTableResult 创建表缓存结果

func BuildOrmTable

func BuildOrmTable[T interface{}]() *OrmTableResult[T]

BuildOrmTable 获取

type OrmWrapper

type OrmWrapper[T any] struct {
	Error error //过程中产生的错误
	// 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 (o *OrmWrapper[T]) Build() *gorm.DB

Build 创建 gorm sql

func (*OrmWrapper[T]) BuildForQuery

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

BuildForQuery 创建成一个子表,用于其他地方子查询

func (*OrmWrapper[T]) Count

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

Count 查询总条数

func (*OrmWrapper[T]) Debug

func (o *OrmWrapper[T]) Debug() *OrmWrapper[T]

Debug 打印sql

func (*OrmWrapper[T]) Delete

func (o *OrmWrapper[T]) Delete() (int64, error)

Delete 根据条件删除

func (*OrmWrapper[T]) DeleteById

func (o *OrmWrapper[T]) DeleteById(idList ...interface{}) error

DeleteById 通过id删除数据,可以传入id集合

func (*OrmWrapper[T]) Distinct added in v0.0.8

func (o *OrmWrapper[T]) Distinct() *OrmWrapper[T]

Distinct 去重

func (*OrmWrapper[T]) FirstOrDefault

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

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

func (*OrmWrapper[T]) GetById

func (o *OrmWrapper[T]) GetById(id interface{}) (*T, error)

GetById 通过id获取数据

func (*OrmWrapper[T]) GetByIds

func (o *OrmWrapper[T]) GetByIds(idList []interface{}) ([]*T, error)

GetByIds 通过id获取数据

func (*OrmWrapper[T]) GetNewDb

func (o *OrmWrapper[T]) GetNewDb() *gorm.DB

GetNewDb 获取新的db

func (*OrmWrapper[T]) GroupBy

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

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

func (*OrmWrapper[T]) Having

func (o *OrmWrapper[T]) Having(having OrmCondition) *OrmWrapper[T]

func (*OrmWrapper[T]) InnerJoin

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

InnerJoin 内连表

func (*OrmWrapper[T]) InnerJoinIf

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

InnerJoinIf 内连表

func (*OrmWrapper[T]) Insert added in v0.0.7

func (o *OrmWrapper[T]) Insert(item *T) error

Insert 插入单条

func (*OrmWrapper[T]) Inserts added in v0.0.7

func (o *OrmWrapper[T]) Inserts(items []*T) error

Inserts 插入多条

func (*OrmWrapper[T]) Join

func (o *OrmWrapper[T]) Join(table schema.Tabler, alias string, leftColumn any, rightColumn any, joinKey string) *OrmWrapper[T]

Join 连表

func (*OrmWrapper[T]) JoinChildTable

func (o *OrmWrapper[T]) JoinChildTable(db *gorm.DB, alias string, leftColumn any, rightColumn any, joinKey string) *OrmWrapper[T]

JoinChildTable 连衍生表

func (*OrmWrapper[T]) JoinChildTableIf

func (o *OrmWrapper[T]) JoinChildTableIf(do bool, table *gorm.DB, alias string, leftColumn any, rightColumn any, joinKey string) *OrmWrapper[T]

JoinChildTableIf 连衍生表

func (*OrmWrapper[T]) JoinIf

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

JoinIf 连表

func (*OrmWrapper[T]) LeftJoin

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

LeftJoin 左连表

func (*OrmWrapper[T]) LeftJoinIf

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

LeftJoinIf 左连表

func (*OrmWrapper[T]) Limit

func (o *OrmWrapper[T]) Limit(limit int) *OrmWrapper[T]

Limit Limit

func (*OrmWrapper[T]) Offset

func (o *OrmWrapper[T]) Offset(limit int) *OrmWrapper[T]

Offset Offset

func (*OrmWrapper[T]) OrderBy

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

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

func (*OrmWrapper[T]) OrderByDesc

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

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

func (*OrmWrapper[T]) Select

func (o *OrmWrapper[T]) Select(columns ...interface{}) *OrmWrapper[T]

Select 选择多个字段;传入字段、字符串;如果是字符串,则对字符串不做任何处理,原样查询

func (*OrmWrapper[T]) SelectOne added in v0.0.7

func (o *OrmWrapper[T]) SelectOne(column interface{}, columnAlias string, tableAlias ...string) *OrmWrapper[T]

SelectOne 选择一个字段(字段、字符串、子查询*gorm.DB),可传入 字段别名,表名;可多次调用

func (*OrmWrapper[T]) SelectOneWithFunc added in v0.0.7

func (o *OrmWrapper[T]) SelectOneWithFunc(column interface{}, columnAlias string, f string, tableAlias ...string) *OrmWrapper[T]

SelectOneWithFunc 在 SelectOne 的基础上传入函数包装,比如 Max、Min、Count 等

func (*OrmWrapper[T]) SelectTable added in v0.0.7

func (o *OrmWrapper[T]) SelectTable(tableAlias string, columns ...interface{}) *OrmWrapper[T]

SelectTable 在 Select 基础上传入表别名

func (*OrmWrapper[T]) SetDb

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

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

func (*OrmWrapper[T]) SetTable

func (o *OrmWrapper[T]) SetTable(alias string, childTable ...*gorm.DB) *OrmWrapper[T]

SetTable 设置查询一个衍生表

func (*OrmWrapper[T]) ToFirstOrDefaultSql added in v0.0.8

func (o *OrmWrapper[T]) ToFirstOrDefaultSql() (string, error)

ToFirstOrDefaultSql 转换成 Sql

func (*OrmWrapper[T]) ToList

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

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

func (*OrmWrapper[T]) ToPagerList

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

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

func (*OrmWrapper[T]) ToSql

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

ToSql 转换成 Sql

func (*OrmWrapper[T]) Unscoped

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

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

func (*OrmWrapper[T]) Update

func (o *OrmWrapper[T]) Update(columnMap map[interface{}]interface{}) (int64, error)

Update 传入字典更新,必须添加查询条件

func (*OrmWrapper[T]) UpdateList

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

UpdateList 更新多个,主键作为条件;传了字段只更新传入字段,否则更新全部

func (*OrmWrapper[T]) UpdateOne

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

UpdateOne 更新单个,主键作为条件;传了字段只更新出入字段,否则更新全部

func (*OrmWrapper[T]) Where

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

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

func (*OrmWrapper[T]) WhereCondition

func (o *OrmWrapper[T]) WhereCondition(query OrmCondition) *OrmWrapper[T]

WhereCondition 通过条件查询

func (*OrmWrapper[T]) WhereConditionIf

func (o *OrmWrapper[T]) WhereConditionIf(do bool, query OrmCondition) *OrmWrapper[T]

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

func (*OrmWrapper[T]) WhereIf

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

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

func (*OrmWrapper[T]) WhereOriginal added in v0.0.8

func (o *OrmWrapper[T]) WhereOriginal(query interface{}, args ...interface{}) *OrmWrapper[T]

WhereOriginal gorm 原生查询

func (*OrmWrapper[T]) WhereOriginalIf added in v0.0.8

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

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

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 TableInfo

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

TableInfo 表信息

type TableSchema

type TableSchema struct {
	Name              string //表名
	DeletedColumnName string //软删除字段名
	DeleteCondition   string //软删除 片段sql:IS NULL,=0
	PrimaryKeyName    string //主键名称
}

func GetTableSchema

func GetTableSchema(table schema.Tabler) *TableSchema

GetTableSchema 获取表元数据

Directories

Path Synopsis
Package ref 反射扩展
Package ref 反射扩展

Jump to

Keyboard shortcuts

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