Skip to content
Open
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
74 changes: 74 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@
"snackbarjs": "^1.1.0",
"sortablejs": "^1.15.2",
"split.js": "^1.6.5",
"sql-formatter": "^15.6.5",
"ssdeep.js": "0.0.3",
"stream-browserify": "^3.0.0",
"tesseract.js": "5.1.0",
Expand Down
24 changes: 21 additions & 3 deletions src/core/operations/SQLBeautify.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/

import vkbeautify from "vkbeautify";
import { format } from "sql-formatter";
import Operation from "../Operation.mjs";

/**
Expand Down Expand Up @@ -39,7 +38,26 @@ class SQLBeautify extends Operation {
*/
run(input, args) {
const indentStr = args[0];
return vkbeautify.sql(input, indentStr);
// Extract and replace bind variables like :Bind1 with __BIND_0__
const bindRegex = /:\w+/g;
const bindMap = {};
let bindCounter=0;
const placeholderInput = input.replace(bindRegex, (match) => {
const placeholder = `__BIND_${bindCounter++}__`;
bindMap[placeholder] = match;
return placeholder;
});
// Format the SQL with chosen options
let formatted= format(placeholderInput, {
language: "mysql", // Use MySQL as the default dialect for better compatibility with real-world SQL
useTabs: indentStr==="\t", // true if tab, false if spaces
tabWidth: indentStr.length || 4, // fallback if empty
indentStyle: "standard" // fine for most SQL
});
// Replace placeholders back with original bind variables
formatted = formatted.replace(/__BIND_\d+__/g, match => bindMap[match] || match);

return formatted;
}

}
Expand Down
16 changes: 9 additions & 7 deletions tests/node/tests/operations.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -867,13 +867,15 @@ pCGTErs=
}),

it("SQL Beautify", () => {
const result = chef.SQLBeautify(`SELECT MONTH, ID, RAIN_I, TEMP_F
FROM STATS;`);
const expected = `SELECT MONTH,
ID,
RAIN_I,
TEMP_F
FROM STATS;`;
const result = chef.SQLBeautify(`SELECT MONTH, ID, RAIN_I, TEMP_F FROM STATS;`);
const expected =
`SELECT
MONTH,
ID,
RAIN_I,
TEMP_F
FROM
STATS;`;
assert.strictEqual(result.toString(), expected);
}),

Expand Down
Loading