query

package
v1.7.0 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package query is a library for mysql query, support for complex conditional paging queries.

Index

Constants

View Source
const (
	// Eq equal
	Eq = "eq"
	// Neq not equal
	Neq = "neq"
	// Gt greater than
	Gt = "gt"
	// Gte greater than or equal
	Gte = "gte"
	// Lt less than
	Lt = "lt"
	// Lte less than or equal
	Lte = "lte"
	// Like fuzzy lookup
	Like = "like"
	// In include
	In = "in"

	// AND logic and
	AND string = "and"
	// OR logic or
	OR string = "or"
)

Variables

View Source
var (
	// ErrNotFound record
	ErrNotFound = gorm.ErrRecordNotFound
)

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"`   // column name
	Exp   string      `json:"exp" form:"exp"`     // expressions, which default to = when the value is null, have =, !=, >, >=, <, <=, like, in
	Value interface{} `json:"value" form:"value"` // column value
	Logic string      `json:"logic" form:"logic"` // logical type, defaults to and when the value is null, with &(and), ||(or)
}

Column query info

type Conditions added in v1.5.3

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

Conditions query conditions

func (*Conditions) CheckValid added in v1.5.3

func (c *Conditions) CheckValid() error

CheckValid check valid

func (*Conditions) ConvertToGorm added in v1.5.3

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

ConvertToGorm conversion to gorm-compliant parameters based on the Columns parameter ignore the logical type of the last column, whether it is a one-column or multi-column query Deprecated: will be moved to package pkg/gorm/query ConvertToGorm

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, size 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) 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

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"`
	Size int    `json:"size" form:"size" binding:"gt=0"`
	Sort string `json:"sort,omitempty" form:"sort" binding:""`

	Columns []Column `json:"columns,omitempty" form:"columns"` // not required
}

Params query parameters

func (*Params) ConvertToGormConditions

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

ConvertToGormConditions conversion to gorm-compliant parameters based on the Columns parameter ignore the logical type of the last column, whether it is a one-column or multi-column query Deprecated: will be moved to package pkg/gorm/query ConvertToGormConditions

func (*Params) ConvertToPage

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

ConvertToPage converted to conform to gorm rules based on the page size sort parameter Deprecated: will be moved to package pkg/gorm/query ConvertToPage

Jump to

Keyboard shortcuts

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