Skip to content

Commit

Permalink
feat(debug): enhance SQL debugging (#317)
Browse files Browse the repository at this point in the history
* feat(debug): enhance SQL debugging

* Remove more newlines around parentheses
  • Loading branch information
benjie authored Oct 15, 2018
1 parent 73e16e8 commit 128b0d5
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 12 deletions.
4 changes: 1 addition & 3 deletions packages/graphile-build-pg/src/plugins/PgAllRows.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// @flow
import debugFactory from "debug";

import type { Plugin } from "graphile-build";

const debugSql = debugFactory("graphile-build-pg:sql");
import debugSql from "./debugSql";

export default (async function PgAllRows(
builder,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @flow
import type { Plugin } from "graphile-build";
import debugFactory from "debug";
const debugSql = debugFactory("graphile-build-pg:sql");
import debugSql from "./debugSql";

export default (async function PgRowByUniqueConstraint(builder) {
builder.hook("GraphQLObjectType:fields", (fields, build, context) => {
Expand Down
3 changes: 1 addition & 2 deletions packages/graphile-build-pg/src/plugins/PgRowNode.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// @flow
import type { Plugin } from "graphile-build";
import debugFactory from "debug";
import debugSql from "./debugSql";

const base64Decode = str => Buffer.from(String(str), "base64").toString("utf8");
const debugSql = debugFactory("graphile-build-pg:sql");

export default (async function PgRowNode(builder) {
builder.hook("GraphQLObjectType", (object, build, context) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import type { Plugin } from "graphile-build";

const base64 = str => Buffer.from(String(str)).toString("base64");

export default (function PgScalarFunctionConnectionPlugin(
Expand Down
75 changes: 75 additions & 0 deletions packages/graphile-build-pg/src/plugins/debugSql.js
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;
3 changes: 1 addition & 2 deletions packages/graphile-build-pg/src/plugins/makeProcField.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// @flow
import debugFactory from "debug";

import type { Build, FieldWithHooksFunction } from "graphile-build";
import type { PgProc } from "./PgIntrospectionPlugin";
import type { SQL } from "pg-sql2";
import debugSql from "./debugSql";

const debugSql = debugFactory("graphile-build-pg:sql");
const firstValue = obj => {
let firstKey;
for (const k in obj) {
Expand Down
4 changes: 1 addition & 3 deletions packages/graphile-build-pg/src/plugins/viaTemporaryTable.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// @flow

import * as sql from "pg-sql2";
import debugFactory from "debug";
import type { Client } from "pg";
import type { SQL, SQLQuery } from "pg-sql2";

const debugSql = debugFactory("graphile-build-pg:sql");
import debugSql from "./debugSql";

/*
* Originally we tried this with a CTE, but:
Expand Down

0 comments on commit 128b0d5

Please sign in to comment.