dbCommon.go
2.91 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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package sdkdb
import (
"common"
"io"
"log"
"github.com/astaxie/beego/orm"
_ "github.com/go-sql-driver/mysql"
)
/**
* 初始化
*/
func DBInit() {
// 设置数据库类型
orm.RegisterDriver("mysql", orm.DRMySQL)
/*
// 注册模型
IceGameTopInit()
IceGameTopSetInit()
IceRewardInit()
IceRewardUserInit()
*/
}
/**
* 注册数据库
*/
func DBRegis(dbmysql common.Mysql) (err error) {
// 正式环境
/* dbUser := "icesimba_root"
dbPasswd := "Icesimba858680668866sdk121"
dbAddr := "rm-2zenjpu465r1ee1rd.mysql.rds.aliyuncs.com"
dbPort := "3306"
dbName := "icesimbasdk_pro"
*/
//测试环境
dbUser := dbmysql.UserName
dbPasswd := dbmysql.Password
dbAddr := dbmysql.Address
dbPort := dbmysql.Port
dbName := dbmysql.DBName
//预上线环境
/*
dbUser := "trump"
dbPasswd := "Trump123"
dbAddr := "rm-2zeia588lj1117u37.mysql.rds.aliyuncs.com"
dbPort := "3306"
dbName := "icesimbasdk_verification"
*/
connStr := dbUser + ":" + dbPasswd + "@tcp(" + dbAddr + ":" + dbPort + ")/" + dbName + "?charset=utf8"
log.Printf("DB connStr[%s]", connStr)
err = orm.RegisterDataBase("default", "mysql", connStr)
return
}
/**
* 切换数据库
*/
func DBChange(dbName string) {
o := orm.NewOrm()
o.Using(dbName)
}
/**
* 重置已经注册的模型struct
*/
func ResetRegisModel() {
orm.ResetModelCache()
}
/**
* 设置debug模式,如果设置debug模式,所有的数据库操作都会产生日志,日志会输出到writer中
* @param isPrintLog 是否打印日志
* @param writer 日志的输出定向
*/
func SetLogDebug(isPrintLog bool, writer io.Writer) {
orm.Debug = isPrintLog
orm.DebugLog = orm.NewLog(writer)
}
/**
* 事务开始
*/
func DBBegin() (orm.Ormer, error) {
o := orm.NewOrm()
err := o.Begin()
return o, err
}
/**
* 事务提交
*/
func DBCommit(o orm.Ormer) error {
err := o.Commit()
return err
}
/**
* 事务回滚
*/
func DBRollback(o orm.Ormer) error {
err := o.Rollback()
return err
}
/**
* 查询某个表的数据
* @param tableName 表名
*/
func QueryTable(tableName string) orm.QuerySeter {
o := orm.NewOrm()
qs := o.QueryTable(tableName)
return qs
}
/**
* 执行sql(非查询)
*/
func SqlExec(sqlstr string, args ...interface{}) {
o, _ := DBBegin()
rawSeter := o.Raw(sqlstr, args...)
res, err := rawSeter.Exec()
if err == nil {
num, _ := res.RowsAffected()
log.Println("mysql row affected nums: ", num)
}
DBCommit(o)
}
/**
* 获取当前数据库的信息
* @return currDBName 当前数据库的名字
* @return currDBType 当前数据库的类型
*/
func CurrDBInfo() (currDBName string, currDBType string) {
o := orm.NewOrm()
dr := o.Driver()
currDBName = dr.Name()
switch dr.Type() {
case orm.DRMySQL:
currDBType = "mysql"
case orm.DRSqlite:
currDBType = "sqlite"
case orm.DROracle:
currDBType = "oracle"
case orm.DRPostgres:
currDBType = "pgsql"
case orm.DRTiDB:
currDBType = "TiDB"
}
return
}