Skip to content
Merged
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
4 changes: 2 additions & 2 deletions api/src/sql.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export function quotedString(input: string): string {
return `'${input.replace(`'`, `''`)}'`;
return `'${input.replaceAll(`'`, `''`)}'`;
}

export function quotedIdentifier(input: string): string {
return `"${input.replace(`"`, `""`)}"`;
return `"${input.replaceAll(`"`, `""`)}"`;
}
43 changes: 43 additions & 0 deletions api/test/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ import {
intervalValue,
listValue,
mapValue,
quotedIdentifier,
quotedString,
structValue,
timeTZValue,
timeValue,
Expand Down Expand Up @@ -2402,4 +2404,45 @@ ORDER BY name
});
});
});

describe('SQL utility functions', () => {
test('quotedString', () => {
// Basic string
assert.equal(quotedString('hello'), "'hello'");

// String with single quotes
assert.equal(quotedString("it's"), "'it''s'");
assert.equal(quotedString("'quoted'"), "'''quoted'''");

// String with multiple single quotes
assert.equal(quotedString("it's 'really' good"), "'it''s ''really'' good'");

// Empty string
assert.equal(quotedString(''), "''");

// String with special characters
assert.equal(quotedString('hello\nworld'), "'hello\nworld'");
assert.equal(quotedString('tab\there'), "'tab\there'");
});

test('quotedIdentifier', () => {
// Basic identifier
assert.equal(quotedIdentifier('table_name'), '"table_name"');

// Identifier with double quotes
assert.equal(quotedIdentifier('my"table'), '"my""table"');
assert.equal(quotedIdentifier('"column"'), '"""column"""');

// Identifier with multiple double quotes
assert.equal(quotedIdentifier('my"special"table'), '"my""special""table"');

// Empty identifier
assert.equal(quotedIdentifier(''), '""');

// Identifier with special characters
assert.equal(quotedIdentifier('table-name'), '"table-name"');
assert.equal(quotedIdentifier('table.name'), '"table.name"');
assert.equal(quotedIdentifier('table name'), '"table name"');
});
});
});
Loading