Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions src/korma/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

(def ^:dynamic *current-db* nil)
(def ^:dynamic *current-conn* nil)
(def ^:dynamic *options* nil)

(defn- ->delimiters [delimiters]
(if delimiters
Expand Down Expand Up @@ -288,12 +289,13 @@
(jdbc/db-is-rollback-only *current-conn*))

(defn- exec-sql [{:keys [results sql-str params]}]
(let [keys (get-in *current-db* [:options :naming :keys])
(let [keys (get-in *current-db* [:options :naming :keys])
sql-params (apply vector sql-str params)]
(case results
:results (jdbc/query *current-conn* sql-params {:identifiers keys})
:keys (jdbc/db-do-prepared-return-keys *current-conn* sql-params)
(jdbc/db-do-prepared *current-conn* sql-params))))
:results (jdbc/query *current-conn* sql-params (merge {:identifiers keys}
*options*))
:keys (jdbc/db-do-prepared-return-keys *current-conn* true sql-params *options*)
(jdbc/db-do-prepared *current-conn* true sql-params *options*))))

(defmacro with-db
"Execute all queries within the body using the given db spec"
Expand All @@ -308,3 +310,9 @@
(exec-sql query)
(with-db (or db @_default)
(exec-sql query))))

(defmacro with-options
"Add options to the statments. For example (with-options {:fetch-size 1000} ...)"
[options & body]
`(binding [*options* ~options]
~@body))