/
dialect.go
182 lines (147 loc) · 3.55 KB
/
dialect.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package footrest
import (
"fmt"
"strconv"
"strings"
"sync"
"unicode"
)
//////////////////////////////////////////////////
func init() {
d := DefaultDialect()
RegisterDialect("", &d)
}
//
var (
dMut sync.Mutex
dialectMap map[string]*Dialect
)
// RegisterDialect add *Dialect d into a global map.
//
// See doc about Dialect.
func RegisterDialect(name string, d *Dialect) {
dMut.Lock()
if dialectMap == nil {
dialectMap = make(map[string]*Dialect)
}
dialectMap[name] = d
dMut.Unlock()
}
func GetDialect(name string) *Dialect {
dMut.Lock()
d, found := dialectMap[name]
if !found {
d = dialectMap[""]
}
dMut.Unlock()
return d
}
//////////////////////////////////////////////////
// The best way to define a dialect is use DefaultDialect() and tweek the returned dialect.
type Dialect struct {
Operators map[string]Operator // key(Operator.Name) must be upper case.
Placeholder func(int) string
Arg func(int, any) any
IsValidName func(string) bool
Paginate func(uint, uint) [2]string // (rows_per_page,page) -> stmt
}
func (d *Dialect) AddOperator(name string, format string, f ...OperatorFormatter) {
o := Operator{}
o.Name = name
o.Format = format
if format == "" {
o.Format = strings.ReplaceAll(DefaultOperatorFormat, "{OPERATOR}", name)
}
if len(f) > 0 {
o.Formatter = f[0]
}
if d.Operators == nil {
d.Operators = make(map[string]Operator)
}
d.Operators[strings.ToUpper(name)] = o
}
func DefaultDialect() Dialect {
d := Dialect{}
d.AddOperator("==", "")
d.AddOperator("=", "")
d.AddOperator("!=", "")
d.AddOperator("<>", "")
d.AddOperator(">", "")
d.AddOperator("<", "")
d.AddOperator(">=", "")
d.AddOperator("<=", "")
d.AddOperator("!<", "")
d.AddOperator("!>", "")
d.AddOperator("LIKE", "")
d.AddOperator("BETWEEN", "$1 BETWEEN $2 AND $3")
d.AddOperator("IS", "")
d.AddOperator("ISNOT", "$1 IS NOT $2")
d.AddOperator("ISNULL", "$1 IS NULL")
d.AddOperator("ISNOTNULL", "$1 IS NOT NULL")
d.AddOperator("NOT", "NOT ($1)")
d.AddOperator("AND", "($1) AND ($2)", func(args ...string) (string, error) {
myargs := make([]string, len(args))
for i := range args {
myargs[i] = "(" + args[i] + ")"
}
return strings.Join(myargs, " AND "), nil
})
d.AddOperator("OR", "($1) OR ($2)", func(args ...string) (string, error) {
myargs := make([]string, len(args))
for i := range args {
myargs[i] = "(" + args[i] + ")"
}
return strings.Join(myargs, " OR "), nil
})
d.AddOperator("||", "")
d.Placeholder = func(int) string {
return "?"
}
d.Arg = func(num int, a any) any {
return a
}
d.IsValidName = func(name string) bool {
for _, c := range name {
if unicode.IsLetter(c) {
continue
}
if unicode.IsDigit(c) {
continue
}
if c == '_' {
continue
}
return false
}
return true
}
d.Paginate = func(rowsPerPage, page uint) [2]string {
if rowsPerPage == 0 || page == 0 {
return [2]string{
"", "",
}
}
offset := rowsPerPage * (page - 1)
return [2]string{
"", fmt.Sprintf("LIMIT %d OFFSET %d", rowsPerPage, offset),
}
}
return d
}
const DefaultOperatorFormat = `$1 {OPERATOR} $2`
type OperatorFormatter func(args ...string) (string, error)
type Operator struct {
Name string
Format string // "$1 == $2", "$1 BETWEEN $2 AND $3"
Formatter OperatorFormatter // optional
}
func (o Operator) ApplyFormat(args ...string) (string, error) {
if o.Formatter != nil {
return o.Formatter(args...)
}
result := o.Format
for i, a := range args {
result = strings.ReplaceAll(result, "$"+strconv.Itoa(i+1), a)
}
return result, nil
}