-
Notifications
You must be signed in to change notification settings - Fork 39
/
dialect.go
61 lines (47 loc) · 1.3 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
package gosql
import (
"fmt"
)
// Dialect interface contains behaviors that differ across SQL database
type Dialect interface {
// GetName get dialect's name
GetName() string
// Quote quotes field name to avoid SQL parsing exceptions by using a reserved word as a field name
Quote(key string) string
// Placeholder is where value holder default "?"
Placeholder() string
}
type commonDialect struct {
}
func (commonDialect) GetName() string {
return "common"
}
func (commonDialect) Quote(key string) string {
return fmt.Sprintf(`"%s"`, key)
}
func (*commonDialect) Placeholder() string {
return "?"
}
var dialectsMap = map[string]Dialect{}
// RegisterDialect register new dialect
func RegisterDialect(name string, dialect Dialect) {
dialectsMap[name] = dialect
}
// GetDialect gets the dialect for the specified dialect name
func GetDialect(name string) (dialect Dialect, ok bool) {
dialect, ok = dialectsMap[name]
return
}
func mustGetDialect(name string) Dialect {
if dialect, ok := dialectsMap[name]; ok {
return dialect
}
panic(fmt.Sprintf("`%v` is not officially supported", name))
}
func newDialect(name string) Dialect {
if value, ok := GetDialect(name); ok {
return value
}
fmt.Printf("`%v` is not officially supported, running under compatibility mode.\n", name)
return &commonDialect{}
}