-
Notifications
You must be signed in to change notification settings - Fork 87
/
query.go
189 lines (158 loc) · 3.76 KB
/
query.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
package query
import (
"context"
"github.com/genjidb/genji/internal/database"
"github.com/genjidb/genji/internal/environment"
"github.com/genjidb/genji/internal/query/statement"
"github.com/genjidb/genji/types"
)
// A Query can execute statements against the database. It can read or write data
// from any table, or even alter the structure of the database.
// Results are returned as streams.
type Query struct {
Statements []statement.Statement
tx *database.Transaction
autoCommit bool
}
// New creates a new query with the given statements.
func New(statements ...statement.Statement) Query {
return Query{Statements: statements}
}
type Context struct {
Ctx context.Context
DB *database.Database
Tx *database.Transaction
Params []environment.Param
}
func (c *Context) GetTx() *database.Transaction {
if c.Tx != nil {
return c.Tx
}
return c.DB.GetAttachedTx()
}
// Prepare the statements by calling their Prepare methods.
// It stops at the first statement that doesn't implement the statement.Preparer interface.
func (q *Query) Prepare(context *Context) error {
var err error
var tx *database.Transaction
ctx := context.Ctx
for i, stmt := range q.Statements {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
p, ok := stmt.(statement.Preparer)
if !ok {
return nil
}
if tx == nil {
tx = context.GetTx()
if tx == nil {
tx, err = context.DB.BeginTx(ctx, &database.TxOptions{
ReadOnly: true,
})
if err != nil {
return err
}
defer tx.Rollback()
}
}
stmt, err := p.Prepare(&statement.Context{
DB: context.DB,
Tx: tx,
Catalog: context.DB.Catalog,
})
if err != nil {
return err
}
q.Statements[i] = stmt
}
return nil
}
// Run executes all the statements in their own transaction and returns the last result.
func (q Query) Run(context *Context) (*statement.Result, error) {
var res statement.Result
var err error
q.tx = context.GetTx()
if q.tx == nil {
q.autoCommit = true
}
ctx := context.Ctx
for i, stmt := range q.Statements {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
// reinitialize the result
res = statement.Result{}
if qa, ok := stmt.(queryAlterer); ok {
err = qa.alterQuery(ctx, context.DB, &q)
if err != nil {
if tx := context.GetTx(); tx != nil {
tx.Rollback()
}
return nil, err
}
continue
}
if q.tx == nil {
q.tx, err = context.DB.BeginTx(ctx, &database.TxOptions{
ReadOnly: stmt.IsReadOnly(),
})
if err != nil {
return nil, err
}
}
res, err = stmt.Run(&statement.Context{
DB: context.DB,
Tx: q.tx,
Catalog: context.DB.Catalog,
Params: context.Params,
})
if err != nil {
if q.autoCommit {
q.tx.Rollback()
}
return nil, err
}
// if there are still statements to be executed,
// and the current statement is not read-only,
// iterate over the result.
if !stmt.IsReadOnly() && i+1 < len(q.Statements) {
err = res.Iterate(func(d types.Document) error { return nil })
if err != nil {
if q.autoCommit {
q.tx.Rollback()
}
return nil, err
}
}
// it there is an opened transaction but there are still statements
// to be executed, close the current transaction.
if q.tx != nil && q.autoCommit && i+1 < len(q.Statements) {
if q.tx.Writable {
err := q.tx.Commit()
if err != nil {
return nil, err
}
} else {
err := q.tx.Rollback()
if err != nil {
return nil, err
}
}
q.tx = nil
}
}
if q.autoCommit {
// the returned result will now own the transaction.
// its Close method is expected to be called.
res.Tx = q.tx
}
return &res, nil
}
type queryAlterer interface {
alterQuery(ctx context.Context, db *database.Database, q *Query) error
}