Allow to provide password to SQL blocks via ID

This is better than having to store the password in plaintext.
This commit is contained in:
Daniel - 2023-08-17 18:38:46 +02:00
parent a6324a668d
commit c085dda842
Signed by: dbo
GPG Key ID: 784AA8DF0CCDF625
1 changed files with 26 additions and 10 deletions

18
init.el
View File

@ -1362,6 +1362,7 @@ point to the beginning of buffer first."
(use-package ob-sql
:config (progn
;; XXX: maybe merge this into the advice for `org-babel-execute:sql'?
(define-advice org-babel-sql-dbstring-oracle (:around
(orig-fun host port user password database)
ask-for-password)
@ -1376,7 +1377,22 @@ point to the beginning of buffer first."
(funcall orig-fun
host port user
(password-read (format "Password for %s@%s: " user database))
database))))))
database))))
(define-advice org-babel-execute:sql (:around
(orig-fun body params)
retrieve-password-by-function)
"Allow to set :dbpassword by an ID of an Org items having the PASSWORD property."
(let* ((dbpassword-id (cdr (assq :dbpassword-by-id params)))
(params params))
(when dbpassword-id
(setq params (cons (cons :dbpassword
(let* ((pom (or (org-id-find dbpassword-id 'marker)
(user-error "Cannot find ID %s" dbpassword-id))))
(or (org-entry-get pom "PASSWORD")
(user-error "No PASSWORD property at ID %s" dbpassword-id))))
params)))
(funcall orig-fun body params)))))
;; Exporting