ob-sqlite: Add ability to open a database in readonly mode

* lisp/ob-sqlite.el (org-babel-header-args:sqlite):
(org-babel-execute:sqlite): Add new header argument :readonly.
* etc/ORG-NEWS (ob-sqlite: Added ability to open a database in
readonly mode): Announce the new header argument.
This commit is contained in:
Daniel M German 2024-06-23 13:48:57 -07:00 committed by Ihor Radchenko
parent de775a36d9
commit 553d9b5798
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 25 additions and 3 deletions

View File

@ -76,6 +76,24 @@ bibliography format requires them to be written in title-case.
# 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
*** Trailing =-= is now allowed in plain links

View File

@ -52,7 +52,8 @@
(line . :any)
(list . :any)
(separator . :any)
(nullvalue . :any))
(nullvalue . :any)
(readonly-p . ((yes no))))
"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)))