-
-
Notifications
You must be signed in to change notification settings - Fork 569
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(debug): enhance SQL debugging (#317)
* feat(debug): enhance SQL debugging * Remove more newlines around parentheses
- Loading branch information
Showing
7 changed files
with
81 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
packages/graphile-build-pg/src/plugins/PgRowByUniqueConstraint.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/graphile-build-pg/src/plugins/PgScalarFunctionConnectionPlugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import debugFactory from "debug"; | ||
import chalk from "chalk"; | ||
|
||
const rawDebugSql = debugFactory("graphile-build-pg:sql"); | ||
|
||
function debugSql(sql) { | ||
if (!rawDebugSql.enabled) { | ||
return; | ||
} | ||
let colourIndex = 0; | ||
let allowedColours = [ | ||
chalk.red, | ||
chalk.green, | ||
chalk.yellow, | ||
chalk.blue, | ||
chalk.magenta, | ||
chalk.cyan, | ||
chalk.white, | ||
chalk.black, | ||
]; | ||
function nextColor() { | ||
colourIndex = (colourIndex + 1) % allowedColours.length; | ||
return allowedColours[colourIndex]; | ||
} | ||
const colours = {}; | ||
|
||
/* Yep - that's `colour` from English and `ize` from American */ | ||
function colourize(str) { | ||
if (!colours[str]) { | ||
colours[str] = nextColor(); | ||
} | ||
return colours[str].bold.call(null, str); | ||
} | ||
|
||
let indentLevel = 0; | ||
function handleIndent(all, rawMatch) { | ||
const match = rawMatch.replace(/ $/, ""); | ||
if (match === "(") { | ||
indentLevel++; | ||
return match + "\n" + " ".repeat(indentLevel); | ||
} else if (match === ")") { | ||
indentLevel--; | ||
return "\n" + " ".repeat(indentLevel) + match; | ||
} else if (match === "),") { | ||
indentLevel--; | ||
return ( | ||
"\n" + | ||
" ".repeat(indentLevel) + | ||
match + | ||
"\n" + | ||
" ".repeat(indentLevel) | ||
); | ||
} else if (match === ",") { | ||
return match + "\n" + " ".repeat(indentLevel); | ||
} else { | ||
return "\n" + " ".repeat(indentLevel) + match.replace(/^\s+/, ""); | ||
} | ||
} | ||
const tidySql = sql | ||
.replace(/\s+/g, " ") | ||
.replace(/\s+(?=$|\n|\))/g, "") | ||
.replace(/(\n|^|\()\s+/g, "$1") | ||
.replace( | ||
/(\(|\)|\), ?|, ?| (select|insert|update|delete|from|where|and|or|order|limit)(?= ))/g, | ||
handleIndent | ||
) | ||
.replace(/\(\s*([A-Za-z0-9_."' =]{1,50})\s*\)/g, "($1)") | ||
.replace(/\(\s*(\([A-Za-z0-9_."' =]{1,50}\))\s*\)/g, "($1)") | ||
.replace(/\n\s*and \(TRUE\)/g, chalk.gray(" and (TRUE)")); | ||
const colouredSql = tidySql.replace(/__local_[0-9]+__/g, colourize); | ||
rawDebugSql("%s", "\n" + colouredSql); | ||
} | ||
Object.assign(debugSql, rawDebugSql); | ||
|
||
export default debugSql; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters