branch: elpa/emacsql commit a09911a2056b725a7526b5cd36ecf34684518945 Author: Christopher Wellons <well...@nullprogram.com> Commit: Christopher Wellons <well...@nullprogram.com>
Add LIMIT and OFFSET expanders. --- README.md | 9 +++++++++ emacsql-tests.el | 13 +++++++++++++ emacsql.el | 6 ++++++ 3 files changed, 28 insertions(+) diff --git a/README.md b/README.md index 0c8f789266..e8d0263b58 100644 --- a/README.md +++ b/README.md @@ -170,6 +170,15 @@ Provides `ORDER BY`. [... :order-by [(width :asc) (- height)]] ``` +#### :limit `<limit>|[<offset> <limit>]` + +Provides `LIMIT` and `OFFSET`. + +```el +[... :limit 50] +[... :limit [150 50]] +``` + #### :insert, :replace Provides `INSERT`, `REPLACE`. diff --git a/emacsql-tests.el b/emacsql-tests.el index eb5fcc880c..377e5a794c 100644 --- a/emacsql-tests.el +++ b/emacsql-tests.el @@ -102,6 +102,19 @@ ([:order-by [(a :asc) ((/ b 2) :desc)]] '() "ORDER BY a ASC, b / 2 DESC;"))) +(ert-deftest emacsql-limit () + (emacsql-tests-with-queries + ([:limit 10] '() + "LIMIT 10;") + ([:limit $1] '(11) + "LIMIT 11;") + ([:limit [12]] '() + "LIMIT 12;") + ([:limit [2 10]] '() + "LIMIT 2, 10;") + ([:limit [$1 $2]] '(4 30) + "LIMIT 4, 30;"))) + (ert-deftest emacsql-system () (should-not (emacsql-sqlite3-unavailable-p)) (emacsql-with-connection (db nil) diff --git a/emacsql.el b/emacsql.el index 1deea2ede0..71206e71ab 100644 --- a/emacsql.el +++ b/emacsql.el @@ -551,6 +551,12 @@ definitions for return from a `emacsql-defexpander'." into parts finally (cl-return (mapconcat #'identity parts ", "))))))) +(emacsql-defexpander :limit (limits) + (emacsql-with-vars "LIMIT " + (if (vectorp limits) + (mapconcat #'expr limits ", ") + (expr limits)))) + (emacsql-defexpander :create-table (table schema) (emacsql-with-vars "CREATE " (let (temporary if-not-exists name)