Back
as-unqualified-lower-maps (clj)
(source)function
(as-unqualified-lower-maps rs opts)
Given a `ResultSet` and options, return a `RowBuilder` / `ResultSetBuilder`
that produces bare vectors of hash map rows, with simple, lower-case keys.
Examples
next-jdbc
What's left to be tested:
* ReadableColumn protocol extension point"
(:require [clojure.core.protocols :as core-p]
[clojure.datafy :as d]
[clojure.string :as str]
[clojure.test :refer [deftest is testing use-fixtures]]
[next.jdbc.protocols :as p]
[next.jdbc.result-set :as rs]
[next.jdbc.specs :as specs]
[next.jdbc.test-fixtures :refer [with-test-db ds column
default-options
derby? mssql? mysql? postgres?]])
(:import (java.sql ResultSet ResultSetMetaData)))
(deftest test-map-row-builder
(testing "default row builder"
(let [row (p/-execute-one (ds)
["select * from fruit where id = ?" 1]
(default-options))]
(is (map? row))
(is (contains? row (column :FRUIT/GRADE)))
(is (nil? ((column :FRUIT/GRADE) row)))
(is (= 1 ((column :FRUIT/ID) row)))
(is (= "Apple" ((column :FRUIT/NAME) row))))
(let [rs (p/-execute-all (ds)
["select * from fruit order by id"]
(default-options))]
(is (every? map? rs))
(is (= 1 ((column :FRUIT/ID) (first rs))))
(is (= "Apple" ((column :FRUIT/NAME) (first rs))))
(is (= 4 ((column :FRUIT/ID) (last rs))))
(is (= "Orange" ((column :FRUIT/NAME) (last rs))))))
(testing "unqualified row builder"
(let [row (p/-execute-one (ds)
["select * from fruit where id = ?" 2]
{:builder-fn rs/as-unqualified-maps})]
(is (map? row))
(is (contains? row (column :COST)))
(is (nil? ((column :COST) row)))
(is (= 2 ((column :ID) row)))
(is (= "Banana" ((column :NAME) row)))))
(testing "lower-case row builder"
(let [row (p/-execute-one (ds)
["select * from fruit where id = ?" 3]
(assoc (default-options)
:builder-fn rs/as-lower-maps))]
(is (map? row))
(is (contains? row :fruit/appearance))
(is (nil? (:fruit/appearance row)))
(is (= 3 (:fruit/id row)))
(is (= "Peach" (:fruit/name row)))))
(testing "unqualified lower-case row builder"
(let [row (p/-execute-one (ds)
["select * from fruit where id = ?" 4]
{:builder-fn rs/as-unqualified-lower-maps})]
(is (map? row))
(is (= 4 (:id row)))
(is (= "Orange" (:name row)))))
(testing "kebab-case row builder"
(let [row (p/-execute-one (ds)
["select id,name,appearance as looks_like from fruit where id = ?" 3]
(assoc (default-options)
:builder-fn rs/as-kebab-maps))]
(is (map? row))
(is (contains? row :fruit/looks-like))
(is (nil? (:fruit/looks-like row)))
(is (= 3 (:fruit/id row)))
(is (= "Peach" (:fruit/name row)))))
(testing "unqualified kebab-case row builder"
(let [row (p/-execute-one (ds)
["select id,name,appearance as looks_like from fruit where id = ?" 4]
{:builder-fn rs/as-unqualified-kebab-maps})]
(is (map? row))
(is (contains? row :looks-like))
(is (= "juicy" (:looks-like row)))
(is (= 4 (:id row)))
(is (= "Orange" (:name row)))))
(testing "custom row builder 1"
(let [row (p/-execute-one (ds)
["select fruit.*, id + 100 as newid from fruit where id = ?" 3]
(assoc (default-options)
:builder-fn rs/as-modified-maps
:label-fn str/lower-case
:qualifier-fn identity))]
(is (map? row))
(is (contains? row (column :FRUIT/appearance)))
(is (nil? ((column :FRUIT/appearance) row)))
(is (= 3 ((column :FRUIT/id) row)))
(is (= 103 (:newid row))) ; no table name here
(is (= "Peach" ((column :FRUIT/name) row)))))
(testing "custom row builder 2"
(let [row (p/-execute-one (ds)
["select fruit.*, id + 100 as newid from fruit where id = ?" 3]
(assoc (default-options)
:builder-fn rs/as-modified-maps
:label-fn str/lower-case
:qualifier-fn (constantly "vegetable")))]
(is (map? row))
(is (contains? row :vegetable/appearance))
(is (nil? (:vegetable/appearance row)))
(is (= 3 (:vegetable/id row)))
(is (= 103 (:vegetable/newid row))) ; constant qualifier here
(is (= "Peach" (:vegetable/name row)))))
(testing "adapted row builder"
(let [row (p/-execute-one (ds)
["select * from fruit where id = ?" 3]
(assoc
(default-options)
:builder-fn (rs/as-maps-adapter
rs/as-modified-maps
(fn [^ResultSet rs
^ResultSetMetaData rsmeta
^Integer i]
(condp = (.getColumnType rsmeta i)
java.sql.Types/VARCHAR
(.getString rs i)
java.sql.Types/INTEGER
(.getLong rs i)
(.getObject rs i))))
:label-fn str/lower-case
:qualifier-fn identity))]
(is (map? row))
(is (contains? row (column :FRUIT/appearance)))
(is (nil? ((column :FRUIT/appearance) row)))
(is (= 3 ((column :FRUIT/id) row)))
(is (= "Peach" ((column :FRUIT/name) row))))
(let [builder (rs/as-maps-adapter
rs/as-modified-maps
(fn [^ResultSet rs _ ^Integer i]
(.getObject rs i)))
row (p/-execute-one (ds)
["select * from fruit where id = ?" 3]
(assoc
(default-options)
:builder-fn (rs/as-maps-adapter
builder
(fn [^ResultSet rs
^ResultSetMetaData rsmeta
^Integer i]
(condp = (.getColumnType rsmeta i)
java.sql.Types/VARCHAR
(.getString rs i)
java.sql.Types/INTEGER
(.getLong rs i)
(.getObject rs i))))
:label-fn str/lower-case
:qualifier-fn identity))]
(is (map? row))
(is (contains? row (column :FRUIT/appearance)))
(is (nil? ((column :FRUIT/appearance) row)))
(is (= 3 ((column :FRUIT/id) row)))
(is (= "Peach" ((column :FRUIT/name) row))))))
(deftest clob-reading
(when-not (or (mssql?) (mysql?) (postgres?)) ; no clob in these
(with-open [con (p/get-connection (ds) {})]
(try
(p/-execute-one con ["DROP TABLE CLOBBER"] {})
(catch Exception _))
(p/-execute-one con [(str "
CREATE TABLE CLOBBER (
ID INTEGER,
STUFF CLOB
)")]
{})
(p/-execute-one con
[(str "insert into clobber (id, stuff)"
"values (?,?), (?,?)")
1 "This is some long string"
2 "This is another long string"]
{})
(is (= "This is some long string"
(-> (p/-execute-all con
["select * from clobber where id = ?" 1]
{:builder-fn (rs/as-maps-adapter
rs/as-unqualified-lower-maps
rs/clob-column-reader)})
(first)
:stuff))))))
TechEmpower/FrameworkBenchmarks
(ns io.github.kit-clj.te-bench.cache.inmem
(:require
[clojure.core.cache :as cache]
[integrant.core :as ig]
[next.jdbc :as jdbc]
[next.jdbc.result-set :as rs]))
(defmethod ig/init-key :cache/inmem
[_ {:keys [db-conn threshold]}]
(cache/fifo-cache-factory
(reduce
(fn [out {:keys [id] :as obj}]
(assoc out id obj))
{}
(jdbc/execute! db-conn ["select * from \"World\""] {:builder-fn rs/as-unqualified-lower-maps}))
{:threshold threshold}))
xtdb/xtdb
(ns xtdb.jdbc-test
(:require [clojure.string :as str]
[clojure.test :as t]
[clojure.tools.logging :as log]
[xtdb.api :as xt]
[xtdb.codec :as c]
[xtdb.db :as db]
[xtdb.fixtures :as fix :refer [*api*]]
[xtdb.fixtures.jdbc :as fj]
[xtdb.fixtures.lubm :as fl]
[juxt.clojars-mirrors.nextjdbc.v1v2v674.next.jdbc :as jdbc]
[juxt.clojars-mirrors.nextjdbc.v1v2v674.next.jdbc.result-set :as jdbcr]
[xtdb.jdbc :as j])
(:import (java.util UUID)))
(t/deftest test-ingest-bench
(when (Boolean/parseBoolean (System/getenv "XTDB_JDBC_PERFORMANCE"))
(fl/with-lubm-data
#(t/is (= 1650
(:num_docs (jdbc/execute-one! (:pool (:tx-log *api*))
["SELECT count(EVENT_KEY) AS num_docs FROM tx_events WHERE TOPIC = 'docs'"]
{:builder-fn jdbcr/as-unqualified-lower-maps}))))))
(t/is true))
jimmyhmiller/PlayGround
(ns sql-playground.core
(:require [next.jdbc :as jdbc]
[honey.sql :as sql]
[dotenv :as env]
[next.jdbc.result-set :as rs])
(:import [org.postgresql Driver]))
(jdbc/execute-one!
db
(sql/format {:select [:*]
:from [:calories]})
{:builder-fn rs/as-unqualified-lower-maps})
(jdbc/execute-one!
db
(sql/format {:select [[[:sum :calories] :total] ]
:from [:calories]})
{:builder-fn rs/as-unqualified-lower-maps})
(jdbc/execute-one!
db
(sql/format {:select [[[:count [:distinct [[:date_trunc [:inline "day"] :timestamp]]]] :days]]
:from [:calories]})
{:builder-fn rs/as-unqualified-lower-maps})
(jdbc/execute-one!
db
(sql/format {:select [[[:count [:distinct [:date_trunc [:inline "week"] :timestamp]]] :weeks]]
:from [:calories]})
{:builder-fn rs/as-unqualified-lower-maps})
(jdbc/execute-one!
db
(sql/format {:select [[[:sum :calories] :total]]
:from [:calories]
:where [[:= [:date_trunc [:inline "day"] :timestamp]
[:cast [:param :timestamp] :timestamptz]]]} {:params {:timestamp "2022-11-23"}})
{:builder-fn rs/as-unqualified-lower-maps})