/
mysql.go
64 lines (54 loc) · 1.58 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
// Package db provides support for access to a mysql database using the mysql driver
package db
import (
"context"
"net/url"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
// Config is the required properties to use the database.
type Config struct {
User string
Password string
Host string
Name string
Schema string
MaxIdleConns int
MaxOpenConns int
DisableTLS bool
}
// Open opens a database specified by a configuration struct
// and a driver-specific data source name, usually consisting
// of at least a database name and connection information.
func Open(config Config) (*sqlx.DB, error) {
q := make(url.Values)
q.Set("parseTime", "true")
q.Set("timeout", "10s")
q.Set("readTimeout", "10s")
q.Set("writeTimeout", "10s")
//q.Set("tls", "custom")
//TO DO: Add TLS encryption
u := url.URL{
User: url.UserPassword(config.User, config.Password),
Host: config.Host,
Path: config.Name,
RawQuery: q.Encode(),
}
decoded, _ := url.QueryUnescape(u.String()[2:])
db, err := sqlx.Open("mysql", decoded)
if err != nil {
return nil, err
}
db.SetMaxIdleConns(config.MaxIdleConns)
db.SetMaxOpenConns(config.MaxOpenConns)
return db, nil
}
// StatusCheck returns nil if it can successfully talk to the database. It
// returns a non-nil error otherwise.
func StatusCheck(ctx context.Context, db *sqlx.DB) error {
// Run a simple query to determine connectivity.
// Running this query forces a round trip through the database.
const q = `SELECT true`
var tmp bool
return db.QueryRowContext(ctx, q).Scan(&tmp)
}