diff --git a/sql/one.sql b/sql/one.sql new file mode 100644 index 0000000..e9588af --- /dev/null +++ b/sql/one.sql @@ -0,0 +1,3 @@ +-- name: one +-- fn: first +select 14 diff --git a/src/suresql/init.janet b/src/suresql/init.janet index 2acaf42..0216f5a 100644 --- a/src/suresql/init.janet +++ b/src/suresql/init.janet @@ -92,8 +92,14 @@ (defn defqueries [sql-file &opt options] (let [queries (->> (slurp sql-file) (parse-queries)) - connection (get options :connection)] + connection (get options :connection) + connected-queries @{}] (loop [q :in queries] - (let [{"name" name} q] - (defglobal (symbol name) (query-fn connection q)))))) + (let [{"name" name} q + q-fn (query-fn connection q)] + (defglobal (symbol name) q-fn) + (put connected-queries + (keyword name) + q-fn))) + connected-queries)) diff --git a/test/suresql/local-binding-test.janet b/test/suresql/local-binding-test.janet new file mode 100644 index 0000000..565619e --- /dev/null +++ b/test/suresql/local-binding-test.janet @@ -0,0 +1,13 @@ +(import ../../src/suresql) +(import ./users) +(import tester :prefix "" :exit true) +(import sqlite3 :as sqlite) + + +(defsuite "local bindings" + (def fns (suresql/defqueries "sql/one.sql" + {:connection (sqlite/open "test.db")})) + + (test "defquery returns functions" + (is (deep= @{:14 14} + ((fns :one) {}))))) diff --git a/test/suresql/pq-users.janet b/test/suresql/pq-users.janet index ceaf7ab..4940b05 100644 --- a/test/suresql/pq-users.janet +++ b/test/suresql/pq-users.janet @@ -1,4 +1,4 @@ -(import "src/suresql/init" :prefix "") +(import ../../src/suresql :prefix "") (var pq/connect identity) (def database-url "postgres://localhost:5432/suresql_test_db") diff --git a/test/suresql/users-2.janet b/test/suresql/users-2.janet index e9747c1..75ba297 100644 --- a/test/suresql/users-2.janet +++ b/test/suresql/users-2.janet @@ -1,4 +1,4 @@ -(import "src/suresql/init" :prefix "") +(import ../../src/suresql :prefix "") (def database-url "users-2.sqlite3") diff --git a/test/suresql/users.janet b/test/suresql/users.janet index 1178a15..fbf9f02 100644 --- a/test/suresql/users.janet +++ b/test/suresql/users.janet @@ -1,4 +1,4 @@ -(import "src/suresql/init" :prefix "") +(import ../../src/suresql :prefix "") (var sqlite3/open identity) (def database-url "test.sqlite3")