]> code.delx.au - gnu-emacs/blobdiff - lisp/org/ob-sql.el
* lisp/loadup.el: Count byte-code functions as well.
[gnu-emacs] / lisp / org / ob-sql.el
index 552bb6172667db3b464a240f6681955ceb8a5a84..68bd95af9f91df5af299fa8843ab4781329047c7 100644 (file)
@@ -1,11 +1,10 @@
 ;;; ob-sql.el --- org-babel functions for sql evaluation
 
-;; Copyright (C) 2009, 2010  Free Software Foundation, Inc.
+;; Copyright (C) 2009-2012  Free Software Foundation, Inc.
 
 ;; Author: Eric Schulte
 ;; Keywords: literate programming, reproducible research
 ;; Homepage: http://orgmode.org
-;; Version: 7.7
 
 ;; This file is part of GNU Emacs.
 
@@ -25,6 +24,7 @@
 ;;; Commentary:
 
 ;; Org-Babel support for evaluating sql source code.
+;; (see also ob-sqlite.el)
 ;;
 ;; SQL is somewhat unique in that there are many different engines for
 ;; the evaluation of sql (Mysql, PostgreSQL, etc...), so much of this
@@ -40,7 +40,7 @@
 ;; - add more useful header arguments (user, passwd, database, etc...)
 ;; - support for more engines (currently only supports mysql)
 ;; - what's a reasonable way to drop table data into SQL?
-;; 
+;;
 
 ;;; Code:
 (require 'ob)
@@ -51,6 +51,9 @@
 
 (defvar org-babel-default-header-args:sql '())
 
+(defvar org-babel-header-arg-names:sql
+  '(engine out-file))
+
 (defun org-babel-expand-body:sql (body params)
   "Expand BODY according to the values of PARAMS."
   (org-babel-sql-expand-vars
@@ -85,31 +88,38 @@ This function is called by `org-babel-execute-src-block'."
       (insert (org-babel-expand-body:sql body params)))
     (message command)
     (shell-command command)
-    (with-temp-buffer
-      ;; need to figure out what the delimiter is for the header row
+    (if (or (member "scalar" result-params)
+           (member "verbatim" result-params)
+           (member "html" result-params)
+           (member "code" result-params)
+           (equal (point-min) (point-max)))
+       (with-temp-buffer
+         (progn (insert-file-contents-literally out-file) (buffer-string)))
       (with-temp-buffer
-        (insert-file-contents out-file)
-        (goto-char (point-min))
-        (when (re-search-forward "^\\(-+\\)[^-]" nil t)
-          (setq header-delim (match-string-no-properties 1)))
-        (goto-char (point-max))
-        (forward-char -1)
-        (while (looking-at "\n")
-          (delete-char 1)
-          (goto-char (point-max))
-          (forward-char -1))
-        (write-file out-file))
-      (org-table-import out-file '(16))
-      (org-babel-reassemble-table
-       (mapcar (lambda (x)
-                 (if (string= (car x) header-delim)
-                     'hline
-                   x))
-               (org-table-to-lisp))
-       (org-babel-pick-name (cdr (assoc :colname-names params))
-                           (cdr (assoc :colnames params)))
-       (org-babel-pick-name (cdr (assoc :rowname-names params))
-                           (cdr (assoc :rownames params)))))))
+       ;; need to figure out what the delimiter is for the header row
+       (with-temp-buffer
+         (insert-file-contents out-file)
+         (goto-char (point-min))
+         (when (re-search-forward "^\\(-+\\)[^-]" nil t)
+           (setq header-delim (match-string-no-properties 1)))
+         (goto-char (point-max))
+         (forward-char -1)
+         (while (looking-at "\n")
+           (delete-char 1)
+           (goto-char (point-max))
+           (forward-char -1))
+         (write-file out-file))
+       (org-table-import out-file '(16))
+       (org-babel-reassemble-table
+        (mapcar (lambda (x)
+                  (if (string= (car x) header-delim)
+                      'hline
+                    x))
+                (org-table-to-lisp))
+        (org-babel-pick-name (cdr (assoc :colname-names params))
+                             (cdr (assoc :colnames params)))
+        (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."