-
Notifications
You must be signed in to change notification settings - Fork 42
/
views.go
330 lines (303 loc) · 8.36 KB
/
views.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// http:https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
package sqlite
import (
"bytes"
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strings"
"github.com/go-kivik/kivik/v4/driver"
internal "github.com/go-kivik/kivik/v4/int/errors"
)
func endKeyOp(descending, inclusive bool) string {
switch {
case descending && inclusive:
return ">="
case descending && !inclusive:
return ">"
case !descending && inclusive:
return "<="
case !descending && !inclusive:
return "<"
}
panic("unreachable")
}
func startKeyOp(descending bool) string {
if descending {
return "<="
}
return ">="
}
const (
viewAllDocs = "_all_docs"
viewLocalDocs = "_local_docs"
viewDesignDocs = "_design_docs"
)
func isBuiltinView(view string) bool {
switch view {
case viewAllDocs, viewLocalDocs, viewDesignDocs:
return true
}
return false
}
func (d *db) AllDocs(ctx context.Context, options driver.Options) (driver.Rows, error) {
return d.Query(ctx, viewAllDocs, "", options)
}
func (d *db) LocalDocs(ctx context.Context, options driver.Options) (driver.Rows, error) {
return d.Query(ctx, viewLocalDocs, "", options)
}
func (d *db) DesignDocs(ctx context.Context, options driver.Options) (driver.Rows, error) {
return d.Query(ctx, viewDesignDocs, "", options)
}
func (d *db) queryBuiltinView(
ctx context.Context,
vopts *viewOptions,
) (driver.Rows, error) {
args := []interface{}{vopts.includeDocs, vopts.conflicts, vopts.updateSeq, vopts.attachments}
where := append([]string{""}, vopts.buildWhere(&args)...)
query := fmt.Sprintf(d.query(leavesCTE+`
SELECT
TRUE AS up_to_date,
FALSE AS reducible,
"" AS reduce_func,
IIF($3, MAX(seq), "") AS update_seq,
NULL,
NULL,
NULL AS attachment_count,
NULL AS filename,
NULL AS content_type,
NULL AS length,
NULL AS digest,
NULL AS rev_pos,
NULL AS data
FROM {{ .Docs }}
UNION ALL
SELECT
CASE WHEN row_number = 1 THEN id END AS id,
CASE WHEN row_number = 1 THEN key END AS key,
CASE WHEN row_number = 1 THEN value END AS value,
CASE WHEN row_number = 1 THEN rev END AS rev,
CASE WHEN row_number = 1 THEN doc END AS doc,
CASE WHEN row_number = 1 THEN conflicts END AS conflicts,
COALESCE(attachment_count, 0) AS attachment_count,
filename,
content_type,
length,
digest,
rev_pos,
data
FROM (
SELECT
view.id,
view.key,
'{"value":{"rev":"' || view.rev || '-' || view.rev_id || '"}}' AS value,
view.rev || '-' || view.rev_id AS rev,
view.doc,
view.conflicts,
SUM(CASE WHEN bridge.pk IS NOT NULL THEN 1 ELSE 0 END) OVER (PARTITION BY view.id, view.rev, view.rev_id) AS attachment_count,
ROW_NUMBER() OVER (PARTITION BY view.id, view.rev, view.rev_id) AS row_number,
att.filename AS filename,
att.content_type AS content_type,
att.length AS length,
att.digest AS digest,
att.rev_pos AS rev_pos,
IIF($4, att.data, NULL) AS data
FROM (
SELECT
view.id AS id,
view.key AS key,
view.rev AS rev,
view.rev_id AS rev_id,
view.doc AS doc,
IIF($2, GROUP_CONCAT(conflicts.rev || '-' || conflicts.rev_id, ','), NULL) AS conflicts
FROM (
SELECT
id AS id,
rev AS rev,
rev_id AS rev_id,
key AS key,
IIF($1, doc, NULL) AS doc,
ROW_NUMBER() OVER (PARTITION BY id ORDER BY rev DESC, rev_id DESC) AS rank
FROM leaves
) AS view
LEFT JOIN leaves AS conflicts ON conflicts.id = view.id AND NOT (view.rev = conflicts.rev AND view.rev_id = conflicts.rev_id)
WHERE view.rank = 1
%[2]s -- WHERE
GROUP BY view.id, view.rev, view.rev_id
%[1]s -- ORDER BY
LIMIT %[3]d OFFSET %[4]d
) AS view
LEFT JOIN {{ .AttachmentsBridge }} AS bridge ON view.id = bridge.id AND view.rev = bridge.rev AND view.rev_id = bridge.rev_id AND $1
LEFT JOIN {{ .Attachments }} AS att ON bridge.pk = att.pk
%[1]s -- ORDER BY
)
`), vopts.buildOrderBy(), strings.Join(where, " AND "), vopts.limit, vopts.skip)
results, err := d.db.QueryContext(ctx, query, args...) //nolint:rowserrcheck // Err checked in Next
if err != nil {
return nil, err
}
meta, err := readFirstRow(results, vopts)
if err != nil {
return nil, err
}
return &rows{
ctx: ctx,
db: d,
rows: results,
updateSeq: meta.updateSeq,
}, nil
}
type viewMetadata struct {
upToDate bool
reducible bool
reduceFuncJS string
updateSeq string
lastSeq int
}
// readFirstRow reads the first row from the resultset, which contains. In the
// case of an error, the result set is closed and an error is returned.
func readFirstRow(results *sql.Rows, vopts *viewOptions) (*viewMetadata, error) {
if !results.Next() {
// should never happen
_ = results.Close() //nolint:sqlclosecheck // Aborting
return nil, errors.New("no rows returned")
}
var meta viewMetadata
var lastSeq *int
if err := results.Scan(
&meta.upToDate, &meta.reducible, &meta.reduceFuncJS, &meta.updateSeq, &lastSeq, discard{},
discard{}, discard{}, discard{}, discard{}, discard{}, discard{}, discard{},
); err != nil {
_ = results.Close() //nolint:sqlclosecheck // Aborting
return nil, err
}
if vopts.reduce != nil && *vopts.reduce && !meta.reducible {
_ = results.Close() //nolint:sqlclosecheck // Aborting
return nil, &internal.Error{Status: http.StatusBadRequest, Message: "reduce is invalid for map-only views"}
}
if lastSeq != nil {
meta.lastSeq = *lastSeq
}
return &meta, nil
}
func descendingToDirection(descending bool) string {
if descending {
return "DESC"
}
return "ASC"
}
type rows struct {
ctx context.Context
db *db
rows *sql.Rows
updateSeq string
}
var _ driver.Rows = (*rows)(nil)
func (r *rows) Next(row *driver.Row) error {
var (
attachmentCount int
full *fullDoc
)
for {
if !r.rows.Next() {
if err := r.rows.Err(); err != nil {
return err
}
return io.EOF
}
var (
key, doc []byte
value, data *[]byte
id, conflicts, rowRev, filename, contentType *string
length *int64
revPos *int
digest *md5sum
)
if err := r.rows.Scan(
&id, &key, &value, &rowRev, &doc, &conflicts,
&attachmentCount,
&filename, &contentType, &length, &digest, &revPos, &data,
); err != nil {
return err
}
if rowRev != nil {
// If rowRev is populated, it means we're on the first row for the
// document. Otherwise, we're on an attachment-only row.
if id != nil {
row.ID = *id
}
row.Key = key
if len(key) == 0 {
row.Key = []byte("null")
}
if value == nil {
row.Value = strings.NewReader("null")
} else {
row.Value = bytes.NewReader(*value)
}
if doc != nil {
full = &fullDoc{
ID: row.ID,
Rev: *rowRev,
Doc: doc,
}
if conflicts != nil {
full.Conflicts = strings.Split(*conflicts, ",")
}
}
}
if filename != nil {
if full.Attachments == nil {
full.Attachments = make(map[string]*attachment)
}
var jsonData json.RawMessage
if data != nil {
var err error
jsonData, err = json.Marshal(*data)
if err != nil {
return err
}
}
full.Attachments[*filename] = &attachment{
ContentType: *contentType,
Length: *length,
Digest: *digest,
RevPos: *revPos,
Data: jsonData,
}
}
if attachmentCount == 0 || attachmentCount == len(full.Attachments) {
break
}
}
if full != nil {
row.Doc = full.toReader()
}
return nil
}
func (r *rows) Close() error {
return r.rows.Close()
}
func (r *rows) UpdateSeq() string {
return r.updateSeq
}
func (r *rows) Offset() int64 {
return 0
}
func (r *rows) TotalRows() int64 {
return 0
}