orm

package module
v1.1.0 Latest Latest
Warning

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

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

README

orm

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

算子语法

mysql
where、order、group、having等语法
where 条件 支持子查询 MySqlQuery or *MySqlQuery
= : "key": "val" or "key__eq": "val" or "key__bin_eq": "val"
< : "key__lt": 1 or "key__bin_lt": 1
<= : "key__lte": 1 or "key__bin_lte": 1
> : "key__gt": 1 or "key__bin_gt": 1
>= : "key__gte": 1 or "key__bin_gte": 1
!= : "key__ne": 1 or "key__bin_ne": 1
in : "key__in": [1] or "key__bin_in": [1]
not in : "key__nin": [1] or "key__bin_nin": [1]
date : "key__date": "2022-01-01"
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 :
		"key__istartswith": "123"
		"key__startswith": "123"
		"key__iendswith": "123"
		"key__endswith": "123"
		"key__icontains": "123" or ["123", "123"]
		"key__contains": "123" or ["123", "123"]
or_like :
		"key__or_istartswith": "123" or ["123", "123"]
		"key__or_startswith": "123" or ["123", "123"]
		"key__or_iendswith": "123" or ["123", "123"]
		"key__or_endswith": "123" or ["123", "123"]
		"key__or_icontains": "123" or ["123", "123"]
		"key__or_contains": "123" or ["123", "123"]

原始数据,#修饰的字段为原始字段,不做处理,其他的字段会根据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 ErrClient = fmt.Errorf("db and tx are all nil")
View Source
var ErrTargetNotSettable = errors.New("[scanner]: target is not settable! a pointer is required")

Functions

func SetGlobalVerify

func SetGlobalVerify(v Verify)

func SimpleCount

func SimpleCount()

func SimpleDeleteByWhere

func SimpleDeleteByWhere()

func SimpleInsertMany

func SimpleInsertMany()

func SimpleInsertManySameClos

func SimpleInsertManySameClos()

func SimpleInsertOne

func SimpleInsertOne()

func SimpleInsertOne2

func SimpleInsertOne2()

func SimpleReplaceMany

func SimpleReplaceMany()

func SimpleReplaceManySameClos

func SimpleReplaceManySameClos()

func SimpleReplaceOne

func SimpleReplaceOne()

func SimpleToData

func SimpleToData()

func SimpleToData2

func SimpleToData2()

func SimpleUpdateByWhere

func SimpleUpdateByWhere()

func SimpleUpdateMany

func SimpleUpdateMany()

func SimpleUpsertManySameClos

func SimpleUpsertManySameClos()

func SimpleUpsertOne

func SimpleUpsertOne()

func Struct2Map

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

func TestTransSession

func TestTransSession()

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 {
	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 Client

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

func NewClient

func NewClient(cfg *Config) *Client

func (*Client) Connect

func (c *Client) Connect() (*DB, error)

type Config

type Config struct {
	Host            string
	Port            int
	Username        string
	Password        string
	DBName          string
	DBDriver        string
	MaxOpenConn     int
	MaxIdleConn     int
	ConnMaxLifeTime int
	ConnMaxIdleTime int
	DSNParams       string
}

type DB

type DB = sql.DB

func GetMySQL

func GetMySQL() *DB

type GroupBy

type GroupBy = []string

type Having

type Having = map[string]interface{}

type Limit

type Limit = []uint

type ORM

type ORM struct {

	// 查询配置数据
	Q *mysqlQuery
	// 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() *ORM

func (*ORM) Count

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

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) Exist added in v1.1.0

func (orm *ORM) Exist() (bool, 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)

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) 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) (*Paging, 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)

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) 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) ToData

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

func (*ORM) ToSQL

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

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)

func (*ORM) UpsertMany

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

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() *Reference

func (*Reference) AddTableDef

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

AddTableDef 添加表定义

func (*Reference) BuildRefs

func (c *Reference) BuildRefs()

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

func (*Reference) GetTableDef

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

type Select

type Select = []string

type Tb1

type Tb1 struct {
	ID   int64  `json:"id"`
	Name string `json:"name"`
	Ref  int64  `json:"ref"`
	JSON []int  `json:"json" type:"json"`
	Tb   *Tb2   `json:"tb" ref:"left;ref=id"`
}

type Tb2

type Tb2 struct {
	ID   int64  `json:"id"`
	Name string `json:"name"`
	Tb   *Tb3   `json:"tb" ref:"left;tb=id"`
}

type Tb3

type Tb3 struct {
	ID   int64  `json:"id"`
	Name string `json:"name"`
	Tb   *Tb1   `json:"tb" ref:"left;tb=id"`
}

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
dao

Jump to

Keyboard shortcuts

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