开发者

Clojure contrib sql makes all numbers a BigDecimal

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


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))))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