Skip to content

Commit cf891f0

Browse files
committed
feat: add prepare statement
1 parent 7fd0d1a commit cf891f0

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

src/pod/babashka/sql.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@
9898
(when-let [conn (get old connection)]
9999
(.close ^java.lang.AutoCloseable conn))))
100100

101+
(defn prepare
102+
([conn statement] (prepare conn statement nil))
103+
([conn statement opts]
104+
(jdbc/prepare conn statement opts)))
105+
101106
(def transact @#'t/transact*)
102107

103108
(defn transaction-begin

test/pod/babashka/hsqldb_test.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
(is (= [#:FOO{:FOO 1} #:FOO{:FOO 2} #:FOO{:FOO 3}]
2626
(db/execute! conn ["select * from foo;"])))
2727
(db/close-connection conn)))
28+
(testing "prepared statements"
29+
(let [conn (db/get-connection db)]
30+
(with-open [ps (db/prepare conn ["select * from foo where foo = ?" 1])]
31+
(let [result (db/execute-one! ps)]
32+
(is (= result #:foo{:foo 1}))))))
2833
(testing "transaction"
2934
(let [conn (db/get-connection db)]
3035
(transaction/begin conn)

test/pod/babashka/mysql_test.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@
5353
(is (= [#:foo{:foo 1} #:foo{:foo 2} #:foo{:foo 3}]
5454
(db/execute! conn ["select * from foo;"])))
5555
(db/close-connection conn)))
56+
(testing "prepared statements"
57+
(let [conn (db/get-connection db)]
58+
(with-open [ps (db/prepare conn ["select * from foo where foo = ?" 1])]
59+
(let [result (db/execute-one! ps)]
60+
(is (= result #:foo{:foo 1}))))))
5661
(testing "input parameters"
5762
(try (db/execute! db ["drop table foo_timed;"])
5863
(catch Exception _ nil))

test/pod/babashka/postgresql_test.clj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@
4646
(db/execute! x ["insert into foo_timed values (?, ?)" 1 start-date])
4747
(let [result (db/execute! x ["select foo from foo_timed where created <= ?" start-date])]
4848
(is (= result [{:foo_timed/foo 1}]))))))
49+
(testing "prepared statements"
50+
(let [conn (db/get-connection db)]
51+
(with-open [ps (db/prepare conn ["select * from foo where foo = ?" 1])]
52+
(let [result (db/execute-one! ps)]
53+
(is (= result #:foo{:foo 1}))))))
4954
(testing "transaction"
5055
(let [conn (db/get-connection db)]
5156
(transaction/begin conn)

0 commit comments

Comments
 (0)