emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/emacsql b2d83c6477 123/427: Fix up ORDER BY, drop :ascendi


From: ELPA Syncer
Subject: [nongnu] elpa/emacsql b2d83c6477 123/427: Fix up ORDER BY, drop :ascending-by.
Date: Tue, 13 Dec 2022 02:59:34 -0500 (EST)

branch: elpa/emacsql
commit b2d83c6477fc120dc4b98d7897bf5b4087b95cd1
Author: Christopher Wellons <wellons@nullprogram.com>
Commit: Christopher Wellons <wellons@nullprogram.com>

    Fix up ORDER BY, drop :ascending-by.
---
 README.md        |  7 ++++---
 emacsql-tests.el |  7 +++++++
 emacsql.el       | 20 ++++++++++++++------
 3 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/README.md b/README.md
index 9675a6fdca..0c8f789266 100644
--- a/README.md
+++ b/README.md
@@ -160,13 +160,14 @@ Provides `GROUP BY`.
 [... :group-by name]
 ```
 
-#### :ascending-by `<expr>`, :descending-by `<expr>`
+#### :order-by `<expr>|(<expr> <:asc|:desc>)|[<expr> ...]`
 
 Provides `ORDER BY`.
 
 ```el
-[... :ascending-by date]
-[... :descending-by [width height]]
+[... :order-by date]
+[... :order-by [(width :asc) (height :desc)]]
+[... :order-by [(width :asc) (- height)]]
 ```
 
 #### :insert, :replace
diff --git a/emacsql-tests.el b/emacsql-tests.el
index 5744e3c3a2..867678aef9 100644
--- a/emacsql-tests.el
+++ b/emacsql-tests.el
@@ -74,6 +74,13 @@
   (emacsql-tests-query [:create-table (:temporary :if-not-exists x) [y]] '()
                        "CREATE TEMPORARY TABLE IF NOT EXISTS x (y);"))
 
+(ert-deftest emacsql-order-by ()
+  (emacsql-tests-query [:order-by foo] '() "ORDER BY foo;")
+  (emacsql-tests-query [:order-by [$1]] '(bar) "ORDER BY bar;")
+  (emacsql-tests-query [:order-by (- foo)] '() "ORDER BY -(foo);")
+  (emacsql-tests-query [:order-by [(a :asc) ((/ b 2) :desc)]] '()
+                       "ORDER BY a ASC, b / 2 DESC;"))
+
 (ert-deftest emacsql-system ()
   (should-not (emacsql-sqlite3-unavailable-p))
   (emacsql-with-connection (db nil)
diff --git a/emacsql.el b/emacsql.el
index 9b428ab93f..1deea2ede0 100644
--- a/emacsql.el
+++ b/emacsql.el
@@ -535,13 +535,21 @@ definitions for return from a `emacsql-defexpander'."
   (emacsql-with-vars "GROUP BY "
     (expr expr)))
 
-(emacsql-defexpander :ascending-by (columns)
+(emacsql-defexpander :order-by (columns)
   (emacsql-with-vars "ORDER BY "
-    (concat (combine (emacsql--idents columns)) " ASC")))
-
-(emacsql-defexpander :descending-by (columns)
-  (emacsql-with-vars "ORDER BY "
-    (concat (combine (emacsql--idents columns)) " DESC")))
+    (cl-flet ((order (k) (cl-ecase k (:asc " ASC") (:desc " DESC"))))
+      (if (not (vectorp columns))
+          (expr columns)
+        (cl-loop for column across columns collect
+                 (cl-etypecase column
+                   (list (let ((kpos (cl-position-if #'keywordp column)))
+                           (if kpos
+                               (concat (expr (nth (- 1 kpos) column))
+                                       (order (nth kpos column)))
+                             (expr column))))
+                   (symbol (var column :identifier)))
+                 into parts
+                 finally (cl-return (mapconcat #'identity parts ", ")))))))
 
 (emacsql-defexpander :create-table (table schema)
   (emacsql-with-vars "CREATE "



reply via email to

[Prev in Thread] Current Thread [Next in Thread]