-
Notifications
You must be signed in to change notification settings - Fork 8
/
mysql.go
209 lines (179 loc) · 4.02 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
200
201
202
203
204
205
206
207
208
209
package driver
import (
"bytes"
"database/sql"
"errors"
"fmt"
"strings"
"sync"
"github.com/eure/kamimai/core"
)
type (
// MySQL driver object.
MySQL struct {
db *sql.DB
tx *sql.Tx
mu sync.Mutex
}
)
// Open is the first function to be called.
// Check the dsn string and open and verify any connection
// that has to be made.
func (d *MySQL) Open(dsn string) error {
z := strings.SplitN(dsn, "mysql://", 2)
if len(z) != 2 {
return errors.New("invalid data source name of mysql")
}
db, err := sql.Open("mysql", z[1])
if err != nil {
return err
}
if err := db.Ping(); err != nil {
return err
}
d.db = db
return d.Version().Create()
}
// Close is the last function to be called.
// Close any open connection here.
func (d *MySQL) Close() error {
return d.db.Close()
}
// Ext returns the sql file extension used by path. The extension is the
// suffix beginning at the final dot in the final element of path; it is
// empty if there is no dot.
func (d *MySQL) Ext() string {
return ".sql"
}
// Transaction starts a db transaction. The isolation level is dependent on the
// driver.
func (d *MySQL) Transaction(fn func(*sql.Tx) error) error {
d.mu.Lock()
defer func() {
d.tx = nil
d.mu.Unlock()
}()
tx, err := d.db.Begin()
if err != nil {
return err
}
d.tx = tx
// Procedure
if err := fn(d.tx); err != nil {
if rberr := d.tx.Rollback(); rberr != nil {
return rberr
}
return err
}
// Commit
if err := d.tx.Commit(); err != nil {
if rberr := d.tx.Rollback(); rberr != nil {
return rberr
}
return err
}
return nil
}
// Exec executes a query without returning any rows. The args are for any
// placeholder parameters in the query.
func (d *MySQL) Exec(query string, args ...interface{}) (sql.Result, error) {
if d.tx != nil {
return d.tx.Exec(query, args...)
}
return d.db.Exec(query, args...)
}
// Version returns a version interface.
func (d *MySQL) Version() core.Version {
return d
}
// Migrate applies migration file.
func (d *MySQL) Migrate(m *core.Migration) error {
b, err := m.Read()
if err != nil {
return err
}
var list []string
stmts := bytes.Split(b, []byte(";"))
for _, stmt := range stmts {
list = append(list, string(stmt))
var query string
if len(list) == 1 {
query = strings.TrimSpace(list[0])
if len(query) == 0 {
continue
}
} else {
query = strings.TrimSpace(strings.Join(list, ";"))
}
_, err = d.Exec(query)
if err != nil {
continue
} else {
list = nil
}
}
return err
}
// Insert inserts the given migration version.
func (d *MySQL) Insert(val uint64) error {
query := fmt.Sprintf(`INSERT INTO %s (version) VALUES (%d)`,
versionTableName, val)
_, err := d.Exec(query)
if err != nil {
return err
}
return nil
}
// Delete deletes the given migration version.
func (d *MySQL) Delete(val uint64) error {
query := fmt.Sprintf(`DELETE FROM %s WHERE version = %d`,
versionTableName, val)
_, err := d.Exec(query)
if err != nil {
return err
}
return nil
}
// Count counts number of row the given migration version.
func (d *MySQL) Count(val uint64) int {
query := fmt.Sprintf(`SELECT count(version) count FROM %s WHERE version = %d`,
versionTableName, val)
var count int
if err := d.db.QueryRow(query).Scan(&count); err != nil {
return 0
}
return count
}
// Current returns the current migration version.
func (d *MySQL) Current() (uint64, error) {
const query = `SELECT version FROM ` +
versionTableName + ` ORDER BY version DESC LIMIT 1`
var version uint64
err := d.db.QueryRow(query).Scan(&version)
switch {
case err == sql.ErrNoRows:
return 0, nil
case err != nil:
return 0, err
}
return version, nil
}
// Create creates
func (d *MySQL) Create() error {
const query = `CREATE TABLE IF NOT EXISTS ` +
versionTableName + ` (version BIGINT NOT NULL PRIMARY KEY);`
_, err := d.Exec(query)
if err != nil {
return err
}
return nil
}
// Drop drops
func (d *MySQL) Drop() error {
const query = `DROP TABLE IF EXISTS ` + versionTableName
_, err := d.Exec(query)
if err != nil {
return err
}
return nil
}