-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdbx.go
201 lines (180 loc) · 4.36 KB
/
dbx.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package dbx
import (
"context"
"database/sql"
"fmt"
"github.com/tietang/dbx/mapping"
"log"
"time"
)
// settings := dbx.Settings{
// DriverName: "mysql",
// User: "root",
// Password: "123456",
// Host: "192.168.232.175:3306",
// //Host: "172.16.1.248:3306",
// Database: "po0",
// MaxOpenConns: 10,
// MaxIdleConns: 2,
// ConnMaxLifetime: time.Minute * 30,
// LoggingEnabled: true,
// Options: map[string]string{
// "charset": "utf8",
// "parseTime": "true",
// },
// }
// var err error
// db, err = dbx.Open(settings)
// if err != nil {
// panic(err)
// }
var _ mapperExecutor = new(Database)
var _ sqlExecutor = new(Database)
var _ mapping.EntityMapper = new(Database)
var _ LoggerSettings = new(Database)
func Open(settings Settings) (db *Database, err error) {
db = &Database{}
db.DB, err = sql.Open(settings.DriverName, settings.DataSourceName())
if err != nil {
panic(err)
}
db.EntityMapper = mapping.NewEntityMapper()
db.ILogger = &defaultLogger{}
db.LoggerSettings = defaultLoggerSettings
db.SetLogging(settings.LoggingEnabled)
//
runner := NewRunner(db.DB, db.EntityMapper)
db.mapperExecutor = runner
runner.LoggerSettings = db.LoggerSettings
runner.ILogger = db.ILogger
runner.EntityMapper = db.EntityMapper
//设置最大打开连接数
db.SetMaxOpenConns(settings.MaxOpenConns)
//设置连接池最大空闲连接数
db.SetMaxIdleConns(settings.MaxIdleConns)
//设置连接最大生存时间
db.SetConnMaxLifetime(settings.ConnMaxLifetime)
db.DefaultAutoCommit = true
if v, ok := settings.Options["autocommit"]; ok {
if v == "false" {
db.DefaultAutoCommit = false
}
}
db.ping()
return db, err
}
type Database struct {
*sql.DB
mapperExecutor
mapping.EntityMapper
ILogger
LoggerSettings
DefaultAutoCommit bool
}
func (r *Database) Tx(fn func(run *TxRunner) error) error {
tx, err := r.DB.Begin()
if err != nil {
return err
}
runner := NewTxRunner(tx)
runner.EntityMapper = r.EntityMapper
runner.LoggerSettings = r.LoggerSettings
runner.ILogger = r.Logger()
if err := fn(runner); err != nil {
e := tx.Rollback()
if e != nil {
return err
}
return err
}
err = tx.Commit()
if err != nil {
return err
}
return err
}
func (r *Database) PrepareTx(sql string, fn func(run *PrepareTxRunner) error) error {
tx, err := r.DB.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare(sql)
if err != nil {
log.Fatal(err)
}
runner := NewPrepareTxRunner(sql, stmt)
runner.LoggerSettings = r.LoggerSettings
runner.ILogger = r.Logger()
if err := fn(runner); err != nil {
e := tx.Rollback()
if e != nil {
return err
}
return err
}
err = tx.Commit()
if err != nil {
return err
}
return err
}
func (r *Database) rollback(tx *sql.Tx) error {
if !r.DefaultAutoCommit {
tx.Exec("SET AUTOCOMMIT=1")
}
e := tx.Rollback()
return e
}
func (r *Database) commit(tx *sql.Tx) error {
if !r.DefaultAutoCommit {
tx.Exec("SET AUTOCOMMIT=1")
}
err := tx.Commit()
return err
}
func (r *Database) ping() {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
status := "up"
if err := r.PingContext(ctx); err != nil {
status = "down"
}
r.Log(&QueryStatus{
Message: &status,
})
}
type Settings struct {
DriverName string
Protocol string
User string
Password string
Database string
Host string
Options map[string]string
ConnMaxLifetime time.Duration
MaxOpenConns int
MaxIdleConns int
LoggingEnabled bool
}
func (s *Settings) DataSourceName() string {
queryString := ""
for key, value := range s.Options {
queryString += key + "=" + value + "&"
}
ustr := fmt.Sprintf("%s:%s@tcp(%s)/%s?%s", s.User, s.Password, s.Host, s.Database, queryString)
if s.Protocol == "http" {
ustr = fmt.Sprintf("http://%s:%s@%s/%s?%s", s.User, s.Password, s.Host, s.Database, queryString)
}
return ustr
}
func (s *Settings) ShortDataSourceName() string {
queryString := ""
for key, value := range s.Options {
queryString += key + "=" + value + "&"
}
ustr := fmt.Sprintf("%s:***@tcp(%s)/%s?%s", s.User, s.Host, s.Database, queryString)
if s.Protocol == "http" {
ustr = fmt.Sprintf("http://%s:***@%s/%s?%s", s.User, s.Host, s.Database, queryString)
}
return ustr
}