-
Notifications
You must be signed in to change notification settings - Fork 162
/
init.go
95 lines (77 loc) · 2.9 KB
/
init.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
// +----------------------------------------------------------------------
// | GoCMS 0.1
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.6574.com.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: zzdboy <zzdboy1616@163.com>
// +----------------------------------------------------------------------
package models
//初始化模型
import "os"
import "fmt"
import "strings"
import _ "github.com/go-sql-driver/mysql"
import "github.com/go-xorm/xorm"
import "github.com/revel/revel"
import "github.com/revel/config"
//数据库链接
//读数据
var DB_Read *xorm.Engine
//写数据
var DB_Write *xorm.Engine
func init() {
revel.OnAppStart(InitDB)
}
//设置数据库
func InitDB() {
//判断是否是系统的分隔符
separator := "/"
if os.IsPathSeparator('\\') {
separator = "\\"
} else {
separator = "/"
}
config_file := (revel.BasePath + "/conf/databases.conf")
config_file = strings.Replace(config_file, "/", separator, -1)
c, _ := config.ReadDefault(config_file)
read_driver, _ := c.String("database", "db.read.driver")
read_dbname, _ := c.String("database", "db.read.dbname")
read_user, _ := c.String("database", "db.read.user")
read_password, _ := c.String("database", "db.read.password")
read_host, _ := c.String("database", "db.read.host")
//read_prefix, _ := c.String("database", "db.read.prefix")
//数据库链接
var err error
DB_Read, err = xorm.NewEngine(read_driver, fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", read_user, read_password, read_host, read_dbname))
if err != nil {
revel.WARN.Printf("DB_Read错误: %v", err)
}
write_driver, _ := c.String("database", "db.write.driver")
write_dbname, _ := c.String("database", "db.write.dbname")
write_user, _ := c.String("database", "db.write.user")
write_password, _ := c.String("database", "db.write.password")
write_host, _ := c.String("database", "db.write.host")
//write_prefix, _ := c.String("database", "db.write.prefix")
DB_Write, err = xorm.NewEngine(write_driver, fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8", write_user, write_password, write_host, write_dbname))
if err != nil {
revel.WARN.Printf("DB_Write错误: %v", err)
}
//缓存方式是存放到内存中,缓存struct的记录数为1000条
//cacher := xorm.NewLRUCacher(xorm.NewMemoryStore(), 1000)
//DB_Read.SetDefaultCacher(cacher)
//DB_Write.SetDefaultCacher(cacher)
//控制台打印SQL语句
//DB_Read.ShowSQL = true
//DB_Write.ShowSQL = true
//控制台打印调试信息
//DB_Read.ShowDebug = true
//DB_Write.ShowDebug = true
//控制台打印错误信息
//DB_Read.ShowErr = true
//DB_Write.ShowErr = true
//控制台打印警告信息
//DB_Read.ShowWarn = true
//DB_Read.ShowWarn = true
}