Skip to content

Commit

Permalink
[#5] Allow passing numbers instead of strings
Browse files Browse the repository at this point in the history
  • Loading branch information
borkdude committed Dec 31, 2020
1 parent a6c47cf commit 86c3660
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 12 deletions.
25 changes: 14 additions & 11 deletions pod/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,23 +64,26 @@ func JsonifyResult(result sql.Result) (*ExecResult, error) {
}, nil
}

func parseQuery(args string) (string, []string, error) {
func parseQuery(args string) (string, string, []interface{}, error) {
podArgs := []json.RawMessage{}
if err := json.Unmarshal([]byte(args), &podArgs); err != nil {
return "", []string{}, err
return "", "", nil, err
}

var db string
if err := json.Unmarshal(podArgs[0], &db); err != nil {
return "", []string{}, err
return "", "", nil, err
}

var query []string
if err := json.Unmarshal(podArgs[1], &query); err != nil {
return "", []string{}, err
var queryArgs []interface{}
if err := json.Unmarshal(podArgs[1], &queryArgs); err != nil {
return "", "", nil, err
}

return db, query, nil
var query string
query = queryArgs[0].(string)

return db, query, queryArgs[1:], nil
}

func makeArgs(query []string) []interface{} {
Expand Down Expand Up @@ -113,7 +116,7 @@ func ProcessMessage(message *babashka.Message) (interface{}, error) {
},
}, nil
case "invoke":
db, query, err := parseQuery(message.Args)
db, query, args, err := parseQuery(message.Args)
if err != nil {
return nil, err
}
Expand All @@ -125,11 +128,11 @@ func ProcessMessage(message *babashka.Message) (interface{}, error) {

defer conn.Close()

args := makeArgs(query)
//args := makeArgs(query)

switch message.Var {
case "pod.babashka.sqlite3/execute!":
res, err := conn.Exec(query[0], args...)
res, err := conn.Exec(query, args...)
if err != nil {
return nil, err
}
Expand All @@ -140,7 +143,7 @@ func ProcessMessage(message *babashka.Message) (interface{}, error) {
return json, nil
}
case "pod.babashka.sqlite3/query!":
res, err := conn.Query(query[0], args...)
res, err := conn.Query(query, args...)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion test/script.clj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

(prn (sqlite/execute! "/tmp/foo.db" ["create table if not exists foo (the_text TEXT, the_int INTEGER, the_real REAL)"]))
(prn (sqlite/execute! "/tmp/foo.db" ["delete from foo"]))
(prn (sqlite/execute! "/tmp/foo.db" ["insert into foo (the_text, the_int, the_real) values (?,?,?)" "foo" "1" "3.14"]))
(prn (sqlite/execute! "/tmp/foo.db" ["insert into foo (the_text, the_int, the_real) values (?,?,?)" "foo" 1 3.14]))
(def results (sqlite/query! "/tmp/foo.db" ["select * from foo"]))
(prn results)

Expand Down

0 comments on commit 86c3660

Please sign in to comment.