-
-
Notifications
You must be signed in to change notification settings - Fork 243
/
store.go
62 lines (55 loc) · 2.22 KB
/
store.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
package pop
import (
"context"
"database/sql"
"github.com/jmoiron/sqlx"
)
// Store is an interface that must be implemented in order for Pop
// to be able to use the value as a way of talking to a datastore.
type store interface {
Select(interface{}, string, ...interface{}) error
Get(interface{}, string, ...interface{}) error
NamedExec(string, interface{}) (sql.Result, error)
NamedQuery(query string, arg interface{}) (*sqlx.Rows, error)
Exec(string, ...interface{}) (sql.Result, error)
PrepareNamed(string) (*sqlx.NamedStmt, error)
Transaction() (*Tx, error)
Rollback() error
Commit() error
Close() error
// Context versions to wrap with contextStore
SelectContext(context.Context, interface{}, string, ...interface{}) error
GetContext(context.Context, interface{}, string, ...interface{}) error
NamedExecContext(context.Context, string, interface{}) (sql.Result, error)
NamedQueryContext(ctx context.Context, query string, arg interface{}) (*sqlx.Rows, error)
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
PrepareNamedContext(context.Context, string) (*sqlx.NamedStmt, error)
TransactionContext(context.Context) (*Tx, error)
TransactionContextOptions(context.Context, *sql.TxOptions) (*Tx, error)
}
// ContextStore wraps a store with a Context, so passes it with the functions that don't take it.
type contextStore struct {
store
ctx context.Context
}
func (s contextStore) Transaction() (*Tx, error) {
return s.store.TransactionContext(s.ctx)
}
func (s contextStore) Select(dest interface{}, query string, args ...interface{}) error {
return s.store.SelectContext(s.ctx, dest, query, args...)
}
func (s contextStore) Get(dest interface{}, query string, args ...interface{}) error {
return s.store.GetContext(s.ctx, dest, query, args...)
}
func (s contextStore) NamedExec(query string, arg interface{}) (sql.Result, error) {
return s.store.NamedExecContext(s.ctx, query, arg)
}
func (s contextStore) Exec(query string, args ...interface{}) (sql.Result, error) {
return s.store.ExecContext(s.ctx, query, args...)
}
func (s contextStore) PrepareNamed(query string) (*sqlx.NamedStmt, error) {
return s.store.PrepareNamedContext(s.ctx, query)
}
func (s contextStore) Context() context.Context {
return s.ctx
}