diff --git a/CHANGELOG.md b/CHANGELOG.md index 22fec55..253235e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Only accretive/fixative changes will be made from now on. Changes made to **develop** since the 1.1.569 release: +* Fix #137 by adding support for specifying username and password per-connection (if your datasource supports this). * Document SQLite handling of `bool` and `bit` columns in a new **Tips & Tricks** section, inspired by #134. * Address #133 by adding `:return-generated-keys` as an option on `execute-batch!`. diff --git a/src/next/jdbc.clj b/src/next/jdbc.clj index 20bd3f6..d4ad409 100644 --- a/src/next/jdbc.clj +++ b/src/next/jdbc.clj @@ -144,13 +144,23 @@ and applies the `:auto-commit` and/or `:read-only` options, if provided. If you call `get-connection` on anything else, it will call `get-datasource` - first to try to get a `DataSource`, and then call `get-connection` on that." + first to try to get a `DataSource`, and then call `get-connection` on that. + + If you want different per-connection username/password values, you can + either put `:user` and `:password` into the `opts` hash map or pass them + as positional arguments." (^java.sql.Connection [spec] (p/get-connection spec {})) (^java.sql.Connection [spec opts] - (p/get-connection spec opts))) + (p/get-connection spec opts)) + (^java.sql.Connection + [spec user password] + (p/get-connection spec {:user user :password password})) + (^java.sql.Connection + [spec user password opts] + (p/get-connection spec (assoc opts :user user :password password)))) (defn prepare "Given a connection to a database, and a vector containing SQL and any diff --git a/src/next/jdbc/connection.clj b/src/next/jdbc/connection.clj index 4884b6f..6cf7f62 100644 --- a/src/next/jdbc/connection.clj +++ b/src/next/jdbc/connection.clj @@ -341,7 +341,11 @@ via reflection, e.g., :autoCommit, :readOnly, :schema..." ^Connection [^DataSource datasource opts] - (let [^Connection connection (.getConnection datasource)] + (let [^Connection connection (if (and (:user opts) (:password opts)) + (.getConnection datasource + (:user opts) + (:password opts)) + (.getConnection datasource))] ;; fast, specific option handling: (when (contains? opts :auto-commit) (.setAutoCommit connection (boolean (:auto-commit opts))))