-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.go
More file actions
45 lines (36 loc) · 1.01 KB
/
db.go
File metadata and controls
45 lines (36 loc) · 1.01 KB
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
package base
import (
"database/sql"
"github.com/KerryJava/goserver/config"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
"log"
)
var DB *sql.DB = NewMysql()
var OrmDB *gorm.DB = NewOrmDB()
func NewOrmDB() *gorm.DB {
log.Println("DSN:", config.Content.DSN)
db, err := gorm.Open("mysql", config.Content.DSN)
if err != nil {
panic(err.Error()) // Just for example purpose. You should use proper error handling instead of panic
}
return db
}
func NewMysql() *sql.DB {
log.Println("DSN:", config.Content.DSN)
db, err := sql.Open("mysql", config.Content.DSN)
if err != nil {
panic(err.Error()) // Just for example purpose. You should use proper error handling instead of panic
}
// Open doesn't open a connection. Validate DSN data:
err = db.Ping()
if err != nil {
panic(err.Error()) // proper error handling instead of panic in your app
}
db.SetMaxIdleConns(config.Content.MaxIdleConns)
db.SetMaxOpenConns(config.Content.MaxOpenConns)
return db
}
func init() {
log.Println("init db")
}