Skip to content

Commit d306b0d

Browse files
authored
Merge pull request #290 from michaelwallabi/mbecke/fix-quoting
Fix quotedIdentifier and quotedString
2 parents e84d88d + 89af877 commit d306b0d

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

api/src/sql.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export function quotedString(input: string): string {
2-
return `'${input.replace(`'`, `''`)}'`;
2+
return `'${input.replaceAll(`'`, `''`)}'`;
33
}
44

55
export function quotedIdentifier(input: string): string {
6-
return `"${input.replace(`"`, `""`)}"`;
6+
return `"${input.replaceAll(`"`, `""`)}"`;
77
}

api/test/api.test.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ import {
105105
intervalValue,
106106
listValue,
107107
mapValue,
108+
quotedIdentifier,
109+
quotedString,
108110
structValue,
109111
timeTZValue,
110112
timeValue,
@@ -2402,4 +2404,45 @@ ORDER BY name
24022404
});
24032405
});
24042406
});
2407+
2408+
describe('SQL utility functions', () => {
2409+
test('quotedString', () => {
2410+
// Basic string
2411+
assert.equal(quotedString('hello'), "'hello'");
2412+
2413+
// String with single quotes
2414+
assert.equal(quotedString("it's"), "'it''s'");
2415+
assert.equal(quotedString("'quoted'"), "'''quoted'''");
2416+
2417+
// String with multiple single quotes
2418+
assert.equal(quotedString("it's 'really' good"), "'it''s ''really'' good'");
2419+
2420+
// Empty string
2421+
assert.equal(quotedString(''), "''");
2422+
2423+
// String with special characters
2424+
assert.equal(quotedString('hello\nworld'), "'hello\nworld'");
2425+
assert.equal(quotedString('tab\there'), "'tab\there'");
2426+
});
2427+
2428+
test('quotedIdentifier', () => {
2429+
// Basic identifier
2430+
assert.equal(quotedIdentifier('table_name'), '"table_name"');
2431+
2432+
// Identifier with double quotes
2433+
assert.equal(quotedIdentifier('my"table'), '"my""table"');
2434+
assert.equal(quotedIdentifier('"column"'), '"""column"""');
2435+
2436+
// Identifier with multiple double quotes
2437+
assert.equal(quotedIdentifier('my"special"table'), '"my""special""table"');
2438+
2439+
// Empty identifier
2440+
assert.equal(quotedIdentifier(''), '""');
2441+
2442+
// Identifier with special characters
2443+
assert.equal(quotedIdentifier('table-name'), '"table-name"');
2444+
assert.equal(quotedIdentifier('table.name'), '"table.name"');
2445+
assert.equal(quotedIdentifier('table name'), '"table name"');
2446+
});
2447+
});
24052448
});

0 commit comments

Comments
 (0)