/
db.go
108 lines (95 loc) · 2.16 KB
/
db.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
package db
import (
"database/sql"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"time"
_ "github.com/go-sql-driver/mysql" // mysql
)
// Option .
type Option struct {
User string `json:"user" opt:"user" default:"root"`
Password string `json:"password" opt:"password" default:"root"`
Host string `json:"host" opt:"host" default:"localhost"`
Port int `json:"port" opt:"port" default:"3306"`
Name string `json:"name" opt:"name" default:"test"`
CharSet string `json:"char_set" opt:"charset" default:"utf8"`
}
// Node .
type Node struct {
Index string
Servers [][]int32 `json:"servers"`
Host string `json:"host"`
StartTimeStr string `json:"start_time"`
StartTime time.Time
NowTime time.Time
Duration time.Duration
maxTotalTime int32
}
// Context .
type Context struct {
*sql.DB
Opt Option
QueryStr string
}
// Config .
type Config struct {
Opt Option `json:"db_opt"`
Hosts map[string]string `json:"hosts"`
DBs map[string]Node `json:"dbs"`
Node Node
context *Context
}
func (o Option) String() string {
return fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=%s",
o.User,
o.Password,
o.Host,
o.Port,
o.Name,
o.CharSet)
}
// NewContext .
func NewContext(opt Option) *Context {
db, err := sql.Open("mysql", opt.String())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
return &Context{
DB: db,
Opt: opt,
}
}
func (c *Context) check() {
stmt, err := c.Prepare(c.QueryStr)
if err != nil {
panic(err)
}
stmt.Exec()
}
func init() {
filepath := path.Join(os.Getenv("LAMP_CONFIG_PATH"), filename)
buf, err := ioutil.ReadFile("db.json")
if err != nil {
panic(err)
}
json.Unmarshal(buf, c)
c.server = c.DBs[dbNo]
c.server.Index = dbNo
c.server.StartTime, _ = time.Parse(timeLayout, conf.server.StartTimeStr)
c.server.NowTime = time.Now()
c.server.Duration = c.server.NowTime.Sub(c.server.StartTime)
conf.server.maxTotalTime = int32(c.server.Duration.Seconds() / 2.5)
if _, err := strconv.Atoi(dbNo); err != nil {
c.Opt.Name = dbNo
} else {
c.Opt.Name = "agame_" + dbNo
}
c.Opt.Host = conf.Hosts[c.server.Host]
c.context = NewContext(c.Opt)
}