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

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

[nongnu] elpa/emacsql 3b70e8f5dd 366/427: Add support for NUL characters


From: ELPA Syncer
Subject: [nongnu] elpa/emacsql 3b70e8f5dd 366/427: Add support for NUL characters in strings (fixes #42)
Date: Tue, 13 Dec 2022 03:00:11 -0500 (EST)

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

    Add support for NUL characters in strings (fixes #42)
---
 emacsql-compiler.el             | 14 +++++++++++++-
 tests/emacsql-external-tests.el | 10 ++++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/emacsql-compiler.el b/emacsql-compiler.el
index e8f3365759..bfa03f228c 100644
--- a/emacsql-compiler.el
+++ b/emacsql-compiler.el
@@ -49,7 +49,19 @@
 
 (defun emacsql-quote-scalar (string)
   "Single-quote (scalar) STRING for use in a SQL expression."
-  (format "'%s'" (replace-regexp-in-string "'" "''" string)))
+  (with-temp-buffer
+    (insert string)
+    (setf (point) (point-min))
+    (while (re-search-forward "'" nil t)
+      (replace-match "''"))
+    (setf (point) (point-min))
+    (while (re-search-forward "\0" nil t)
+      (replace-match "\\\\0"))
+    (setf (point) (point-min))
+    (insert "'")
+    (setf (point) (point-max))
+    (insert "'")
+    (buffer-string)))
 
 (defun emacsql-quote-identifier (string)
   "Double-quote (identifier) STRING for use in a SQL expression."
diff --git a/tests/emacsql-external-tests.el b/tests/emacsql-external-tests.el
index 99150dc0d7..b00b6c8de9 100644
--- a/tests/emacsql-external-tests.el
+++ b/tests/emacsql-external-tests.el
@@ -39,6 +39,16 @@
         (should (equal (emacsql db [:select * :from foo])
                        '((1) (2) (3))))))))
 
+(ert-deftest emacsql-nul-character ()
+  "Try inserting and retrieving strings with a NUL byte."
+  (let ((emacsql-global-timeout emacsql-tests-timeout))
+    (dolist (factory emacsql-tests-connection-factories)
+      (emacsql-with-connection (db (funcall (cdr factory)))
+        (emacsql db [:create-temporary-table foo ([x])])
+        (emacsql db [:insert :into foo :values (["a\0bc"])])
+        (should (equal (emacsql db [:select * :from foo])
+                       '(("a\0bc"))))))))
+
 (ert-deftest emacsql-foreign-key ()
   "Tests that foreign keys work properly through EmacSQL."
   (let ((emacsql-global-timeout emacsql-tests-timeout))



reply via email to

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