diff options
author | Eric Schulte <schulte.eric@gmail.com> | 2010-11-30 19:32:01 -0700 |
---|---|---|
committer | Eric Schulte <schulte.eric@gmail.com> | 2010-11-30 19:32:01 -0700 |
commit | beab6b4bfe87661a8f67ff586b99c920da444b7d (patch) | |
tree | b54f6a7deeffe0ad69037eadc10faf016f51a27e | |
parent | c4418fc472af7c0b3fcc9420b4f6a65eed91f04f (diff) | |
download | org-mode-beab6b4bfe87661a8f67ff586b99c920da444b7d.tar.gz |
ob-sql: added variable handling
* lisp/ob-sql.el (org-babel-expand-body:sql): Expand the body of a sql
code block.
(org-babel-execute:sql): Use sql specific body expansion function.
(org-babel-sql-expand-vars): Insert variables into a sql code block.
* lisp/ob-sqlite.el (org-babel-execute:sqlite): Remove unused variable
declaration.
-rw-r--r-- | lisp/ob-sql.el | 29 | ||||
-rw-r--r-- | lisp/ob-sqlite.el | 1 |
2 files changed, 28 insertions, 2 deletions
diff --git a/lisp/ob-sql.el b/lisp/ob-sql.el index 78e8a3b..de6d10d 100644 --- a/lisp/ob-sql.el +++ b/lisp/ob-sql.el @@ -50,6 +50,11 @@ (defvar org-babel-default-header-args:sql '()) +(defun org-babel-expand-body:sql (body params) + "Expand BODY according to the values of PARAMS." + (org-babel-sql-expand-vars + body (mapcar #'cdr (org-babel-get-header params :var)))) + (defun org-babel-execute:sql (body params) "Execute a block of Sql code with Babel. This function is called by `org-babel-execute-src-block'." @@ -70,7 +75,7 @@ This function is called by `org-babel-execute-src-block'." (or cmdline ""))) (t (error "no support for the %s sql engine" engine))))) (with-temp-file in-file - (insert (org-babel-expand-body:generic body params))) + (insert (org-babel-expand-body:sql body params))) (message command) (shell-command command) (with-temp-buffer @@ -82,6 +87,28 @@ This function is called by `org-babel-execute-src-block'." (org-babel-pick-name (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))) +(defun org-babel-sql-expand-vars (body vars) + "Expand the variables held in VARS in BODY." + (mapc + (lambda (pair) + (setq body + (replace-regexp-in-string + (format "\$%s" (car pair)) + ((lambda (val) + (if (listp val) + ((lambda (data-file) + (with-temp-file data-file + (insert (orgtbl-to-csv + val '(:fmt (lambda (el) (if (stringp el) + el + (format "%S" el))))))) + data-file) + (org-babel-temp-file "sql-data-")) + (if (stringp val) val (format "%S" val)))) + (cdr pair)) + body))) + vars) + body) (defun org-babel-prep-session:sql (session params) "Raise an error because Sql sessions aren't implemented." diff --git a/lisp/ob-sqlite.el b/lisp/ob-sqlite.el index dd4ea87..498e11e 100644 --- a/lisp/ob-sqlite.el +++ b/lisp/ob-sqlite.el @@ -53,7 +53,6 @@ "Execute a block of Sqlite code with Babel. This function is called by `org-babel-execute-src-block'." (let ((result-params (split-string (or (cdr (assoc :results params)) ""))) - (vars (org-babel-get-header params :var)) (db (cdr (assoc :db params))) (separator (cdr (assoc :separator params))) (nullvalue (cdr (assoc :nullvalue params))) |