Skip to content

Commit b34f855

Browse files
authored
Fix generating query with column names containing colons (#1538)
1 parent 3825d19 commit b34f855

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/data/sqlGenerator.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,30 @@ describe('SQL Generator', () => {
485485
const sql = generateSql(opts);
486486
expect(sql).toEqual(expectedSqlParts.join(' '));
487487
});
488+
489+
it('generates table query with column names containing colons', () => {
490+
const opts: QueryBuilderOptions = {
491+
database: 'default',
492+
table: 'verifications',
493+
queryType: QueryType.Table,
494+
columns: [
495+
{ name: 'verification:id', type: 'String' },
496+
{ name: 'my:name', type: 'String' },
497+
{ name: 'regular_column', type: 'String' },
498+
],
499+
limit: 1000,
500+
filters: [],
501+
orderBy: [],
502+
};
503+
504+
const expectedSqlParts = [
505+
'SELECT "verification:id", "my:name", regular_column',
506+
'FROM "default"."verifications" LIMIT 1000',
507+
];
508+
509+
const sql = generateSql(opts);
510+
expect(sql).toEqual(expectedSqlParts.join(' '));
511+
});
488512
});
489513

490514
describe('isAggregateQuery', () => {
@@ -551,6 +575,10 @@ describe('getColumnIdentifier', () => {
551575
{ input: { name: 'test with alias', alias: 'a' }, expected: `"test with alias" as "a"` },
552576
{ input: { name: 'test_with_alias', alias: 'b' }, expected: `test_with_alias as "b"` },
553577
{ input: { name: '"test" as a', alias: '' }, expected: `"test" as a` },
578+
{ input: { name: 'verification:id' }, expected: `"verification:id"` },
579+
{ input: { name: 'my:name' }, expected: `"my:name"` },
580+
{ input: { name: 'namespace:field:value' }, expected: `"namespace:field:value"` },
581+
{ input: { name: 'verification:id', alias: 'vid' }, expected: `"verification:id" as "vid"` },
554582
];
555583

556584
it.each(cases)('returns correct identifier (case %#)', (c) => {

src/data/sqlGenerator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ const getColumnIdentifier = (col: SelectedColumn): string => {
641641
colName.includes(' as ')
642642
) {
643643
colName = col.name;
644-
} else if (colName.includes(' ')) {
644+
} else if (colName.includes(' ') || colName.includes(':')) {
645645
colName = escapeIdentifier(col.name);
646646
}
647647

0 commit comments

Comments
 (0)