Skip to content

Commit

Permalink
Query database should take precedence over current connection
Browse files Browse the repository at this point in the history
  • Loading branch information
scttnlsn committed Jul 31, 2014
1 parent e2225bf commit c7cfb74
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/korma/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,15 @@
*current-conn* conn#]
~@body)))

(defn- use-current-conn? [db]
"Returns true if the current connection should be used
instead of the given db spec"
(and *current-conn*
(or (nil? db)
(identical? db *current-db*))))

(defn do-query [{:keys [db] :as query}]
(if *current-conn*
(if (use-current-conn? db)
(exec-sql query)
(with-db (or db @_default)
(exec-sql query))))
11 changes: 10 additions & 1 deletion test/korma/test/integration/per_query_database.clj
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,13 @@
(is (= [{:id 1 :name "Chris" :email [{:id 1 :user_id 1 :email_address "chris@email.com"}]}]
(select user
(database mem-db)
(with email)))))
(with email)))))

(deftest per-query-database-takes-precedence
(let [other-db (create-db (h2 {:db "mem:query_other_database"}))]
(is (= [{:id 1 :name "Chris" :email [{:id 1 :user_id 1 :email_address "chris@email.com"}]}]
(with-db other-db
(dorun (map exec-raw schema))
(select user
(database mem-db)
(with email)))))))

0 comments on commit c7cfb74

Please sign in to comment.