branch: elpa/emacsql commit e956d91f9908e3715aba7677bb8f9f5a51282ec8 Author: Christopher Wellons <well...@nullprogram.com> Commit: Christopher Wellons <well...@nullprogram.com>
Add -identifier to emacsql-escape. --- emacsql-tests.el | 16 ++++++++-------- emacsql.el | 15 ++++++++------- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/emacsql-tests.el b/emacsql-tests.el index ac96be14b2..bf1741298a 100644 --- a/emacsql-tests.el +++ b/emacsql-tests.el @@ -5,14 +5,14 @@ (require 'ert) (require 'emacsql) -(ert-deftest emacsql-escape () - (should (string= (emacsql-escape "foo") "foo")) - (should (string= (emacsql-escape 'foo) "foo")) - (should (string= (emacsql-escape :foo) "':foo'")) - (should (string= (emacsql-escape "a b") "'a b'")) - (should (string= (emacsql-escape '$foo) "'$foo'")) - (should (string= (emacsql-escape "foo$") "foo$")) - (should (string= (emacsql-escape "they're") "'they''re'"))) +(ert-deftest emacsql-escape-identifier () + (should (string= (emacsql-escape-identifier "foo") "foo")) + (should (string= (emacsql-escape-identifier 'foo) "foo")) + (should (string= (emacsql-escape-identifier :foo) "':foo'")) + (should (string= (emacsql-escape-identifier "a b") "'a b'")) + (should (string= (emacsql-escape-identifier '$foo) "'$foo'")) + (should (string= (emacsql-escape-identifier "foo$") "foo$")) + (should (string= (emacsql-escape-identifier "they're") "'they''re'"))) (ert-deftest emacsql-escape-value () (should (string= (emacsql-escape-value 'foo) "'foo'")) diff --git a/emacsql.el b/emacsql.el index 10ae4eb93f..dddbe638f0 100644 --- a/emacsql.el +++ b/emacsql.el @@ -186,7 +186,7 @@ buffer. This is for debugging purposes." collect row into rows and do (setf row ()) finally (cl-return rows))))) -(defun emacsql-escape (identifier &optional force) +(defun emacsql-escape-identifier (identifier &optional force) "Escape an identifier, always with quotes when FORCE is non-nil." (let ((string (if (stringp identifier) identifier @@ -219,7 +219,7 @@ buffer. This is for debugging purposes." (defun emacsql--column-to-string (column) "Convert COLUMN schema into a SQL string." - (let ((name (emacsql-escape (pop column))) + (let ((name (emacsql-escape-identifier (pop column))) (output ()) (type nil)) (while column @@ -243,7 +243,8 @@ buffer. This is for debugging purposes." (defun emacsql--schema-to-string (schema) "Convert SCHEMA into a SQL-consumable string." (cl-loop for column being the elements of schema - when (symbolp column) collect (emacsql-escape column) into parts + when (symbolp column) + collect (emacsql-escape-identifier column) into parts else collect (emacsql--column-to-string column) into parts finally (cl-return (mapconcat #'identity parts ", ")))) @@ -252,7 +253,7 @@ buffer. This is for debugging purposes." (let ((print-escape-newlines t)) (cond ((null value) "NULL") ((numberp value) (prin1-to-string value)) - ((emacsql-escape (prin1-to-string value) t))))) + ((emacsql-escape-identifier (prin1-to-string value) t))))) (defun emacsql-escape-vector (vector) "Encode VECTOR into a SQL vector scalar." @@ -309,11 +310,11 @@ a list of (<string> [arg-pos] ...)." (cl-loop for (i . kind) in vars collect (let ((thing (nth i args))) (cl-ecase kind - (:identifier (emacsql-escape thing)) + (:identifier (emacsql-escape-identifier thing)) (:value (emacsql-escape-value thing)) (:vector (emacsql-escape-vector thing)) (:auto (if (symbolp thing) - (emacsql-escape thing) + (emacsql-escape-identifier thing) (emacsql-escape-value thing))))))))) (defun emacsql (conn sql &rest args) @@ -339,7 +340,7 @@ KIND should be :value or :identifier." (replace-regexp-in-string "%" "%%" (cl-case kind (:value (emacsql-escape-value thing)) - (:identifier (emacsql-escape thing)) + (:identifier (emacsql-escape-identifier thing)) (:vector (emacsql-escape-vector thing)) (otherwise thing))))