1

1) I have a luminus app. I want to execute multiple db requests within a single db connection, meaning, without having to open a connection a second time. Now I have this:

(defn page1 [id]
  (layout/render "page1.html"
    (my-var1 (db/get-single-article {:id (Integer/parseInt id)}))))

I want to execute something else, say, db/get-something-else, within the same db connection where db/get-single-article is executed. How?

2) In resources/sql/queries.sql I have this:

-- :name get-single-article :? :1
-- :doc retrieve an article given the id.
SELECT * FROM article
WHERE id = :id

How can I add one more query to this so that it'll execute within db/get-single-article call and return a different result set? Like this:

-- :name get-single-article :? :1
-- :doc retrieve an article given the id.
SELECT * FROM article
WHERE id = :id

select * from another_table
where ...

How can I navigate them those when calling db/get-single-article then?

1 Answers1

1

Go ahead and define a second query in your SQL file:

-- :name get-something-else :? :*
select * from another_table where ...

Then assign the query results for both queries with something like this:

(defn page1 [id]
  (layout/render "page1.html"
    {:my-var1 (db/get-single-article {:id (Integer/parseInt id)})
     :my-var2 (db/get-something-else)}))
Curtis Summers
  • 586
  • 5
  • 7
  • but that'll be 2 different queries. I'd like 1 also which returns 2 result sets. –  May 25 '16 at 12:27
  • That's just not how it works. One query = one result set. If you want to create a separate wrapper function that calls both db functions and returns them you can. – Curtis Summers May 25 '16 at 12:33
  • JDBC. Databases in general. Is there something that you are comparing this behavior to that is causing confusion? – Curtis Summers May 25 '16 at 12:47
  • Everything else. All databases support that. So why isn't it supported here, are you sure? –  May 25 '16 at 13:03
  • You're welcome to dig deep into the internals of various JDBC drivers where you will find that multiple result set support with `.getMoreResults` is often not supported or has caveats. For a very few databases, like MS SQL Server, you may be interested in [this example retrieving multiple result sets with clojure.java.jdbc](https://gist.github.com/codelahoma/5716957) – Curtis Summers May 25 '16 at 13:13