-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql_storage.go
57 lines (46 loc) · 1.18 KB
/
mysql_storage.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
package main
import (
"fmt"
"log"
"os"
"time"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func NewMStorage(dsn string) (*MStorage, *gorm.DB, error) {
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
logger.Config{
SlowThreshold: time.Second, // Slow SQL threshold
LogLevel: logger.Silent, // Log level
IgnoreRecordNotFoundError: true, // Ignore ErrRecordNotFound error for logger
Colorful: false, // Disable color
},
)
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
Logger: newLogger.LogMode(logger.Silent),
})
if err != nil {
return nil, nil, err
}
return &MStorage{client: db}, db, nil
}
type MStorage struct {
client *gorm.DB
//logger *log.Logger
}
// var a WStorage = &MStorage{}
func (m *MStorage) Update(wallet Wallet) error {
err := m.client.Save(wallet).Error
return err
}
func (m *MStorage) Get(id int) (*Wallet, error) {
var wallet Wallet
err := m.client.First(&wallet, "id = ?", fmt.Sprint(id)).Error
return &wallet, err
}
func (m *MStorage) New(wallet Wallet) error {
err := m.client.Create(&wallet).Error
return err
}