Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

The following is the solution from Jordon Biondo at "Writing PHP with SQL queries in emacs""Writing PHP with SQL queries in emacs" supplemented by alignment of the first spaces and equal signs. You can further adapt the alignment rules in align to your liking (see the description of the variable align-rules-list).

The packages expand-region and sql-indent from MELPA are required. (That is already mentioned at the above linkthe above link.)

For aligning an sql-string put the point into the string and call sql-indent-string. There is a nice video at the above linkthe above link that demonstrates the usage.

NOTE: Once more for clarification: The original codeoriginal code is from Jordon Biondo. I have just added the alignment stuff that was wanted here.

The following is the solution from Jordon Biondo at "Writing PHP with SQL queries in emacs" supplemented by alignment of the first spaces and equal signs. You can further adapt the alignment rules in align to your liking (see the description of the variable align-rules-list).

The packages expand-region and sql-indent from MELPA are required. (That is already mentioned at the above link.)

For aligning an sql-string put the point into the string and call sql-indent-string. There is a nice video at the above link that demonstrates the usage.

NOTE: Once more for clarification: The original code is from Jordon Biondo. I have just added the alignment stuff that was wanted here.

The following is the solution from Jordon Biondo at "Writing PHP with SQL queries in emacs" supplemented by alignment of the first spaces and equal signs. You can further adapt the alignment rules in align to your liking (see the description of the variable align-rules-list).

The packages expand-region and sql-indent from MELPA are required. (That is already mentioned at the above link.)

For aligning an sql-string put the point into the string and call sql-indent-string. There is a nice video at the above link that demonstrates the usage.

NOTE: Once more for clarification: The original code is from Jordon Biondo. I have just added the alignment stuff that was wanted here.

added 44 characters in body
Source Link
Tobias
  • 33.7k
  • 1
  • 38
  • 78

TwoSome facts can be recognized from this example:

  • There is some context-sensitive indentation through sql-indent (see the AND operator).
  • The second token on a line is aligned.
  • Keywords are up-cased.
  • Equal signs are aligned in groups.

Two facts can be recognized from this example:

  • There is some context-sensitive indentation through sql-indent (see the AND operator).
  • Keywords are up-cased.
  • Equal signs are aligned in groups.

Some facts can be recognized from this example:

  • There is some context-sensitive indentation through sql-indent (see the AND operator).
  • The second token on a line is aligned.
  • Keywords are up-cased.
  • Equal signs are aligned in groups.
added 1018 characters in body
Source Link
Tobias
  • 33.7k
  • 1
  • 38
  • 78

Two (maybe, somewhat extreme) examples how formatted sql strings look like:

$query = 'SELECT  SUM(T01.foobar)
          FROM    db_foo.bar T01
          WHERE   T01.fooIsomeD = 1
              AND T01.barI      = 2
              AND T01.foo IS NOT NULL'

$query = 'SELECT  SUM(T01.foobar)
          FROM    db_foo.bar T01 AND fill IS nil
          WHERE   T01.fooID           = 1
              AND T01.barIDuhfsdhufue = 2
              AND T01.foo IS NOT NULL AND fill IS nil
              AND t01.bar1zdfjiifajjdijfuheuwihfufheuiwfheufu = 0
              AND t02.bar2                                    = 1'

Two facts can be recognized from this example:

  • There is some context-sensitive indentation through sql-indent (see the AND operator).
  • Keywords are up-cased.
  • Equal signs are aligned in groups.

It is not exactly what the original poster wanted but I hope it satisfies the requirements.

Two (maybe, somewhat extreme) examples how formatted sql strings look like:

$query = 'SELECT  SUM(T01.foobar)
          FROM    db_foo.bar T01
          WHERE   T01.fooIsomeD = 1
              AND T01.barI      = 2
              AND T01.foo IS NOT NULL'

$query = 'SELECT  SUM(T01.foobar)
          FROM    db_foo.bar T01 AND fill IS nil
          WHERE   T01.fooID           = 1
              AND T01.barIDuhfsdhufue = 2
              AND T01.foo IS NOT NULL AND fill IS nil
              AND t01.bar1zdfjiifajjdijfuheuwihfufheuiwfheufu = 0
              AND t02.bar2                                    = 1'

Two facts can be recognized from this example:

  • There is some context-sensitive indentation through sql-indent (see the AND operator).
  • Keywords are up-cased.
  • Equal signs are aligned in groups.

It is not exactly what the original poster wanted but I hope it satisfies the requirements.

Added `(require 'align)`
Source Link
Tobias
  • 33.7k
  • 1
  • 38
  • 78
Loading
see UPDATE 2
Source Link
Tobias
  • 33.7k
  • 1
  • 38
  • 78
Loading
Bounty Ended with 50 reputation awarded by ReneFroger
add sqlup-capitalize-keywords-in-region
Source Link
Tobias
  • 33.7k
  • 1
  • 38
  • 78
Loading
Source Link
Tobias
  • 33.7k
  • 1
  • 38
  • 78
Loading