-
Notifications
You must be signed in to change notification settings - Fork 0
/
queryfunc.go
97 lines (78 loc) · 1.64 KB
/
queryfunc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package queryfunc
import (
"gorm.io/gorm"
)
type BuilderFunc = func(builder *Builder)
type Handler = func(db DB, rawSQL *gorm.DB) (interface{}, error)
type Builder struct {
rawSQL string
countRawSQL string
orderBy string
groupBy string
having string
isWrapJSON bool
paginationFn Handler
}
func NewBuilder(rawSQL string, countRawSQL ...string) *Builder {
var b = &Builder{
rawSQL: rawSQL,
countRawSQL: "",
orderBy: "",
groupBy: "",
isWrapJSON: false,
}
if len(countRawSQL) > 0 {
b.countRawSQL = countRawSQL[0]
}
return b
}
func (b *Builder) WithPaginationFunc(fn Handler) *Builder {
b.paginationFn = fn
return b
}
func (b *Builder) WithRawSQL(sql string) *Builder {
b.rawSQL = sql
return b
}
func (b *Builder) WithWrapJSON(wrap bool) *Builder {
b.isWrapJSON = wrap
return b
}
func (b *Builder) WithCountRawSQL(sql string) *Builder {
b.countRawSQL = sql
return b
}
func (b *Builder) WithOrderBy(sql string) *Builder {
b.orderBy = sql
return b
}
func (b *Builder) WithGroupBy(sql string) *Builder {
b.groupBy = sql
return b
}
func (b *Builder) WithHaving(sql string) *Builder {
b.having = sql
return b
}
/* Implement interface */
func (b *Builder) GetRawSQL() string {
return b.rawSQL
}
func (b *Builder) GetCountRawSQL() string {
return b.countRawSQL
}
func (b *Builder) GetGroupByRawSQL() string {
return b.groupBy
}
func (b *Builder) GetOrderByRawSQL() string {
return b.orderBy
}
func (b *Builder) GetHavingRawSQL() string {
return b.having
}
func (b *Builder) IsWrapJSON() bool {
return b.isWrapJSON
}
func (b *Builder) GetPaginationFunc() Handler {
return b.paginationFn
}