gou

package
v0.10.3 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2023 License: Apache-2.0 Imports: 18 Imported by: 2

Documentation

Index

Constants

View Source
const Star = -1

Star 数组索引 *

Variables

View Source
var OPs = map[string]func(cond Condition) error{
	"=":     condShouldHaveValue,
	">":     condShouldHaveValue,
	">=":    condShouldHaveValue,
	"<":     condShouldHaveValue,
	"<=":    condShouldHaveValue,
	"<>":    condShouldHaveValue,
	"like":  condShouldHaveValue,
	"match": condShouldHaveValue,
	"in":    condShouldHaveValue,
	"is":    condShouldHaveNull,
}

OPs 可用的操作符

View Source
var RegAlias = regexp.MustCompile("[ ]+[Aa][Ss][ ]+")

RegAlias 别名正则表达式

View Source
var RegArrayIndex = regexp.MustCompile("\\[([0-9\\*]+)\\]")

RegArrayIndex 字段表达式数组下标

View Source
var RegCommaSpaces = regexp.MustCompile("[ ]*,[ ]*")

RegCommaSpaces 连续空格 + 逗号+连续空格 " , " | "," | ", " | " ,"

View Source
var RegField = regexp.MustCompile("^[A-Za-z0-9_\u4e00-\u9fa5]+$")

RegField 字段表达式字段为数据表字段

View Source
var RegFieldFun = regexp.MustCompile("^\\:([A-Za-z0-9_]+)\\((.*)\\)$")

RegFieldFun 字段表达式为函数

View Source
var RegFieldIsArray = regexp.MustCompile("^([A-Za-z0-9_\u4e00-\u9fa5]+)([@\\[]{1})")

RegFieldIsArray 字段表达式字段是否为数组

View Source
var RegFieldIsArrayObject = regexp.MustCompile("\\.[A-Za-z0-9_\u4e00-\u9fa5]+")

RegFieldIsArrayObject 字段表达式字段是否为数组

View Source
var RegFieldTable = regexp.MustCompile("^[$]*([A-Za-z0-9_\u4e00-\u9fa5]+)\\.")

RegFieldTable 字段表达式字段中的指定的数据表

View Source
var RegFieldType = regexp.MustCompile("\\([ ]*([a-zA-Z0-9, ]+)[ ]*\\)[ ]*$")

RegFieldType 字段表达式的类型声明

View Source
var RegIsNumber = regexp.MustCompile("^[0-9]{1}[\\.]*[0-9]*$")

RegIsNumber 字段表达式字段是否为数字

View Source
var RegSelectSTMT = regexp.MustCompile("^select(.+)from")

RegSelectSTMT SQL语句中的 Select 语句

View Source
var RegSpaces = regexp.MustCompile("[ ]+")

RegSpaces 连续空格

View Source
var RegTable = regexp.MustCompile("^[A-Za-z0-9_\u4e00-\u9fa5]+$")

RegTable Table 正则表达式

Functions

This section is empty.

Types

type Any

type Any = interface{}

Any interface{} 别名

type Condition

type Condition struct {
	Field           *Expression `json:"field"`             // 查询字段
	Value           interface{} `json:"value,omitempty"`   // 匹配数值
	ValueExpression *Expression `json:"-"`                 // 数值表达式
	OP              string      `json:"op"`                // 匹配关系运算符
	OR              bool        `json:"or,omitempty"`      // true 查询条件逻辑关系为 or, 默认为 false 查询条件逻辑关系为 and
	Query           *QueryDSL   `json:"query,omitempty"`   // 子查询, 如设定 query 则忽略 value 数值。
	Comment         string      `json:"comment,omitempty"` // 查询条件注释
}

Condition 查询条件

func ConditionOf

func ConditionOf(input map[string]interface{}) Condition

ConditionOf 从 map[string]interface{}

func (Condition) MarshalJSON

func (cond Condition) MarshalJSON() ([]byte, error)

MarshalJSON for json marshalJSON

func (*Condition) SetQuery

func (cond *Condition) SetQuery(v interface{})

SetQuery 设定子查询

func (*Condition) SetValue

func (cond *Condition) SetValue(v interface{})

SetValue 设定数值

func (Condition) ToMap

func (cond Condition) ToMap() map[string]interface{}

ToMap Condition 转换为 map[string]interface{}

func (*Condition) UnmarshalJSON

func (cond *Condition) UnmarshalJSON(data []byte) error

UnmarshalJSON for json marshalJSON

func (Condition) Validate

func (cond Condition) Validate() []error

Validate 校验数据

type Expression

