orm

package module
v1.8.15 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2023 License: Apache-2.0 Imports: 14 Imported by: 7

README

orm

mysql mariadb sqlserver postgres opengauss sqllite oracle orm工具,提供丰富的CURD的功能,将sql的研发编写转为orm语法结构的编写,大大提高研发效率,降低业务复杂度

算子语法

where、order、group、having等语法
每种算子均支持 强制忽略大小写查询和强制区分大小写查询,用法为:key__ignore_eq,key__bin_eq 或简写 key__i_eq,key__b_eq
where 条件 支持子查询
= : "key": "val" or "key__eq": "val"
< : "key__lt": 1
<= : "key__lte": 1
> : "key__gt": 1
>= : "key__gte": 1
!= : "key__ne": 1
in : "key__in": [1]
not in : "key__nin": [1]
date : "key__date": "2022-01-01" or time.Time(oracle必须)
between : "key__between": [1, 2]

以下不支持子查询
is null : "key__null": true
is not null : "key__null": false
$or : map[string]interface{} or []map[string]interface{}
$and : map[string]interface{} or []map[string]interface{}
and_like : 参数为数组,针对数组每个元素分别取 like,之后条件之间取 and
        "key__startswith": "123"
        "key__endswith": "123"
        "key__contains": "123" or ["123", "123"]
        "key__customlike": "__st" or ["%test", "%test%", "test%"] // 自定义查询语句
or_like : 参数为数组,针对数组每个元素分别取 like,之后条件之间取 or
        "key__orstartswith": "123" or ["123", "123"]
        "key__orendswith": "123" or ["123", "123"]
        "key__orcontains": "123" or ["123", "123"]
        "key__orcustomlike": "__st" or ["%test", "%test%", "test%"] // 自定义查询语句

原始数据,#修饰的字段为原始字段,不做处理,其他的字段会根据tag计算
~ 为条件取反,必须在最前面,可用在所有算子前面,如果与#连用,#应在~后面,如:~#test
select语法
Select 参数:* 主表所有字段;tag.* tag对应表所有字段;tag1.tag2.* tag1表的tag2的所有字段;以此类推
*0 等价 * 只考虑主表,不展开子表
*1 对应层级的表展开一层(主表+一级关联表,二级以下联表不算)
*2 对应层级的表展开二层(主表+一级关联表+二级关联表,三级以下联表不算)
*n 对应层级的表展开n层(主表+1级关联表+2级关联表+...+n级关联表,n+1级以下联表不算)
上述法则同样适用于tag.*1,此时的主表为tag,以此类推 tag1.tag2.*1,主表为tag1.tag2
字段排除:优先级最高
-name:删除name字段;-* 移除主表字段; -tag.name:删除tag表对应的name字段;-tag.*:删除整个tag表所有字段
排除字段也支持 *n 语法,-*n
*n与- 均不可与 # 混用

Documentation

Overview

Package orm

Package orm

Package orm

Package orm

Package orm

Package orm

Package orm

Package orm

Package orm

Index

Constants

This section is empty.

Variables

View Source
var ErrBetweenValueMatch = errors.New("[between]: the parameter array length is required to be 2")
View Source
var ErrClient = fmt.Errorf("db and tx are all nil")
View Source
var ErrCustomSQL = errors.New("custom sql does not allow this operation")
View Source
var ErrDBFunc = errors.New("this method is not currently supported in the current database")
View Source
var ErrDBType = errors.New("the current database type is not currently supported")
View Source
var ErrTargetNotSettable = errors.New("[scanner]: target is not settable! a pointer is required")
View Source
var ErrTooFewColumn = errors.New("too few columns")
View Source
var ErrTooManyColumn = errors.New("too many columns")

Functions

func InitNotReadyLastInsertID added in v1.5.0

func InitNotReadyLastInsertID(db ...int)

InitNotReadyLastInsertID 初始化方法 LastInsertID 没有实现的数据库类型,会覆盖默认设置

func SetGlobalVerify

func SetGlobalVerify(v Verify)

func Struct2Map

func Struct2Map(raw interface{}, excludeKey ...string) map[string]interface{}

func TransSession

func TransSession(ctx context.Context, db *DB, f func(ctx context.Context, tx *Tx) error) (err error)

Types

type BaseQuery

type BaseQuery struct {
	// 用户自定义sql
	CustomSQL  string
	PrivateKey string
	RefConf    *Reference

	// 字段别名链接字符串,SelectRaw 为false有效
	SelectColLinkStr string
	// true:使用原始字段名;false:使用别名
	SelectRaw bool

	TableName       string
	Distinct        bool
	SelectForUpdate bool
	Select          Select
	Order           Order
	Limit           Limit
	Where           Where
	GroupBy         GroupBy
	Having          Having
	// contains filtered or unexported fields
}

