wp-go/model/condition.go

114 lines
2.2 KiB
Go
Raw Permalink Normal View History

package model
2023-05-19 15:14:58 +00:00
import "context"
type QueryCondition struct {
2023-05-19 15:14:58 +00:00
Where ParseWhere
From string
Fields string
Group string
Order SqlBuilder
Join SqlBuilder
Having SqlBuilder
Limit int
Offset int
2023-12-03 14:42:44 +00:00
TotalRow int
2023-05-19 15:14:58 +00:00
In [][]any
2023-05-20 17:38:19 +00:00
RelationFn []func() (bool, bool, *QueryCondition, RelationFn)
}
2023-12-03 14:42:44 +00:00
type RelationFn func() (func(any) []any, func(any, any), func(bool) any, Relationship)
2023-05-20 17:38:19 +00:00
2023-05-18 14:27:28 +00:00
func Conditions(fns ...Condition) *QueryCondition {
r := &QueryCondition{}
for _, fn := range fns {
2023-05-18 14:27:28 +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
}
}
2023-03-06 15:43:58 +00:00
func Order[T ~[][]string](order T) Condition {
return func(c *QueryCondition) {
2023-03-06 15:43:58 +00:00
c.Order = SqlBuilder(order)
}
}
2023-03-06 15:43:58 +00:00
func Join[T ~[][]string](join T) Condition {
return func(c *QueryCondition) {
2023-03-06 15:43:58 +00:00
c.Join = SqlBuilder(join)
}
}
2023-03-06 15:43:58 +00:00
func Having[T ~[][]string](having T) Condition {
return func(c *QueryCondition) {
2023-03-06 15:43:58 +00:00
c.Having = SqlBuilder(having)
}
}
func Limit(limit int) Condition {
return func(c *QueryCondition) {
2023-02-25 15:10:42 +00:00
c.Limit = limit
}
}
// TotalRaw only effect on Pagination, ChunkFind,when TotalRaw>0 ,will not query count
2023-12-03 14:42:44 +00:00
func TotalRaw(total int) Condition {
return func(c *QueryCondition) {
c.TotalRow = total
}
}
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...)
}
}
2023-05-17 14:22:31 +00:00
2023-05-19 15:14:58 +00:00
func WithCtx(ctx *context.Context) Condition {
2023-05-17 14:22:31 +00:00
return func(c *QueryCondition) {
2023-05-19 15:14:58 +00:00
*ctx = context.WithValue(*ctx, "ancestorsQueryCondition", c)
2023-05-17 14:22:31 +00:00
}
}
2023-05-18 14:27:28 +00:00
2023-12-03 14:42:44 +00:00
func WithFn(getVal, isJoin bool, q *QueryCondition, fn func() (func(any) []any, func(any, any), func(bool) any, Relationship)) Condition {
2023-05-18 14:27:28 +00:00
return func(c *QueryCondition) {
2023-05-20 17:38:19 +00:00
c.RelationFn = append(c.RelationFn, func() (bool, bool, *QueryCondition, RelationFn) {
2023-05-19 15:14:58 +00:00
return getVal, isJoin, q, fn
})
2023-05-18 14:27:28 +00:00
}
}