type Expression struct {
	Origin        string       // 原始数据
	Table         string       // 数据表名称
	Field         string       // 字段名称
	Value         interface{}  // 常量数值
	FunName       string       // 函数名称
	FunArgs       []Expression // 函数参数表
	Alias         string       // 字段别名
	Type          *FieldType   // 字段类型(用于自动转换和JSON Table)
	Index         int          // 数组字段索引 const Star -1 全部, 0 ~ n 数组
	Key           string       // 对象字段键名
	IsModel       bool         // 数据表是否为模型 $model.name
	IsFun         bool         // 是否为函数  :max(name)
	IsConst       bool         // 是否为常量  1,0.618, 'foo'
	IsNumber      bool         // 是否为数字常量
	IsString      bool         // 是否为字符串常量
	IsArray       bool         // 是否为数组  array@, array[0], array[*]
	IsObject      bool         // 是否为对象  object$.foo
	IsAES         bool         // 是否为加密字段  name*
	IsArrayObject bool         // 是否为对象数组  array@.foo.bar
	IsBinding     bool         // 是否为绑定参数  ?:name
}

Expression 字段表达式

func MakeExpression

func MakeExpression(s string) (Expression, error)

MakeExpression 解析表达式

func NewExpression

func NewExpression(s string) *Expression

NewExpression 创建一个表达式

func (Expression) FullPath

func (exp Expression) FullPath() string

FullPath JSON 完整字段路径带 $

func (*Expression) MarshalJSON

func (exp *Expression) MarshalJSON() ([]byte, error)

MarshalJSON for json marshalJSON

func (Expression) Path

func (exp Expression) Path() string

Path JSON 字段路径

func (Expression) ToString

func (exp Expression) ToString() string

ToString 还原为字符串

func (*Expression) UnmarshalJSON

func (exp *Expression) UnmarshalJSON(data []byte) error

UnmarshalJSON for json marshalJSON

func (Expression) Validate

func (exp Expression) Validate() error

Validate 校验表达式格式

type F

type F = float64

F float64 别名

type FieldNode

type FieldNode struct {
	Index int
	Field *Expression
}

FieldNode 字段表达式节点

type FieldType

type FieldType struct {
	Name      string `json:"name,omitempty"`      // JSON数组字段类型(用于生成 JSON Table)
	Length    int    `json:"length,omitempty"`    // 字段长度,对 string 等类型字段有效
	Precision int    `json:"precision,omitempty"` // 字段位数(含小数位),对 float、decimal 等类型字段有效
	Scale     int    `json:"scale,omitempty"`     // 字段小数位位数,对 float、decimal 等类型字段有效
}

FieldType 字段类型(用于自动转换和JSON Table)

type GetTableName

type GetTableName = func(string) string

GetTableName 读取表格名称

type Group

type Group struct {
	Field   *Expression `json:"field"`             // 排序字段
	Rollup  string      `json:"rollup,omitempty"`  // 同时返回多层级统计结果,对应聚合字段数值的名称。
	Comment string      `json:"comment,omitempty"` // 查询条件注释
}

Group 聚合条件

func (Group) MarshalJSON

func (group Group) MarshalJSON() ([]byte, error)

MarshalJSON for json marshalJSON

func (Group) ToMap

func (group Group) ToMap() map[string]interface{}

ToMap Order 转换为 map[string]interface{}

type Groups

type Groups []Group

Groups 聚合条件集合

func (*Groups) Push

func (groups *Groups) Push(group Group)

Push 添加一个排序条件 Group

func (*Groups) PushMap

func (groups *Groups) PushMap(group map[string]interface{}) error

PushMap 添加一个排序条件 map[string]inteface{}

func (*Groups) PushString

func (groups *Groups) PushString(group string) error

PushString 添加一个排序条件 string

func (*Groups) UnmarshalJSON

func (groups *Groups) UnmarshalJSON(data []byte) error

UnmarshalJSON for json marshalJSON

func (Groups) Validate

func (groups Groups) Validate() []error

Validate 校验数据

type Having

type Having struct {
	Condition
	Havings []Having `json:"havings,omitempty"` // 分组查询。用于 condition 1 and ( condition 2 OR condition 3) 的场景
}

Having 聚合结果筛选条件

func HavingOf

func HavingOf(input map[string]interface{}) Having

HavingOf 从 maps 载入 having

func (Having) MarshalJSON

func (having Having) MarshalJSON() ([]byte, error)

MarshalJSON for json marshalJSON

func (Having) ToMap

func (having Having) ToMap() map[string]interface{}

ToMap 转换为 map[string]interface{}

func (*Having) UnmarshalJSON

func (having *Having) UnmarshalJSON(data []byte) error

UnmarshalJSON for json marshalJSON

type Join

