-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJDatabase.go
40 lines (32 loc) · 848 Bytes
/
JDatabase.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
package toolset
import (
"container/list"
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type FuncCreateTable func()
var (
DB *gorm.DB
tables *list.List = list.New()
)
func RegisterTable(createTableFunc FuncCreateTable) {
tables.PushBack(createTableFunc)
}
func InitDatabase(dbUser, dbPasswd, dbHost, dbPort, dbName string) bool {
dsn := dbUser + ":" + dbPasswd + "@(" + dbHost + ":" + dbPort + ")/" + dbName + "?charset=utf8mb4&parseTime=True&loc=UTC"
var err error
DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent), NowFunc: func() time.Time { return time.Now().UTC() }})
if err != nil {
return false
}
createTables()
return true
}
func createTables() {
for e := tables.Front(); e != nil; e = e.Next() {
c := e.Value.(FuncCreateTable)
c()
}
}