Function: with-sqlite-transaction

with-sqlite-transaction is a macro defined in sqlite.el.gz.

Signature

(with-sqlite-transaction DB &rest BODY)

Documentation

Execute BODY while holding a transaction for DB.

If BODY completes normally, commit the changes and return the value of BODY. If BODY signals an error, or transaction commit fails, roll back the transaction changes before allowing the signal to propagate.

View in manual

Probably introduced at or before Emacs version 29.2.

Source Code

;; Defined in /usr/src/emacs/lisp/sqlite.el.gz
(defmacro with-sqlite-transaction (db &rest body)
  "Execute BODY while holding a transaction for DB.
If BODY completes normally, commit the changes and return
the value of BODY.
If BODY signals an error, or transaction commit fails, roll
back the transaction changes before allowing the signal to
propagate."
  (declare (indent 1) (debug (form body)))
  (cl-with-gensyms (db-var func-var res-var commit-var)
    `(let ((,db-var ,db)
           (,func-var (lambda () ,@body))
           ,res-var ,commit-var)
       (if (sqlite-available-p)
           (unwind-protect
               (progn
                 (sqlite-transaction ,db-var)
                 (setq ,res-var (funcall ,func-var))
                 (setq ,commit-var (sqlite-commit ,db-var))
                 ,res-var)
             (or ,commit-var (sqlite-rollback ,db-var)))
         (funcall ,func-var)))))