From 510efe892ec67975cb2e2cfe83b15c1e4dd31f86 Mon Sep 17 00:00:00 2001 From: Artem Chistyakov Date: Wed, 13 Aug 2014 23:51:57 -0400 Subject: [PATCH] =?UTF-8?q?Added=20support=20for=20MySQL=E2=80=99s=20LIMIT?= =?UTF-8?q?=20with=20offset=20argument.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/sqlingvo/compiler.clj | 6 ++++-- src/sqlingvo/core.clj | 6 ++++-- test/sqlingvo/core_test.clj | 10 ++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/sqlingvo/compiler.clj b/src/sqlingvo/compiler.clj index 721eb58..ab3330b 100644 --- a/src/sqlingvo/compiler.clj +++ b/src/sqlingvo/compiler.clj @@ -407,8 +407,10 @@ (defmethod compile-sql :keyword [db {:keys [form]}] [(sql-quote db form)]) -(defmethod compile-sql :limit [db {:keys [count]}] - (concat-sql "LIMIT " (if (number? count) (str count) "ALL"))) +(defmethod compile-sql :limit [db {:keys [offset count]}] + (if-let [args (->> [offset count] (filter number?) (map str) seq)] + (concat-sql "LIMIT " (join-sql ", " args)) + (concat-sql "LIMIT ALL"))) (defmethod compile-sql :like [db {:keys [excluding including table]}] (concat-sql diff --git a/src/sqlingvo/core.clj b/src/sqlingvo/core.clj index 55c42e3..2ade6f4 100644 --- a/src/sqlingvo/core.clj +++ b/src/sqlingvo/core.clj @@ -307,8 +307,10 @@ Examples: (defn limit "Returns a fn that adds a LIMIT clause to an SQL statement." - [count] - (assoc-op :limit :count count)) + ([count] + (assoc-op :limit :count count)) + ([offset count] + (assoc-op :limit :offset offset :count count))) (defn nulls "Parse `expr` and return an NULLS FIRST/LAST expr." diff --git a/test/sqlingvo/core_test.clj b/test/sqlingvo/core_test.clj index de0587b..964fc1f 100644 --- a/test/sqlingvo/core_test.clj +++ b/test/sqlingvo/core_test.clj @@ -629,6 +629,16 @@ (is (= [(parse-table :continents)] (:from stmt))) (is (= {:op :limit :count 10} (:limit stmt)))) +(deftest-stmt test-select-limit-with-mysql-offset + ["SELECT * FROM \"continents\" LIMIT 20, 10"] + (select [*] + (from :continents) + (limit 20 10)) + (is (= :select (:op stmt))) + (is (= [(parse-expr *)] (:exprs stmt))) + (is (= [(parse-table :continents)] (:from stmt))) + (is (= {:op :limit :count 10 :offset 20} (:limit stmt)))) + (deftest-stmt test-select-offset ["SELECT * FROM \"continents\" OFFSET 15"] (select [*]