Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Handle multiline sql without spaces #215

Open
wants to merge 1 commit into
base: typescript
Choose a base branch
from
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
8 changes: 1 addition & 7 deletions src/reader/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,7 @@ class Query {
public category: string;

public getContent() {
let content: string = "";

this.lines.forEach((line) => {
content += line.content;
});

return content;
return this.lines.map(line => line.content.trim()).join(" ");
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/reader/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ export function putContentIntoLines(contents: string): Query[] {
}

if (contents[i] === Keyword.Newline) {
if (currentQueryContent.length > 0) {
if (currentQueryContent.trim().length > 0) {
query.lines.push(new Line(currentQueryContent, lineNumber));
}
currentQueryContent = "";
lineNumber++;
}

if (contents[i] === ";") {
if (currentQueryContent.length > 0) {
if (currentQueryContent.trim().length > 0) {
query.lines.push(new Line(currentQueryContent, lineNumber));
}
queriesFromFile.push(query);
Expand All @@ -53,7 +53,7 @@ export function putContentIntoLines(contents: string): Query[] {
* 3. Rejoin the lines together as a single string.
*/
function stripComments(content: string): string {
content = content.replace(reMultilineComments, "");
content = content.replace(reMultilineComments, " ");
const contentInLines = content.split(Keyword.Newline);

for (let i = 0; i < contentInLines.length; i++) {
Expand Down
46 changes: 23 additions & 23 deletions test/unit/reader/reader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,63 @@ import {
} from "../../../src/reader/reader";
import { Query } from "../../../src/reader/query";
import { Line } from "../../../src/reader/line";

let query, queryWithComments;
beforeEach(() => {
this.query = new Query();
this.query.lines = [
new Line("DELETE", 1),
new Line(" FROM ", 2),
new Line(" person WHERE ", 4),
query = new Query();
query.lines = [
new Line(" DELETE", 1),
new Line("FROM", 2),
new Line("person WHERE", 4),
new Line(" age > 5;", 5),
];

this.queryWithComments = new Query();
this.queryWithComments.lines = [
queryWithComments = new Query();
queryWithComments.lines = [
new Line("DELETE", 1),
new Line(" FROM ", 2),
new Line(" person WHERE ", 4),
new Line(" age > 5;", 6),
new Line("FROM", 2),
new Line("person WHERE", 4),
new Line("age > 5;", 6),
];
});

test("We correctly read a file", () => {
const expected: any = [this.query];
const input = "DELETE\n FROM \n\n person WHERE \n age > 5;";
const expected: any = [query];
const input = " DELETE\nFROM\n\nperson WHERE\n age > 5;";
const actual = putContentIntoLines(input);
expect(actual).toEqual(expected);
});

test.each([
// Test we ignore '--' comments
["DELETE\n FROM \n\n person WHERE \n-- Remove old people\n age > 5;"],
["DELETE\nFROM\n\nperson WHERE\n-- Remove old people\nage > 5;"],

// We ignore '#' comments
["DELETE\n FROM \n\n person WHERE \n# Remove old people\n age > 5;"],
["DELETE\nFROM\n\nperson WHERE\n# Remove old people\nage > 5;"],

// We ignore '/*' comments on a single line
["DELETE\n FROM \n\n person WHERE \n/* Remove old people*/\n age > 5;"],
["DELETE\nFROM\n\nperson WHERE\n/* Remove old people*/\nage > 5;"],
])("We ignore comments in files", (input) => {
const actual = putContentIntoLines(input);
expect(actual).toEqual([this.queryWithComments]);
expect(actual).toEqual([queryWithComments]);
});

test("We correctly reconstruct our query from lines", () => {
const expected: string = "DELETE FROM person WHERE age > 5;";
const actual = this.query.getContent();
const expected: string = "DELETE FROM person WHERE age > 5;";
const actual = query.getContent();
expect(actual).toEqual(expected);
});

test("We correctly construct lines in a query from a string", () => {
const expected: any = [this.query];
const expected: any = [query];

const input = "DELETE\n FROM \n\n person WHERE \n age > 5;";
const input = " DELETE\nFROM\n\nperson WHERE\n age > 5;";
const actual = getQueryFromLine(input);
expect(actual).toEqual(expected);
});

test("We should ignore multiline comments", () => {
const input =
"/*\n * catpants\n*/DELETE\n FROM \n\n person /*comment */WHERE \n/*\n\n this is useless*/ age > 5;";
"/*\n * catpants\n*/DELETE\nFROM\n\nperson/*comment */WHERE\n/*\n\n this is useless*/age > 5;";

expect(getQueryFromLine(input)).toEqual([this.query]);
expect(getQueryFromLine(input)).toEqual([query]);
});