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

feat: add prepare statement #57

Open
wants to merge 3 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
28 changes: 22 additions & 6 deletions src/pod/babashka/sql.clj
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,26 @@
(defn execute!
([db-spec sql-params]
(execute! db-spec sql-params nil))
([db-spec sql-params opts]
([db-spec-or-stmt sql-params opts]
;; (.println System/err (str sql-params))
(let [conn (->connectable db-spec)
res (jdbc/execute! conn sql-params opts)]
(let [conn (->connectable db-spec-or-stmt)
res (if (some? conn)
(jdbc/execute! conn sql-params opts)
;; a statement is provide
(let [stmt db-spec-or-stmt]
(jdbc/execute! stmt sql-params opts)))]
res)))

(defn execute-one!
([db-spec sql-params]
(execute-one! db-spec sql-params nil))
([db-spec sql-params opts]
(let [conn (->connectable db-spec)
res (jdbc/execute-one! conn sql-params opts)]
([db-spec-or-stmt sql-params opts]
(let [conn (->connectable db-spec-or-stmt)
res (if (some? conn)
(jdbc/execute-one! conn sql-params opts)
;; a statement is provide
(let [stmt db-spec-or-stmt]
(jdbc/execute-one! stmt sql-params opts)))]
res)))

(defn insert-multi!
Expand All @@ -98,6 +106,12 @@
(when-let [conn (get old connection)]
(.close ^java.lang.AutoCloseable conn))))

(defn prepare
([conn-map statement] (prepare conn-map statement nil))
([conn-map statement opts]
(let [conn (->connectable conn-map)]
(jdbc/prepare conn statement opts))))

(def transact @#'t/transact*)

(defn transaction-begin
Expand Down Expand Up @@ -153,6 +167,7 @@
'execute-one! execute-one!
'get-connection get-connection
'close-connection close-connection
'prepare prepare
'transaction/begin transaction-begin
'transaction/rollback transaction-rollback
'transaction/commit transaction-commit
Expand Down Expand Up @@ -215,6 +230,7 @@
{:name execute-one!}
{:name get-connection}
{:name close-connection}
{:name prepare}
{:name with-transaction
:code ~with-transaction}]}
{:name ~(symbol (str sql-ns ".transaction"))
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 @@ -47,6 +47,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 nil)]
(is (= result #:foo{:foo 1}))))))
(testing "transaction"
(let [conn (db/get-connection db)]
(transaction/begin conn)
Expand Down