* 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