Fixes #137 by adding user/password arities to get-connection

Also calls `.getConnection` with user/password under the hood if those 
are present in the `opts` hash map.
This commit is contained in:
Sean Corfield 2020-07-31 11:56:22 -07:00
parent e006be8ac4
commit 049f34e311
3 changed files with 18 additions and 3 deletions

View file

@ -3,6 +3,7 @@
Only accretive/fixative changes will be made from now on. Only accretive/fixative changes will be made from now on.
Changes made to **develop** since the 1.1.569 release: 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. * 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!`. * Address #133 by adding `:return-generated-keys` as an option on `execute-batch!`.

View file

@ -144,13 +144,23 @@
and applies the `:auto-commit` and/or `:read-only` options, if provided. 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` 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 (^java.sql.Connection
[spec] [spec]
(p/get-connection spec {})) (p/get-connection spec {}))
(^java.sql.Connection (^java.sql.Connection
[spec opts] [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 (defn prepare
"Given a connection to a database, and a vector containing SQL and any "Given a connection to a database, and a vector containing SQL and any

View file

@ -341,7 +341,11 @@
via reflection, e.g., :autoCommit, :readOnly, :schema..." via reflection, e.g., :autoCommit, :readOnly, :schema..."
^Connection ^Connection
[^DataSource datasource opts] [^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: ;; fast, specific option handling:
(when (contains? opts :auto-commit) (when (contains? opts :auto-commit)
(.setAutoCommit connection (boolean (:auto-commit opts)))) (.setAutoCommit connection (boolean (:auto-commit opts))))