Skip to content

Commit

Permalink
Merge pull request #944 from go-kivik/Query
Browse files Browse the repository at this point in the history
Beginning support for querying views
  • Loading branch information
flimzy committed Apr 18, 2024
2 parents e2287f0 + 9cb17e8 commit 174a241
Show file tree
Hide file tree
Showing 20 changed files with 704 additions and 126 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.17
require (
github.com/ajg/form v1.5.1
github.com/cenkalti/backoff/v4 v4.1.3
github.com/dop251/goja v0.0.0-20240220182346-e401ed450204
github.com/go-chi/chi v1.5.5
github.com/go-chi/chi/v5 v5.0.10
github.com/go-playground/validator/v10 v10.16.0
Expand All @@ -29,11 +30,14 @@ require (
require (
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/google/pprof v0.0.0-20230207041349-798e818bf904 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
Expand All @@ -54,7 +58,6 @@ require (
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.9.3 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.41.0 // indirect
Expand Down
38 changes: 35 additions & 3 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion x/sqlite/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func newDB(t *testing.T) *testDB {
dsn = fmt.Sprintf("file:%x?mode=memory&cache=shared", md5sum.Sum(nil))
}
d := drv{}
client, err := d.NewClient(dsn, nil)
client, err := d.NewClient(dsn, mock.NilOption)
if err != nil {
t.Fatal(err)
}
Expand Down
18 changes: 12 additions & 6 deletions x/sqlite/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,27 @@ package sqlite
import (
"context"
"database/sql"
"log"

"github.com/go-kivik/kivik/v4/driver"
)

type db struct {
db *sql.DB
name string
db *sql.DB
name string
logger *log.Logger
}

var _ driver.DB = (*db)(nil)

func (c *client) newDB(name string) *db {
return &db{
db: c.db,
name: name,
logger: c.logger,
}
}

func (d *db) Close() error {
return d.db.Close()
}
Expand All @@ -52,10 +62,6 @@ func (db) ViewCleanup(context.Context) error {
return nil
}

func (db) Query(context.Context, string, string, driver.Options) (driver.Rows, error) {
return nil, nil
}

func (db) BulkDocs(context.Context, []interface{}, driver.Options) ([]driver.BulkResult, error) {
return nil, nil
}
Expand Down
4 changes: 2 additions & 2 deletions x/sqlite/designdocs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ func TestDBDesignDocs(t *testing.T) {
want: []rowResult{
{
ID: "_design/bar",
Rev: rev2,
Value: `{"value":{"rev":"` + rev2 + `"}}` + "\n",
Key: "_design/bar",
Value: `{"value":{"rev":"` + rev2 + `"}}`,
},
},
}
Expand Down
4 changes: 2 additions & 2 deletions x/sqlite/getattachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func (d *db) GetAttachment(ctx context.Context, docID string, filename string, o
}
} else {
requestedRev, err = d.winningRev(ctx, tx, docID)
if errors.Is(err, sql.ErrNoRows) {
return nil, &internal.Error{Status: http.StatusNotFound, Message: "missing"}
if err != nil {
return nil, err
}
}

Expand Down
4 changes: 2 additions & 2 deletions x/sqlite/getattachmentmeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ func (d *db) GetAttachmentMeta(ctx context.Context, docID, filename string, opti
}
} else {
requestedRev, err = d.winningRev(ctx, tx, docID)
if errors.Is(err, sql.ErrNoRows) {
return nil, &internal.Error{Status: http.StatusNotFound, Message: "missing"}
if err != nil {
return nil, err
}
}

Expand Down
24 changes: 23 additions & 1 deletion x/sqlite/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ type designDocData struct {
ValidateDocUpdates string `json:"validate_doc_update,omitempty"`
// AutoUpdate indicates whether to automatically build indexes defined in
// this design document. Default is true.
AutoUpdate *bool `json:"autoupdate,omitempty"`
AutoUpdate *bool `json:"autoupdate,omitempty"`
Options map[string]interface{} `json:"options,omitempty"`
}

// RevID returns calculated revision ID, possibly setting the MD5sum if it is
Expand Down Expand Up @@ -412,3 +413,24 @@ func jsonMarshal(s interface{}) []byte {
j, _ := json.Marshal(s)
return j
}

func (d *fullDoc) toMap() map[string]interface{} {
var result map[string]interface{}
if err := json.Unmarshal(d.Doc, &result); err != nil {
panic(err)
}
result["_id"] = d.ID
result["_rev"] = d.Rev
if d.Deleted {
result["_deleted"] = true
}
/*
Conflicts []string `json:"_conflicts,omitempty"`
DeletedConflicts []string `json:"_deleted_conflicts,omitempty"`
RevsInfo []map[string]string `json:"_revs_info,omitempty"`
Revisions *revsInfo `json:"_revisions,omitempty"`
LocalSeq int `json:"_local_seq,omitempty"`
Attachments map[string]*attachment `json:"_attachments,omitempty"`
*/
return result
}
4 changes: 2 additions & 2 deletions x/sqlite/localdocs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func TestDBLocalDocs(t *testing.T) {
want: []rowResult{
{
ID: "_local/bar",
Rev: rev2,
Value: `{"value":{"rev":"` + rev2 + `"}}` + "\n",
Key: "_local/bar",
Value: `{"value":{"rev":"` + rev2 + `"}}`,
},
},
}
Expand Down
38 changes: 38 additions & 0 deletions x/sqlite/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 (
"log"

"github.com/go-kivik/kivik/v4"
)

type optionLogger struct {
*log.Logger
}

var _ kivik.Option = (*optionLogger)(nil)

func (o optionLogger) Apply(target interface{}) {
if client, ok := target.(*client); ok {
client.logger = o.Logger
}
}

// OptionLogger is an option to set a custom logger for the SQLite driver. The
// logger will be used to log any errors that occur during asynchronous
// operations such as background index rebuilding.
func OptionLogger(logger *log.Logger) kivik.Option {
return optionLogger{Logger: logger}
}
30 changes: 30 additions & 0 deletions x/sqlite/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,33 @@ func (o optsMap) latest() bool {
func (o optsMap) revs() bool {
return toBool(o["revs"])
}

const (
updateModeTrue = "true"
updateModeFalse = "false"
updateModeLazy = "lazy"
)

func (o optsMap) update() (string, error) {
v, ok := o["update"]
if !ok {
return updateModeTrue, nil
}
switch t := v.(type) {
case bool:
if t {
return updateModeTrue, nil
}
return updateModeFalse, nil
case string:
switch t {
case "true":
return updateModeTrue, nil
case "false":
return updateModeFalse, nil
case "lazy":
return updateModeLazy, nil
}
}
return "", &internal.Error{Status: http.StatusBadRequest, Message: "invalid value for `update`"}
}
2 changes: 2 additions & 0 deletions x/sqlite/put_designdocs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ func TestDBPut_designDocs(t *testing.T) {
/*
TODO:
- unsupported language? -- ignored?
- Drop old indexes when a ddoc changes
*/

tests.Run(t, func(t *testing.T, tt test) {
Expand Down
Loading

0 comments on commit 174a241

Please sign in to comment.