Skip to content

Commit

Permalink
- Prettier SQL printing in StringUtil.prettySQL()
Browse files Browse the repository at this point in the history
  • Loading branch information
lmajano committed Jan 10, 2024
1 parent 691abaa commit 8f83367
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 14 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Prettier SQL printing in `StringUtil.prettySQL()`

## [7.2.1] - 2023-12-11

### ColdBox HMVC
Expand Down
44 changes: 30 additions & 14 deletions system/core/delegates/StringUtil.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -18,44 +18,60 @@ component singleton {
*/
function prettySql( string target = "" ){
var keywords = [
"SELECT",
"ALTER TABLE",
"CREATE TABLE",
"DELETE",
"DROP TABLE",
"FROM",
"WHERE",
"GROUP BY",
"HAVING",
"ORDER BY",
"INSERT INTO",
"LIMIT",
"ORDER BY",
"OFFSET",
"SELECT",
"UNION",
"UPDATE",
"DELETE",
"CREATE TABLE",
"ALTER TABLE",
"DROP TABLE",
"UNION"
"WHERE"
];
var indentedKeywords = [
"FULL JOIN",
"INNER JOIN",
"JOIN",
"LEFT JOIN",
"INNER JOIN",
"OUTER JOIN",
"FULL JOIN"
"OUTER JOIN"
];
var indent = " ";

return arguments.target
.listToArray( variables.NEW_LINE )
.map( ( item ) => item.trim() )
// comma spacing
.map( ( item ) => item.reReplace(
"(\s)*,(\s)*",
"\s*(?![^()]*\))(,)\s*",
",#variables.NEW_LINE##indent#",
"all"
) )
// Parenthesis spacing
.map( ( item ) => item.reReplace(
"\((\w)",
"( \1",
"all"
) )
.map( ( item ) => item.reReplace(
"(\w)\)",
"\1 )",
"all"
) )
// Keyword spacing
.map( ( item ) => {
return item.reReplacenocase(
"(\s)*(#keywords.toList( "|" )#)(\s)*",
"\2#variables.NEW_LINE##indent#",
"(\s)*(#keywords.toList( "|" )#)(\s)+",
"#variables.NEW_LINE#\2#variables.NEW_LINE##indent#",
"all"
)
} )
// Indented keyword spacing
.map( ( item ) => {
return item.reReplacenocase(
"(#indentedKeywords.toList( "|" )#)",
Expand Down

0 comments on commit 8f83367

Please sign in to comment.