Signed-off-by: Sean Corfield <sean@corfield.org>
This commit is contained in:
Sean Corfield 2025-02-13 16:44:56 -08:00
parent 946d160409
commit e6d1abf3ec
No known key found for this signature in database
4 changed files with 29 additions and 6 deletions

View file

@ -2,6 +2,9 @@
Only accretive/fixative changes will be made from now on. Only accretive/fixative changes will be made from now on.
* 1.3.next in progress
* Address [#295](https://github.com/seancorfield/next-jdbc/issues/295) by providing a way to tell `next.jdbc` that certain options should be passed "as-is" in the `Properties` object when creating a `Connection`.
* 1.3.994 -- 2025-01-28 * 1.3.994 -- 2025-01-28
* Fix [#293](https://github.com/seancorfield/next-jdbc/issues/293) by no longer `locking` on the `Connection` retrieved from a `DataSource`. * Fix [#293](https://github.com/seancorfield/next-jdbc/issues/293) by no longer `locking` on the `Connection` retrieved from a `DataSource`.
* Fix documentation examples of `execute-batch!` via PR [#292](https://github.com/seancorfield/next-jdbc/pull/292) from [@devurandom](https://github.com/devurandom). * Fix documentation examples of `execute-batch!` via PR [#292](https://github.com/seancorfield/next-jdbc/pull/292) from [@devurandom](https://github.com/devurandom).

View file

@ -33,6 +33,9 @@ Any path that calls `get-connection` will accept the following options:
If you need additional options set on a connection, you can either use Java interop to set them directly, or provide them as part of the "db spec" hash map passed to `get-datasource` (although then they will apply to _all_ connections obtained from that datasource). If you need additional options set on a connection, you can either use Java interop to set them directly, or provide them as part of the "db spec" hash map passed to `get-datasource` (although then they will apply to _all_ connections obtained from that datasource).
Additional options passed are set as `java.util.Properties` and, by default, are coerced to strings.
If you are working with a driver that requires a non-string value for a property (such as the Snowflake driver), you can provide a `:next.jdbc/as-is-properties` option containing a sequence of options that should be added as-is, rather than coerced to strings.
> Note: If `plan`, `execute!`, or `execute-one!` are passed a `DataSource`, a "db spec" hash map, or a JDBC URL string, they will call `get-connection`, so they will accept the above options in those cases. > Note: If `plan`, `execute!`, or `execute-one!` are passed a `DataSource`, a "db spec" hash map, or a JDBC URL string, they will call `get-connection`, so they will accept the above options in those cases.
## Generating SQL ## Generating SQL

View file

@ -1,4 +1,4 @@
;; copyright (c) 2018-2024 Sean Corfield, all rights reserved ;; copyright (c) 2018-2025 Sean Corfield, all rights reserved
(ns next.jdbc.connection (ns next.jdbc.connection
"Standard implementations of `get-datasource` and `get-connection`. "Standard implementations of `get-datasource` and `get-connection`.
@ -374,9 +374,12 @@
(defn- as-properties (defn- as-properties
"Convert any seq of pairs to a `java.util.Properties` instance." "Convert any seq of pairs to a `java.util.Properties` instance."
^Properties [m] ^Properties [m]
(let [p (Properties.)] (let [p (Properties.)
(doseq [[k v] m] as-is (set (:next.jdbc/as-is-properties m))]
(.setProperty p (name k) (str v))) (doseq [[k v] (dissoc m :next.jdbc/as-is-properties)]
(if (contains? as-is k)
(.put p (name k) v)
(.setProperty p (name k) (str v))))
p)) p))
(defn uri->db-spec (defn uri->db-spec

View file

@ -1,4 +1,4 @@
;; copyright (c) 2019-2024 Sean Corfield, all rights reserved ;; copyright (c) 2019-2025 Sean Corfield, all rights reserved
(ns next.jdbc.connection-string-test (ns next.jdbc.connection-string-test
"Tests for the main hash map spec to JDBC URL logic and the get-datasource "Tests for the main hash map spec to JDBC URL logic and the get-datasource
@ -11,7 +11,8 @@
[next.jdbc.connection :as c] [next.jdbc.connection :as c]
[next.jdbc.protocols :as p] [next.jdbc.protocols :as p]
[next.jdbc.specs :as specs] [next.jdbc.specs :as specs]
[next.jdbc.test-fixtures :refer [with-test-db db]])) [next.jdbc.test-fixtures :refer [db with-test-db]])
(:import [java.util Properties]))
(set! *warn-on-reflection* true) (set! *warn-on-reflection* true)
@ -39,3 +40,16 @@
(when (and user password) (when (and user password)
(with-open [con (p/get-connection ds {})] (with-open [con (p/get-connection ds {})]
(is (instance? java.sql.Connection con))))))) (is (instance? java.sql.Connection con)))))))
(deftest property-tests
(is (string? (.getProperty ^Properties (#'c/as-properties {:foo [42]}) "foo")))
(is (string? (.get ^Properties (#'c/as-properties {:foo [42]}) "foo")))
(is (vector? (.get ^Properties (#'c/as-properties
{:foo [42]
:next.jdbc/as-is-properties [:foo]})
"foo")))
;; because .getProperty drops non-string values!
(is (nil? (.getProperty ^Properties (#'c/as-properties
{:foo [42]
:next.jdbc/as-is-properties [:foo]})
"foo"))))