Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing through JDBC options. #189

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
20 changes: 20 additions & 0 deletions src/korma/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,26 @@
[s]
(utils/generated s))

(defn fetch-size
"Sets the JDBC fetch size for this query, i.e. the number of rows pulled back
with each round trip to the server. Fetch size defaults to 10 on most drivers.
This option will not affect which rows are returned, but if you are running
big queries then turning fetch size up (e.g. to 1000) can make them finish much faster.

(select users
(fetch-size 1000))"
[query fs]
(assoc-in query [:jdbc-options :fetch-size] fs))

(defn max-rows
"Sets the JDBC max rows option. Useful as an alternative to (limit) for DBMSs that do
not support LIMIT (e.g. Oracle).

(select users
(max-rows 10))"
[query mr]
(assoc-in query [:jdbc-options :max-rows] mr))

;;*****************************************************
;; Query exec
;;*****************************************************
Expand Down
10 changes: 7 additions & 3 deletions src/korma/db.clj
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,15 @@
(jdbc/print-sql-exception e)))
(throw e))

(defn- exec-sql [{:keys [results sql-str params]}]
(defn- exec-sql [{:keys [results sql-str params jdbc-options]}]
(try
(case results
:results (jdbc/with-query-results rs (apply vector sql-str params)
(vec rs))
:results (jdbc/with-query-results
rs
(if jdbc-options
(apply vector jdbc-options sql-str params)
(apply vector sql-str params))
(vec rs))
:keys (jdbc/do-prepared-return-keys sql-str params)
(jdbc/do-prepared sql-str params))
(catch Exception e
Expand Down
8 changes: 8 additions & 0 deletions test/korma/test/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,14 @@
(select user2))))
(set-delimiters "\""))

(deftest jdbc-options
(is (= {:fetch-size 1000}
(:jdbc-options (query-only (select users (fetch-size 1000))))))
(is (= {:max-rows 10}
(:jdbc-options (query-only (select users (max-rows 10))))))
(is (= {:fetch-size 1 :max-rows 2}
(:jdbc-options (query-only (select users (fetch-size 1) (max-rows 2)))))))

(deftest naming-delim-options
(sql-only
(is (= "SELECT DELIMS.* FROM DELIMS"
Expand Down