Skip to content

Commit

Permalink
Merge pull request #977 from go-kivik/viewOptions
Browse files Browse the repository at this point in the history
Improve view options, part 1
  • Loading branch information
flimzy authored May 20, 2024
2 parents b9485b5 + 6edbf8b commit ace00c6
Show file tree
Hide file tree
Showing 7 changed files with 637 additions and 77 deletions.
32 changes: 27 additions & 5 deletions x/sqlite/changes.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,30 @@ func (d *db) newNormalChanges(ctx context.Context, opts optsMap, since, lastSeq
c.lastSeq = strconv.FormatUint(*lastSeq, 10)
}

descending, err := opts.descending()
if err != nil {
return nil, err
}

includeDocs, err := opts.includeDocs()
if err != nil {
return nil, err
}

var query string
if opts.includeDocs() {
query = d.normalChangesQueryWithDocs(descendingToDirection(opts.descending()))
if includeDocs {
query = d.normalChangesQueryWithDocs(descendingToDirection(descending))
} else {
query = d.normalChangesQueryWithoutDocs(descendingToDirection(opts.descending()))
query = d.normalChangesQueryWithoutDocs(descendingToDirection(descending))
}
if limit > 0 {
query += " LIMIT " + strconv.FormatUint(limit+1, 10)
}
c.rows, err = d.db.QueryContext(ctx, query, since, opts.attachments()) //nolint:rowserrcheck,sqlclosecheck // Err checked in Next
attachments, err := opts.attachments()
if err != nil {
return nil, err
}
c.rows, err = d.db.QueryContext(ctx, query, since, attachments) //nolint:rowserrcheck,sqlclosecheck // Err checked in Next
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -345,8 +359,16 @@ func (d *db) Changes(ctx context.Context, options driver.Options) (driver.Change
if err != nil {
return nil, err
}
includeDocs, err := opts.includeDocs()
if err != nil {
return nil, err
}
if sinceNow && feed == feedLongpoll {
return d.newLongpollChanges(ctx, opts.includeDocs(), opts.attachments())
attachments, err := opts.attachments()
if err != nil {
return nil, err
}
return d.newLongpollChanges(ctx, includeDocs, attachments)
}

return d.newNormalChanges(ctx, opts, since, lastSeq, sinceNow, feed)
Expand Down
4 changes: 2 additions & 2 deletions x/sqlite/changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func TestDBChanges(t *testing.T) {
})
tests.Add("malformed sequence id", test{
options: kivik.Param("since", "invalid"),
wantErr: "malformed sequence supplied in 'since' parameter",
wantErr: "malformed sequence supplied in 'since' parameter: invalid",
wantStatus: http.StatusBadRequest,
})
tests.Add("future since value returns only latest change", func(t *testing.T) interface{} {
Expand Down Expand Up @@ -192,7 +192,7 @@ func TestDBChanges(t *testing.T) {
})
tests.Add("invalid limit value", test{
options: kivik.Param("limit", "invalid"),
wantErr: "malformed 'limit' parameter",
wantErr: "invalid value for 'limit': invalid",
wantStatus: http.StatusBadRequest,
})
tests.Add("longpoll + since in past should return all historical changes since that seqid", func(t *testing.T) interface{} {
Expand Down
12 changes: 10 additions & 2 deletions x/sqlite/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ func (d *db) Get(ctx context.Context, id string, options driver.Options) (*drive
toMerge.LocalSeq = 0
}

if opts.conflicts() {
conflicts, err := opts.conflicts()
if err != nil {
return nil, err
}
if conflicts {
revs, err := d.conflicts(ctx, tx, id, r, false)
if err != nil {
return nil, err
Expand Down Expand Up @@ -136,7 +140,11 @@ func (d *db) Get(ctx context.Context, id string, options driver.Options) (*drive
}
}

atts, err := d.getAttachments(ctx, tx, id, r, opts.attachments(), opts.attsSince())
attachments, err := opts.attachments()
if err != nil {
return nil, err
}
atts, err := d.getAttachments(ctx, tx, id, r, attachments, opts.attsSince())
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit ace00c6

Please sign in to comment.