Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change defqueries to return table of fns #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions sql/one.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
-- name: one
-- fn: first
select 14
12 changes: 9 additions & 3 deletions src/suresql/init.janet
Original file line number Diff line number Diff line change
Expand Up @@ -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))
13 changes: 13 additions & 0 deletions test/suresql/local-binding-test.janet
Original file line number Diff line number Diff line change
@@ -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) {})))))
2 changes: 1 addition & 1 deletion test/suresql/pq-users.janet
Original file line number Diff line number Diff line change
@@ -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")
Expand Down
2 changes: 1 addition & 1 deletion test/suresql/users-2.janet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(import "src/suresql/init" :prefix "")
(import ../../src/suresql :prefix "")

(def database-url "users-2.sqlite3")

Expand Down
2 changes: 1 addition & 1 deletion test/suresql/users.janet
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(import "src/suresql/init" :prefix "")
(import ../../src/suresql :prefix "")

(var sqlite3/open identity)
(def database-url "test.sqlite3")
Expand Down