Use high-level API for CLOB tests
This commit is contained in:
parent
90f27cab3d
commit
6482d38960
2 changed files with 38 additions and 39 deletions
|
|
@ -7,13 +7,12 @@
|
|||
* ReadableColumn protocol extension point"
|
||||
(:require [clojure.core.protocols :as core-p]
|
||||
[clojure.datafy :as d]
|
||||
[clojure.java.io :as io]
|
||||
[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.test-fixtures :refer [with-test-db ds postgres?]])
|
||||
(:import (java.sql Clob ResultSet ResultSetMetaData)))
|
||||
(:import (java.sql ResultSet ResultSetMetaData)))
|
||||
|
||||
(set! *warn-on-reflection* true)
|
||||
|
||||
|
|
@ -290,38 +289,3 @@
|
|||
(= "fruit" (-> % val name str/lower-case)))
|
||||
row))
|
||||
metadata))))
|
||||
|
||||
(defn- clob-reader
|
||||
[^ResultSet rs ^ResultSetMetaData _ ^Integer i]
|
||||
(let [obj (.getObject rs i)]
|
||||
(cond (instance? Clob obj)
|
||||
(with-open [rdr (io/reader (.getCharacterStream ^Clob obj))]
|
||||
(slurp rdr))
|
||||
:default
|
||||
obj)))
|
||||
|
||||
(deftest clob-reading
|
||||
(when-not (postgres?)
|
||||
(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"
|
||||
(:stuff
|
||||
(p/-execute-one con
|
||||
["select * from clobber where id = ?" 1]
|
||||
{:builder-fn (rs/as-maps-adapter
|
||||
rs/as-unqualified-lower-maps
|
||||
clob-reader)})))))))
|
||||
|
|
|
|||
|
|
@ -2,7 +2,8 @@
|
|||
|
||||
(ns next.jdbc-test
|
||||
"Not exactly a test suite -- more a series of examples."
|
||||
(:require [clojure.string :as str]
|
||||
(:require [clojure.java.io :as io]
|
||||
[clojure.string :as str]
|
||||
[clojure.test :refer [deftest is testing use-fixtures]]
|
||||
[next.jdbc :as jdbc]
|
||||
[next.jdbc.connection :as c]
|
||||
|
|
@ -11,7 +12,7 @@
|
|||
[next.jdbc.prepare :as prep]
|
||||
[next.jdbc.result-set :as rs]
|
||||
[next.jdbc.specs :as specs])
|
||||
(:import (java.sql ResultSet ResultSetMetaData)))
|
||||
(:import (java.sql Clob ResultSet ResultSetMetaData)))
|
||||
|
||||
(set! *warn-on-reflection* true)
|
||||
|
||||
|
|
@ -212,3 +213,37 @@ VALUES ('Pear', 'green', 49, 47)
|
|||
(into [] (map pr-str) (jdbc/plan (ds) ["select * from fruit"]))))
|
||||
(is (thrown? IllegalArgumentException
|
||||
(doall (take 3 (jdbc/plan (ds) ["select * from fruit"]))))))
|
||||
|
||||
(defn- clob-reader
|
||||
[^ResultSet rs ^ResultSetMetaData _ ^Integer i]
|
||||
(let [obj (.getObject rs i)]
|
||||
(cond (instance? Clob obj)
|
||||
(with-open [rdr (io/reader (.getCharacterStream ^Clob obj))]
|
||||
(slurp rdr))
|
||||
:default
|
||||
obj)))
|
||||
|
||||
(deftest clob-reading
|
||||
(when-not (postgres?)
|
||||
(with-open [con (jdbc/get-connection (ds))]
|
||||
(try
|
||||
(jdbc/execute-one! con ["DROP TABLE CLOBBER"])
|
||||
(catch Exception _))
|
||||
(jdbc/execute-one! con [(str "
|
||||
CREATE TABLE CLOBBER (
|
||||
ID INTEGER,
|
||||
STUFF CLOB
|
||||
)")])
|
||||
(jdbc/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"
|
||||
(-> (jdbc/execute! con
|
||||
["select * from clobber where id = ?" 1]
|
||||
{:builder-fn (rs/as-maps-adapter
|
||||
rs/as-unqualified-lower-maps
|
||||
clob-reader)})
|
||||
(first)
|
||||
:stuff))))))
|
||||
|
|
|
|||
Loading…
Reference in a new issue