Skip to content

Commit 5fba698

Browse files
committed
Adding support for row- and result-set- handlers.
This partially addresses issue #10.
1 parent 325b003 commit 5fba698

2 files changed

Lines changed: 21 additions & 4 deletions

File tree

src/yesql/generate.clj

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,25 @@
3838
;; single-element list with `first`.
3939
(tc-ignore
4040
(defn execute-handler
41-
[db sql-and-params]
41+
[db sql-and-params call-options]
4242
(first (jdbc/execute! db sql-and-params))))
4343

4444
(tc-ignore
4545
(defn insert-handler
46-
[db [statement & params]]
46+
[db [statement & params] call-options]
4747
(jdbc/db-do-prepared-return-keys db statement params)))
4848

49+
(tc-ignore
50+
(defn query-handler
51+
[db sql-and-params
52+
{:keys [row-fn result-set-fn]
53+
:or {row-fn identity
54+
result-set-fn doall}
55+
:as call-options}]
56+
(jdbc/query db sql-and-params
57+
:row-fn row-fn
58+
:result-set-fn result-set-fn)))
59+
4960
;; (ann ^:no-check generate-query-fn
5061
;; [yesql.types.Query -> (IFn [Any * -> Any])])
5162

@@ -63,7 +74,7 @@
6374
(let [jdbc-fn (cond
6475
(= (take-last 2 name) [\< \!]) insert-handler
6576
(= (last name) \!) execute-handler
66-
:else jdbc/query)
77+
:else query-handler)
6778
required-args (expected-parameter-list statement)
6879
global-connection (:connection query-options)
6980
real-fn (fn [args call-options]
@@ -75,7 +86,8 @@
7586
"Check the docs, and supply {:connection ...} as an option to the function call, or globally to the defquery declaration."])
7687
name))
7788
(jdbc-fn connection
78-
(rewrite-query-for-jdbc statement args))))
89+
(rewrite-query-for-jdbc statement args)
90+
call-options)))
7991
[display-args generated-function] (let [named-args (if-let [as-vec (seq (mapv (comp symbol clojure.core/name)
8092
required-args))]
8193
{:keys as-vec}

test/yesql/core_test.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
:value2 2
3737
:? [0 0]}))
3838

39+
;;; Processor functions
40+
(expect java.util.Date
41+
(current-time-query {} {:result-set-fn first
42+
:row-fn :time}))
43+
3944
;;; Test Metadata.
4045
(expect (more-> "Just selects the current time.\nNothing fancy." :doc
4146
'current-time-query :name

0 commit comments

Comments
 (0)