diff --git a/src/pod/babashka/sql.clj b/src/pod/babashka/sql.clj index 3fbb136..de6028e 100644 --- a/src/pod/babashka/sql.clj +++ b/src/pod/babashka/sql.clj @@ -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 diff --git a/test/pod/babashka/hsqldb_test.clj b/test/pod/babashka/hsqldb_test.clj index eaabec7..5c5c6e7 100644 --- a/test/pod/babashka/hsqldb_test.clj +++ b/test/pod/babashka/hsqldb_test.clj @@ -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) diff --git a/test/pod/babashka/mysql_test.clj b/test/pod/babashka/mysql_test.clj index cc91b20..f4f212e 100644 --- a/test/pod/babashka/mysql_test.clj +++ b/test/pod/babashka/mysql_test.clj @@ -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)) diff --git a/test/pod/babashka/postgresql_test.clj b/test/pod/babashka/postgresql_test.clj index 4874f75..20cb8c4 100644 --- a/test/pod/babashka/postgresql_test.clj +++ b/test/pod/babashka/postgresql_test.clj @@ -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)