-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtosqlconfig.go
77 lines (62 loc) · 1.74 KB
/
tosqlconfig.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
package gmodel
import (
"database/sql/driver"
"github.com/lingdor/gmodel/common"
"reflect"
"strings"
)
func init() {
RegisterToSqlConfigLoader(func(db DBHandler) common.ToSqlConfig {
tp := reflect.TypeOf(db.Driver())
if tp.Elem() != nil {
tp = tp.Elem()
}
pkgPath := tp.PkgPath()
if strings.Contains(pkgPath, "mysql") {
return &mysqlConfigLoader{}
} else if strings.Contains(pkgPath, "/pg") {
return &pgsqlConfigLoader{}
} else if strings.Contains(pkgPath, "mssql") {
return &mssqlConfigLoader{}
}
return nil
})
}
func GetToSqlConfig(db DBHandler) common.ToSqlConfig {
if conf, ok := tosqlConfigCache[db.Driver()]; ok {
return conf
}
for _, loader := range tosqlConfigLoader {
if conf := loader(db); conf != nil {
tosqlConfigCache[db.Driver()] = conf
return conf
}
}
return &common.DefaultConfigLoader{}
}
var tosqlConfigCache = make(map[driver.Driver]common.ToSqlConfig, 1)
var tosqlConfigLoader = make([]func(db DBHandler) common.ToSqlConfig, 0, 1)
func RegisterToSqlConfigLoader(loader func(db DBHandler) common.ToSqlConfig) {
tosqlConfigLoader = append(tosqlConfigLoader, loader)
}
type mysqlConfigLoader struct{}
func (m mysqlConfigLoader) FieldFormat(name string) string {
return "`" + name + "`"
}
func (m mysqlConfigLoader) TableFormat(name string) string {
return "`" + name + "`"
}
type pgsqlConfigLoader struct{}
func (m pgsqlConfigLoader) FieldFormat(name string) string {
return "\"" + name + "\""
}
func (m pgsqlConfigLoader) TableFormat(name string) string {
return "\"" + name + "\""
}
type mssqlConfigLoader struct{}
func (m mssqlConfigLoader) FieldFormat(name string) string {
return "[" + name + "]"
}
func (m mssqlConfigLoader) TableFormat(name string) string {
return "[" + name + "]"
}