Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.

Commit 386f758

Browse files
committed
add test for raw with placeholders
1 parent 9cbefa7 commit 386f758

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/connections/sql-base.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,11 @@ export abstract class SqlConnection extends Connection {
6767
}
6868

6969
// Named placeholder
70-
const { query: newQuery, bindings } = namedPlaceholder(query, params!);
70+
const { query: newQuery, bindings } = namedPlaceholder(
71+
query,
72+
params!,
73+
this.numberedPlaceholder
74+
);
7175
return await this.internalQuery({
7276
query: newQuery,
7377
parameters: bindings,

src/utils/placeholder.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ function parse(query: string): [string] | [string[], (string | number)[]] {
2828
do {
2929
for (i = curpos, end = ppos.index; i < end; ++i) {
3030
let chr = query.charCodeAt(i);
31-
console.log(i, query[i], inQuote, qchr);
3231
if (chr === BSLASH) escape = !escape;
3332
else {
3433
if (escape) {
@@ -90,7 +89,7 @@ export function namedPlaceholder(
9089
const key = placeholders[i];
9190

9291
if (numbered) {
93-
newQuery += `$${key}`;
92+
newQuery += `$${i + 1}`;
9493
} else {
9594
newQuery += `?`;
9695
}

tests/connections/connection.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,34 @@ function cleanup(data: Record<string, unknown>[]) {
3636
}
3737

3838
describe('Database Connection', () => {
39+
test('Support named parameters', async () => {
40+
if (process.env.CONNECTION_TYPE === 'mongo') return;
41+
42+
const sql =
43+
process.env.CONNECTION_TYPE === 'mysql'
44+
? 'SELECT CONCAT(:hello, :world) AS testing_word'
45+
: 'SELECT (:hello || :world) AS testing_word';
46+
47+
const { data } = await db.raw(sql, {
48+
hello: 'hello ',
49+
world: 'world',
50+
});
51+
52+
expect(data).toEqual([{ testing_word: 'hello world' }]);
53+
});
54+
55+
test('Support positional placeholder', async () => {
56+
if (process.env.CONNECTION_TYPE === 'mongo') return;
57+
58+
const sql =
59+
process.env.CONNECTION_TYPE === 'mysql'
60+
? 'SELECT CONCAT(?, ?) AS testing_word'
61+
: 'SELECT (? || ?) AS testing_word';
62+
63+
const { data } = await db.raw(sql, ['hello ', 'world']);
64+
expect(data).toEqual([{ testing_word: 'hello world' }]);
65+
});
66+
3967
test('Create table', async () => {
4068
const { error: createTableTeamError } = await db.createTable(
4169
DEFAULT_SCHEMA,

0 commit comments

Comments
 (0)