all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Nicolas Graves <ngraves@ngraves.fr>
Cc: 65274@debbugs.gnu.org
Subject: bug#65274: 29.0.91; [feature request] Readonly argument for emacs built-in sqlite support.
Date: Sat, 19 Aug 2023 12:21:05 +0300	[thread overview]
Message-ID: <83v8dbz8u6.fsf@gnu.org> (raw)
In-Reply-To: <87wmxvl2ar.fsf@ngraves.fr> (message from Nicolas Graves on Wed,  16 Aug 2023 12:17:00 +0200)

> From: Nicolas Graves <ngraves@ngraves.fr>
> Cc: 65274@debbugs.gnu.org
> Date: Wed, 16 Aug 2023 12:17:00 +0200
> 
> My use-case is the package ibrowse.el, which uses sql commands to parse
> in real time the database of browser (for history or bookmarks,
> depending on the case)
> (https://git.sr.ht/~ngraves/ibrowse.el/tree/master/item/ibrowse-sql.el).
> 
> Web browsers use very frequent updates and most of the time, even
> trying to access it with readonly mode is not enough (I don't remember
> all the details, but it's something along the lines of you can't open
> even in read-only mode if the database file is currently being
> updated).
> 
> Most other emacs packages which try to do the same thing circumvent the
> issue by copying the whole database to /tmp and parsing it from
> there. I've found another solution which doesn't require to copy the
> file and it's to open it in immutable mode
> (https://git.sr.ht/~ngraves/ibrowse.el/tree/master/item/ibrowse-sql.el#L58)
> I remember the following stackoverflow link lead me to try this:
> https://stackoverflow.com/questions/7857755/is-it-possible-to-open-a-locked-sqlite-database-in-read-only-mode
> 
> On the CLI, it's simply done by adding the uri file:...?immutable=1. The SQL
> manual warns for possible errors if the database is being updated at the
> same time, but I don't keep an open connection so I don't experience any
> error whatsoever. One stackoverflow comment also hints about how it
> could be done, this probably applies to emacs as well:
> 
> "To achieve the same thing with most SQLite drivers, because we can't directly set SQLITE_IOCAP_IMMUTABLE, the best way is to open the connection with the flag SQLITE_OPEN_READONLY | SQLITE_OPEN_URI to allow passing the file:...?immutable=1 URI as filename."

Thanks.  I'm not sure I understood all the intricacies of this use
case, but could you please see if the patches below make your use case
possible?

diff --git a/src/sqlite.c b/src/sqlite.c
index fd528f2..f68edb9 100644
--- a/src/sqlite.c
+++ b/src/sqlite.c
@@ -277,13 +277,20 @@ check_sqlite (Lisp_Object db, bool is_statement)
 
 static int db_count = 0;
 
-DEFUN ("sqlite-open", Fsqlite_open, Ssqlite_open, 0, 1, 0,
+DEFUN ("sqlite-open", Fsqlite_open, Ssqlite_open, 0, 2, 0,
        doc: /* Open FILE as an sqlite database.
-If FILE is nil, an in-memory database will be opened instead.  */)
-  (Lisp_Object file)
+If FILE is nil, an in-memory database will be opened instead.
+If READONLY is non-nil, open the database in read-only mode,
+otherwise open it in read-write mode.  */)
+  (Lisp_Object file, Lisp_Object readonly)
 {
   Lisp_Object name;
-  int flags = (SQLITE_OPEN_CREATE  | SQLITE_OPEN_READWRITE);
+  int flags;
+
+  if (!NILP (readonly))
+    flags = SQLITE_OPEN_READONLY;
+  else
+    flags = (SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE);
 #ifdef SQLITE_OPEN_FULLMUTEX
   flags |= SQLITE_OPEN_FULLMUTEX;
 #endif





  reply	other threads:[~2023-08-19  9:21 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <878rbc1eem.fsf@ngraves.fr>
2023-08-13 20:35 ` bug#65274: 29.0.91; [feature request] Readonly argument for emacs built-in sqlite support Nicolas Graves via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-14 16:40   ` Eli Zaretskii
2023-08-16 10:17     ` Nicolas Graves via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-08-19  9:21       ` Eli Zaretskii [this message]
2023-09-02  7:17         ` Eli Zaretskii
2023-09-15  6:54           ` Eli Zaretskii
     [not found]             ` <87il8b4k6s.fsf@ngraves.fr>
2023-11-21 13:41               ` 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

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

  git send-email \
    --in-reply-to=83v8dbz8u6.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=65274@debbugs.gnu.org \
    --cc=ngraves@ngraves.fr \
    /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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.