From mboxrd@z Thu Jan  1 00:00:00 1970
Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail
From: Eli Zaretskii <eliz@gnu.org>
Newsgroups: gmane.emacs.bugs
Subject: bug#67142: 29.1;
 with-sqlite-transaction commits on exception rather than rolling back
Date: Thu, 16 Nov 2023 16:12:00 +0200
Message-ID: <83pm09pz2n.fsf@gnu.org>
References: <ZVFyBTEDhg345UhN@odonien.localdomain>
Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214";
	logging-data="2763"; mail-complaints-to="usenet@ciao.gmane.io"
Cc: 67142@debbugs.gnu.org
To: Vasilij Schneidermann <mail@vasilij.de>
Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Nov 16 15:13:22 2023
Return-path: <bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org>
Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org
Original-Received: from lists.gnu.org ([209.51.188.17])
	by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
	(Exim 4.92)
	(envelope-from <bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org>)
	id 1r3d7R-0000T6-UW
	for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 16 Nov 2023 15:13:22 +0100
Original-Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <bug-gnu-emacs-bounces@gnu.org>)
	id 1r3d7C-0004dx-BE; Thu, 16 Nov 2023 09:13:07 -0500
Original-Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <Debian-debbugs@debbugs.gnu.org>)
 id 1r3d78-0004de-Kf
 for bug-gnu-emacs@gnu.org; Thu, 16 Nov 2023 09:13:02 -0500
Original-Received: from debbugs.gnu.org ([2001:470:142:5::43])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)
 (Exim 4.90_1) (envelope-from <Debian-debbugs@debbugs.gnu.org>)
 id 1r3d78-00055O-8s
 for bug-gnu-emacs@gnu.org; Thu, 16 Nov 2023 09:13:02 -0500
Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2)
 (envelope-from <Debian-debbugs@debbugs.gnu.org>) id 1r3d77-0004hc-T2
 for bug-gnu-emacs@gnu.org; Thu, 16 Nov 2023 09:13:01 -0500
X-Loop: help-debbugs@gnu.org
Resent-From: Eli Zaretskii <eliz@gnu.org>
Original-Sender: "Debbugs-submit" <debbugs-submit-bounces@debbugs.gnu.org>
Resent-CC: bug-gnu-emacs@gnu.org
Resent-Date: Thu, 16 Nov 2023 14:13:01 +0000
Resent-Message-ID: <handler.67142.B67142.170014394318024@debbugs.gnu.org>
Resent-Sender: help-debbugs@gnu.org
X-GNU-PR-Message: followup 67142
X-GNU-PR-Package: emacs
Original-Received: via spool by 67142-submit@debbugs.gnu.org id=B67142.170014394318024
 (code B ref 67142); Thu, 16 Nov 2023 14:13:01 +0000
Original-Received: (at 67142) by debbugs.gnu.org; 16 Nov 2023 14:12:23 +0000
Original-Received: from localhost ([127.0.0.1]:54823 helo=debbugs.gnu.org)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <debbugs-submit-bounces@debbugs.gnu.org>)
 id 1r3d6V-0004ge-62
 for submit@debbugs.gnu.org; Thu, 16 Nov 2023 09:12:23 -0500
Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:58110)
 by debbugs.gnu.org with esmtp (Exim 4.84_2)
 (envelope-from <eliz@gnu.org>) id 1r3d6T-0004gS-Br
 for 67142@debbugs.gnu.org; Thu, 16 Nov 2023 09:12:22 -0500
Original-Received: from fencepost.gnu.org ([2001:470:142:3::e])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <eliz@gnu.org>)
 id 1r3d6N-0004rw-CD; Thu, 16 Nov 2023 09:12:15 -0500
DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org;
 s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date:
 mime-version; bh=luG99csJbJaUCKbXmVlk8zC3EZNSxx/k1sb9+cpaHYc=; b=qgV9X55+vO3z
 05yImpXcU+e0Nxx4otpRnhKvt/h9WjOLBWh4w11kYWr+Sje9HfFO3pIFGNCasOiuIwt29c7Al1ivx
 LDBzf89WWTaWgU6Is9FdxjteNAg+9mUpC24iDLvcW5glvnFXDLGsjTcN05gnpbNhgOpoQjEY1b/UB
 gUKaJmSAJDU4c9sEivEOOHLWg+keSOu8/BojLoA2yz3v6LF7F3Ads0bCVjKF9zNBVelRBKuq4qFm4
 J8YnyH3Fy51DI8e/XjtQRJHxWNpNP5S7c0nJv078zxUkNL3oJ1SsPzpCliEoSBbAQTGSa4ZbrtjuI
 2apL2sus6M2vBdLV9FNWdQ==;
In-Reply-To: <ZVFyBTEDhg345UhN@odonien.localdomain> (message from Vasilij
 Schneidermann on Mon, 13 Nov 2023 01:47:01 +0100)
X-BeenThere: debbugs-submit@debbugs.gnu.org
X-Mailman-Version: 2.1.18
Precedence: list
X-BeenThere: bug-gnu-emacs@gnu.org
List-Id: "Bug reports for GNU Emacs,
 the Swiss army knife of text editors" <bug-gnu-emacs.gnu.org>
List-Unsubscribe: <https://lists.gnu.org/mailman/options/bug-gnu-emacs>,
 <mailto:bug-gnu-emacs-request@gnu.org?subject=unsubscribe>
List-Archive: <https://lists.gnu.org/archive/html/bug-gnu-emacs>
List-Post: <mailto:bug-gnu-emacs@gnu.org>
List-Help: <mailto:bug-gnu-emacs-request@gnu.org?subject=help>
List-Subscribe: <https://lists.gnu.org/mailman/listinfo/bug-gnu-emacs>,
 <mailto:bug-gnu-emacs-request@gnu.org?subject=subscribe>
Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org
Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org
Xref: news.gmane.io gmane.emacs.bugs:274454
Archived-At: <http://permalink.gmane.org/gmane.emacs.bugs/274454>

> Date: Mon, 13 Nov 2023 01:47:01 +0100
> From: Vasilij Schneidermann <mail@vasilij.de>
> 
> During experimentation with built-in SQLite support, I've noticed that
> if I use `with-sqlite-transaction' and there is a mistake in the body,
> `sqlite-commit' is executed rather than `sqlite-rollback', thereby
> committing the changes done up to the error instead of backing out.
> 
> This appears to be unusual behavior. From my quick research, the
> following SQLite bindings in other languages have an equivalent to
> `with-sqlite-transaction' and back out changes on error:

Thanks.

Does the change below look correct?  (I'm not an expert on SQLite or
DB programming in general.)

diff --git a/lisp/sqlite.el b/lisp/sqlite.el
index aad0aa4..df06647 100644
--- a/lisp/sqlite.el
+++ b/lisp/sqlite.el
@@ -24,7 +24,11 @@
 ;;; 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)))
@@ -32,10 +36,13 @@ with-sqlite-transaction
            (,func-var (lambda () ,@body)))
        (if (sqlite-available-p)
            (unwind-protect
-               (progn
+               (let (result)
                  (sqlite-transaction ,db-var)
-                 (funcall ,func-var))
-             (sqlite-commit ,db-var))
+                 (setq result (funcall ,func-var))
+                 (or (sqlite-commit ,db-var)
+                     (signal 'sqlite-error (list "SQLite commit failed")))
+                 result)
+             (sqlite-rollback ,db-var))
          (funcall ,func-var)))))
 
 (provide 'sqlite)