java.jdbc - (sql/where ...) with multiple values (i.e. 'x in (1,2,3')

276 views
Skip to first unread message

Colin Yates

unread,
Jul 8, 2013, 5:28:07 PM7/8/13
to clo...@googlegroups.com
Using the latest release of java.jdbc, does anybody know how I can construct a where clause when I want to check if the value is one of many values?

For example, if I have a filter {:age [1 2 3 4]} then (sql/where filter) causes an error: "Wrong data type: java.lang.NumberFormatException: For input string: "[1 2 3 4]".  

Any idea how to do this in clojure.java.jdbc?  

Jeremy Heiler

unread,
Jul 8, 2013, 6:47:49 PM7/8/13
to clo...@googlegroups.com

It doesn't look like `where` supports OR or IN at the moment.

https://github.com/clojure/java.jdbc/blob/f9ecadd03c1c2e01f107155b03061ac0b20f976c/src/main/clojure/clojure/java/jdbc/sql.clj#L292

Perhaps you can look at honeysql for a more sophisticated DSL?

https://github.com/jkk/honeysql

James Ferguson

unread,
Jul 8, 2013, 8:55:54 PM7/8/13
to clo...@googlegroups.com
I've been using http://sqlkorma.com/ for CRUD. A query with your filter would be

(select tablename
  (where {:age [in [1 2 3 4]]}))

Colin Yates

unread,
Jul 9, 2013, 3:47:58 AM7/9/13
to clo...@googlegroups.com
Thanks both.


--
--
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
---
You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/7A_HAAISvEk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Colin Yates

unread,
Jul 9, 2013, 5:02:49 AM7/9/13
to clo...@googlegroups.com
Just an idle musing - I think DSLs (in general) are really powerful when you can declare them in your application.  However, whenever I have to programatically construct them (constructing them based on JSON from the UI for example) I find it is often easier to just use build up the SQL directly.  

Given Clojure's excellent 'structural manipulation' and 'pretty much everything is structure' capabilities maybe that is no longer the case.  The transformation from JSON to sqlkorma for example will be almost entirely be structural changes. In other languages you don't have that, you tend to need to build up state through method calls. 

End of musing.

Sean Corfield

unread,
Jul 9, 2013, 1:30:36 PM7/9/13
to clo...@googlegroups.com
clojure.java.jdbc.sql is a deliberately minimal DSL - Justin Kramer's
HoneySQL is what I recommend for more expressive SQL construction
(that's the "official" recommendation based on discussions Justin and
I had about java.jdbc and HoneySQL at Clojure/conj 2012).

Sean
> --
> --
> 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
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
--
Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
World Singles, LLC. -- http://worldsingles.com/

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

Max Penet

unread,
Jul 11, 2013, 6:22:42 AM7/11/13
to clo...@googlegroups.com
Honeysql is nice but it only supports select statements FYI.

That said it's probably easy to extend, but my point is that it's far from complete, so I am not sure it's a good recommendation at the moment.

Reply all
Reply to author
Forward
0 new messages