-
Notifications
You must be signed in to change notification settings - Fork 13
/
store_mysql.go
97 lines (75 loc) · 1.95 KB
/
store_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
package store
import (
"database/sql"
"strconv"
)
type mysqlStore struct {
dbProvider DbProvider
}
func NewMysqlStore(dbProvider DbProvider) Store {
return mysqlStore{dbProvider}
}
func (ms mysqlStore) Put(name string, value string, checksum string) (string, error) {
db, err := ms.dbProvider.Db()
if err != nil {
return "", err
}
result, err := //nolint:ineffassign,staticcheck
db.Exec("INSERT INTO configurations (name, value, checksum) VALUES(?,?,?)", name, value, checksum)
id, err := result.LastInsertId()
if err != nil {
return "", err
}
return strconv.Itoa(int(id)), err
}
func (ms mysqlStore) GetByName(name string) (Configurations, error) {
var results Configurations
db, err := ms.dbProvider.Db()
if err != nil {
return results, err
}
rows, err := db.Query("SELECT id, name, value, checksum FROM configurations WHERE name = ? ORDER BY id DESC", name)
if err != nil {
if err == sql.ErrNoRows {
return results, nil
}
return results, err
}
defer rows.Close()
for rows.Next() {
var config Configuration
if err := rows.Scan(&config.ID, &config.Name, &config.Value, &config.ParameterChecksum); err != nil {
return results, err
}
results = append(results, config)
}
return results, err
}
func (ms mysqlStore) GetByID(id string) (Configuration, error) {
result := Configuration{}
db, err := ms.dbProvider.Db()
if err != nil {
return result, err
}
err = db.QueryRow("SELECT id, name, value FROM configurations WHERE id = ?", id).Scan(&result.ID, &result.Name, &result.Value)
if err == sql.ErrNoRows {
return result, nil
}
return result, err
}
func (ms mysqlStore) Delete(name string) (int, error) {
deletedCount := 0
db, err := ms.dbProvider.Db()
if err != nil {
return deletedCount, err
}
result, err := db.Exec("DELETE FROM configurations WHERE name = ?", name)
if err != nil {
return 0, err
}
if result != nil {
rows, err := result.RowsAffected()
return int(rows), err
}
return 0, err
}