2023-02-01 15:48:53 +00:00
|
|
|
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
|
|
|
|
Limit int
|
|
|
|
Offset int
|
|
|
|
In [][]any
|
2023-02-01 15:48:53 +00:00
|
|
|
}
|
|
|
|
|
2023-02-25 15:10:42 +00:00
|
|
|
func Conditions(fns ...Condition) QueryCondition {
|
|
|
|
r := QueryCondition{}
|
2023-02-01 15:48:53 +00:00
|
|
|
for _, fn := range fns {
|
2023-02-25 15:10:42 +00:00
|
|
|
fn(&r)
|
2023-02-01 15:48:53 +00:00
|
|
|
}
|
2023-02-25 15:10:42 +00:00
|
|
|
if r.Fields == "" {
|
|
|
|
r.Fields = "*"
|
2023-02-01 15:48:53 +00:00
|
|
|
}
|
|
|
|
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
|
2023-02-01 15:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
func Fields(fields string) Condition {
|
|
|
|
return func(c *QueryCondition) {
|
2023-02-25 15:10:42 +00:00
|
|
|
c.Fields = fields
|
2023-02-01 15:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-01 15:48:53 +00:00
|
|
|
func Group(group string) Condition {
|
|
|
|
return func(c *QueryCondition) {
|
2023-02-25 15:10:42 +00:00
|
|
|
c.Group = group
|
2023-02-01 15:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:43:58 +00:00
|
|
|
func Order[T ~[][]string](order T) Condition {
|
2023-02-01 15:48:53 +00:00
|
|
|
return func(c *QueryCondition) {
|
2023-03-06 15:43:58 +00:00
|
|
|
c.Order = SqlBuilder(order)
|
2023-02-01 15:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:43:58 +00:00
|
|
|
func Join[T ~[][]string](join T) Condition {
|
2023-02-01 15:48:53 +00:00
|
|
|
return func(c *QueryCondition) {
|
2023-03-06 15:43:58 +00:00
|
|
|
c.Join = SqlBuilder(join)
|
2023-02-01 15:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-06 15:43:58 +00:00
|
|
|
func Having[T ~[][]string](having T) Condition {
|
2023-02-01 15:48:53 +00:00
|
|
|
return func(c *QueryCondition) {
|
2023-03-06 15:43:58 +00:00
|
|
|
c.Having = SqlBuilder(having)
|
2023-02-01 15:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Limit(limit int) Condition {
|
|
|
|
return func(c *QueryCondition) {
|
2023-02-25 15:10:42 +00:00
|
|
|
c.Limit = limit
|
2023-02-01 15:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Offset(offset int) Condition {
|
|
|
|
return func(c *QueryCondition) {
|
2023-02-25 15:10:42 +00:00
|
|
|
c.Offset = offset
|
2023-02-01 15:48:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func In(in ...[]any) Condition {
|
|
|
|
return func(c *QueryCondition) {
|
2023-02-25 15:10:42 +00:00
|
|
|
c.In = append(c.In, in...)
|
2023-02-01 15:48:53 +00:00
|
|
|
}
|
|
|
|
}
|