You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.4 KiB

;; -*- 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)))
(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 "
(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)))))
(cl-defmacro with-sql-parse-output (&rest body)
`(with-current-buffer "*SQL*"
(progn ,@body)
(cl-defun --sql-output-filter (str)
(with-current-buffer (get-buffer-create "**sql output**")
(insert str)
(setq sql-parsed-rows (sql-parse str))))
(cl-defun --shell-output-filter (str)
(with-current-buffer (get-buffer-create "**shell output**")
(insert str)
;(ansi-color-process-output comint-postoutput-scroll-to-bottom comint-watch-for-password-prompt)