[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[nongnu] elpa/emacsql dc178532e5 203/427: Add emacsql-with-bind.
From: |
ELPA Syncer |
Subject: |
[nongnu] elpa/emacsql dc178532e5 203/427: Add emacsql-with-bind. |
Date: |
Tue, 13 Dec 2022 02:59:42 -0500 (EST) |
branch: elpa/emacsql
commit dc178532e5bf44ed0a9b2c9288c520101b3106d6
Author: Christopher Wellons <wellons@nullprogram.com>
Commit: Christopher Wellons <wellons@nullprogram.com>
Add emacsql-with-bind.
---
emacsql.el | 27 +++++++++++++++++++++++++++
1 file changed, 27 insertions(+)
diff --git a/emacsql.el b/emacsql.el
index 37ac8cc966..f853df04d5 100644
--- a/emacsql.el
+++ b/emacsql.el
@@ -252,6 +252,33 @@ single transaction at the lowest level."
(not emacsql--completed))
(emacsql emacsql--connection [:rollback])))))
+(defmacro emacsql-with-bind (connection-sql-args &rest body)
+ "For each result row bind the column names for each returned row.
+Returns the result of the last evaluated BODY.
+
+All column names must be provided in the query ($ and * are not
+allowed). Hint: all of the bound identifiers must be known at
+compile time. For example, in the expression below the variables
+'name' and 'phone' will be bound for the body.
+
+ (emacsql-with-bind (db [:select [name phone] :from people])
+ (message \"Found %s with %s\" name phone))
+
+Each column must be a plain symbol, no expressions allowed here."
+ (declare (indent 1))
+ (cl-destructuring-bind (connection sql . args) connection-sql-args
+ (cl-assert (eq :select (elt sql 0)))
+ (let ((vars (elt sql 1)))
+ (when (eq '* vars)
+ (error "Must explicitly list columns in `emacsql-with-bind'."))
+ (cl-assert (cl-every #'symbolp vars))
+ `(let ((emacsql--results (emacsql ,connection ,sql ,@args))
+ (emacsql--final nil))
+ (dolist (emacsql--result emacsql--results emacsql--final)
+ (setf emacsql--final
+ (cl-destructuring-bind ,(cl-coerce vars 'list) emacsql--result
+ ,@body)))))))
+
;; User interaction functions:
(defvar emacsql-show-buffer-name "*emacsql-show*"
- [nongnu] elpa/emacsql b1da2ba9d2 297/427: Change compiler flag order (work around old gcc bug)., (continued)
- [nongnu] elpa/emacsql b1da2ba9d2 297/427: Change compiler flag order (work around old gcc bug)., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql d21cc86540 188/427: Add lexical binding to tests., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 3d556608ef 394/427: emacsql-mysql: Abort if mysql cannot be found, ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 568e663032 193/427: Fold dashes into underscores in identifiers., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql c85ab1f7b4 194/427: Enable foreign keys in SQLite by default., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql f51bb4bfaf 195/427: Allow foreign key specifications within columns., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql c4abd187a3 196/427: Remove junk file., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 3dfd0d4b8c 199/427: Fix missing argument typo., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 7341144e49 201/427: Fix ordering bug in emacsql--var-vars., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql d8d4bf4b98 202/427: Add emacsql-with-transaction macro., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql dc178532e5 203/427: Add emacsql-with-bind.,
ELPA Syncer <=
- [nongnu] elpa/emacsql cc92c154ee 205/427: Disallow nil as an identifier., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 75c0c4aeb7 204/427: Tweak emacsql-with-bind macro., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 3f38b357c3 206/427: Detect empty SQL vectors (better error messages)., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql ad24b1c508 208/427: Add JOIN syntax., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql fe2eef212c 210/427: Fix header typo., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 89654ad76b 211/427: Add identifier note., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 543189295d 215/427: Drop the pointless autoloads., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 3127283cd1 218/427: Clean up middleware a bit., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql cf16cdb2f8 217/427: Add a special characters test., ELPA Syncer, 2022/12/13
- [nongnu] elpa/emacsql 2db4a84598 225/427: Allow os tuples to be specified as an env var., ELPA Syncer, 2022/12/13