emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Add  'readonly'  option to ob-sqlite
@ 2024-06-07  6:47 Daniel M. German
  2024-06-07 15:04 ` Ihor Radchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Daniel M. German @ 2024-06-07  6:47 UTC (permalink / raw)
  To: emacs-orgmode

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



hi everybody,

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

This will result in the following error:

Runtime error near line 2: attempt to write a readonly database (8)
[ Babel evaluation exited with code 1 ]


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-sqlite.el-add-a-new-parameter-called-readonly.patch --]
[-- Type: text/x-patch, Size: 1756 bytes --]

From 7df14e4ac2dcdc8fbdf647bff31c9e03f33bd73a Mon Sep 17 00:00:00 2001
From: Daniel M German <dmg@turingmachine.org>
Date: Thu, 6 Jun 2024 23:34:28 -0700
Subject: [PATCH] lisp/ob-sqlite.el: add a new parameter called readonly.

If present, the database is open in read-only mode.

This is implemented by passing the parameter -readonly to sqlite
---
 lisp/ob-sqlite.el | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/lisp/ob-sqlite.el b/lisp/ob-sqlite.el
index 96d93b815..b15c4f4d3 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  . :any))
   "Sqlite specific header args.")
 
 (defun org-babel-expand-body:sqlite (body params)
@@ -77,10 +78,10 @@ This function is called by `org-babel-execute-src-block'."
 	(separator (cdr (assq :separator params)))
 	(nullvalue (cdr (assq :nullvalue params)))
 	(headers-p (equal "yes" (cdr (assq :colnames params))))
-	(others (delq nil (mapcar
+        (others (delq nil (mapcar
 			   (lambda (arg) (car (assq arg params)))
 			   (list :header :echo :bail :column
-				 :csv :html :line :list)))))
+				 :csv :html :line :list :readonly)))))
     (with-temp-buffer
       (insert
        (org-babel-eval
@@ -88,7 +89,7 @@ This function is called by `org-babel-execute-src-block'."
 	 "%cmd %header %separator %nullvalue %others %csv %db "
 	 (list
 	  (cons "cmd" org-babel-sqlite3-command)
-	  (cons "header" (if headers-p "-header" "-noheader"))
+          (cons "header" (if headers-p "-header" "-noheader"))
 	  (cons "separator"
 		(if separator (format "-separator %s" separator) ""))
 	  (cons "nullvalue"
-- 
2.45.1


[-- Attachment #3: Type: text/plain, Size: 248 bytes --]




--
Daniel M. German                  "A machine --computer-- can only do
   Lady Lovelace ->                what we tell it to do"
http://turingmachine.org/
http://silvernegative.com/
dmg (at) uvic (dot) ca
replace (at) with @ and (dot) with .


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: Add  'readonly'  option to ob-sqlite
  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
  0 siblings, 1 reply; 4+ messages in thread
From: Ihor Radchenko @ 2024-06-07 15:04 UTC (permalink / raw)
  To: dmg; +Cc: emacs-orgmode

"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)

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: Add  'readonly'  option to ob-sqlite
  2024-06-07 15:04 ` Ihor Radchenko
@ 2024-06-23 21:07   ` dmg
  2024-06-25 11:08     ` Ihor Radchenko
  0 siblings, 1 reply; 4+ messages in thread
From: dmg @ 2024-06-23 21:07 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

[-- 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 .


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: Add  'readonly'  option to ob-sqlite
  2024-06-23 21:07   ` dmg
@ 2024-06-25 11:08     ` Ihor Radchenko
  0 siblings, 0 replies; 4+ messages in thread
From: Ihor Radchenko @ 2024-06-25 11:08 UTC (permalink / raw)
  To: dmg; +Cc: emacs-orgmode

dmg <dmg@turingmachine.org> writes:

> 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!
Applied, onto main, after changing the commit message to follow our
rules. I also changed the allowed values of the new header argument to
"yes" and "no" (for the purposes of completion).

https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=553d9b579
https://git.sr.ht/~bzg/worg/commit/b8455c84

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-06-25 11:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2024-06-25 11:08     ` Ihor Radchenko

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).