module SQLBeautifier

Extended Modules

Defined in:

sql_beautifier/sql_beautifier.cr

Constant Summary

BRACKETS = "[\\(\\)]"
COMMENT_CONTENT = /[\S]+[\s\S]*[\S]+/
INLINE_VERBS = (["WITHASC", "IN", "COALESCE", "AS", "WHEN", "THEN", "ELSE", "END", "AND", "UNION", "ALL", "ON", "DISTINCT", "INTERSECT", "EXCEPT", "EXISTS", "NOT", "COUNT", "ROUND", "CAST"] of ::String).join("\\b|\\b")
NEW_LINE_VERBS = "SELECT|FROM|WHERE|CASE|ORDER BY|LIMIT|GROUP BY|(RIGHT |LEFT )*(INNER |OUTER )*JOIN( LATERAL)*|HAVING|OFFSET|UPDATE"
POSSIBLE_INLINER = /(ORDER BY|CASE)/
SQL_COMMENTS = /(\s*?--.+\s*)|(\s*?\/\*[^\/\*]*\*\/\s*)/
STRINGS = /("[^"]+")|('[^']+')/
VERBS = "#{NEW_LINE_VERBS}|#{INLINE_VERBS}"

Instance Method Summary

Instance Method Detail

def beautify(sql : String, indentation_base = 2, open_bracket_is_newliner = false) : String #

This code is basically a stripped port of https://github.com/alekseyl/niceql. Some pieces of code were removed to simplify it, some were changed to support queries in lower case, the code removals injected some minor bugs, but this is ok while we don't have a SQL parser to do the job.


[View source]