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

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

[nongnu] elpa/emacsql ed77eac846 237/427: Add string prepared statements


From: ELPA Syncer
Subject: [nongnu] elpa/emacsql ed77eac846 237/427: Add string prepared statements.
Date: Tue, 13 Dec 2022 02:59:47 -0500 (EST)

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

    Add string prepared statements.
---
 emacsql-compiler.el | 78 ++++++++++++++++++++++++++++++-----------------------
 1 file changed, 45 insertions(+), 33 deletions(-)

diff --git a/emacsql-compiler.el b/emacsql-compiler.el
index 5fa0fed840..23c22256e7 100644
--- a/emacsql-compiler.el
+++ b/emacsql-compiler.el
@@ -279,44 +279,56 @@ which will be combined with variable definitions."
     (setf emacsql--vars (nconc emacsql--vars vars))
     string))
 
+(defun emacsql-prepare--string (string)
+  "Create a prepared statement from STRING."
+  (emacsql-with-params ""
+    (replace-regexp-in-string
+     "\\$[isv][0-9]+" (lambda (v) (param (intern v))) string)))
+
+(defun emacsql-prepare--sexp (sexp)
+  "Create a prepared statement from SEXP."
+  (emacsql-with-params ""
+    (cl-loop with items = (cl-coerce sexp 'list)
+             and last = nil
+             while (not (null items))
+             for item = (pop items)
+             collect
+             (cl-typecase item
+               (keyword (if (eq :values item)
+                            (concat "VALUES " (svector (pop items)))
+                          (emacsql--from-keyword item)))
+               (symbolp (if (eq item '*)
+                            "*"
+                          (param item)))
+               (vector (if (emacsql-sql-p item)
+                           (subsql item)
+                         (let ((idents (combine
+                                        (emacsql--*idents item))))
+                           (if (keywordp last)
+                               idents
+                             (format "(%s)" idents)))))
+               (list (if (vectorp (car item))
+                         (emacsql-escape-format
+                          (format "(%s)"
+                                  (emacsql-prepare-schema item)))
+                       (combine (emacsql--*expr item))))
+               (otherwise
+                (emacsql-escape-format
+                 (emacsql-escape-scalar item))))
+             into parts
+             do (setf last item)
+             finally (cl-return
+                      (mapconcat #'identity parts " ")))))
+
 (defun emacsql-prepare (sql)
-  "Expand SQL into a SQL-consumable string, with parameters."
+  "Expand SQL (string or sexp) into a prepared statement."
   (let* ((cache emacsql-prepare-cache)
          (key (cons emacsql-type-map sql)))
     (or (gethash key cache)
         (setf (gethash key cache)
-              (emacsql-with-params ""
-                (cl-loop with items = (cl-coerce sql 'list)
-                         and last = nil
-                         while (not (null items))
-                         for item = (pop items)
-                         collect
-                         (cl-typecase item
-                           (keyword (if (eq :values item)
-                                        (concat "VALUES " (svector (pop 
items)))
-                                      (emacsql--from-keyword item)))
-                           (symbolp (if (eq item '*)
-                                        "*"
-                                      (param item)))
-                           (vector (if (emacsql-sql-p item)
-                                       (subsql item)
-                                     (let ((idents (combine
-                                                    (emacsql--*idents item))))
-                                       (if (keywordp last)
-                                           idents
-                                         (format "(%s)" idents)))))
-                           (list (if (vectorp (car item))
-                                     (emacsql-escape-format
-                                      (format "(%s)"
-                                              (emacsql-prepare-schema item)))
-                                   (combine (emacsql--*expr item))))
-                           (otherwise
-                            (emacsql-escape-format
-                             (emacsql-escape-scalar item))))
-                         into parts
-                         do (setf last item)
-                         finally (cl-return
-                                  (mapconcat #'identity parts " "))))))))
+              (if (stringp sql)
+                  (emacsql-prepare--string sql)
+                (emacsql-prepare--sexp sql))))))
 
 (defun emacsql-format (expansion &rest args)
   "Fill in the variables EXPANSION with ARGS."



reply via email to

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