BaseQuery # 为内置符号,标志为原始字段,不进行任何处理,仅在以下数据有效: Select Order GroupBy Where Having

func (*BaseQuery) Count

func (q *BaseQuery) Count() string

func (*BaseQuery) GetWhere

func (q *BaseQuery) GetWhere() string

func (*BaseQuery) SQL

func (q *BaseQuery) SQL() string

type DB

type DB = sql.DB

type GroupBy

type GroupBy = []string

type Having

type Having = map[string]interface{}

type Limit

type Limit = []uint

type ORM

type ORM struct {

	// 查询配置数据
	Q *databaseQuery
	// contains filtered or unexported fields
}

ORM # 为内置符号,标志为原始字段,不进行任何处理,仅在以下数据有效: Select Order GroupBy Where Having

func NewORM

func NewORM(ctx context.Context, tableName string, db *DB, ref *Reference) *ORM

func NewORMWithTx

func NewORMWithTx(ctx context.Context, tableName string, tx *Tx, ref *Reference) *ORM

func (*ORM) ClearCache

func (orm *ORM) ClearCache() *ORM

func (*ORM) Clone

func (orm *ORM) Clone(ctx context.Context) *ORM

func (*ORM) Count

func (orm *ORM) Count(clearCache bool) (c int64, err error)

func (*ORM) CustomSQL added in v1.5.0

func (orm *ORM) CustomSQL(sql string) *ORM

CustomSQL 设置自定义SQL,可以为空,为空表示移除自定义sql

func (*ORM) DeleteByWhere

func (orm *ORM) DeleteByWhere(where map[string]interface{}) (affected int64, err error)

func (*ORM) Distinct

func (orm *ORM) Distinct(b bool) *ORM

func (*ORM) ExecuteSQL added in v1.5.0

func (orm *ORM) ExecuteSQL(customSQL string) (affectedRow int64, err error)

func (*ORM) Exist added in v1.1.0

func (orm *ORM) Exist() (b bool, err error)

Exist 检查数据是否存在

func (*ORM) GroupBy

func (orm *ORM) GroupBy(cols ...string) *ORM

func (*ORM) Having

func (orm *ORM) Having(col string, value interface{}) *ORM

func (*ORM) HavingSome

func (orm *ORM) HavingSome(where Having) *ORM

func (*ORM) InsertMany

func (orm *ORM) InsertMany(data []interface{}, trans bool) (affected int64, insertIDs []int64, err error)

InsertMany 每条数据字段不一致或者想获取每条数据的主键,使用此方法

func (*ORM) InsertManySameClos

func (orm *ORM) InsertManySameClos(data []interface{}, cols []string, batchSize int, trans bool) (affected int64, err error)

InsertManySameClos 字段一致使用此方法 data 需要处理的数据集合,数据格式为:map、*struct、struct,字段不在 cols 的被赋值null cols 需要处理的字段集合 batchSize 单次并发数量 trans 是否使用事务

func (*ORM) InsertOne

func (orm *ORM) InsertOne(data interface{}) (insertID int64, err error)

func (*ORM) KeepQuery

func (orm *ORM) KeepQuery(b bool) *ORM

func (*ORM) Limit

func (orm *ORM) Limit(size uint) *ORM

func (*ORM) OracleMergeUnionAll added in v1.5.0

func (orm *ORM) OracleMergeUnionAll(all bool) *ORM

OracleMergeUnionAll oracle merge union 类型,默认是 true

func (*ORM) Order

func (orm *ORM) Order(cols ...string) *ORM

func (*ORM) OverLimit

func (orm *ORM) OverLimit(over, size uint) *ORM

func (*ORM) Page

func (orm *ORM) Page(pageNo, pageSize uint) *ORM

func (*ORM) PageData

func (orm *ORM) PageData(result interface{}, flat bool, pageNo, pageSize uint) (pg *Paging, err error)

func (*ORM) PrimaryKey

func (orm *ORM) PrimaryKey(k string) *ORM

func (*ORM) Query

func (orm *ORM) Query(pair ...interface{}) *ORM

Query 条件对 "id__gt", 1, "name": "test"

func (*ORM) ReplaceMany

func (orm *ORM) ReplaceMany(data []interface{}, trans bool) (affected int64, insertIds []int64, err error)

ReplaceMany 每条数据字段不一致或者想获取每条数据的主键,使用此方法

func (*ORM) ReplaceManySameClos

func (orm *ORM) ReplaceManySameClos(data []interface{}, cols []string, batchSize int, trans bool) (affected int64, err error)

ReplaceManySameClos data 需要处理的数据集合,数据格式为:map、*struct、struct,字段不在 cols 的被赋值null cols 需要处理的字段集合 batchSize 单次并发数量 trans 是否使用事务

func (*ORM) ReplaceOne

