From f5ddaeed932777ec1e5636c1222881ba358728ca Mon Sep 17 00:00:00 2001 From: James Ryan Carr Date: Fri, 22 Nov 2013 11:35:36 -0500 Subject: [PATCH] Added fetch-size and max-rows functions. --- src/korma/core.clj | 20 ++++++++++++++++++++ src/korma/db.clj | 10 +++++++--- test/korma/test/core.clj | 8 ++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/korma/core.clj b/src/korma/core.clj index a4bc8f2..d5ceaca 100644 --- a/src/korma/core.clj +++ b/src/korma/core.clj @@ -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 ;;***************************************************** diff --git a/src/korma/db.clj b/src/korma/db.clj index 49ea6aa..e7728d1 100644 --- a/src/korma/db.clj +++ b/src/korma/db.clj @@ -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 diff --git a/test/korma/test/core.clj b/test/korma/test/core.clj index 2ae0262..0446fa8 100644 --- a/test/korma/test/core.clj +++ b/test/korma/test/core.clj @@ -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"