-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorm.go
184 lines (154 loc) · 4.12 KB
/
gorm.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
package kv
import (
"context"
"database/sql"
"fmt"
"net/url"
"strings"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/driver/sqlite"
"gorm.io/driver/sqlserver"
"gorm.io/gorm"
)
type GromKeyValue struct {
gorm.Model
Key []byte `gorm:"primary_key" sql:"key"`
Val []byte `sql:"val"`
}
type GormDB struct {
*gorm.DB
}
type gormTransaction struct {
*GormDB
}
type gormIterator struct {
*sql.Rows
}
func NewGormDbFromUrl(u *url.URL) (*GormDB, error) {
var db *gorm.DB
var err error
passw, _ := u.User.Password()
switch u.Scheme {
case "postgres":
dsn := fmt.Sprintf("host=%s port=%s user=%s dbname=%s password=%s", u.Host, u.Port(), u.User.Username(), u.Path, passw)
db, err = gorm.Open(postgres.Open(dsn), &gorm.Config{})
case "mysql":
dsn := fmt.Sprintf("%s@%s/%s?%s", u.User.String(), u.Host, u.Path, u.RawQuery)
db, err = gorm.Open(mysql.Open(dsn), &gorm.Config{})
case "sqlite3":
p := strings.TrimPrefix(u.Path, "/")
db, err = gorm.Open(sqlite.Open(p), &gorm.Config{})
case "sqlserver":
fallthrough
case "mssql":
uCopy, err := url.Parse(u.String())
if err != nil {
return nil, err
}
uCopy.Scheme = "sqlserver"
dsn := uCopy.String()
db, err = gorm.Open(sqlserver.Open(dsn), &gorm.Config{})
default:
return nil, ErrInvalidDb
}
if err != nil {
return nil, err
}
return NewGormFromDB(db)
}
func NewGormFromDB(db *gorm.DB) (*GormDB, error) {
db.AutoMigrate(&GromKeyValue{})
return &GormDB{
db,
}, nil
}
// gorm db
func (gdb *GormDB) Close() error {
d, err := gdb.DB.DB()
if err != nil {
return err
}
return d.Close()
}
// Get gets the value of a key within a single query transaction
func (gdb *GormDB) Get(ctx context.Context, key []byte) ([]byte, error) {
kv := &GromKeyValue{}
if result := gdb.DB.Where("key = ?", key).First(&kv); result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return nil, ErrNotFound
}
return nil, result.Error
}
return kv.Val, nil
}
// Put sets the value of a key within a single query transaction
func (gdb *GormDB) Put(ctx context.Context, key, value []byte) error {
kv := &GromKeyValue{
Key: key,
Val: value,
}
if result := gdb.DB.Save(&kv); result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return ErrNotFound
}
return result.Error
}
return nil
}
// Delete removes a key within a single transaction
func (gdb *GormDB) Delete(ctx context.Context, key []byte) error {
kv := &GromKeyValue{
Key: key,
}
if result := gdb.DB.Where("key = ?", key).Delete(&kv); result.Error != nil {
if result.Error == gorm.ErrRecordNotFound {
return ErrNotFound
}
return result.Error
}
return nil
}
// NewTransaction for batching multiple values inside a transaction
func (gdb *GormDB) NewTransaction(ctx context.Context, readOnly bool) (OrderedTransaction, error) {
return &gormTransaction{
&GormDB{
gdb.DB.Begin(&sql.TxOptions{ReadOnly: readOnly}),
},
}, nil
}
// gormTransaction
// Seeks initializes an iterator at the given key (inclusive)
func (gdb *gormTransaction) Seek(ctx context.Context, StartKey []byte) (Iterator, error) {
rows, err := gdb.DB.Model(&GromKeyValue{}).Select("key, val").Order("key").Where("key >= ?", StartKey).Rows()
return &gormIterator{rows}, err
}
// Discard removes all sides effects of the transaction
func (gdb *gormTransaction) Discard(ctx context.Context) error {
e := gdb.DB.Rollback()
return e.Error
}
// Commit persists all side effects of the transaction and returns an error if there are any conflics
func (gdb *gormTransaction) Commit(ctx context.Context) error {
e := gdb.DB.Commit()
return e.Error
}
// gormIterator
// Next yeilds the next key-value in iterator. Key-values can not be re-used between iterations. Make sure top copy the values if you must.
func (it *gormIterator) Next(ctx context.Context) (key, value []byte, err error) {
if !it.Rows.Next() {
return nil, nil, ErrNotFound
}
var k, v []byte
if err := it.Rows.Scan(&k, &v); err != nil {
return nil, nil, err
}
if k == nil {
return nil, nil, ErrNotFound
}
return k, v, nil
}
// Close must always be called to clean up iterators.
func (gdb *gormIterator) Close() error {
return nil
}