-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdbo.go
152 lines (140 loc) · 3.87 KB
/
dbo.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
package evo
import (
"fmt"
"github.com/getevo/evo-ng/lib/generic"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"gorm.io/gorm/schema"
"gorm.io/plugin/dbresolver"
"log"
"os"
"strings"
"time"
)
var Database *gorm.DB
func setupDatabase() {
Events.Trigger("database.setup")
config := Config.Database
var err error
if config.Enabled == false {
return
}
var logLevel = logger.Silent
config.Debug = strings.ToLower(config.Debug)
switch config.Debug {
case "true", "all", "*", "any":
logLevel = logger.Info
case "warn", "warning":
logLevel = logger.Warn
case "err", "error":
logLevel = logger.Error
default:
logLevel = logger.Silent
}
if config.Debug == "true" || config.Debug == "all" {
logLevel = logger.Info
}
if config.Debug == "warn" || config.Debug == "warning" {
logLevel = logger.Warn
}
if config.Debug == "err" || config.Debug == "error" {
logLevel = logger.Error
}
var newLog = logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logLevel, // Log level
Colorful: true, // Disable color
},
)
cfg := &gorm.Config{
Logger: newLog,
PrepareStmt: config.StmtCache,
SkipDefaultTransaction: config.SkipDefaultTransaction,
FullSaveAssociations: config.FullSaveAssociations,
DisableAutomaticPing: config.DisableAutomaticPing,
DisableForeignKeyConstraintWhenMigrating: config.DisableForeignKeyConstraintWhenMigrating,
QueryFields: config.QueryFields,
CreateBatchSize: config.CreateBatchSize,
NamingStrategy: schema.NamingStrategy{
TablePrefix: config.TablePrefix,
},
}
switch strings.ToLower(config.Dialect) {
case "mysql":
Database, err = gorm.Open(mysql.Open(config.DSN), cfg)
case "postgres":
Database, err = gorm.Open(postgres.Open(config.DSN), cfg)
case "mssql":
Database, err = gorm.Open(sqlserver.Open(config.DSN), cfg)
case "sqlite":
Database, err = gorm.Open(sqlite.Open(config.DSN), cfg)
default:
Panic("invalid database dialect passed to configuration")
}
if err != nil {
Panic(err)
return
}
var connMaxLifeTime, connMaxIdleTime time.Duration
var db, _ = Database.DB()
connMaxLifeTime, err = generic.Parse(config.ConnMaxLifeTime).Duration()
if err != nil {
Panic("invalid db connection_max_lifetime")
return
}
if connMaxLifeTime < 1 {
connMaxLifeTime = 1 * time.Hour
}
connMaxIdleTime, err = generic.Parse(config.ConnMaxIdleTime).Duration()
if err != nil {
fmt.Println("invalid db connection_max_idle_time")
}
if connMaxIdleTime < 1 {
connMaxIdleTime = 1 * time.Hour
}
if config.MaxIdleConns < 1 {
config.MaxIdleConns = 1
}
if config.MaxOpenConns < 1 {
config.MaxOpenConns = 1
}
if config.MaxIdleConns > config.MaxOpenConns {
config.MaxOpenConns = config.MaxIdleConns
}
db.SetConnMaxLifetime(connMaxLifeTime)
db.SetMaxIdleConns(config.MaxIdleConns)
db.SetMaxOpenConns(config.MaxOpenConns)
db.SetConnMaxIdleTime(connMaxIdleTime)
if len(config.Replicas) > 0 {
var dialectics []gorm.Dialector
for _, item := range config.Replicas {
dialectics = append(dialectics, mysql.Open(item))
}
err := Database.Use(dbresolver.Register(dbresolver.Config{
Replicas: dialectics,
}))
if err != nil {
Panic(err)
}
}
Events.Trigger("database.connected")
}
// GetDBO return database object instance
func GetDBO(name ...string) *gorm.DB {
if Database == nil {
setupDatabase()
}
return Database
}
type Model struct {
ID uint `json:"id" gorm:"primary_key"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"deleted_at" sql:"index"`
}