Skip to content
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
6 changes: 4 additions & 2 deletions src/sqlingvo/compiler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/sqlingvo/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand Down
10 changes: 10 additions & 0 deletions test/sqlingvo/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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 [*]
Expand Down