Skip to content

Commit

Permalink
feat: add prepare statement
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Feb 19, 2023
1 parent 7fd0d1a commit cf891f0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/pod/babashka/sql.clj
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@
(when-let [conn (get old connection)]
(.close ^java.lang.AutoCloseable conn))))

(defn prepare
([conn statement] (prepare conn statement nil))
([conn statement opts]
(jdbc/prepare conn statement opts)))

(def transact @#'t/transact*)

(defn transaction-begin
Expand Down
5 changes: 5 additions & 0 deletions test/pod/babashka/hsqldb_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
(is (= [#:FOO{:FOO 1} #:FOO{:FOO 2} #:FOO{:FOO 3}]
(db/execute! conn ["select * from foo;"])))
(db/close-connection conn)))
(testing "prepared statements"
(let [conn (db/get-connection db)]
(with-open [ps (db/prepare conn ["select * from foo where foo = ?" 1])]
(let [result (db/execute-one! ps)]
(is (= result #:foo{:foo 1}))))))
(testing "transaction"
(let [conn (db/get-connection db)]
(transaction/begin conn)
Expand Down
5 changes: 5 additions & 0 deletions test/pod/babashka/mysql_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@
(is (= [#:foo{:foo 1} #:foo{:foo 2} #:foo{:foo 3}]
(db/execute! conn ["select * from foo;"])))
(db/close-connection conn)))
(testing "prepared statements"
(let [conn (db/get-connection db)]
(with-open [ps (db/prepare conn ["select * from foo where foo = ?" 1])]
(let [result (db/execute-one! ps)]
(is (= result #:foo{:foo 1}))))))
(testing "input parameters"
(try (db/execute! db ["drop table foo_timed;"])
(catch Exception _ nil))
Expand Down
5 changes: 5 additions & 0 deletions test/pod/babashka/postgresql_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
(db/execute! x ["insert into foo_timed values (?, ?)" 1 start-date])
(let [result (db/execute! x ["select foo from foo_timed where created <= ?" start-date])]
(is (= result [{:foo_timed/foo 1}]))))))
(testing "prepared statements"
(let [conn (db/get-connection db)]
(with-open [ps (db/prepare conn ["select * from foo where foo = ?" 1])]
(let [result (db/execute-one! ps)]
(is (= result #:foo{:foo 1}))))))
(testing "transaction"
(let [conn (db/get-connection db)]
(transaction/begin conn)
Expand Down

0 comments on commit cf891f0

Please sign in to comment.