Skip to content

Commit

Permalink
Fix quote escaping
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubknejzlik committed Aug 17, 2024
1 parent 1506f21 commit ec6439e
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { ISQLFlavor } from "./Flavor";
import { AWSTimestreamFlavor } from "./flavors/aws-timestream";
import { MySQLFlavor } from "./flavors/mysql";
import { SQLiteFlavor } from "./flavors/sqlite";
import { Fn } from "./Function";
import {
IMetadata,
Expand All @@ -24,6 +25,7 @@ import { DeleteMutation, InsertMutation, UpdateMutation } from "./Mutation";
const flavors = {
mysql: new MySQLFlavor(),
awsTimestream: new AWSTimestreamFlavor(),
sqlite: new SQLiteFlavor(),
};

type TableSource = string | SelectQuery;
Expand Down
29 changes: 29 additions & 0 deletions src/flavors/awsdefault.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Cond } from "../Condition";
import { Fn } from "../Function";
import { Q } from "../Query";
import { DefaultFlavor } from "./default";

const flavor = new DefaultFlavor();

describe("Default Flavor flavor", () => {
it("should render correct select", () => {
const query = Q.select()
.addField("foo", "blah")
.addField(Fn.max("foo"), "blahMax")
.from("table")
.where(Cond.equal("foo", 123))
.where(Cond.equal("blah", "hello"))
.orderBy("foo", "DESC")
.limit(100)
.offset(2);
expect(query.toSQL(flavor)).toEqual(
'SELECT `foo` AS `blah`, MAX(`foo`) AS `blahMax` FROM `table` WHERE `foo` = 123 AND `blah` = "hello" ORDER BY `foo` DESC LIMIT 100 OFFSET 2'
);
});
it("should render correct update", () => {
const query = Q.update("table").set({ foo: `'aa'xx"` });
expect(query.toSQL(flavor)).toEqual(
'UPDATE `table` SET `foo` = "\'aa\'xx"""'
);
});
});
5 changes: 4 additions & 1 deletion src/flavors/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export class DefaultFlavor implements ISQLFlavor {
}`;
}
if (typeof value === "string") {
return `${this.stringQuotes}${value}${this.stringQuotes}`;
return `${this.stringQuotes}${value.replace(
new RegExp(`${this.stringQuotes}`, "g"),
`${this.stringQuotes}${this.stringQuotes}`
)}${this.stringQuotes}`;
}
return `${value}`;
}
Expand Down
29 changes: 29 additions & 0 deletions src/flavors/sqlite.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Cond } from "../Condition";
import { Fn } from "../Function";
import { Q } from "../Query";
import { DefaultFlavor } from "./default";

const flavor = Q.flavors.sqlite;

describe("SQLite Flavor flavor", () => {
it("should render correct select", () => {
const query = Q.select()
.addField("foo", "blah")
.addField(Fn.max("foo"), "blahMax")
.from("table")
.where(Cond.equal("foo", 123))
.where(Cond.equal("blah", "hello"))
.orderBy("foo", "DESC")
.limit(100)
.offset(2);
expect(query.toSQL(flavor)).toEqual(
"SELECT `foo` AS `blah`, MAX(`foo`) AS `blahMax` FROM `table` WHERE `foo` = 123 AND `blah` = 'hello' ORDER BY `foo` DESC LIMIT 100 OFFSET 2"
);
});
it("should render correct update", () => {
const query = Q.update("table").set({ foo: `'aa'xx"` });
expect(query.toSQL(flavor)).toEqual(
"UPDATE `table` SET `foo` = '''aa''xx\"'"
);
});
});

0 comments on commit ec6439e

Please sign in to comment.