clojure.java.jdbc: mapping BigDecimal to double

314 views
Skip to first unread message

HiHeelHottie

unread,
Aug 23, 2011, 9:16:57 PM8/23/11
to Clojure

Hi,

It looks like Oracle NUMBER types get mapped to BigDecimal in a result
seq from clojure.java.jdbc. Is there an easy way to configure
clojure.java.jdbc/ResultSet to map Oracle NUMBERS to doubles?

The resultset-seq from https://github.com/clojure/java.jdbc/blob/master/src/main/clojure/clojure/java/jdbc/internal.clj
is below.

(defn resultset-seq*
"Creates and returns a lazy sequence of structmaps corresponding to
the rows in the java.sql.ResultSet rs. Based on clojure.core/
resultset-seq
but it respects the current naming strategy."
[^ResultSet rs]
(let [rsmeta (.getMetaData rs)
idxs (range 1 (inc (.getColumnCount rsmeta)))
keys (map (comp keyword *as-key*)
(map (fn [^Integer i] (.getColumnLabel rsmeta i))
idxs))
check-keys
(or (apply distinct? keys)
(throw (Exception. "ResultSet must have unique
column labels")))
row-struct (apply create-struct keys)
row-values (fn [] (map (fn [^Integer i] (.getObject rs i))
idxs))
rows (fn thisfn []
(when (.next rs)
(cons (apply struct row-struct (row-values)) (lazy-
seq (thisfn)))))]
(rows)))

Sean Corfield

unread,
Aug 23, 2011, 9:41:53 PM8/23/11
to clo...@googlegroups.com
No, you'd have to do it yourself. Since not all BigDecimal values
would fit correctly in double, it would be dangerous for resultset-seq
to do it.

I expect there are all sorts of JDBC data types that don't quite match
Clojure types but I don't think automatically mapping them would be a
good idea...

Sean

HiHeelHottie

unread,
Aug 23, 2011, 9:54:39 PM8/23/11
to Clojure

Hey Sean,

I really appreciate the quick response and your work with java.jdbc.
Completely agree with you that it shouldn't automatically map out of
the box. As a newbie to clojure and jdbc, do you have any advice on
how I can get into resultset-seq* to do the mapping? I think it would
be better not to have to map a BigDecimal to double after resultset-
seq* returns a row.

Are there any future plans to add a mapping api to resultset-seq or is
the pattern just to chain any custom mappings after resultset-seq?

Ken Wesson

unread,
Aug 23, 2011, 10:01:42 PM8/23/11
to clo...@googlegroups.com
On Tue, Aug 23, 2011 at 9:54 PM, HiHeelHottie <hiheel...@gmail.com> wrote:
> Are there any future plans to add a mapping api to resultset-seq or is
> the pattern just to chain any custom mappings after resultset-seq?

Is wrapping in (map double ...) too much typing? :)

--
Protege: What is this seething mass of parentheses?!
Master: Your father's Lisp REPL. This is the language of a true
hacker. Not as clumsy or random as C++; a language for a more
civilized age.

Sean Corfield

unread,
Aug 23, 2011, 10:03:20 PM8/23/11
to clo...@googlegroups.com
On Tue, Aug 23, 2011 at 6:54 PM, HiHeelHottie <hiheel...@gmail.com> wrote:
> Completely agree with you that it shouldn't automatically map out of
> the box. As a newbie to clojure and jdbc, do you have any advice on
> how I can get into resultset-seq* to do the mapping? I think it would
> be better not to have to map a BigDecimal to double after resultset-
> seq* returns a row.

Apply something like this to the result?

(map (fn [row] (into {} (map (fn [[k v]] [k (some-mapping v)]) row))) results)

> Are there any future plans to add a mapping api to resultset-seq or is
> the pattern just to chain any custom mappings after resultset-seq?

Currently no plans. I don't want to add the overhead of map-over-map
for all users when I suspect only a few would want / need such a
mapping.
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/
Railo Technologies, Inc. -- http://www.getrailo.com/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)

gaz jones

unread,
Aug 23, 2011, 10:05:16 PM8/23/11
to clo...@googlegroups.com
the oracle jdbc adapter returns a whole host of strange datatypes. for
instance, it returns bigdecimals for numbers you have mapped to be
numbers (with a precision, without a scale) in the table. it also
returns its own custom time classes. these generally have a toJdbc()
method to convert them to the 'expected' jdbc type.

a quick look under the covers of any other language's db libraries
(such as activerecord or sequel) shows a whole host of these annoying
conversions back and forth. to be honest, the lack of the conversion
in java.jdbc is great as i can decide what i want to convert / not
convert.

tldr oracle jdbc is shit :P

> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clo...@googlegroups.com
> Note that posts from new members are moderated - please be patient with your first post.
> To unsubscribe from this group, send email to
> clojure+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

Reply all
Reply to author
Forward
0 new messages