type Join struct {
	From    *Table      `json:"from"`              // 查询数据表名称或数据模型
	Key     *Expression `json:"key"`               // 关联连接表字段名称
	Foreign *Expression `json:"foreign"`           // 关联目标表字段名称(需指定表名或别名)
	Left    bool        `json:"left,omitempty"`    // true 连接方式为 LEFT JOIN, 默认为 false 连接方式为 JOIN
	Right   bool        `json:"right,omitempty"`   // true 连接方式为 RIGHT JOIN, 默认为 false 连接方式为 JOIN
	Comment string      `json:"comment,omitempty"` // 关联条件注释
}

Join 数据表连接

type M

type M = map[string]interface{}

M map[string]interface{} 别名

type Order

type Order struct {
	Field   *Expression `json:"field"`             // 排序字段
	Sort    string      `json:"sort,omitempty"`    // 排序方式
	Comment string      `json:"comment,omitempty"` // 查询条件注释
}

Order 排序条件

func (Order) MarshalJSON

func (order Order) MarshalJSON() ([]byte, error)

MarshalJSON for json marshalJSON

func (Order) ToMap

func (order Order) ToMap() map[string]interface{}

ToMap Order 转换为 map[string]interface{}

func (Order) Validate

func (order Order) Validate() error

Validate 校验 order

type Orders

type Orders []Order

Orders 排序条件集合

func (*Orders) Push

func (orders *Orders) Push(order Order)

Push 添加一个排序条件 Order

func (*Orders) PushMap

func (orders *Orders) PushMap(order map[string]interface{}) error

PushMap 添加一个排序条件 map[string]inteface{}

func (*Orders) PushString

func (orders *Orders) PushString(order string) error

PushString 添加一个排序条件 string

func (*Orders) UnmarshalJSON

func (orders *Orders) UnmarshalJSON(data []byte) error

UnmarshalJSON for json marshalJSON

func (Orders) Validate

func (orders Orders) Validate() []error

Validate 校验数据

type Query

type Query struct {
	QueryDSL
	Query        query.Query
	GetTableName GetTableName
	Bindings     []interface{}
	Selects      map[string]FieldNode
	AESKey       string
	STMT         string
}

Query Query share.DSL

func Make

func Make(input []byte) *Query

Make 创建 Gou Query share.DSL

func New

func New() *Query

New 克隆对象

func Open

func Open(filename string) *Query

Open 创建 Gou Query share.DSL (文件)

func Read

func Read(reader io.Reader) *Query

Read 创建 Gou Query share.DSL (输入接口)

func (*Query) Build

func (gou *Query) Build()

Build 设定查询条件

func (*Query) Clone

func (gou *Query) Clone() *Query

Clone 克隆对象

func (Query) First

func (gou Query) First(data maps.Map) share.Record

First 执行查询并返回一条数据记录

func (Query) Get

func (gou Query) Get(data maps.Map) []share.Record

Get 执行查询并返回数据记录集合

func (Query) GetBindings

func (gou Query) GetBindings() []interface{}

GetBindings 返回SQL绑定数据

func (Query) GetPage

func (gou Query) GetPage(data maps.Map) int

GetPage get Page

func (Query) GetPageSize

func (gou Query) GetPageSize(data maps.Map) int

GetPageSize get Page Size

func (*Query) ID

func (gou *Query) ID(exp Expression) string

ID 字段唯一标识

func (*Query) Load

func (gou *Query) Load(data interface{}) (share.DSL, error)

Load 加载查询条件

func (*Query) NameOf

func (gou *Query) NameOf(exp Expression) string

NameOf 字段名称

func (Query) Paginate

func (gou Query) Paginate(data maps.Map) share.Paginate

Paginate 执行查询并返回带分页信息的数据记录数组

func (Query) Run

func (gou Query) Run(data maps.Map) interface{}

Run 执行查询根据查询条件返回结果

func (*Query) SetAESKey

func (gou *Query) SetAESKey(key string) *Query

SetAESKey 设定 AES KEY

func (Query) SetLimit

func (gou Query) SetLimit(qb query.Query, data maps.Map)

SetLimit set Limit

func (Query) SetOffset

func (gou Query) SetOffset(qb query.Query, data maps.Map)

SetOffset set Offset

func (*Query) TableName

func (gou *Query) TableName(getTableName GetTableName) *Query

TableName 绑定数据模型数据表读取方式

func (Query) ToSQL

func (gou Query) ToSQL() string

ToSQL 返回查询语句

func (*Query) With

func (gou *Query) With(qb query.Query, getTableName ...GetTableName) *Query

With 关联查询器

func (*Query) WrapNameOf

func (gou *Query) WrapNameOf(exp Expression) string

WrapNameOf 字段名称

type QueryDSL

