69 lines
2.4 KiB
EmacsLisp
69 lines
2.4 KiB
EmacsLisp
|
;; -*- mode: emacs-lisp; lexical-binding: t -*-
|
||
|
|
||
|
(require 'sql)
|
||
|
|
||
|
(defvar sql-current-output "")
|
||
|
(defvar sql-recent-output "")
|
||
|
(defvar sql-prompt-string "\nmysql> ")
|
||
|
(defvar sql-prompt-string-length (length sql-prompt-string))
|
||
|
(defvar sql-row-separator ?|)
|
||
|
(defvar sql-row-separator-regexp (format "[ ]+%c[ ]*" sql-row-separator))
|
||
|
(defvar sql-parsed-rows '())
|
||
|
(defvar sql-column-readers
|
||
|
'((starttime . parse-time-string)))
|
||
|
|
||
|
(defun sql-split-and-filter (s sep)
|
||
|
(let ((r (split-string s sep t)))
|
||
|
r))
|
||
|
|
||
|
(cl-defun sql-parse-header-row (hdr)
|
||
|
(mapcar (lambda(s) (or (assoc-default s sql-column-readers #'eql 'read) 'read)) (mapcar #'read hdr)))
|
||
|
(defun sql-read-row (readers row)
|
||
|
(cl-do* ((readers readers (cdr readers))
|
||
|
(row row (cdr row))
|
||
|
(x (funcall (car readers) (car row)) (funcall (car readers) (car row)))
|
||
|
(result (list x) (push x result)))
|
||
|
((or (>= 1 (length readers)) (>= 1 (length row))) (reverse result))))
|
||
|
|
||
|
(cl-defun sql-parse (s)
|
||
|
(let ((lines (split-string s "
\n"))
|
||
|
readers
|
||
|
(rows '()))
|
||
|
(dolist (line lines (setq rows (reverse rows)))
|
||
|
(when (and (> (length line) 0) (eq (elt line 0) sql-row-separator))
|
||
|
(push (sql-split-and-filter (substring line 1) sql-row-separator-regexp) rows)))
|
||
|
(when (> (length rows) 1)
|
||
|
(setq readers (sql-parse-header-row (car rows)))
|
||
|
(mapcar (lambda(row) (sql-read-row readers row)) rows))))
|
||
|
|
||
|
(cl-defun sql-save/filter-current-output (str)
|
||
|
(let* ((n (length str))
|
||
|
(m (min sql-prompt-string-length n)))
|
||
|
(cond ((string-equal (substring str (- 0 m)) (substring sql-prompt-string (- 0 m)))
|
||
|
(let ((s (concat sql-current-output str)))
|
||
|
(setq sql-current-output "")
|
||
|
(setq sql-parsed-rows (append sql-parsed-rows (sql-parse s)))))
|
||
|
(t (setq sql-current-output (concat sql-current-output str)))))
|
||
|
str)
|
||
|
|
||
|
(cl-defmacro with-sql-parse-output (&rest body)
|
||
|
`(with-current-buffer "*SQL*"
|
||
|
(prog1
|
||
|
(progn ,@body)
|
||
|
)))
|
||
|
|
||
|
(cl-defun --sql-output-filter (str)
|
||
|
(with-current-buffer (get-buffer-create "**sql output**")
|
||
|
(erase-buffer)
|
||
|
(insert str)
|
||
|
(setq sql-parsed-rows (sql-parse str))))
|
||
|
|
||
|
(cl-defun --shell-output-filter (str)
|
||
|
(with-current-buffer (get-buffer-create "**shell output**")
|
||
|
;(erase-buffer)
|
||
|
(insert str)
|
||
|
))
|
||
|
|
||
|
|
||
|
;(ansi-color-process-output comint-postoutput-scroll-to-bottom comint-watch-for-password-prompt)
|