-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.go
More file actions
105 lines (83 loc) · 2.96 KB
/
function.go
File metadata and controls
105 lines (83 loc) · 2.96 KB
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
98
99
100
101
102
103
104
105
package gorm_ext
import (
"github.com/linxlib/gorm-ext/constants"
"strings"
)
type Function struct {
funStr string
}
func (f *Function) As(asName any) string {
return f.funStr + " " + constants.As + " " + getColumnName(asName)
}
func (f *Function) Eq(value int64) (string, int64) {
return buildFunStr(f.funStr, constants.Eq, value)
}
func (f *Function) Ne(value int64) (string, int64) {
return buildFunStr(f.funStr, constants.Ne, value)
}
func (f *Function) Gt(value int64) (string, int64) {
return buildFunStr(f.funStr, constants.Gt, value)
}
func (f *Function) Ge(value int64) (string, int64) {
return buildFunStr(f.funStr, constants.Ge, value)
}
func (f *Function) Lt(value int64) (string, int64) {
return buildFunStr(f.funStr, constants.Lt, value)
}
func (f *Function) Le(value int64) (string, int64) {
return buildFunStr(f.funStr, constants.Le, value)
}
func (f *Function) In(values ...any) (string, []any) {
// 构建占位符
placeholder := buildPlaceholder(values)
return f.funStr + " " + constants.In + placeholder.String(), values
}
func (f *Function) NotIn(values ...any) (string, []any) {
// 构建占位符
placeholder := buildPlaceholder(values)
return f.funStr + " " + constants.Not + " " + constants.In + placeholder.String(), values
}
func (f *Function) Between(start int64, end int64) (string, int64, int64) {
return f.funStr + " " + constants.Between + " ? " + constants.And + " ?", start, end
}
func (f *Function) NotBetween(start int64, end int64) (string, int64, int64) {
return f.funStr + " " + constants.Not + " " + constants.Between + " ? " + constants.And + " ?", start, end
}
func Sum(columnName any) *Function {
return &Function{funStr: addBracket(constants.SUM, getColumnName(columnName))}
}
func Avg(columnName any) *Function {
return &Function{funStr: addBracket(constants.AVG, getColumnName(columnName))}
}
func Max(columnName any) *Function {
return &Function{funStr: addBracket(constants.MAX, getColumnName(columnName))}
}
func Min(columnName any) *Function {
return &Function{funStr: addBracket(constants.MIN, getColumnName(columnName))}
}
func Count(columnName any) *Function {
return &Function{funStr: addBracket(constants.COUNT, getColumnName(columnName))}
}
func As(columnName any, asName any) string {
return getColumnName(columnName) + " " + constants.As + " " + getColumnName(asName)
}
func addBracket(function string, columnNameStr string) string {
return function + constants.LeftBracket + columnNameStr + constants.RightBracket
}
func buildFunStr(funcStr string, typeStr string, value int64) (string, int64) {
return funcStr + " " + typeStr + " ?", value
}
func buildPlaceholder(values []any) strings.Builder {
var placeholder strings.Builder
placeholder.WriteString(constants.LeftBracket)
for i := 0; i < len(values); i++ {
if i == len(values)-1 {
placeholder.WriteString("?")
placeholder.WriteString(constants.RightBracket)
break
}
placeholder.WriteString("?")
placeholder.WriteString(",")
}
return placeholder
}