这是一种方法(轻微测试,使用你提到的缩进函数——我不使用 SQL,但你应该能够插入任何函数作为只要它在整个缓冲区上运行):
(defun my-sql-indent-region (beg end)
"Indent the SQL statement in the region."
(interactive "*r")
(save-excursion
(save-restriction
(narrow-to-region beg end)
;; http://www.emacswiki.org/emacs/download/sql-indent.el
(sql-indent-buffer))))
如果我标记以下 sql 查询(从“SELECT”到“f2.PLAYERID”),嵌入到 elisp
字符串,然后执行 M-x my-sql-indent-region RET:
(defvar my-sql-query "
SELECT p1.PLAYERID,
f1.PLAYERNAME,
p2.PLAYERID,
f2.PLAYERNAME
FROM PLAYER f1,
PLAYER f2,
PLAYS p1
FULL OUTER JOIN PLAYS p2
ON p1.PLAYERID < p2.PLAYERID
AND p1.TEAMID = p2.TEAMID
GROUP BY p1.PLAYERID,
f1.PLAYERID,
p2.PLAYERID,
f2.PLAYERID
HAVING Count(p1.PLAYERID) = Count(*)
AND Count(p2.PLAYERID) = Count(*)
AND p1.PLAYERID = f1.PLAYERID
AND p2.PLAYERID = f2.PLAYERID;
")
我最终得到:
(defvar my-sql-query "
SELECT p1.PLAYERID,
f1.PLAYERNAME,
p2.PLAYERID,
f2.PLAYERNAME
FROM PLAYER f1,
PLAYER f2,
PLAYS p1
FULL OUTER JOIN PLAYS p2
ON p1.PLAYERID < p2.PLAYERID
AND p1.TEAMID = p2.TEAMID
GROUP BY p1.PLAYERID,
f1.PLAYERID,
p2.PLAYERID,
f2.PLAYERID
HAVING Count(p1.PLAYERID) = Count(*)
AND Count(p2.PLAYERID) = Count(*)
AND p1.PLAYERID = f1.PLAYERID
AND p2.PLAYERID = f2.PLAYERID;
")