-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1013 from go-kivik/sqliteFind
Minimal _find support for sqlite backend
- Loading branch information
Showing
8 changed files
with
194 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// 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://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 ( | ||
"context" | ||
"encoding/json" | ||
"net/http" | ||
|
||
"github.com/go-kivik/kivik/v4/driver" | ||
"github.com/go-kivik/kivik/v4/int/errors" | ||
"github.com/go-kivik/kivik/v4/x/mango" | ||
) | ||
|
||
func parseQuery(query interface{}) (*mango.Selector, error) { | ||
var selector []byte | ||
switch t := query.(type) { | ||
case string: | ||
selector = []byte(t) | ||
case []byte: | ||
selector = t | ||
case json.RawMessage: | ||
selector = t | ||
default: | ||
var err error | ||
selector, err = json.Marshal(query) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
var s mango.Selector | ||
err := json.Unmarshal(selector, &s) | ||
return &s, err | ||
} | ||
|
||
func (d *db) Find(ctx context.Context, query interface{}, options driver.Options) (driver.Rows, error) { | ||
opts := newOpts(options) | ||
vopts, err := opts.viewOptions(viewAllDocs) | ||
if err != nil { | ||
return nil, err | ||
} | ||
vopts.includeDocs = true | ||
|
||
selector, err := parseQuery(query) | ||
if err != nil { | ||
return nil, &errors.Error{Status: http.StatusBadRequest, Err: err} | ||
} | ||
return d.queryBuiltinView(ctx, vopts, selector) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
// 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://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 ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"gitlab.com/flimzy/testy" | ||
|
||
"github.com/go-kivik/kivik/v4" | ||
"github.com/go-kivik/kivik/v4/driver" | ||
"github.com/go-kivik/kivik/v4/int/mock" | ||
) | ||
|
||
func TestFind(t *testing.T) { | ||
t.Parallel() | ||
type test struct { | ||
db *testDB | ||
query any | ||
options driver.Options | ||
want []rowResult | ||
wantStatus int | ||
wantErr string | ||
} | ||
|
||
tests := testy.NewTable() | ||
tests.Add("no docs in db", test{ | ||
want: nil, | ||
}) | ||
tests.Add("query is invalid json", test{ | ||
query: "invalid json", | ||
wantStatus: http.StatusBadRequest, | ||
wantErr: "invalid character 'i' looking for beginning of value", | ||
}) | ||
tests.Add("field equality", func(t *testing.T) interface{} { | ||
d := newDB(t) | ||
rev := d.tPut("foo", map[string]string{"foo": "bar"}) | ||
_ = d.tPut("bar", map[string]string{"bar": "baz"}) | ||
|
||
return test{ | ||
db: d, | ||
query: map[string]interface{}{ | ||
"foo": "bar", | ||
}, | ||
want: []rowResult{ | ||
{ID: "foo", Doc: `{"_id":"foo","_rev":"` + rev + `","foo":"bar"}`}, | ||
}, | ||
} | ||
}) | ||
|
||
/* | ||
TODO: | ||
- Include _design_docs in results? | ||
- Include _local_docs in results? | ||
- limit | ||
- skip | ||
- fields | ||
- use_index | ||
- bookmark | ||
- execution_stats | ||
*/ | ||
|
||
tests.Run(t, func(t *testing.T, tt test) { | ||
t.Parallel() | ||
db := tt.db | ||
if db == nil { | ||
db = newDB(t) | ||
} | ||
opts := tt.options | ||
if opts == nil { | ||
opts = mock.NilOption | ||
} | ||
rows, err := db.Find(context.Background(), tt.query, opts) | ||
if !testy.ErrorMatchesRE(tt.wantErr, err) { | ||
t.Errorf("Unexpected error: %s", err) | ||
} | ||
if status := kivik.HTTPStatus(err); status != tt.wantStatus { | ||
t.Errorf("Unexpected status: %d", status) | ||
} | ||
if err != nil { | ||
return | ||
} | ||
checkRows(t, rows, tt.want) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters