diff options
author | Nicholas Savage <nick@nicksavage.ca> | 2021-03-03 07:47:15 -0500 |
---|---|---|
committer | Kyle Meyer <kyle@kyleam.com> | 2021-03-09 00:43:43 -0500 |
commit | 37749c1657192031a7fab676a86fb91f3ef543a4 (patch) | |
tree | 55ab1ee6bf825a537747a2350a62cdbdc33e2985 | |
parent | 4d463ee4baf8a7ce8b7c633149a9452bc70ef557 (diff) | |
download | org-mode-37749c1657192031a7fab676a86fb91f3ef543a4.tar.gz |
Reduce code duplication in ob-sqlite.el and ob-sql.el
* lisp/ob-sqlite.el (org-babel-sqlite-expand-vars): Marked function as obsolete.
* lisp/ob-sql.el (org-babel-sql-expand-vars): Updated to support
expanding sqlite vars.
-rw-r--r-- | lisp/ob-sql.el | 15 | ||||
-rw-r--r-- | lisp/ob-sqlite.el | 23 |
2 files changed, 16 insertions, 22 deletions
diff --git a/lisp/ob-sql.el b/lisp/ob-sql.el index 68d5ddd..b1c6920 100644 --- a/lisp/ob-sql.el +++ b/lisp/ob-sql.el @@ -350,8 +350,13 @@ SET COLSEP '|' (org-babel-pick-name (cdr (assq :rowname-names params)) (cdr (assq :rownames params)))))))) -(defun org-babel-sql-expand-vars (body vars) - "Expand the variables held in VARS in BODY." +(defun org-babel-sql-expand-vars (body vars &optional sqlite) + "Expand the variables held in VARS in BODY. + +If SQLITE has been provided, prevent passing a format to +`orgtbl-to-csv'. This prevents overriding the default format, which if +there were commas in the context of the table broke the table as an +argument mechanism." (mapc (lambda (pair) (setq body @@ -362,9 +367,11 @@ SET COLSEP '|' (let ((data-file (org-babel-temp-file "sql-data-"))) (with-temp-file data-file (insert (orgtbl-to-csv - val '(:fmt (lambda (el) (if (stringp el) + val (if sqlite + nil + '(:fmt (lambda (el) (if (stringp el) el - (format "%S" el))))))) + (format "%S" el)))))))) data-file) (if (stringp val) val (format "%S" val)))) body))) diff --git a/lisp/ob-sqlite.el b/lisp/ob-sqlite.el index 6e21fa9..4cd905e 100644 --- a/lisp/ob-sqlite.el +++ b/lisp/ob-sqlite.el @@ -27,6 +27,7 @@ ;;; Code: (require 'ob) +(require 'ob-sql) (declare-function org-table-convert-region "org-table" (beg0 end0 &optional separator)) @@ -51,8 +52,8 @@ (defun org-babel-expand-body:sqlite (body params) "Expand BODY according to the values of PARAMS." - (org-babel-sqlite-expand-vars - body (org-babel--get-vars params))) + (org-babel-sql-expand-vars + body (org-babel--get-vars params) t)) (defvar org-babel-sqlite3-command "sqlite3") @@ -112,22 +113,8 @@ This function is called by `org-babel-execute-src-block'." (defun org-babel-sqlite-expand-vars (body vars) "Expand the variables held in VARS in BODY." - ;; FIXME: Redundancy with org-babel-sql-expand-vars! - (mapc - (lambda (pair) - (setq body - (replace-regexp-in-string - (format "$%s" (car pair)) - (let ((val (cdr pair))) - (if (listp val) - (let ((data-file (org-babel-temp-file "sqlite-data-"))) - (with-temp-file data-file - (insert (orgtbl-to-csv val nil))) - data-file) - (if (stringp val) val (format "%S" val)))) - body))) - vars) - body) + (declare (obsolete "use `org-babel-sql-expand-vars' instead." "Org 9.5")) + (org-babel-sql-expand-vars body vars t)) (defun org-babel-sqlite-table-or-scalar (result) "If RESULT looks like a trivial table, then unwrap it." |