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

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

[nongnu] elpa/emacsql 5ebd12b6ff 377/427: Add some support for the ESCAP


From: ELPA Syncer
Subject: [nongnu] elpa/emacsql 5ebd12b6ff 377/427: Add some support for the ESCAPE operator
Date: Tue, 13 Dec 2022 03:00:13 -0500 (EST)

branch: elpa/emacsql
commit 5ebd12b6ffaa9fbadefe8518eab07a028bbaf7c1
Author: Ákos Kiss <ak@coram.pub>
Commit: Ákos Kiss <ak@coram.pub>

    Add some support for the ESCAPE operator
---
 emacsql-compiler.el             | 18 ++++++++++++++++++
 tests/emacsql-compiler-tests.el |  4 +++-
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/emacsql-compiler.el b/emacsql-compiler.el
index 305aaaeaf5..5345643b4a 100644
--- a/emacsql-compiler.el
+++ b/emacsql-compiler.el
@@ -60,6 +60,12 @@
     (insert "'")
     (buffer-string)))
 
+(defun emacsql-quote-character (c)
+  "Single-quote character C for use in a SQL expression."
+  (if (char-equal c ?')
+      "''''"
+    (format "'%c'" c)))
+
 (defun emacsql-quote-identifier (string)
   "Double-quote (identifier) STRING for use in a SQL expression."
   (format "\"%s\"" (replace-regexp-in-string "\"" "\"\"" string)))
@@ -399,6 +405,18 @@ string returned is wrapped with parentheses."
                             (recur (if (eq op '>=) 2 0))
                             (recur (if (eq op '>=) 0 2))))
                  (otherwise (nops op))))
+              ;; enforce second argument to be a character
+              ((escape)
+               (let ((second-arg (cadr args)))
+                 (cond
+                  ((not (= 2 (length args))) (nops op))
+                  ((not (characterp second-arg))
+                   (emacsql-error
+                    "Second operand of escape has to be a character, got %s"
+                    second-arg))
+                  (t (format format-string
+                             (recur 0)
+                             (emacsql-quote-character second-arg))))))
               ;; Ordering
               ((asc desc)
                (format "%s %s" (recur 0) (upcase (symbol-name op))))
diff --git a/tests/emacsql-compiler-tests.el b/tests/emacsql-compiler-tests.el
index aad9b53fcd..70228f358c 100644
--- a/tests/emacsql-compiler-tests.el
+++ b/tests/emacsql-compiler-tests.el
@@ -181,7 +181,9 @@
     ([:where (= name 'foo)] '()
      "WHERE name = 'foo';")
     ([:where (= name '$s1)] '(qux)
-     "WHERE name = 'qux';")))
+     "WHERE name = 'qux';")
+    ([:where (like url (escape "%`%%" ?`))] '()
+     "WHERE url LIKE '\"%`%%\"' ESCAPE '`';")))
 
 (ert-deftest emacsql-expr ()
   (emacsql-tests-with-queries



reply via email to

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