Skip to content

Commit

Permalink
feat: dd prepare to describe map and lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Feb 20, 2023
1 parent ce3f3cc commit c477fc0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
29 changes: 20 additions & 9 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 @@ -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*)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"))
Expand Down
2 changes: 1 addition & 1 deletion test/pod/babashka/postgresql_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down

0 comments on commit c477fc0

Please sign in to comment.