type QueryDSL struct {
	Select   []Expression `json:"select"`              // 查询字段列表
	From     *Table       `json:"from,omitempty"`      // 查询数据表名称或数据模型
	Wheres   []Where      `json:"wheres,omitempty"`    // 数据查询条件
	Orders   Orders       `json:"orders,omitempty"`    // 排序条件
	Groups   *Groups      `json:"groups,omitempty"`    // 聚合条件
	Havings  []Having     `json:"havings,omitempty"`   // 聚合查询结果筛选条件
	First    interface{}  `json:"first,omitempty"`     // 限定读取单条数据
	Limit    interface{}  `json:"limit,omitempty"`     // 限定读取记录的数量
	Offset   interface{}  `json:"offset,omitempty"`    // 记录开始位置
	Page     interface{}  `json:"page,omitempty"`      // 分页查询当前页面页码
	PageSize interface{}  `json:"pagesize,omitempty"`  // 每页读取记录的数量
	DataOnly interface{}  `json:"data-only,omitempty"` // 设定为 true, 查询结果为 []Record; 设定为 false, 查询结果为 Paginate
	Unions   []QueryDSL   `json:"unions,omitempty"`    // 联合查询
	SubQuery *QueryDSL    `json:"query,omitempty"`     // 子查询
	Alias    string       `json:"name,omitempty"`      // 子查询别名
	Joins    []Join       `json:"joins,omitempty"`     // 表连接
	SQL      *SQL         `json:"sql,omitempty"`       // SQL语句
	Comment  string       `json:"comment,omitempty"`   // 查询条件注释
	Debug    bool         `json:"debug,omitempty"`     // 是否开启调试(开启后计入查询日志)
}

QueryDSL Gou Query Domain Specific Language

func (QueryDSL) MarshalJSON

func (gou QueryDSL) MarshalJSON() ([]byte, error)

MarshalJSON for json marshalJSON

func (QueryDSL) ToMap

func (gou QueryDSL) ToMap() map[string]interface{}

ToMap QueryDSL 转换为 map[string]interface{}

func (QueryDSL) Validate

func (gou QueryDSL) Validate() []error

Validate 校验DSL格式

func (QueryDSL) ValidateFrom

func (gou QueryDSL) ValidateFrom() []error

ValidateFrom 校验 from

func (QueryDSL) ValidateGroups

func (gou QueryDSL) ValidateGroups() []error

ValidateGroups 校验 groups

func (QueryDSL) ValidateHavings

func (gou QueryDSL) ValidateHavings() []error

ValidateHavings 校验 havings

func (QueryDSL) ValidateJoins

func (gou QueryDSL) ValidateJoins() []error

ValidateJoins 校验 joins

func (QueryDSL) ValidateOrders

func (gou QueryDSL) ValidateOrders() []error

ValidateOrders 校验 orders

func (QueryDSL) ValidateQuery

func (gou QueryDSL) ValidateQuery() []error

ValidateQuery 校验 query

func (QueryDSL) ValidateSQL

func (gou QueryDSL) ValidateSQL() []error

ValidateSQL 校验 sql

func (QueryDSL) ValidateSelect

func (gou QueryDSL) ValidateSelect() []error

ValidateSelect 校验 select

func (QueryDSL) ValidateUnions

func (gou QueryDSL) ValidateUnions() []error

ValidateUnions 校验 unions

func (QueryDSL) ValidateWheres

func (gou QueryDSL) ValidateWheres() []error

ValidateWheres 校验 wheres

type SQL

type SQL struct {
	STMT    string        `json:"stmt,omitempty"`    // SQL 语句
	Args    []interface{} `json:"args,omitempty"`    // 绑定参数表
	Comment string        `json:"comment,omitempty"` // SQL语句注释
}

SQL 语句

type Table

type Table struct {
	Alias   string // 别名
	Name    string // 名称
	IsModel bool   // 是否为数据模型
}

Table 数据表名称或数据模型

func (Table) MarshalJSON

func (tab Table) MarshalJSON() ([]byte, error)

MarshalJSON for json marshalJSON

func (Table) ToString

func (tab Table) ToString() string

ToString for json marshalJSON

func (*Table) UnmarshalJSON

func (tab *Table) UnmarshalJSON(data []byte) error

UnmarshalJSON for json marshalJSON

func (Table) Validate

func (tab Table) Validate() error

Validate 校验表达式格式

type Where

type Where struct {
	Condition
	Wheres []Where `json:"wheres,omitempty"` // 分组查询。用于 condition 1 and ( condition 2 OR condition 3) 的场景
}

Where 查询条件

func WhereOf

func WhereOf(input map[string]interface{}) Where

WhereOf 从 maps 载入 where

func (Where) MarshalJSON

func (where Where) MarshalJSON() ([]byte, error)

MarshalJSON for json marshalJSON

func (Where) ToMap

func (where Where) ToMap() map[string]interface{}

ToMap 转换为 map[string]interface{}

func (*Where) UnmarshalJSON

func (where *Where) UnmarshalJSON(data []byte) error

UnmarshalJSON for json marshalJSON

Jump to

Keyboard shortcuts

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