-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
87 lines (72 loc) · 1.91 KB
/
db.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
package utils
import (
"fmt"
"os"
"strings"
"github.com/go-sql-driver/mysql"
"github.com/go-xorm/core"
"github.com/go-xorm/xorm"
)
// LoadMySQLConfigEnv initializes MySQL config using Environment Variables.
func LoadMySQLConfigEnv() *mysql.Config {
conf := &mysql.Config{
Net: "tcp",
Addr: os.Getenv("DATABASE_HOST"),
DBName: os.Getenv("DATABASE_NAME"),
User: os.Getenv("DATABASE_USER"),
Passwd: os.Getenv("DATABASE_PASSWORD"),
AllowNativePasswords: true,
}
return conf
}
// InitMySQLEngine initializes xorm engine.
func InitMySQLEngine(conf *mysql.Config) (*xorm.Engine, error) {
engine, err := xorm.NewEngine("mysql", conf.FormatDSN())
if err != nil {
return nil, err
}
engine.SetMapper(core.GonicMapper{})
charset, ok := conf.Params["charset"]
if !ok {
charset = "utf8mb4"
}
engine.Charset(charset)
engine.StoreEngine("InnoDb")
logLevel, err := parseLogLevel(os.Getenv("LOG_LEVEL"))
if err != nil {
return nil, err
}
engine.SetLogLevel(logLevel)
// Show sql log if logLevel is "debug" or "info".
engine.ShowSQL(logLevel == core.LOG_DEBUG || logLevel == core.LOG_INFO)
return engine, nil
}
func parseLogLevel(lvl string) (core.LogLevel, error) {
switch strings.ToLower(lvl) {
case "panic", "fatal", "error":
return core.LOG_ERR, nil
case "warn", "warning":
return core.LOG_WARNING, nil
case "info":
return core.LOG_INFO, nil
case "debug":
return core.LOG_DEBUG, nil
}
return core.LOG_DEBUG, fmt.Errorf("cannot parse \"%v\" into go-xorm/core.LogLevel", lvl)
}
// EscapeMySQLString prevents from SQL-injection.
func EscapeMySQLString(value string) string {
replace := map[string]string{
"\\": "\\\\",
"'": `\'`,
"\\0": "\\\\0",
"\n": "\\n",
"\r": "\\r",
`"`: `\"`,
"\x1a": "\\Z",
}
for b, a := range replace {
value = strings.Replace(value, b, a, -1)
}
return value
}