Browse Source

ob-sql: header line in results table

Charles Sebold 9 years ago
parent
commit
9c4b936c0e
1 changed files with 21 additions and 2 deletions
  1. 21 2
      lisp/ob-sql.el

+ 21 - 2
lisp/ob-sql.el

@@ -65,6 +65,7 @@ This function is called by `org-babel-execute-src-block'."
          (in-file (org-babel-temp-file "sql-in-"))
          (out-file (or (cdr (assoc :out-file params))
                        (org-babel-temp-file "sql-out-")))
+	 (header-delim "")
          (command (case (intern engine)
                     ('msosql (format "osql %s -s \"\t\" -i %s -o %s"
                                      (or cmdline "")
@@ -74,7 +75,8 @@ This function is called by `org-babel-execute-src-block'."
                                     (or cmdline "")
 				    (org-babel-process-file-name in-file)
 				    (org-babel-process-file-name out-file)))
-		    ('postgresql (format "psql -A -P footer=off -F \"\t\"  -f %s -o %s %s"
+		    ('postgresql (format
+				  "psql -A -P footer=off -F \"\t\"  -f %s -o %s %s"
 				    (org-babel-process-file-name in-file)
 				    (org-babel-process-file-name out-file)
 				    (or cmdline "")))
@@ -84,9 +86,26 @@ This function is called by `org-babel-execute-src-block'."
     (message command)
     (shell-command command)
     (with-temp-buffer
+      ;; 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
-       (org-table-to-lisp)
+       (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))