-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.go
More file actions
237 lines (210 loc) · 5.94 KB
/
Copy pathconfig.go
File metadata and controls
237 lines (210 loc) · 5.94 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package gosql
import (
"context"
"database/sql"
"fmt"
"log"
"os"
"strings"
"sync"
"time"
"github.com/jackc/pgx/v4/pgxpool"
)
type Sqlconfig struct {
UserName string
Password string
Host string
Port int
DbName string
ClientFoundRows bool
AllowCleartextPasswords bool
InterpolateParams bool
ColumnsWithAlias bool
MultiStatements bool
ParseTime bool
TLS bool
ReadTimeout time.Duration
Timeout time.Duration
WriteTimeout time.Duration
AllowOldPasswords bool
Charset string
Loc string
MaxAllowedPacket uint64 // insert 插入大量数据的时候会用到 或者用insertmany
Collation string
MaxOpenConns int // 请设置小于等于mysql 的max_connections值, 建议等于max_connections
MaxIdleConns int // 如果设置了 MaxOpenConns, 那么此直将等于 MaxOpenConns
ConnMaxLifetime time.Duration // 连接池设置
WriteLogWhenFailed bool
LogFile string
Debug bool // 打印sql
}
func (s *Sqlconfig) GetMysqlDataSource() string {
s.setDefaultConfig()
//判断是否是空map
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&clientFoundRows=%t&allowCleartextPasswords=%t&interpolateParams=%t&columnsWithAlias=%t&multiStatements=%t&parseTime=%t&tls=%t&readTimeout=%s&timeout=%s&allowOldPasswords=%t&loc=%s&maxAllowedPacket=%d&collation=%s&writeTimeout=%s",
s.UserName, s.Password, s.Host, s.Port, s.DbName, s.Charset, s.ClientFoundRows, s.AllowCleartextPasswords, s.InterpolateParams, s.ColumnsWithAlias, s.MultiStatements, s.ParseTime, s.TLS, s.ReadTimeout, s.Timeout, s.AllowCleartextPasswords, s.Loc, s.MaxAllowedPacket, s.Collation, s.WriteTimeout,
)
}
func (s *Sqlconfig) GetPostgreDataSource() string {
s.setpgDefaultConfig()
//判断是否是空map
return fmt.Sprintf("postgres://%s:%s@%s:%d/%s?pool_max_conns=%d&pool_min_conns=%d&pool_max_conn_lifetime=%v&pool_max_conn_idle_time=%v",
s.UserName, s.Password, s.Host, s.Port, s.DbName, s.MaxOpenConns, s.MaxIdleConns, s.ConnMaxLifetime, s.ConnMaxLifetime,
)
}
func (s *Sqlconfig) GetPostgreDNS() string {
s.setpgDefaultConfig()
// user=jack password=secret host=pg.example.com port=5432 dbname=mydb sslmode=verify-ca pool_max_conns=10
dns := make([]string, 0)
dns = append(dns, fmt.Sprintf("user=%s", s.UserName))
dns = append(dns, fmt.Sprintf("password=%s", s.Password))
dns = append(dns, fmt.Sprintf("host=%s", s.Host))
dns = append(dns, fmt.Sprintf("port=%d", s.Port))
dns = append(dns, fmt.Sprintf("dbname=%s", s.DbName))
dns = append(dns, fmt.Sprintf("pool_max_conns=%d", s.MaxOpenConns))
dns = append(dns, fmt.Sprintf("pool_min_conns=%d", s.MaxIdleConns))
dns = append(dns, fmt.Sprintf("pool_max_conn_lifetime=%s", s.ConnMaxLifetime))
dns = append(dns, fmt.Sprintf("pool_max_conn_idle_time=%s", s.ConnMaxLifetime))
//判断是否是空map
return strings.Join(dns, " ")
}
// 如果tag 是空的, 那么默认dbname
func (s *Sqlconfig) NewMysqlDb() (*Db, error) {
return s.conndb(s.GetMysqlDataSource())
}
func NewMysqlDb(dns string) (*Db, error) {
s := &Sqlconfig{}
return s.conndb(dns)
}
func NewPGDb(dns string) (*PGConn, error) {
s := &Sqlconfig{}
return s.connPg(dns)
}
func (s *Sqlconfig) NewPGPool() (*PGConn, error) {
return s.connPg(s.GetPostgreDNS())
}
// 不存在就创建database
func (s *Sqlconfig) CreateDB(name string) (*Db, error) {
db, err := s.conndb(s.GetMysqlDataSource())
if err != nil {
return nil, err
}
newdb, err := db.Use(name)
if err != nil {
return nil, err
}
db.Close()
return newdb, err
}
func (s *Sqlconfig) connPg(conf string) (*PGConn, error) {
conn, err := pgxpool.Connect(context.Background(), s.GetPostgreDNS())
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
os.Exit(1)
}
if err = conn.Ping(context.Background()); err != nil {
conn.Close()
return nil, err
}
db := &PGConn{
conn,
conf,
context.Background(),
s,
nil,
s.Debug,
}
if s.ReadTimeout == 0 {
s.ReadTimeout = time.Second * 30
}
// 防止开始就有很多连接,导致
// ch = make(chan struct{}, db.maxConn)
return db, nil
}
func (s *Sqlconfig) conndb(conf string) (*Db, error) {
conn, err := sql.Open("mysql", conf)
if err != nil {
return nil, err
}
if err = conn.Ping(); err != nil {
conn.Close()
return nil, err
}
db := &Db{
conn,
conf,
context.Background(),
s,
nil,
nil,
0,
0,
s.Debug,
}
if s.ReadTimeout == 0 {
s.ReadTimeout = time.Second * 30
}
db.SetMaxIdleConns(s.MaxIdleConns)
if s.MaxOpenConns > 0 {
db.maxConn = s.MaxOpenConns
} else {
db.maxConn = 100
}
db.maxpacket = s.MaxAllowedPacket
if s.MaxAllowedPacket == 0 {
db.maxpacket = 4 * 1024 * 1024 // 默认4m
}
// 防止开始就有很多连接,导致
// ch = make(chan struct{}, db.maxConn)
db.SetMaxOpenConns(s.MaxOpenConns)
db.SetConnMaxLifetime(s.ConnMaxLifetime)
if db.sc.WriteLogWhenFailed {
db.mu = &sync.RWMutex{}
if db.sc.LogFile == "" {
db.sc.LogFile = ".failed.sql"
}
var err error
db.f, err = os.OpenFile(db.sc.LogFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0755)
if err != nil {
log.Fatal(err)
}
}
return db, nil
}
func (s *Sqlconfig) setDefaultConfig() {
if s.Charset == "" {
s.Charset = "utf8mb4"
}
if s.Collation == "" {
s.Collation = "utf8mb4_general_ci"
}
if s.Loc == "" {
s.Loc = "UTC"
}
if s.Timeout == 0 {
s.Timeout = time.Second * 2
}
if s.ReadTimeout == 0 {
s.ReadTimeout = time.Second * 20
}
}
func (s *Sqlconfig) setpgDefaultConfig() {
if s.UserName == "" {
s.UserName = "postgres"
}
if s.Host == "" {
s.Host = "127.0.0.1"
}
if s.Port == 0 {
s.Port = 5432
}
if s.DbName == "" {
s.DbName = "postgres"
}
if s.MaxOpenConns == 0 {
s.MaxOpenConns = 10
}
if s.MaxIdleConns == 0 {
s.MaxOpenConns = 1
}
}