query

package
v2.0.7 Latest Latest
Warning

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

Go to latest
Published: May 3, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Base

type Base interface {
	Sql() (string, []interface{})
}

Base sql expression

type Criteria

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

Criteria wrap a group of column, value and operator such as name = 20

Example
query := C().Col("name").Eq("wubin").Or(C().Col("school").Eq("havard")).And(C().Col("age").Eq(18))
fmt.Println(query.Sql())

query = C().Col("name").Eq("wubin").Or(C().Col("school").Eq("havard")).And(C().Col("delete_at").IsNotNull())
fmt.Println(query.Sql())

query = C().Col("name").Eq("wubin").Or(C().Col("school").In("havard")).And(C().Col("delete_at").IsNotNull())
fmt.Println(query.Sql())

query = C().Col("name").Eq("wubin").Or(C().Col("school").In([]string{"havard", "beijing unv"})).And(C().Col("delete_at").IsNotNull())
fmt.Println(query.Sql())

query = C().Col("name").Eq("wubin").Or(C().Col("age").In([]int{5, 10})).And(C().Col("delete_at").IsNotNull())
fmt.Println(query.Sql())

query = C().Col("name").Ne("wubin").Or(C().Col("create_at").Lt("now()"))
fmt.Println(query.Sql())

page := Page{
	Orders: []Order{
		{
			Col:  "create_at",
			Sort: sortenum.Desc,
		},
	},
	Offset: 20,
	Size:   10,
}
page = page.Order(Order{
	Col:  "score",
	Sort: sortenum.Asc,
})
page = page.Limit(30, 5)
fmt.Println(page.Sql())
pageRet := NewPageRet(page)
fmt.Println(pageRet.PageNo)

fmt.Println(P().Order(Order{
	Col:  "score",
	Sort: sortenum.Asc,
}).Limit(20, 10).Sql())
Output:

((`name` = ? or `school` = ?) and `age` = ?) [wubin havard 18]
((`name` = ? or `school` = ?) and `delete_at` is not null) [wubin havard]
((`name` = ? or `school` in (?)) and `delete_at` is not null) [wubin havard]
((`name` = ? or `school` in (?,?)) and `delete_at` is not null) [wubin havard beijing unv]
((`name` = ? or `age` in (?,?)) and `delete_at` is not null) [wubin 5 10]
(`name` != ? or `create_at` < ?) [wubin now()]
order by `create_at` desc,`score` asc limit ?,? [30 5]
7
order by `score` asc limit ?,? [20 10]

func C

func C() Criteria

C new a Criteria

func (Criteria) And

func (c Criteria) And(cri Base) Where

And concat another sql expression builder with And

func (Criteria) Append

func (c Criteria) Append(cri Base) Where

Append concat another sql expression builder with Append

func (Criteria) Col

func (c Criteria) Col(col string) Criteria

Col set column name

func (Criteria) End

func (c Criteria) End(q Base) Where

End does nothing for Criteria same as Append empty

func (Criteria) Eq

func (c Criteria) Eq(val interface{}) Criteria

Eq set = operator and column value

func (Criteria) Gt

func (c Criteria) Gt(val interface{}) Criteria

Gt set > operator and column value

func (Criteria) Gte

func (c Criteria) Gte(val interface{}) Criteria

Gte set >= operator and column value

func (Criteria) In

func (c Criteria) In(val interface{}) Criteria

In set in operator and column value, val should be a slice type value

func (Criteria) IsNotNull

func (c Criteria) IsNotNull() Criteria

IsNotNull set is not null

func (Criteria) IsNull

func (c Criteria) IsNull() Criteria

IsNull set is null

func (Criteria) Like

func (c Criteria) Like(val interface{}) Criteria

Like set like operator and column value, val should be a slice type value

func (Criteria) Lt

func (c Criteria) Lt(val interface{}) Criteria

Lt set < operator and column value

func (Criteria) Lte

func (c Criteria) Lte(val interface{}) Criteria

Lte set <= operator and column value

func (Criteria) Ne

func (c Criteria) Ne(val interface{}) Criteria

Ne set != operator and column value

func (Criteria) NotIn

func (c Criteria) NotIn(val interface{}) Criteria

NotIn set not in operator and column value, val should be a slice type value

func (Criteria) Or

func (c Criteria) Or(cri Base) Where

Or concat another sql expression builder with Or

func (Criteria) Sql

func (c Criteria) Sql() (string, []interface{})

Sql implement Base interface, return sql expression

func (Criteria) ToWhere

func (c Criteria) ToWhere() Where

type Order

type Order struct {
	Col  string
	Sort sortenum.Sort
}

Order by Col Sort

type Page

type Page struct {
	Orders []Order
	Offset int
	Size   int
}

Page a sql expression builder for order by clause

func NewPage

func NewPage(pageNo, pageSize int, orders ...Order) Page

func P

func P() Page

P new a Page

func (Page) Limit

func (p Page) Limit(offset, size int) Page

Limit set Offset and Size

func (Page) Order

func (p Page) Order(o Order) Page

Order append an Order

func (Page) Sql

func (p Page) Sql() (string, []interface{})

Sql implement Base interface, order by age desc limit 2,1

type PageRet

type PageRet struct {
	Items    interface{}
	PageNo   int
	PageSize int
	Total    int
	HasNext  bool
}

PageRet wrap page query result

func NewPageRet

func NewPageRet(page Page) PageRet

NewPageRet new a PageRet

type Q

type Q interface {
	Base
	And(q Base) Where
	Or(q Base) Where
	Append(q Base) Where
	End(q Base) Where
}

Q used for building sql expression

type String

type String string

String is an alias of string

func (String) Sql

func (s String) Sql() (string, []interface{})

Sql implements Base

type Where

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

Where concat children clauses with one of logic operators And, Or, Append

func (Where) And

func (w Where) And(whe Base) Where

And concat another sql expression builder with And

func (Where) Append

func (w Where) Append(whe Base) Where

Append concat another sql expression builder with Append

func (Where) End

func (w Where) End(q Base) Where

func (Where) IsEmpty added in v2.0.3

func (w Where) IsEmpty() bool

func (Where) Or

func (w Where) Or(whe Base) Where

Or concat another sql expression builder with Or

func (Where) Sql

func (w Where) Sql() (string, []interface{})

Sql implement Base interface, return string sql expression

Jump to

Keyboard shortcuts

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