-
Notifications
You must be signed in to change notification settings - Fork 2
/
connection.go
98 lines (80 loc) · 1.99 KB
/
connection.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
// proxysql
package proxysql
import (
"database/sql"
"flag"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/juju/errors"
)
type (
// connect proxysql use admin user.
Conn struct {
Addr string
Port uint64
User string
Password string
Database string
Charset string
Collation string
DBI string
Retry uint64
}
)
var proxysql_addr = flag.String("addr", "127.0.0.1", "proxysql listen address.default 127.0.0.1")
var proxysql_port = flag.Uint64("port", 6032, "proxysql listen port,default 6032")
var proxysql_user = flag.String("user", "admin", "proxysql administrator name.default admin")
var proxysql_pass = flag.String("pass", "admin", "proxysql administrator password.default admin")
func NewConn(addr string, port uint64, user string, password string) (*Conn, error) {
ps := new(Conn)
ps.Addr = addr
ps.Port = port
ps.User = user
ps.Password = password
ps.Database = "stats"
ps.Charset = "utf8"
ps.Collation = "utf8_general_ci"
ps.Retry = 3
return ps, nil
}
// set character set .such as : utf8
func (ps *Conn) SetCharset(charset string) {
ps.Charset = charset
}
// set collation.such as : utf8_general_ci
func (ps *Conn) SetCollation(collation string) {
ps.Collation = collation
}
// set default schema,default is stats
func (ps *Conn) SetDefaultSchema(schema string) {
if len(schema) != 0 {
ps.Database = schema
}
}
// set retrys.
func (ps *Conn) SetRetry(retry uint64) {
ps.Retry = retry
}
func (ps *Conn) MakeDBI() {
ps.DBI = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s&collation=%s", ps.User, ps.Password, ps.Addr, ps.Port, ps.Database, ps.Charset, ps.Collation)
}
func (ps *Conn) OpenConn() (*sql.DB, error) {
db, err := sql.Open("mysql", ps.DBI)
if err != nil {
return nil, errors.Trace(err)
}
err = db.Ping()
if err != nil {
return nil, errors.Trace(err)
}
//defer db.Close()
return db, nil
}
// close connection.
func (ps *Conn) CloseConn(db *sql.DB) error {
err := db.Close()
if err != nil {
return err
}
return nil
}