Skip to content

Instantly share code, notes, and snippets.

@simonholgate
Created January 18, 2012 12:22
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonholgate/1632764 to your computer and use it in GitHub Desktop.
Save simonholgate/1632764 to your computer and use it in GitHub Desktop.
clojure.java.jdbc Oracle clob
(defn clob-to-string [clob]
"Turn an Oracle Clob into a String"
(with-open [rdr (java.io.BufferedReader. (.getCharacterStream clob))]
(apply str (line-seq rdr))))
(with-connection mydb
(transaction
(with-query-results rs ["select documentation from station"]
; rs will be a sequence of maps,
; one for each record in the result set.
(doseq [row rs] (println (clob-to-string (:documentation row)))))))
(comment "documentation" is stored as an Oracle clob in the table "station".
The above works in clojure.java.jdbc if I def mydb.
How do I use this with a connection defdb'd in korma?)
;; A partial solution:
(with-connection (get-connection mydb)
(transaction
(with-query-results rs ["select documentation from station"]
; rs will be a sequence of maps,
; one for each record in the result set.
(doseq [row rs] (println (clob-to-string (:documentation row)))))))
(comment is there a more elegant way to do this?)
@bowmanb
Copy link

bowmanb commented Sep 13, 2015

Thanks, clob-to-string works with SQL Server+jTDS too.

@seancorfield
Copy link

With the latest java.jdbc API (0.6.1 onwards) this whole example would become:

(defn clob-to-string [clob]
  "Turn an Oracle Clob into a String"
  (with-open [rdr (java.io.BufferedReader. (.getCharacterStream clob))]
    (apply str (line-seq rdr))))

(query mydb ["select documentation from station"]
       {:row-fn (comp clob-to-string :documentation)
        :result-set-fn #(doseq [doc %] (println doc))})

Or if you just wanted to expand the documentation column in the rows:

(let [rs (query mydb ["select * from station"]
                {:row-fn #(update % :documentation clob-to-string)})]
  ;; process the result set
  (doseq [row rs] (println (:documentation row))))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment