Skip to content

Commit 1ec2faf

Browse files
fix: add JSON pg_catalog prefix removal logic to TypeName method
Co-Authored-By: Dan Lynch <[email protected]>
1 parent 8f52dc3 commit 1ec2faf

File tree

1 file changed

+52
-41
lines changed

1 file changed

+52
-41
lines changed

packages/transform/src/transformers/v16-to-v17.ts

Lines changed: 52 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ export class V16ToV17Transformer {
5353
return node;
5454
}
5555

56+
5657
getNodeType(node: PG16.Node): any {
5758
return Object.keys(node)[0];
5859
}
@@ -307,9 +308,10 @@ export class V16ToV17Transformer {
307308
const result: any = {};
308309

309310
if (node.ctes !== undefined) {
311+
const cteContext = { ...context, inCTE: true };
310312
result.ctes = Array.isArray(node.ctes)
311-
? node.ctes.map(item => this.transform(item as any, context))
312-
: this.transform(node.ctes as any, context);
313+
? node.ctes.map(item => this.transform(item as any, cteContext))
314+
: this.transform(node.ctes as any, cteContext);
313315
}
314316
if (node.recursive !== undefined) {
315317
result.recursive = node.recursive;
@@ -440,27 +442,36 @@ export class V16ToV17Transformer {
440442
!this.isInValuesContext(context);
441443
}
442444

445+
private shouldAddPgCatalogPrefix(context: TransformerContext): boolean {
446+
const hasSelectStmt = context.parentNodeTypes.includes('SelectStmt');
447+
const hasWithClause = context.parentNodeTypes.includes('WithClause');
448+
const hasCommonTableExpr = context.parentNodeTypes.includes('CommonTableExpr');
449+
450+
return hasSelectStmt && !hasWithClause && !hasCommonTableExpr;
451+
}
452+
443453
TypeName(node: PG16.TypeName, context: TransformerContext): any {
444454
const result: any = {};
445455

446456
if (node.names !== undefined) {
447457
let names = Array.isArray(node.names)
448458
? node.names.map(item => this.transform(item as any, context))
449459
: this.transform(node.names as any, context);
450-
451-
if (Array.isArray(names) && names.length === 1) {
452-
const singleElement = names[0];
453-
if (singleElement && typeof singleElement === 'object' && 'String' in singleElement) {
454-
const typeName = singleElement.String.str || singleElement.String.sval;
455-
if (typeName === 'json') {
456-
names = [
457-
{ String: { sval: 'pg_catalog' } },
458-
singleElement
459-
];
460+
461+
// Remove pg_catalog prefix from JSON types
462+
if (Array.isArray(names) && names.length === 2) {
463+
const firstElement = names[0];
464+
const secondElement = names[1];
465+
if (firstElement && typeof firstElement === 'object' && 'String' in firstElement &&
466+
secondElement && typeof secondElement === 'object' && 'String' in secondElement) {
467+
const prefixStr = firstElement.String.str || firstElement.String.sval;
468+
const typeNameStr = secondElement.String.str || secondElement.String.sval;
469+
if (prefixStr === 'pg_catalog' && (typeNameStr === 'json' || typeNameStr === 'jsonb')) {
470+
names = [secondElement];
460471
}
461472
}
462473
}
463-
474+
464475
result.names = names;
465476
}
466477

@@ -562,33 +573,7 @@ export class V16ToV17Transformer {
562573
result.arg = this.transform(node.arg as any, context);
563574
}
564575
if (node.typeName !== undefined) {
565-
let transformedTypeName = this.transform(node.typeName as any, context);
566-
567-
// Handle both wrapped and unwrapped TypeName results
568-
let typeName = transformedTypeName;
569-
if (transformedTypeName && typeof transformedTypeName === 'object' && 'TypeName' in transformedTypeName) {
570-
typeName = transformedTypeName.TypeName;
571-
}
572-
573-
if (typeName && typeName.names && Array.isArray(typeName.names) && typeName.names.length === 1) {
574-
const singleElement = typeName.names[0];
575-
if (singleElement && typeof singleElement === 'object' && 'String' in singleElement) {
576-
const typeNameStr = singleElement.String.str || singleElement.String.sval;
577-
if (typeNameStr === 'json') {
578-
typeName.names = [
579-
{ String: { sval: 'pg_catalog' } },
580-
singleElement
581-
];
582-
if (transformedTypeName && typeof transformedTypeName === 'object' && 'TypeName' in transformedTypeName) {
583-
transformedTypeName.TypeName = typeName;
584-
} else {
585-
transformedTypeName = typeName;
586-
}
587-
}
588-
}
589-
}
590-
591-
result.typeName = transformedTypeName;
576+
result.typeName = this.transform(node.typeName as any, context);
592577
}
593578
if (node.location !== undefined) {
594579
result.location = node.location;
@@ -784,7 +769,33 @@ export class V16ToV17Transformer {
784769
}
785770

786771
CommonTableExpr(node: PG16.CommonTableExpr, context: TransformerContext): any {
787-
return { CommonTableExpr: node };
772+
const result: any = {};
773+
774+
if (node.ctename !== undefined) {
775+
result.ctename = node.ctename;
776+
}
777+
if (node.aliascolnames !== undefined) {
778+
result.aliascolnames = Array.isArray(node.aliascolnames)
779+
? node.aliascolnames.map(item => this.transform(item as any, context))
780+
: this.transform(node.aliascolnames as any, context);
781+
}
782+
if (node.ctematerialized !== undefined) {
783+
result.ctematerialized = node.ctematerialized;
784+
}
785+
if (node.ctequery !== undefined) {
786+
result.ctequery = this.transform(node.ctequery as any, context);
787+
}
788+
if (node.search_clause !== undefined) {
789+
result.search_clause = this.transform(node.search_clause as any, context);
790+
}
791+
if (node.cycle_clause !== undefined) {
792+
result.cycle_clause = this.transform(node.cycle_clause as any, context);
793+
}
794+
if (node.location !== undefined) {
795+
result.location = node.location;
796+
}
797+
798+
return { CommonTableExpr: result };
788799
}
789800

790801
ParamRef(node: PG16.ParamRef, context: TransformerContext): any {

0 commit comments

Comments
 (0)