views:

74

answers:

1

The clojure.contrib.sql library returns BigDecimals for all numeric fields. What's a good way to have some fields as Integers? Example code below:

(sql/with-connection my-db 
   (sql/with-query-results res 
      [sql-str 6722] 
      (into [] res)))

In the resulting collection of records all numbers are BigDecimal. Some of these are foreign keys, and for reasons of my own, I need them to be integer.

I know I can iterate over the collection and convert them, but I would rather not do this as it is a very large collection, and it seems right to have the library use ResultsSet.getInteger if the number fits into an integer.

The DB is Oracle, and the integer DB fields are defined as NUMBER(10)

Thanks

+2  A: 

As atreyu noted, a 10-digit integer won't necessarily fit within an Integer.

More importantly, the seq you are given is created by clojure.core/resultset-seq, which in turn is calling ResultSet.getObject(int). As per the JDBC spec, the BigDecimals are being returned because that's the java type that corresponds to the column's SQL type.

Also, you don't need to worry about "iterat[ing] over the collection". The resultset-seq is lazy, and map is lazy, so you'll just end up converting each number right before you consume them. E.g.,

(sql/with-connection my-db 
   (sql/with-query-results res 
      [sql-str 6722] 
      (do-stuff 
         (map (comp int :id) res))))
Alex Taggart