From c477fc027864b288cc280cf71ed9b04eb02c9caf Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Mon, 20 Feb 2023 10:48:37 +0800 Subject: [PATCH] feat: dd prepare to describe map and lookup --- src/pod/babashka/sql.clj | 29 ++++++++++++++++++--------- test/pod/babashka/postgresql_test.clj | 2 +- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/pod/babashka/sql.clj b/src/pod/babashka/sql.clj index de6028e..f100add 100644 --- a/src/pod/babashka/sql.clj +++ b/src/pod/babashka/sql.clj @@ -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! @@ -99,9 +107,10 @@ (.close ^java.lang.AutoCloseable conn)))) (defn prepare - ([conn statement] (prepare conn statement nil)) - ([conn statement opts] - (jdbc/prepare conn statement opts))) + ([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*) @@ -158,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 @@ -220,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")) diff --git a/test/pod/babashka/postgresql_test.clj b/test/pod/babashka/postgresql_test.clj index b83d34c..34bd867 100644 --- a/test/pod/babashka/postgresql_test.clj +++ b/test/pod/babashka/postgresql_test.clj @@ -50,7 +50,7 @@ (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)] + (let [result (db/execute-one! ps nil)] (is (= result #:foo{:foo 1})))))) (testing "transaction" (let [conn (db/get-connection db)]