-
Notifications
You must be signed in to change notification settings - Fork 0
/
expression.go
103 lines (88 loc) · 1.84 KB
/
expression.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
98
99
100
101
102
103
package gosql
import "strings"
// EnumDelimiter for join strings
const EnumDelimiter = "#"
// Expression slice
type expression struct {
// list of expressions
list strings.Builder
// params
params []any
}
// Append expressions
func (e *expression) Append(expr string, args ...any) {
e.Add(expr)
e.Arg(args...)
}
// Len of expressions
func (e *expression) Len() int {
if e == nil {
return 0
}
return e.list.Len()
}
// ArgLen len of arguments
func (e *expression) ArgLen() int {
if e == nil {
return 0
}
return len(e.params)
}
// Reset expressions
func (e *expression) Reset() {
if e.Len() == 0 {
return
}
e.list.Reset()
e.params = e.params[:0]
}
// String all expressions
func (e *expression) String(delimiter string) string {
if e.Len() == 0 {
return ""
}
return strings.ReplaceAll(e.list.String(), EnumDelimiter, delimiter)
}
// Arg add params
func (e *expression) Arg(args ...any) {
e.params = append(e.params, args...)
return
}
// Add expression items
func (e *expression) Add(item ...string) {
for _, arg := range item {
if e.Len() == 0 {
e.list.WriteString(arg)
} else {
e.list.WriteString(EnumDelimiter + arg)
}
}
return
}
// GetArguments return params
func (e *expression) GetArguments() []any {
if e == nil {
return nil
}
return e.params
}
// Get expressions
func (e *expression) Get(delimiter string) (string, []any) {
return e.String(delimiter), e.GetArguments()
}
// Grow memory. Multiplier is 32
func (e *expression) Grow(n int) *expression {
e.list.Grow(n * 32)
args := make([]any, 2*len(e.params)+n)
copy(args[0:], e.params)
e.params = args[:len(e.params)]
return e
}
// Split to slice of string
func (e *expression) Split() []string {
return strings.SplitN(e.list.String(), EnumDelimiter, -1)
}
// NewExpression init expression
func NewExpression() *expression {
return (&expression{}).Grow(4)
}