From 2656d20c92dc7740d83e1ee584f839d9c8a855ac Mon Sep 17 00:00:00 2001 From: Eli Zaretskii Date: Mon, 14 Apr 2025 12:42:28 +0300 Subject: [PATCH] Support file:// URIs and readonly DB in 'sqlite-open' * src/sqlite.c (Fsqlite_open): Two new optional arguments, READONLY and DISABLE-URI. Doc fix. * etc/NEWS: * doc/lispref/text.texi (Database): Document the new optional arguments to 'sqlite-open'. (Bug#65274) (cherry picked from commit 4918de1699152e98c7aaa3ecb21795a3cbd05194) --- doc/lispref/text.texi | 10 ++++++++-- src/sqlite.c | 20 +++++++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi index 447b093953e..726a48f1161 100644 --- a/doc/lispref/text.texi +++ b/doc/lispref/text.texi @@ -5373,11 +5373,17 @@ available in this Emacs session. When SQLite support is available, the following functions can be used. @cindex database object -@defun sqlite-open &optional file +@defun sqlite-open &optional file readonly disable-uri This function opens @var{file} as an SQLite database file. If @var{file} doesn't exist, a new database will be created and stored in that file. If @var{file} is omitted or @code{nil}, a new in-memory -database is created instead. +database is created instead. Second optional argument @var{readonly}, +if non-@code{nil}, means open the database only for reading; the +database must already exist in that case. By default, @var{file} can be +a @file{file://} URI as well as a file name; in the unusual case that +you have a local file whose name begins with @file{file:}, specify a +non-@code{nil} value for the third optional argument @var{disable-uri} +to disable the automatic recognition and processing of URIs. The return value is a @dfn{database object} that can be used as the argument to most of the subsequent functions described below. diff --git a/src/sqlite.c b/src/sqlite.c index 0de7488d8fd..99f91fd6da6 100644 --- a/src/sqlite.c +++ b/src/sqlite.c @@ -277,18 +277,28 @@ 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, 3, 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 or omitted, an in-memory database will be opened instead. +If READONLY is non-nil or omitted, open the database in read-only mode, +otherwise open it in read-write mode. +By default, file:// URIs are automatically recognized, unless +DISABLE-URI is non-nil. */) + (Lisp_Object file, Lisp_Object readonly, Lisp_Object disable_uri) { 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 #ifdef SQLITE_OPEN_URI - flags |= SQLITE_OPEN_URI; + if (NILP (disable_uri)) + flags |= SQLITE_OPEN_URI; #endif if (!init_sqlite_functions ()) -- 2.39.5