Skip to content

Commit

Permalink
Add support for both string and vector queries (#10)
Browse files Browse the repository at this point in the history
* Gracefully handle query not being a vector

* Dummy commit to trigger CI

* allow both strings and vec as query, bump deps

Co-authored-by: Miikka Koskinen <miikka.koskinen@iki.fi>
  • Loading branch information
lispyclouds and miikka authored Feb 16, 2022
1 parent 0d87f28 commit 500de17
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: 2.1
jobs:
linux:
docker:
- image: circleci/golang:1.15
- image: cimg/go:1.17
steps:
- checkout
- restore_cache:
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,7 @@
main
fiddle
pod-babashka-go-sqlite3

.clj-kondo/
.nrepl-port
.lsp/
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ to the sqlite database and a query vector:
(= (count png) (count (:the_blob row)))
```

Additionally, unparameterised queries are supported if a string is passed
```clojure
(sqlite/query "/tmp/foo.db" "select * from foo")
```

Passing any other kind of data apart from a string or a vector will throw.

See [test/script.clj](test/script.clj) for an example test script.

### HoneySQL
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ module github.com/babashka/pod-babashka-go-sqlite3
go 1.15

require (
github.com/google/uuid v1.3.0 // indirect
github.com/jackpal/bencode-go v1.0.0
github.com/mattn/go-sqlite3 v1.14.6
github.com/mattn/go-sqlite3 v1.14.11
github.com/pborman/uuid v1.2.1 // indirect
github.com/russolsen/ohyeah v0.0.0-20160324131710-f4938c005315 // indirect
github.com/russolsen/same v0.0.0-20160222130632-f089df61f51d // indirect
github.com/russolsen/transit v0.0.0-20180705123435-0794b4c4505a
github.com/shopspring/decimal v1.2.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
)
11 changes: 6 additions & 5 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackpal/bencode-go v1.0.0 h1:lzbSPPqqSfWQnqVNe/BBY1NXdDpncArxShL10+fmFus=
github.com/jackpal/bencode-go v1.0.0/go.mod h1:5FSBQ74yhCl5oQ+QxRPYzWMONFnxbL68/23eezsBI5c=
github.com/mattn/go-sqlite3 v1.14.6 h1:dNPt6NO46WmLVt2DLNpwczCmdV5boIZ6g/tlDrlRUbg=
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/mattn/go-sqlite3 v1.14.11 h1:gt+cp9c0XGqe9S/wAHTL3n/7MqY+siPWgWJgqdsFrzQ=
github.com/mattn/go-sqlite3 v1.14.11/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pborman/uuid v1.2.1 h1:+ZZIw58t/ozdjRaXh/3awHfmWRbzYxJoAdNJxe/3pvw=
github.com/pborman/uuid v1.2.1/go.mod h1:X/NO0urCmaxf9VXbdlT7C2Yzkj2IKimNn4k+gtPdI/k=
github.com/russolsen/ohyeah v0.0.0-20160324131710-f4938c005315 h1:H3hCXwP92pH/hSgNrCLtjxvsKJ50sq26nICbZuoR1tQ=
Expand All @@ -12,5 +13,5 @@ github.com/russolsen/same v0.0.0-20160222130632-f089df61f51d h1:A926QrjwToaPS7gi
github.com/russolsen/same v0.0.0-20160222130632-f089df61f51d/go.mod h1:Cpq811GTlHevuU6BZxk3ObOdK8AY5gHu9QGmDak0DT4=
github.com/russolsen/transit v0.0.0-20180705123435-0794b4c4505a h1:yVNJFSzkEG8smsvd9udiQcMJA0MIsFvlG7ba314cu+s=
github.com/russolsen/transit v0.0.0-20180705123435-0794b4c4505a/go.mod h1:TPq+fcJOdGrkpZpXF4UVmFjYxH0gGqnxdgZ+OzAmvJk=
github.com/shopspring/decimal v1.2.0 h1:abSATXmQEYyShuxI4/vyW3tV1MrKAJzCZ/0zLUXYbsQ=
github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
github.com/shopspring/decimal v1.3.1 h1:2Usl1nmF/WZucqkFZhnfFYxxxu8LG21F6nPQBE5gKV8=
github.com/shopspring/decimal v1.3.1/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFRcu2hWCYk4o=
12 changes: 9 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bytes"
"errors"
"fmt"
"os"
"strings"
Expand Down Expand Up @@ -99,10 +100,15 @@ func parseQuery(args string) (string, string, []interface{}, error) {

argSlice := listToSlice(value.(*list.List))
db := argSlice[0].(string)
queryArgs := argSlice[1].([]interface{})
query := queryArgs[0].(string)

return db, query, queryArgs[1:], nil
switch queryArgs := argSlice[1].(type) {
case string:
return db, queryArgs, make([]interface{}, 0), nil
case []interface{}:
return db, queryArgs[0].(string), queryArgs[1:], nil
default:
return "", "", nil, errors.New("unexpected query type, expected a string or a vector")
}
}

func makeArgs(query []string) []interface{} {
Expand Down
23 changes: 17 additions & 6 deletions test/script.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,31 @@

(def results-min-png (mapv #(dissoc % :the_blob) results))

(def expected [{:the_int 1, :the_real 3.14, :the_text "foo"}
{:the_int 2, :the_real 1.5, :the_text "foo"}
{:the_int 3, :the_real 1.5, :the_text "bar"}
{:the_int 4, :the_real 1.5, :the_text "baz"}])

(deftest results-test
(is (= [{:the_int 1, :the_real 3.14, :the_text "foo"}
{:the_int 2, :the_real 1.5, :the_text "foo"}
{:the_int 3, :the_real 1.5, :the_text "bar"}
{:the_int 4, :the_real 1.5, :the_text "baz"}]
results-min-png)))
(is (= expected results-min-png)))

(def direct-results (sqlite/query "/tmp/foo.db" "select * from foo order by the_int asc"))

(def direct-results-min-png (mapv #(dissoc % :the_blob) direct-results))

(deftest direct-results-test
(is (= expected direct-results-min-png)))

(deftest bytes-roundtrip
(is (= (count png) (count (get-in results [0 :the_blob])))))

(deftest error-test
(is (thrown-with-msg?
Exception #"no such column: non_existing"
(sqlite/query "/tmp/foo.db" ["select non_existing from foo"]))))
(sqlite/query "/tmp/foo.db" ["select non_existing from foo"])))
(is (thrown-with-msg?
Exception #"unexpected query type, expected a string or a vector"
(sqlite/query "/tmp/foo.db" 42))))

(let [{:keys [:fail :error]} (t/run-tests)]
(System/exit (+ fail error)))

0 comments on commit 500de17

Please sign in to comment.