缩小SQL在另一个主要模式在Emacs [英] Indenting SQL in another major mode in Emacs

查看:121
本文介绍了缩小SQL在另一个主要模式在Emacs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常,我正在写一些脚本来做一些不同的主要模式的东西,通常涉及SQL。也许看起来像这样:

Oftentimes I'm writing some script to do some stuff, often involving SQL, in a different major mode. Maybe it looks like this:

sql = """
SELECT * FROM table WHERE row_awesomeness > 1000
"""

我想要能够缩进SQL的权限,所以看起来如下所示:

I'd like to be able to indent the SQL propertly, so it looks something like:

sql = """
SELECT *
  FROM table
 WHERE row_awesomeness > 1000
"""

我不太了解使用的SQL缩进算法,但我根本无法做任何工作。我不是 sql-indent.el ,但是我甚至不能在新的缓冲区中使用它(函数 sql-indent-buffer 不改变任何东西从我的第一个描述,我绝对希望 SELECT FROM WHERE 子句是分开的,我认为是非常标准的)。

I'm not picky about the SQL indentation algorithm used, but I can't get anything to work at all. I'm not a huge fan of sql-indent.el, but I can't even get that to work using it in a new buffer (the function sql-indent-buffer doesn't change anything from my first description, and I definitely want the SELECT, FROM and WHERE clauses to be on separate lines which I think is pretty standard).

理想情况下,我将突出显示包含SQL的区域, Mx sql-indent-region RET - 不需要在换行符上缩进的内容。

Ideally, I would highlight the region that contains the SQL and do something like M-x sql-indent-region RET - no need for something that indents upon a newline.

推荐答案

这是一种方法(轻轻地测试,使用您提到的缩进功能 - 我不使用SQL,但只要在整个缓冲区中运行,您应该能够插入任何函数):

This is one way of doing it (lightly tested, using the indenting function you mentioned -- I don't work with SQL but you should be able to plug in any function in its place as long as it operates on the whole buffer):

(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
字符串中,并且执行 Mx < kbd> my-sql-indent-region RET

If I mark the following sql query (from "SELECT" through "f2.PLAYERID"), embedded in an elisp string, and do 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;
")

这篇关于缩小SQL在另一个主要模式在Emacs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