From 0491d3a672e7a99142da6ac6ade26666ae809b04 Mon Sep 17 00:00:00 2001 From: Daniel M German 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