wp-go/model/condition.go

94 lines
1.4 KiB
Go
Raw Normal View History

package model
type QueryCondition struct {
2023-02-25 15:10:42 +00:00
Where ParseWhere
From string
Fields string
Group string
Order SqlBuilder
Join SqlBuilder
Having SqlBuilder
Page int
Limit int
Offset int
In [][]any
}
2023-02-25 15:10:42 +00:00
func Conditions(fns ...Condition) QueryCondition {
r := QueryCondition{}
for _, fn := range fns {
2023-02-25 15:10:42 +00:00
fn(&r)
}
2023-02-25 15:10:42 +00:00
if r.Fields == "" {
r.Fields = "*"
}
return r
}
type Condition func(c *QueryCondition)
func Where(where ParseWhere) Condition {
return func(c *QueryCondition) {
2023-02-25 15:10:42 +00:00
c.Where = where
}
}
func Fields(fields string) Condition {
return func(c *QueryCondition) {
2023-02-25 15:10:42 +00:00
c.Fields = fields
}
}
2023-02-22 10:50:27 +00:00
func From(from string) Condition {
return func(c *QueryCondition) {
2023-02-25 15:10:42 +00:00
c.From = from
2023-02-22 10:50:27 +00:00
}
}
func Group(group string) Condition {
return func(c *QueryCondition) {
2023-02-25 15:10:42 +00:00
c.Group = group
}
}
func Order(order SqlBuilder) Condition {
return func(c *QueryCondition) {
2023-02-25 15:10:42 +00:00
c.Order = order
}
}
func Join(join SqlBuilder) Condition {
return func(c *QueryCondition) {
2023-02-25 15:10:42 +00:00
c.Join = join
}
}
func Having(having SqlBuilder) Condition {
return func(c *QueryCondition) {
2023-02-25 15:10:42 +00:00
c.Having = having
}
}
func Page(page int) Condition {
return func(c *QueryCondition) {
2023-02-25 15:10:42 +00:00
c.Page = page
}
}
func Limit(limit int) Condition {
return func(c *QueryCondition) {
2023-02-25 15:10:42 +00:00
c.Limit = limit
}
}
func Offset(offset int) Condition {
return func(c *QueryCondition) {
2023-02-25 15:10:42 +00:00
c.Offset = offset
}
}
func In(in ...[]any) Condition {
return func(c *QueryCondition) {
2023-02-25 15:10:42 +00:00
c.In = append(c.In, in...)
}
}