query

package
v1.1.48 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package query 是一个自定义条件查询库,支持复杂的分页查询。

Index

Constants

View Source
const (
	// Eq 等于
	Eq = "eq"
	// Neq 不等于
	Neq = "neq"
	// Gt 大于
	Gt = "gt"
	// Gte 大于等于
	Gte = "gte"
	// Lt 小于
	Lt = "lt"
	// Lte 小于等于
	Lte = "lte"
	// Like 模糊查询
	Like = "like"
	// In 包含
	In = "in"
	// NotIN 不包含
	NotIN = "notin"
	// IsNull 是否为空
	IsNull = "isnull"
	// IsNotNull 是否不为空
	IsNotNull = "isnotnull"

	// AND 逻辑与
	AND string = "and"
	// OR 逻辑或
	OR string = "or"
)

Variables

This section is empty.

Functions

func SetMaxSize

func SetMaxSize(max int)

SetMaxSize change the default maximum number of pages per page

Types

type Column

type Column struct {
	Name  string      `json:"name" form:"name"`   // 列名
	Exp   string      `json:"exp" form:"exp"`     // 表达式,默认值为"=", 支持 =, !=, >, >=, <, <=, like, in, notin, isnull, isnotnull
	Value interface{} `json:"value" form:"value"` // 列值
	Logic string      `json:"logic" form:"logic"` // 逻辑运算符,默认为"and",支持 &(and), ||(or)
}

Column 查询列信息结构体

type Conditions

type Conditions struct {
	Columns []Column `json:"columns" form:"columns" binding:"min=1"` // 列信息
}

Conditions 查询条件结构体

func (*Conditions) CheckValid

func (c *Conditions) CheckValid() error

CheckValid 校验查询条件是否合法

func (*Conditions) ConvertToGorm

func (c *Conditions) ConvertToGorm() (string, []interface{}, error)

ConvertToGorm 将查询条件转换为 GORM 兼容的参数 忽略最后一列的逻辑类型,无论是单列还是多列查询

type Page

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

Page info

func DefaultPage

func DefaultPage(page int) *Page

DefaultPage default page, number 20 per page, sorted by id backwards

func NewPage

func NewPage(page int, limit int, columnNames string) *Page

NewPage custom page, starting from page 0. the parameter columnNames indicates a sort field, if empty means id descending, if there are multiple column names, separated by a comma, a '-' sign in front of each column name indicates descending order, otherwise ascending order.

func (*Page) Limit

func (p *Page) Limit() int

Limit number per page

func (*Page) Offset

func (p *Page) Offset() int

Offset get offset value

func (*Page) Page

func (p *Page) Page() int

Page get page value

func (*Page) Size

func (p *Page) Size() int

Size number per page Deprecated: use Limit instead

func (*Page) Sort

func (p *Page) Sort() string

Sort get sort field

type Params

type Params struct {
	Page  int    `json:"page" form:"page" binding:"gte=0"`      // 分页页码,默认从0开始
	Limit int    `json:"limit" form:"limit" binding:"gte=1"`    // 每页条数,最小值为1
	Sort  string `json:"sort,omitempty" form:"sort" binding:""` // 排序字段

	Columns []Column `json:"columns,omitempty" form:"columns"` // 查询列信息,非必填

	// Deprecated: 在sunshine版本v1.8.6中建议使用Limit代替Size,未来将移除
	Size int `json:"size" form:"size"`
}

Params 查询参数结构体

func (*Params) ConvertToGormConditions

func (p *Params) ConvertToGormConditions() (string, []interface{}, error)

ConvertToGormConditions 将查询条件转换为 GORM 兼容的参数 忽略最后一列的逻辑类型,无论是单列还是多列查询

func (*Params) ConvertToPage

func (p *Params) ConvertToPage() (order string, limit int, offset int)

ConvertToPage 转换为分页参数

Jump to

Keyboard shortcuts

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