-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.go
122 lines (90 loc) · 3.45 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
package patchain
import (
"fmt"
"github.com/ncodes/jsq"
)
var (
// ErrNotFound indicates a missing data
ErrNotFound = fmt.Errorf("not found")
)
// DB defines an interface for database operations
type DB interface {
// Connect connects to the database returing an error if it failed
Connect(maxOpenConn, maxIdleConn int) error
// GetConn returns the database connection or session for use by external services.
GetConn() interface{}
// SetConn sets the connection or session
SetConn(interface{}) error
// NewDB create a new DB connection
NewDB() DB
// CreateTables creates the tables required for the patchain model.
CreateTables() error
// Create creates an object and adds it the the patchain table
Create(obj interface{}, options ...Option) error
// CreateBulk is like Create but supports multiple objects
CreateBulk(objs []interface{}, options ...Option) error
// UpdatePeerHash updates the peer hash of an object
UpdatePeerHash(obj interface{}, newPeerHash string, options ...Option) error
// Count counts the number of objects in the patchain that matches a query
Count(q Query, out interface{}, options ...Option) error
// GetLast gets the last and most recent object that match the query
GetLast(q Query, out interface{}, options ...Option) error
// GetAll returns all the objects that match the query
GetAll(q Query, out interface{}, options ...Option) error
// GetValidObjectFields returns a slice of field names or column
// names that can be included in a JSQ query.
GetValidObjectFields() []string
// NewQuery returns a JSQ query parser. See http://github.com/ncodes/jsq
NewQuery() jsq.Query
// Begin returns a DB object with an active transaction
Begin() DB
// Transact starts a transaction and calls the TxFunc. It will auto commit or rollback
// the transaction if TxFunc returns nil or error respectively.
Transact(TxFunc) error
// TransactWithDB is like Transact but it allows the use of external transaction db/session
// and will only perform auto commit/rollback if finishTx is set to true.
TransactWithDB(db DB, finishTx bool, txF TxFunc) error
// Commit commits the current transaction
Commit() error
// Rollback rolls back the current transaction
Rollback() error
// NoLogging disables logging
NoLogging()
// Close frees up all resources held by the object
Close() error
}
// QueryOption provides fields that can be used to
// alter a query
type QueryOption struct {
OrderBy string
Limit int64
Offset int64
}
// Option represents an option to be used in a DB operation
type Option interface {
GetName() string
GetValue() interface{}
}
// Query represents all kinds of database queries
type Query interface {
GetQueryParams() *QueryParams
}
// Expr describes a query expression
type Expr struct {
Expr string
Args []interface{}
}
// QueryParams represents object query options
type QueryParams struct {
Expr Expr `json:"-" structs:"-" mapstructure:"-" gorm:"-"`
KeyStartsWith string `json:"-" structs:"-" mapstructure:"-" gorm:"-"`
OrderBy string `json:"-" structs:"-" mapstructure:"-" gorm:"-"`
Limit int `json:"-" structs:"-" mapstructure:"-" gorm:"-"`
}
// TxFunc is called after a transaction is started.
// DB represents the transaction object
type TxFunc func(DB, CommitFunc, RollbackFunc) error
// RollbackFunc represents a function that rolls back a transaction
type RollbackFunc func() error
// CommitFunc represents a function that commits a transaction
type CommitFunc func() error