Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions sql-formatter-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,38 @@ FROM
a
FROM
t
WHERE b = 1`,
WHERE
b = 1`,
},
{
name: 'select-line-comment',
sql: "SELECT -- keep comment\n a\nFROM t",
expectIncludes: ['SELECT -- keep comment'],
},
{
name: 'select-group-by',
sql: 'SELECT a, b FROM t GROUP BY a, b',
expect: `SELECT
a
, b
FROM
t
GROUP BY
a
, b`,
},
{
name: 'select-order-by',
sql: 'SELECT a, b FROM t ORDER BY a DESC, b',
expect: `SELECT
a
, b
FROM
t
ORDER BY
a DESC
, b`,
},
{
name: 'nested-subquery',
sql: 'SELECT u.name FROM (SELECT id, name FROM users WHERE active = 1) AS u',
Expand All @@ -37,7 +62,8 @@ FROM
, name
FROM
users
WHERE active = 1
WHERE
active = 1
) u`,
},
];
Expand Down
3 changes: 2 additions & 1 deletion sql-formatter.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ function renderSelect(node, ctx) {
lines.push(`${indent(ctx, 1)}${renderNode(node.from, { ...ctx, indent: ctx.indent + 1 })}`);
}
if (node.where) {
lines.push(`${indent(ctx)}${renderNode(node.where, { ...ctx, indent: ctx.indent + 1 })}`);
lines.push(`${indent(ctx)}WHERE`);
lines.push(`${indent(ctx, 1)}${renderNode(node.where.expr, { ...ctx, indent: ctx.indent + 1 })}`);
}
if (node.groupBy) {
lines.push(`${indent(ctx)}${renderNode(node.groupBy, ctx)}`);
Expand Down