From 660469288696ee0e1604728c4b4caceae0188c4a Mon Sep 17 00:00:00 2001 From: Maxim Penzin Date: Fri, 8 May 2020 12:19:58 +0800 Subject: [PATCH 1/3] tips-and-tricks: SQL Array as Clojure vector example --- doc/tips-and-tricks.md | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/doc/tips-and-tricks.md b/doc/tips-and-tricks.md index 719d547..2555931 100644 --- a/doc/tips-and-tricks.md +++ b/doc/tips-and-tricks.md @@ -116,6 +116,40 @@ You can get PostgreSQL to stream very large result sets (when you are reducing o * `:auto-commit false` -- when opening the connection * `:fetch-size 4000, :concurrency :read-only, :cursors :close, :result-type :forward-only` -- when running `plan` (or when creating a `PreparedStatement`). +### Working with Arrays + +ResultSet protocol extension to read SQL arrays as Clojure vectors. + +```clojure +(import '[java.sql Array]) +(require '[next.jdbc.result-set :as rs]) + +(extend-protocol rs/ReadableColumn + Array + (read-column-by-label [^Array v _] (vec (.getArray v))) + (read-column-by-index [^Array v _ _] (vec (.getArray v)))) + +``` + +Insert and read vector example: + +```sql +create table example( + tags varchar[] +); +``` +```clojure + +(execute-one! db-spec + ["insert into example(tags) values (?)" + (into-array String ["tag1" "tag2"])) + +(execute-one! db-spec + ["select * from example limit 1"]) + +;; => #:example{:tags ["tag1" "tag2"]} +``` + ### Working with Date and Time By default, PostgreSQL's JDBC driver does not always perform conversions from `java.util.Date` to a SQL data type. From 87d051fb564fb5367e7c765cbd4802ee7c63de63 Mon Sep 17 00:00:00 2001 From: Maxim Penzin Date: Fri, 8 May 2020 12:27:15 +0800 Subject: [PATCH 2/3] link to jdbc docs --- doc/tips-and-tricks.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/tips-and-tricks.md b/doc/tips-and-tricks.md index 2555931..5b223e0 100644 --- a/doc/tips-and-tricks.md +++ b/doc/tips-and-tricks.md @@ -150,6 +150,9 @@ create table example( ;; => #:example{:tags ["tag1" "tag2"]} ``` +Note: PostgreSQL JDBC driver supports only 7 primitive array types, but not such as UUID[] - +[PostgreSQL™ Extensions to the JDBC API](https://jdbc.postgresql.org/documentation/head/arrays.html). + ### Working with Date and Time By default, PostgreSQL's JDBC driver does not always perform conversions from `java.util.Date` to a SQL data type. From 63331e37425775cb73d784a9f106915bcb93f071 Mon Sep 17 00:00:00 2001 From: Maxim Penzin Date: Fri, 8 May 2020 12:32:21 +0800 Subject: [PATCH 3/3] link to jdbc docs --- doc/tips-and-tricks.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/tips-and-tricks.md b/doc/tips-and-tricks.md index 5b223e0..5b8b08a 100644 --- a/doc/tips-and-tricks.md +++ b/doc/tips-and-tricks.md @@ -150,7 +150,7 @@ create table example( ;; => #:example{:tags ["tag1" "tag2"]} ``` -Note: PostgreSQL JDBC driver supports only 7 primitive array types, but not such as UUID[] - +Note: PostgreSQL JDBC driver supports only 7 primitive array types, but not such as `UUID[]` - [PostgreSQL™ Extensions to the JDBC API](https://jdbc.postgresql.org/documentation/head/arrays.html). ### Working with Date and Time