-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql.go
199 lines (174 loc) · 4.4 KB
/
mysql.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
/*
* Copyright (c) 2022-2024 Mikhail Knyazhev <markus621@yandex.com>. All rights reserved.
* Use of this source code is governed by a BSD 3-Clause license that can be found in the LICENSE file.
*/
package ormmysql
import (
"database/sql"
"fmt"
"net/url"
"sync"
"time"
_ "github.com/go-sql-driver/mysql" // nolint: golint
"go.osspkg.com/goppy/errors"
"go.osspkg.com/goppy/sqlcommon"
)
const (
defaultTimeout = time.Second * 5
defaultTimeoutConn = time.Second * 60
)
var (
_ sqlcommon.Connector = (*pool)(nil)
_ sqlcommon.ConfigInterface = (*Config)(nil)
)
type (
// Config pool of configs
Config struct {
Pool []Item `yaml:"mysql"`
}
// Item config model
Item struct {
Name string `yaml:"name"`
Host string `yaml:"host"`
Port int `yaml:"port"`
Schema string `yaml:"schema"`
User string `yaml:"user"`
Password string `yaml:"password"`
Timezone string `yaml:"timezone"`
TxIsolationLevel string `yaml:"txisolevel"`
Charset string `yaml:"charset"`
Collation string `yaml:"collation"`
MaxIdleConn int `yaml:"maxidleconn"`
MaxOpenConn int `yaml:"maxopenconn"`
InterpolateParams bool `yaml:"interpolateparams"`
MaxConnTTL time.Duration `yaml:"maxconnttl"`
Timeout time.Duration `yaml:"timeout"`
ReadTimeout time.Duration `yaml:"readtimeout"`
WriteTimeout time.Duration `yaml:"writetimeout"`
OtherParams string `yaml:"other_params"`
}
pool struct {
conf sqlcommon.ConfigInterface
db map[string]*sql.DB
l sync.RWMutex
}
)
// List getting all configs
func (c *Config) List() (list []sqlcommon.ItemInterface) {
for _, item := range c.Pool {
list = append(list, item)
}
return
}
// GetName getting config name
func (i Item) GetName() string {
return i.Name
}
// Setup setting config conntections params
func (i Item) Setup(s sqlcommon.SetupInterface) {
s.SetMaxIdleConns(i.MaxIdleConn)
s.SetMaxOpenConns(i.MaxOpenConn)
s.SetConnMaxLifetime(i.MaxConnTTL)
}
// GetDSN connection params
func (i Item) GetDSN() string {
params, err := url.ParseQuery(i.OtherParams)
if err != nil {
params = url.Values{}
}
params.Add("autocommit", "true")
params.Add("interpolateParams", fmt.Sprintf("%t", i.InterpolateParams))
// ---
if len(i.Charset) == 0 {
i.Charset = "utf8mb4"
}
params.Add("charset", i.Charset)
// ---
if len(i.Collation) == 0 {
i.Collation = "utf8mb4_unicode_ci"
}
params.Add("collation", i.Collation)
// ---
if i.Timeout == 0 {
i.Timeout = defaultTimeoutConn
}
params.Add("timeout", i.Timeout.String())
// ---
if i.ReadTimeout == 0 {
i.ReadTimeout = defaultTimeout
}
params.Add("readTimeout", i.ReadTimeout.String())
// ---
if i.WriteTimeout == 0 {
i.WriteTimeout = defaultTimeout
}
params.Add("writeTimeout", i.WriteTimeout.String())
// ---
if len(i.TxIsolationLevel) > 0 {
params.Add("transaction_isolation", i.TxIsolationLevel)
}
// ---
if len(i.Timezone) == 0 {
i.Timezone = "UTC"
}
params.Add("loc", i.Timezone)
// ---
// ---
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?%s", i.User, i.Password, i.Host, i.Port, i.Schema, params.Encode())
}
// New init new mysql connection
func New(conf sqlcommon.ConfigInterface) sqlcommon.Connector {
c := &pool{
conf: conf,
db: make(map[string]*sql.DB),
}
return c
}
// Dialect getting sql dialect
func (p *pool) Dialect() string {
return sqlcommon.MySQLDialect
}
// Reconnect update connection to database
func (p *pool) Reconnect() error {
if err := p.Close(); err != nil {
return err
}
p.l.Lock()
defer p.l.Unlock()
for _, item := range p.conf.List() {
db, err := sql.Open("mysql", item.GetDSN())
if err != nil {
if er := p.Close(); er != nil {
return errors.Wrap(err, er)
}
return err
}
item.Setup(db)
p.db[item.GetName()] = db
}
return nil
}
// Close closing connection
func (p *pool) Close() error {
p.l.Lock()
defer p.l.Unlock()
if len(p.db) > 0 {
for name, db := range p.db {
if err := db.Close(); err != nil {
return err
}
delete(p.db, name)
}
}
return nil
}
// Pool getting connection pool by name
func (p *pool) Pool(name string) (*sql.DB, error) {
p.l.RLock()
defer p.l.RUnlock()
db, ok := p.db[name]
if !ok {
return nil, sqlcommon.ErrPoolNotFound
}
return db, db.Ping()
}