emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: dmg <dmg@turingmachine.org>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: emacs-orgmode@gnu.org
Subject: Re: Add  'readonly'  option to ob-sqlite
Date: Sun, 23 Jun 2024 14:07:07 -0700	[thread overview]
Message-ID: <m2tthjl66c.fsf@turingmachine.org> (raw)
In-Reply-To: <87tti494ko.fsf@localhost> (Ihor Radchenko's message of "Fri, 07 Jun 2024 15:04:55 +0000")

[-- Attachment #1: Type: text/plain, Size: 1345 bytes --]



hi Ihor,

Ihor Radchenko <yantar92@posteo.net> writes:

> "Daniel M. German" <dmg@turingmachine.org> writes:
>
>> Sometimes I think it is useful to open a DB in readonly mode
>>
>> eg. to avoid modifying by mistake
>>
>> This is a patch to ob-sqlite.el to accomplish that
>>
>> It adds support for opening a sqlite database in readonly only.
>>
>> It does it by adding a babel parameter readonly. If present, the
>> database will be opened in readonly mode. Example:
>>
>> #+begin_src sqlite :readonly :db /tmp/rip.db
>> insert into a values (1,2);
>> select * from a;
>> #+end_src
>
> Thanks for the patch!
>
> May you please change the patch to avoid header arguments without value?
> What ob-sqlite doing with "others" is not right because it prevents
> setting ":readonly yes" as default header argument and then selectively
> disabling it via ":readonly no".
>
> Also, since you are adding a new feature, please announce it in
> etc/ORG-NEWS and document it in
> https://orgmode.org/worg/org-contrib/babel/languages/ob-doc-sqlite.html
> (the source code is in
> https://git.sr.ht/~bzg/worg/tree/master/item/org-contrib/babel/languages/ob-doc-sqlite.org)


I have changed the code.

1. It is enabled only with ":readonly yes. Disabled with any other value

2. Documented change in NEWS

3. Added a patch for Worg

Thanks again,

--daniel


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Documenting-readonly-parameter-for-sqlite-s-babel.patch --]
[-- Type: text/x-patch, Size: 1782 bytes --]

From 2949d23c1e06f5cb9c4a07a4b7ec5322b87d8b8e Mon Sep 17 00:00:00 2001
From: Daniel M German <dmg@turingmachine.org>
Date: Sun, 23 Jun 2024 13:55:57 -0700
Subject: [PATCH] Documenting readonly parameter for sqlite's babel

org-contrib/babel/languages/ob-doc-sqlite.org:
	new option :readonly  to ob-sqlite to open the database
	in readonly mode:
---
 org-contrib/babel/languages/ob-doc-sqlite.org | 21 +++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/org-contrib/babel/languages/ob-doc-sqlite.org b/org-contrib/babel/languages/ob-doc-sqlite.org
index d7febb0c..d909d24b 100644
--- a/org-contrib/babel/languages/ob-doc-sqlite.org
+++ b/org-contrib/babel/languages/ob-doc-sqlite.org
@@ -110,6 +110,7 @@ There are 11 SQLite-specific header arguments.
                 SQLite `list' output mode and by the SQLite dot command
                 =.import=. 
  - nullvalue :: a string to use in place of NULL values.
+ - readonly :: if yes, open the database in readonly mode.
 
 *** Variables 
 
@@ -252,3 +253,23 @@ where exists (select * from updates where updates.id = bookreview.id);
 By editing the intermediary table to replace "null" values with a
 numerical rating, and then running the second source block, the SQLite
 table will be updated correctly.
+
+** Open database in read-only mode
+
+Sometimes it is useful to open the database in readonly mode to make sure no
+modifications are made to it.
+
+#+BEGIN_EXAMPLE
+,#+begin_src sqlite :readonly yes :db /tmp/test.db
+create table atable(a int, b int);
+,#+end_src
+#+END_EXAMPLE
+
+Attempting to run this block will generate the following error:
+
+#+BEGIN_EXAMPLE
+Runtime error near line 2: attempt to write a readonly database (8)
+[ Babel evaluation exited with code 1 ]
+#+END_EXAMPLE
+
+
-- 
2.45.2


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0001-ob-sqlite-Added-ability-to-open-a-database-in-readon.patch --]
[-- Type: text/x-patch, Size: 3154 bytes --]

From 0491d3a672e7a99142da6ac6ade26666ae809b04 Mon Sep 17 00:00:00 2001
From: Daniel M German <dmg@turingmachine.org>
Date: Sun, 23 Jun 2024 13:48:57 -0700
Subject: [PATCH] ob-sqlite: Added ability to open a database in readonly mode

Added option :readonly to ob-sqlite.

lisp/ob-sqlite.el:
        When :readonly=true the database is opened in readonly mode.
        For example:

        #+begin_src sqlite :db /tmp/rip.db  :readonly yes  :exports both
        create table rip(a,b);
        #+end_src

        This results in an error such as:

        Runtime error near line 2: attempt to write a readonly database (8)

etc/ORG-NEWS:
        documented changes
---
 etc/ORG-NEWS      | 18 ++++++++++++++++++
 lisp/ob-sqlite.el | 10 +++++++---
 2 files changed, 25 insertions(+), 3 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 1252bbca1..357c41bb2 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -51,6 +51,24 @@ all the references are resolved in the generated png.
 
 # This also includes changes in function behavior from Elisp perspective.
 
+*** ob-sqlite: Added ability to open a database in readonly mode
+
+Added option :readonly to ob-sqlite.
+
+When :readonly=true the database is opened in readonly mode. For example:
+
+#+begin_src sqlite :db /tmp/rip.db  :readonly yes  :exports both
+create table rip(a,b);
+#+end_src
+
+This results in an error such as:
+
+#+begin_example
+Runtime error near line 2: attempt to write a readonly database (8)
+[ Babel evaluation exited with code 1 ]
+#+end_example
+
+
 ** Miscellaneous
 
 * Version 9.7
diff --git a/lisp/ob-sqlite.el b/lisp/ob-sqlite.el
index 96d93b815..e125d927c 100644
--- a/lisp/ob-sqlite.el
+++ b/lisp/ob-sqlite.el
@@ -52,7 +52,8 @@
     (line      . :any)
     (list      . :any)
     (separator . :any)
-    (nullvalue . :any))
+    (nullvalue . :any)
+    (readonly-p . :any))
   "Sqlite specific header args.")
 
 (defun org-babel-expand-body:sqlite (body params)
@@ -76,7 +77,8 @@ This function is called by `org-babel-execute-src-block'."
 	(db (cdr (assq :db params)))
 	(separator (cdr (assq :separator params)))
 	(nullvalue (cdr (assq :nullvalue params)))
-	(headers-p (equal "yes" (cdr (assq :colnames params))))
+        (headers-p (equal "yes" (cdr (assq :colnames params))))
+        (readonly-p (equal "yes" (cdr (assq :readonly params))))
 	(others (delq nil (mapcar
 			   (lambda (arg) (car (assq arg params)))
 			   (list :header :echo :bail :column
@@ -85,7 +87,7 @@ This function is called by `org-babel-execute-src-block'."
       (insert
        (org-babel-eval
 	(org-fill-template
-	 "%cmd %header %separator %nullvalue %others %csv %db "
+	 "%cmd %header %separator %nullvalue %others %csv %readonly %db "
 	 (list
 	  (cons "cmd" org-babel-sqlite3-command)
 	  (cons "header" (if headers-p "-header" "-noheader"))
@@ -103,6 +105,8 @@ This function is called by `org-babel-execute-src-block'."
 			      (member :html others) separator)
 			  ""
 			"-csv"))
+	  (cons "readonly"
+		(if readonly-p "-readonly" ""))
           (cons "db" (or db ""))))
 	;; body of the code block
 	(org-babel-expand-body:sqlite body params)))
-- 
2.45.2


[-- Attachment #4: Type: text/plain, Size: 250 bytes --]






--
Daniel M. German                  ""Geek" is a badge of honor."
                                    Eric Schmidt, CEO of Novell
http://turingmachine.org/
http://silvernegative.com/
dmg (at) uvic (dot) ca
replace (at) with @ and (dot) with .


  reply	other threads:[~2024-06-23 21:08 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-07  6:47 Add 'readonly' option to ob-sqlite Daniel M. German
2024-06-07 15:04 ` Ihor Radchenko
2024-06-23 21:07   ` dmg [this message]
2024-06-25 11:08     ` Ihor Radchenko

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.orgmode.org/

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

  git send-email \
    --in-reply-to=m2tthjl66c.fsf@turingmachine.org \
    --to=dmg@turingmachine.org \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@posteo.net \
    /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/org-mode.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).