From e9c75a7afd2c423a685b4242898d5d7e791e082a Mon Sep 17 00:00:00 2001 From: Sean Corfield Date: Wed, 5 Aug 2020 18:18:30 -0700 Subject: [PATCH] Prep 1.1.582 -- fixes #138 --- CHANGELOG.md | 3 +++ README.md | 4 ++-- doc/getting-started.md | 6 +++--- pom.xml | 4 ++-- src/next/jdbc/connection.clj | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 45 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e057246..ac4bbdf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ Only accretive/fixative changes will be made from now on. ## Stable Builds +* 2020-08-05 -- 1.1.582 + * Fix #138 by exposing `next.jdbc.connection/jdbc-url` to build `:jdbcUrl` values that can be passed to `->pool` or `component`. + * 2020-08-03 -- 1.1.581 * 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. diff --git a/README.md b/README.md index 596c682..b196d18 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ The next generation of `clojure.java.jdbc`: a new low-level Clojure wrapper for The latest versions on Clojars and on cljdoc: -[![Clojars Project](https://clojars.org/seancorfield/next.jdbc/latest-version.svg)](https://clojars.org/seancorfield/next.jdbc) [![cljdoc badge](https://cljdoc.org/badge/seancorfield/next.jdbc?1.1.581)](https://cljdoc.org/d/seancorfield/next.jdbc/CURRENT) +[![Clojars Project](https://clojars.org/seancorfield/next.jdbc/latest-version.svg)](https://clojars.org/seancorfield/next.jdbc) [![cljdoc badge](https://cljdoc.org/badge/seancorfield/next.jdbc?1.1.582)](https://cljdoc.org/d/seancorfield/next.jdbc/CURRENT) The documentation on [cljdoc.org](https://cljdoc.org/d/seancorfield/next.jdbc/CURRENT) is for the current version of `next.jdbc`: @@ -14,7 +14,7 @@ The documentation on [cljdoc.org](https://cljdoc.org/d/seancorfield/next.jdbc/CU * [Migrating from `clojure.java.jdbc`](https://cljdoc.org/d/seancorfield/next.jdbc/CURRENT/doc/migration-from-clojure-java-jdbc) * Feedback via [issues](https://github.com/seancorfield/next-jdbc/issues) or in the [`#sql` channel on the Clojurians Slack](https://clojurians.slack.com/messages/C1Q164V29/details/) or the [`#sql` stream on the Clojurians Zulip](https://clojurians.zulipchat.com/#narrow/stream/152063-sql). -The documentation on GitHub is for **develop** since the 1.1.581 release -- [see the CHANGELOG](https://github.com/seancorfield/next-jdbc/blob/develop/CHANGELOG.md) and then read the [corresponding updated documentation](https://github.com/seancorfield/next-jdbc/tree/develop/doc) on GitHub if you want. +The documentation on GitHub is for **develop** since the 1.1.582 release -- [see the CHANGELOG](https://github.com/seancorfield/next-jdbc/blob/develop/CHANGELOG.md) and then read the [corresponding updated documentation](https://github.com/seancorfield/next-jdbc/tree/develop/doc) on GitHub if you want. This project follows the version scheme MAJOR.MINOR.COMMITS where MAJOR and MINOR provide some relative indication of the size of the change, but do not follow semantic versioning. In general, all changes endeavor to be non-breaking (by moving to new names rather than by breaking existing names). COMMITS is an ever-increasing counter of commits since the beginning of this repository. diff --git a/doc/getting-started.md b/doc/getting-started.md index addc466..67b0b07 100644 --- a/doc/getting-started.md +++ b/doc/getting-started.md @@ -9,12 +9,12 @@ It is designed to work with Clojure 1.10 or later, supports `datafy`/`nav`, and You can add `next.jdbc` to your project with either: ```clojure -seancorfield/next.jdbc {:mvn/version "1.1.581"} +seancorfield/next.jdbc {:mvn/version "1.1.582"} ``` for `deps.edn` or: ```clojure -[seancorfield/next.jdbc "1.1.581"] +[seancorfield/next.jdbc "1.1.582"] ``` for `project.clj` or `build.boot`. @@ -29,7 +29,7 @@ For the examples in this documentation, we will use a local H2 database on disk, ```clojure ;; deps.edn {:deps {org.clojure/clojure {:mvn/version "1.10.1"} - seancorfield/next.jdbc {:mvn/version "1.1.581"} + seancorfield/next.jdbc {:mvn/version "1.1.582"} com.h2database/h2 {:mvn/version "1.4.199"}}} ``` diff --git a/pom.xml b/pom.xml index 7c48d8a..7f8bbda 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 seancorfield next.jdbc - 1.1.581 + 1.1.582 next.jdbc The next generation of clojure.java.jdbc: a new low-level Clojure wrapper for JDBC-based access to databases. https://github.com/seancorfield/next-jdbc @@ -22,7 +22,7 @@ https://github.com/seancorfield/next-jdbc scm:git:git://github.com/seancorfield/next-jdbc.git scm:git:ssh://git@github.com/seancorfield/next-jdbc.git - v1.1.581 + v1.1.582 diff --git a/src/next/jdbc/connection.clj b/src/next/jdbc/connection.clj index 6cf7f62..723381a 100644 --- a/src/next/jdbc/connection.clj +++ b/src/next/jdbc/connection.clj @@ -7,6 +7,7 @@ the `->pool` and `component` functions for creating pooled datasource objects." (:require [clojure.java.data :as j] + [clojure.string :as str] [next.jdbc.protocols :as p]) (:import (java.sql Connection DriverManager) (javax.sql DataSource) @@ -207,6 +208,40 @@ db-spec))) [url etc])))) +(defn jdbc-url + "Given a database spec (as a hash map), return a JDBC URL with all the + attributes added to the query string. The result is suitable for use in + calls to `->pool` and `component` as the `:jdbcUrl` key in the parameter + map for the connection pooling library. + + This allows you to build a connection-pooled datasource that needs + additional settings that the pooling library does not support, such as + `:serverTimezone`: + +```clojure + (def db-spec {:dbtype .. :dbname .. :user .. :password .. + :serverTimezone \"UTC\"}) + (def ds (next.jdbc.connection/->pool + HikariCP {:jdbcUrl (next.jdbc.connection/jdbc-url db-spec) + :maximumPoolSize 15})) +``` + + This also clearly separates the attributes that should be part of the + JDBC URL from the attributes that should be configured on the pool. + + Since JDBC drivers can handle URL encoding differently, if you are + trying to pass attributes that might need encoding, you should make + sure they are properly URL-encoded as values in the database spec hash map. + This function does **not** attempt to URL-encode values for you!" + [db-spec] + (let [[url etc] (spec->url+etc db-spec) + url-and (if (str/index-of url "?") "&" "?")] + (str url url-and (str/join "&" + (reduce-kv (fn [pairs k v] + (conj pairs (str (name k) "=" v))) + [] + etc))))) + (defn ->pool "Given a (connection pooled datasource) class and a database spec, return a connection pool object built from that class and the database spec.