unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Vasilij Schneidermann <mail@vasilij.de>
Cc: 67142@debbugs.gnu.org
Subject: bug#67142: 29.1; with-sqlite-transaction commits on exception rather than rolling back
Date: Fri, 17 Nov 2023 09:48:41 +0200	[thread overview]
Message-ID: <831qcoq0py.fsf@gnu.org> (raw)
In-Reply-To: <ZVZ60TBPWWZdV9F_@odonien.localdomain> (message from Vasilij Schneidermann on Thu, 16 Nov 2023 21:25:53 +0100)

> Date: Thu, 16 Nov 2023 21:25:53 +0100
> From: Vasilij Schneidermann <mail@vasilij.de>
> Cc: 67142@debbugs.gnu.org
> 
> > Does the change below look correct?  (I'm not an expert on SQLite or
> > DB programming in general.)
> 
> The `unwind-protect` part does not look correct since the rollback is
> always performed, even after the body form completed successfully and a
> commit was done. Either a commit or a rollback should be done, not both.

Oops.  Is the below better?

> A minor mistake is the result variable not using an uninterned symbol
> (or alternatively, `prog1` could replace the use of the result variable).

I don't understand this part, sorry.  Why do we need a symbol to
return the result of the body?

diff --git a/lisp/sqlite.el b/lisp/sqlite.el
index aad0aa4..4488896 100644
--- a/lisp/sqlite.el
+++ b/lisp/sqlite.el
@@ -24,18 +24,25 @@
 ;;; Code:
 
 (defmacro with-sqlite-transaction (db &rest body)
-  "Execute BODY while holding a transaction for DB."
+  "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."
   (declare (indent 1) (debug (form body)))
   (let ((db-var (gensym))
         (func-var (gensym)))
     `(let ((,db-var ,db)
            (,func-var (lambda () ,@body)))
        (if (sqlite-available-p)
-           (unwind-protect
-               (progn
-                 (sqlite-transaction ,db-var)
-                 (funcall ,func-var))
-             (sqlite-commit ,db-var))
+           (let (result commit)
+             (unwind-protect
+                 (progn
+                   (sqlite-transaction ,db-var)
+                   (setq result (funcall ,func-var))
+                   (setq commit (sqlite-commit ,db-var))
+                   result)
+             (or commit (sqlite-rollback ,db-var))))
          (funcall ,func-var)))))
 
 (provide 'sqlite)





  reply	other threads:[~2023-11-17  7:48 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-13  0:47 bug#67142: 29.1; with-sqlite-transaction commits on exception rather than rolling back Vasilij Schneidermann
2023-11-16 14:12 ` Eli Zaretskii
2023-11-16 20:25   ` Vasilij Schneidermann
2023-11-17  7:48     ` Eli Zaretskii [this message]
2023-11-17  8:38       ` Visuwesh
2023-11-17  9:08         ` Vasilij Schneidermann
2023-11-18 10:37           ` Eli Zaretskii
2023-11-20 21:14             ` Vasilij Schneidermann
2023-11-21 13:39               ` Eli Zaretskii
2024-03-03 14:04               ` J.P.
2024-03-11  3:43                 ` J.P.
2024-03-11 13:25                   ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=831qcoq0py.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=67142@debbugs.gnu.org \
    --cc=mail@vasilij.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).