I've been trying to do the following in Korma to no avail:
sql:
PREPARE q (int) AS SELECT * FROM post a
WHERE EXISTS
(SELECT parent_id
FROM post_map
WHERE parent_id=a.id AND parent_id=$1);
EXECUTE q(1);
my best Korma attempt:
(defn children [parent-id] ;; clojure
(if (number? parent-id)
(exec-raw (str
"PREPARE q (int) AS SELECT * FROM post a WHERE EXISTS
(SELECT parent_id FROM post_map WHERE parent_id=a.id AND parent_id=$1);
EXECUTE q(" parent-id ")")
:results)))
And this is the error I keep getting: (I don't really understand the :: operator below:)
Failure to execute query with SQL:
PREPARE q (int) AS SELECT * FROM post a WHERE EXISTS
(SELECT parent_id FROM post_map WHERE parent_id=a.id AND parent_id=$1);
EXECUTE q(1) :: nil
PSQLException:
Message: No results were returned by the query.
SQLState: 02000
Error Code: 0
PSQLException No results were returned by the query. org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery (AbstractJdbc2Statement.java:274)
I don't think this is a terribly outlandish thing to want to do with a query, so I'm wondering if Korma just isn't going to work for my project. Am I just doing it wrong?
UPDATE: this is what I ended up doing (after I bailed on Korma [sorry Korma]).
(defn children [parent-id]
(if (unsigned? parent-id)
(sql/with-connection db
(sql/with-query-results results
[(str "select " field-list ", b.parent_id from post a, post_map b where a.id=b.child_id and a.id in "
"(select child_id from post c, post_map d where c.id=d.parent_id and c.id=?)") parent-id]
(into [] results)))))