func (orm *ORM) ReplaceOne(data interface{}) (affected int64, err error)

func (*ORM) SQLServerExcludePK added in v1.5.0

func (orm *ORM) SQLServerExcludePK(exclude bool) *ORM

SQLServerExcludePK sql server 排除主键

func (*ORM) SaveMany

func (orm *ORM) SaveMany(data []interface{}, trans bool) (affected int64, err error)

SaveMany data 需要处理的数据集合,数据格式为:map、*struct、struct,字段不在 cols 的被赋值null cols 需要处理的字段集合 batchSize 单次并发数量 trans 是否使用事务

func (*ORM) Select

func (orm *ORM) Select(cols ...string) *ORM

Select 参数:* 主表所有字段;tag.* tag对应表所有字段;tag1.tag2.* tag1表的tag2的所有字段;以此类推 *0 等价 * 只考虑主表,不展开子表 *1 对应层级的表展开一层(主表+一级关联表,二级以下联表不算) *2 对应层级的表展开二层(主表+一级关联表+二级关联表,三级以下联表不算) *n 对应层级的表展开n层(主表+1级关联表+2级关联表+...+n级关联表,n+1级以下联表不算) 上述法则同样适用于tag.*1,此时的主表为tag,以此类推 tag1.tag2.*1,主表为tag1.tag2 字段排除:优先级最高 -name:删除name字段;-* 移除主表字段; -tag.name:删除tag表对应的name字段;-tag.*:删除整个tag表所有字段 排除字段也支持 *n 语法,-*n *n与- 均不可与 # 混用

func (*ORM) SelectColLinkStr

func (orm *ORM) SelectColLinkStr(s string) *ORM

func (*ORM) SelectForUpdate

func (orm *ORM) SelectForUpdate(b bool) *ORM

func (*ORM) SetDefLimit added in v1.8.10

func (orm *ORM) SetDefLimit(n uint) *ORM

SetDefLimit 修改默认limit

func (*ORM) ToData

func (orm *ORM) ToData(result interface{}, flat bool) (err error)

func (*ORM) ToSQL

func (orm *ORM) ToSQL(flat bool) string

func (*ORM) UniqueKeys added in v1.5.0

func (orm *ORM) UniqueKeys(uniqueKey ...string) *ORM

UniqueKeys 设置新的唯一键,用于 upsert

func (*ORM) UpdateByWhere

func (orm *ORM) UpdateByWhere(update map[string]interface{}, where Where) (affected int64, err error)

func (*ORM) UpdateMany

func (orm *ORM) UpdateMany(data []interface{}, trans bool) (affected int64, err error)

UpdateMany 主键,id 不能为空,为空将更新失败

func (*ORM) UpdateOne

func (orm *ORM) UpdateOne(data interface{}) (affected int64, err error)

UpdateOne 主键,id 不能为空,为空将更新失败

func (*ORM) UpsertMany

func (orm *ORM) UpsertMany(data []interface{}, trans bool) (affected int64, insertIDs []int64, err error)

UpsertMany 每条数据字段不一致或者想获取每条数据的主键,使用此方法

func (*ORM) UpsertManySameClos

func (orm *ORM) UpsertManySameClos(data []interface{}, cols []string, batchSize int, trans bool) (affected int64, err error)

UpsertManySameClos data 需要处理的数据集合,数据格式为:map、*struct、struct,字段不在 cols 的被赋值null cols 需要处理的字段集合 batchSize 单次并发数量 trans 是否使用事务

func (*ORM) UpsertOne

func (orm *ORM) UpsertOne(data interface{}) (insertID int64, err error)

func (*ORM) Where

func (orm *ORM) Where(col string, value interface{}) *ORM

func (*ORM) Wheres

func (orm *ORM) Wheres(where Where) *ORM

type Order

type Order = []string

type Paging

type Paging struct {
	PageNo    int `json:"page_no"`    //当前页
	PageSize  int `json:"page_size"`  //每页条数
	Total     int `json:"total"`      //总条数
	PageTotal int `json:"page_total"` //总页数
}

type Reference

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

func NewReference

func NewReference(dbType int) *Reference

func (*Reference) AddTableDef

func (c *Reference) AddTableDef(table string, def interface{})

AddTableDef 添加表定义

func (*Reference) BuildRefs

func (c *Reference) BuildRefs()

BuildRefs 构建数据表关系,此操作必须在表定义完成时调用

func (*Reference) GetDBType added in v1.5.0

func (c *Reference) GetDBType() int

func (*Reference) GetTableDef

func (c *Reference) GetTableDef(table string) []string

type Select

type Select = []string

type Tx

type Tx = sql.Tx

type Verify

type Verify interface {
	VerifyTableName(name string) error
	VerifyFieldName(name string) error
	VerifyTagName(name string) error
}

type Where

type Where = map[string]interface{}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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