diff --git a/CHANGELOG.md b/CHANGELOG.md index 3063871..6e03de4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ 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 * 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). diff --git a/doc/all-the-options.md b/doc/all-the-options.md index 29400fc..994828f 100644 --- a/doc/all-the-options.md +++ b/doc/all-the-options.md @@ -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). +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. ## Generating SQL diff --git a/src/next/jdbc/connection.clj b/src/next/jdbc/connection.clj index a7cf0b5..467b50e 100644 --- a/src/next/jdbc/connection.clj +++ b/src/next/jdbc/connection.clj @@ -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 "Standard implementations of `get-datasource` and `get-connection`. @@ -374,9 +374,12 @@ (defn- as-properties "Convert any seq of pairs to a `java.util.Properties` instance." ^Properties [m] - (let [p (Properties.)] - (doseq [[k v] m] - (.setProperty p (name k) (str v))) + (let [p (Properties.) + as-is (set (:next.jdbc/as-is-properties m))] + (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)) (defn uri->db-spec diff --git a/test/next/jdbc/connection_string_test.clj b/test/next/jdbc/connection_string_test.clj index 66ed1d8..aa3767b 100644 --- a/test/next/jdbc/connection_string_test.clj +++ b/test/next/jdbc/connection_string_test.clj @@ -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 "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.protocols :as p] [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) @@ -39,3 +40,16 @@ (when (and user password) (with-open [con (p/get-connection ds {})] (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"))))