SQL Indentation in Another Main Mode in Emacs

Often I write a few scripts to do some things, often including SQL, in another major mode. Maybe it looks like this:

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

I would like to be able to backtrack from SQL, so it looks something like this:

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

I'm not picky about the SQL indentation algorithm used, but I can't get anything to work. I'm not a big fan sql-indent.el, but I can not even get it to work with him in a new buffer (the function sql-indent-bufferdoes not change anything from my first description, and I definitely want to offer SELECT, FROMand WHEREis on a line, which, I think, are fairly standard )

, SQL, - M-x sql-indent-region RET - -, .

+4
1

( , , 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 string 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;
")
+2

All Articles