Skip to content

Commit 1222916

Browse files
authored
Merge pull request #666 from KxSystems/ee-qsql
Preserve new lines in qsql queries
2 parents ce3d272 + 4537291 commit 1222916

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/utils/queryUtils.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,19 +245,23 @@ export function normalizeQuery(query: string): string {
245245

246246
export function normalizeQSQLQuery(query: string): string {
247247
return (
248-
normalizeQuery(query)
248+
queryLimitCheck(query)
249+
// Remove block comments
250+
.replace(/^\/[\t ]*$[^]*?^\\[\t ]*$/gm, "")
251+
// Remove terminate comments
252+
.replace(/^\\[\t ]*(?:\r\n|[\r\n])[^]*/gm, "")
253+
// Remove single line comments
254+
.replace(/^\/.+/gm, "")
249255
// Replace system commands
250256
.replace(/^\\([a-zA-Z_1-2\\]+)[\t ]*(.*)/gm, (matched, command, args) =>
251257
matched === "\\\\"
252258
? 'system"\\\\"'
253259
: `system"${command} ${args.trim()}"`,
254260
)
261+
// Trim white space
262+
.trim()
255263
// Replace end of statements
256-
.replace(/\r\n/gs, ";")
257-
// Remove start of file
258-
.replace(/^[;\s]+/gs, "")
259-
// Remove end of file
260-
.replace(/[;\s]+$/gs, "")
264+
.replace(/(?<!;[\t ]*)(\r\n|[\r\n])+(?![\t\r\n ])/gs, ";$1")
261265
);
262266
}
263267

test/suite/utils/queryUtils.test.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -458,10 +458,9 @@ describe("queryUtils", () => {
458458
assert.strictEqual(res, "a:1");
459459
});
460460

461-
it("should remove line comment", () => {
461+
it("should preserve line comment", () => {
462462
const res = queryUtils.normalizeQSQLQuery("a:1 / line comment");
463-
464-
assert.strictEqual(res, "a:1");
463+
assert.strictEqual(res, "a:1 / line comment");
465464
});
466465

467466
it("should ignore line comment in a string", () => {
@@ -470,18 +469,18 @@ describe("queryUtils", () => {
470469
assert.strictEqual(res, 'a:"1 / not line comment"');
471470
});
472471

473-
it("should replace EOS with semicolon", () => {
472+
it("should replace EOS with semicolon preserve new lines", () => {
474473
let res = queryUtils.normalizeQSQLQuery("a:1\na");
475-
assert.strictEqual(res, "a:1;a");
474+
assert.strictEqual(res, "a:1;\na");
476475
res = queryUtils.normalizeQSQLQuery("a:1\r\na");
477-
assert.strictEqual(res, "a:1;a");
476+
assert.strictEqual(res, "a:1;\r\na");
478477
});
479478

480-
it("should escpae new lines in strings", () => {
481-
let res = queryUtils.normalizeQSQLQuery('a:"a\n \nb"');
482-
assert.strictEqual(res, 'a:"a\\n \\nb"');
483-
res = queryUtils.normalizeQSQLQuery('a:"a\r\n \r\nb"');
484-
assert.strictEqual(res, 'a:"a\\n \\nb"');
479+
it("should preserve new lines in strings", () => {
480+
let res = queryUtils.normalizeQSQLQuery('a:"a\n\n b"');
481+
assert.strictEqual(res, 'a:"a\n\n b"');
482+
res = queryUtils.normalizeQSQLQuery('a:"a\r\n\r\n b"');
483+
assert.strictEqual(res, 'a:"a\r\n\r\n b"');
485484
});
486485
});
487486

@@ -602,10 +601,9 @@ describe("queryUtils", () => {
602601
describe("getQSQLWrapper", () => {
603602
let queryWrappeStub: sinon.SinonStub;
604603

605-
it("should normalize q code", () => {
604+
it("should not add extra semicolon", () => {
606605
const res = queryUtils.getQSQLWrapper("a:1;\na");
607-
608-
assert.strictEqual(res, "a:1;;a");
606+
assert.strictEqual(res, "a:1;\na");
609607
});
610608

611609
it("should normalize python code using wrapper", () => {

0 commit comments

Comments
 (0)