[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."
- [nongnu] elpa/emacsql de0bde9a02 207/427: Use Cask package-file directive instead., (continued)
- [nongnu] elpa/emacsql de0bde9a02 207/427: Use Cask package-file directive instead., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql bb3dbfc891 213/427: Set a global query timeout., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 394bb062aa 219/427: Add a small library for determining binary to use., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql f030b9952d 224/427: Fix up psql-connection., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql dc2afbbc1f 221/427: Adjust the Emacsql wire protocol., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 6318472b20 223/427: Link the Makefiles together., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql a7728339ba 216/427: Switch to custom middleware., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql ab53199d07 232/427: Fix escaping issue in middleware., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 6ad267f93b 229/427: Change :value to :scalar., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 1146f2e26a 233/427: Flesh out the changes in the README., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql ed77eac846 237/427: Add string prepared statements.,
ELPA Syncer <=
- [nongnu] elpa/emacsql 847196cafa 226/427: Set tty to raw in psql., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 3e5ce21b0d 242/427: Tweak the new implementation notes., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql a459708684 244/427: Move tests into their own directory., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 4db068920d 247/427: Provide MySQL front-end., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql dce6736e04 249/427: Oops, add new MySQL module to the Makefile., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql fcba876d0e 251/427: Solve the tty (pty) issue in MySQL., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql fb0e2d5423 256/427: Fix typecase typo., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql a6e859f1b5 260/427: Normalize darwin OS., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 4332bd67ec 271/427: Flesh out the main header a bit more., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 4e9297233d 273/427: Don't rely on C99 mode., ELPA Syncer, 2022/12/13