Skip to content

Commit 4a36d0e

Browse files
authored
fix: boolean parameter escape in SQLiteDriver (typeorm#9400)
* fix: sqlite boolean parameter escape sqlite does not support boolean parameters. Even though sqlite is able to transform true to 1 and false to 0 there might be some limitations with other implementations that build up on this. Fixes: typeorm#1981 (again) * fix: remove obsolete where boolean value transformation 3cbbe90 already handles the boolean value transformation so it is not necessary to have additional code in the query runner for handling this * test: add test cases for sqlite query parameter escape * fix typo
1 parent 6eb674b commit 4a36d0e

File tree

3 files changed

+85
-7
lines changed

3 files changed

+85
-7
lines changed

src/driver/better-sqlite3/BetterSqlite3QueryRunner.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,6 @@ export class BetterSqlite3QueryRunner extends AbstractSqliteQueryRunner {
8383

8484
const connection = this.driver.connection
8585

86-
parameters = parameters || []
87-
for (let i = 0; i < parameters.length; i++) {
88-
// in "where" clauses the parameters are not escaped by the driver
89-
if (typeof parameters[i] === "boolean")
90-
parameters[i] = +parameters[i]
91-
}
92-
9386
this.driver.connection.logger.logQuery(query, parameters, this)
9487
const queryStartTime = +new Date()
9588

src/driver/sqlite-abstract/AbstractSqliteDriver.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,16 @@ export abstract class AbstractSqliteDriver implements Driver {
480480
return String(value)
481481
}
482482

483+
// Sqlite does not have a boolean data type so we have to transform
484+
// it to 1 or 0
485+
if (typeof value === "boolean") {
486+
escapedParameters.push(+value)
487+
return this.createParameter(
488+
key,
489+
escapedParameters.length - 1,
490+
)
491+
}
492+
483493
if (value instanceof Date) {
484494
escapedParameters.push(
485495
DateUtils.mixedDateToUtcDatetimeString(value),
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import { DataSource } from "../../../../src"
2+
import {
3+
createTestingConnections,
4+
reloadTestingDatabases,
5+
closeTestingConnections,
6+
} from "../../../utils/test-utils"
7+
8+
describe("escape sqlite query parameters", () => {
9+
let connections: DataSource[]
10+
before(
11+
async () =>
12+
(connections = await createTestingConnections({
13+
entities: [__dirname + "/entity/*{.js,.ts}"],
14+
enabledDrivers: ["sqlite", "better-sqlite3"],
15+
})),
16+
)
17+
beforeEach(() => reloadTestingDatabases(connections))
18+
after(() => closeTestingConnections(connections))
19+
20+
it("should transform boolean parameters with value `true` into `1`", () =>
21+
Promise.all(
22+
connections.map((connection) => {
23+
const [_, parameters] =
24+
connection.driver.escapeQueryWithParameters(
25+
"SELECT nothing FROM irrelevant WHERE a = :param1",
26+
{ param1: true },
27+
{},
28+
)
29+
30+
parameters.should.eql([1])
31+
}),
32+
))
33+
34+
it("should transform boolean parameters with value `false` into `0`", () =>
35+
Promise.all(
36+
connections.map((connection) => {
37+
const [_, parameters] =
38+
connection.driver.escapeQueryWithParameters(
39+
"SELECT nothing FROM irrelevant WHERE a = :param1",
40+
{ param1: false },
41+
{},
42+
)
43+
44+
parameters.should.eql([0])
45+
}),
46+
))
47+
48+
it("should transform boolean nativeParameters with value `true` into `1`", () =>
49+
Promise.all(
50+
connections.map((connection) => {
51+
const [_, parameters] =
52+
connection.driver.escapeQueryWithParameters(
53+
"SELECT nothing FROM irrelevant",
54+
{},
55+
{ nativeParam1: true },
56+
)
57+
58+
parameters.should.eql([1])
59+
}),
60+
))
61+
62+
it("should transform boolean nativeParameters with value `false` into 0", () =>
63+
Promise.all(
64+
connections.map((connection) => {
65+
const [_, parameters] =
66+
connection.driver.escapeQueryWithParameters(
67+
"SELECT nothing FROM irrelevant",
68+
{},
69+
{ nativeParam1: false },
70+
)
71+
72+
parameters.should.eql([0])
73+
}),
74+
))
75+
})

0 commit comments

Comments
 (0)