forked from chaisql/chai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
244 lines (205 loc) · 5.66 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package genji
import (
"database/sql"
"database/sql/driver"
"github.com/asdine/genji/database"
"github.com/asdine/genji/document"
"github.com/asdine/genji/engine"
"github.com/asdine/genji/engine/badgerengine"
"github.com/asdine/genji/engine/boltengine"
"github.com/asdine/genji/sql/parser"
"github.com/asdine/genji/sql/query"
"github.com/dgraph-io/badger/v2"
)
// Open creates a Genji database at the given path.
// If path is equal to ":memory:" it will open an in memory database,
// otherwise it will create an on-disk database using the BoltDB engine.
func Open(path string) (*DB, error) {
var ng engine.Engine
var err error
switch path {
case ":memory:":
ng, err = badgerengine.NewEngine(badger.DefaultOptions("").WithInMemory(true).WithLogger(nil))
default:
ng, err = boltengine.NewEngine(path, 0660, nil)
}
if err != nil {
return nil, err
}
return New(ng)
}
// DB represents a collection of tables stored in the underlying engine.
type DB struct {
DB *database.Database
}
// New initializes the DB using the given engine.
func New(ng engine.Engine) (*DB, error) {
db, err := database.New(ng)
if err != nil {
return nil, err
}
return &DB{
DB: db,
}, nil
}
// Close the database.
func (db *DB) Close() error {
return db.DB.Close()
}
// Begin starts a new transaction.
// The returned transaction must be closed either by calling Rollback or Commit.
func (db *DB) Begin(writable bool) (*Tx, error) {
tx, err := db.DB.Begin(writable)
if err != nil {
return nil, err
}
return &Tx{
Transaction: tx,
}, nil
}
// View starts a read only transaction, runs fn and automatically rolls it back.
func (db *DB) View(fn func(tx *Tx) error) error {
tx, err := db.Begin(false)
if err != nil {
return err
}
defer tx.Rollback()
return fn(tx)
}
// Update starts a read-write transaction, runs fn and automatically commits it.
func (db *DB) Update(fn func(tx *Tx) error) error {
tx, err := db.Begin(true)
if err != nil {
return err
}
defer tx.Rollback()
err = fn(tx)
if err != nil {
return err
}
return tx.Commit()
}
// Exec a query against the database without returning the result.
func (db *DB) Exec(q string, args ...interface{}) error {
res, err := db.Query(q, args...)
if err != nil {
return err
}
return res.Close()
}
// Query the database and return the result.
// The returned result must always be closed after usage.
func (db *DB) Query(q string, args ...interface{}) (*query.Result, error) {
pq, err := parser.ParseQuery(q)
if err != nil {
return nil, err
}
return pq.Run(db.DB, argsToNamedValues(args))
}
// QueryDocument runs the query and returns the first document.
// If the query returns no error, QueryDocument returns ErrDocumentNotFound.
func (db *DB) QueryDocument(q string, args ...interface{}) (document.Document, error) {
res, err := db.Query(q, args...)
if err != nil {
return nil, err
}
defer res.Close()
r, err := res.First()
if err != nil {
return nil, err
}
if r == nil {
return nil, database.ErrDocumentNotFound
}
var fb document.FieldBuffer
err = fb.ScanDocument(r)
if err != nil {
return nil, err
}
return &fb, nil
}
// ViewTable starts a read only transaction, fetches the selected table, calls fn with that table
// and automatically rolls back the transaction.
func (db *DB) ViewTable(tableName string, fn func(*Tx, *database.Table) error) error {
return db.View(func(tx *Tx) error {
tb, err := tx.GetTable(tableName)
if err != nil {
return err
}
return fn(tx, tb)
})
}
// UpdateTable starts a read/write transaction, fetches the selected table, calls fn with that table
// and automatically commits the transaction.
// If fn returns an error, the transaction is rolled back.
func (db *DB) UpdateTable(tableName string, fn func(*Tx, *database.Table) error) error {
return db.Update(func(tx *Tx) error {
tb, err := tx.GetTable(tableName)
if err != nil {
return err
}
return fn(tx, tb)
})
}
// Tx represents a database transaction. It provides methods for managing the
// collection of tables and the transaction itself.
// Tx is either read-only or read/write. Read-only can be used to read tables
// and read/write can be used to read, create, delete and modify tables.
type Tx struct {
*database.Transaction
}
// Query the database withing the transaction and returns the result.
// Closing the returned result after usage is not mandatory.
func (tx *Tx) Query(q string, args ...interface{}) (*query.Result, error) {
pq, err := parser.ParseQuery(q)
if err != nil {
return nil, err
}
return pq.Exec(tx.Transaction, argsToNamedValues(args), false)
}
// QueryDocument runs the query and returns the first document.
// If the query returns no error, QueryDocument returns ErrDocumentNotFound.
func (tx *Tx) QueryDocument(q string, args ...interface{}) (document.Document, error) {
res, err := tx.Query(q, args...)
if err != nil {
return nil, err
}
defer res.Close()
r, err := res.First()
if err != nil {
return nil, err
}
if r == nil {
return nil, database.ErrDocumentNotFound
}
return r, nil
}
// Exec a query against the database within tx and without returning the result.
func (tx *Tx) Exec(q string, args ...interface{}) error {
res, err := tx.Query(q, args...)
if err != nil {
return err
}
return res.Close()
}
func argsToNamedValues(args []interface{}) []driver.NamedValue {
nv := make([]driver.NamedValue, len(args))
for i := range args {
switch t := args[i].(type) {
case sql.NamedArg:
nv[i].Name = t.Name
nv[i].Value = t.Value
case *sql.NamedArg:
nv[i].Name = t.Name
nv[i].Value = t.Value
case driver.NamedValue:
nv[i] = t
case *driver.NamedValue:
nv[i] = *t
default:
nv[i].Ordinal = i + 1
nv[i].Value = args[i]
}
}
return nv
}