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

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

[nongnu] elpa/emacsql d8d4bf4b98 202/427: Add emacsql-with-transaction m


From: ELPA Syncer
Subject: [nongnu] elpa/emacsql d8d4bf4b98 202/427: Add emacsql-with-transaction macro.
Date: Tue, 13 Dec 2022 02:59:42 -0500 (EST)

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

    Add emacsql-with-transaction macro.
---
 emacsql.el | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/emacsql.el b/emacsql.el
index 5e8562be48..37ac8cc966 100644
--- a/emacsql.el
+++ b/emacsql.el
@@ -228,6 +228,30 @@ A statement can be a list, containing a statement with its 
arguments."
                else
                collect (append (list 'emacsql 'emacsql--conn) statement))))
 
+(defvar emacsql--transaction-level 0
+  "Keeps track of nested transactions in `emacsql-with-transaction'.")
+
+(defmacro emacsql-with-transaction (connection &rest body)
+  "Evaluate BODY inside a single transaction, issuing a rollback on error.
+This macro can be nested indefinitely, wrapping everything in a
+single transaction at the lowest level."
+  (declare (indent 1))
+  `(let ((emacsql--connection ,connection)
+         (emacsql--completed nil)
+         (emacsql--transaction-level (1+ emacsql--transaction-level)))
+     (unwind-protect
+         (progn
+           (when (= 1 emacsql--transaction-level)
+             (emacsql emacsql--connection [:begin :transaction]))
+           (let ((result (progn ,@body)))
+             (prog1 result
+               (when (= 1 emacsql--transaction-level)
+                 (emacsql emacsql--connection [:commit]))
+               (setf emacsql--completed t))))
+       (when (and (= 1 emacsql--transaction-level)
+                  (not emacsql--completed))
+         (emacsql emacsql--connection [:rollback])))))
+
 ;; User interaction functions:
 
 (defvar emacsql-show-buffer-name "*emacsql-show*"



reply via email to

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