@@ -4,28 +4,25 @@ import { TransformerContext } from './context';
44export class V13ToV14Transformer {
55
66 transform ( node : PG13 . Node , context : TransformerContext = { parentNodeTypes : [ ] } ) : any {
7- if ( ! context . parentNodeTypes || ! Array . isArray ( context . parentNodeTypes ) ) {
8- context = { ... context , parentNodeTypes : [ ] } ;
7+ if ( node == null ) {
8+ return null ;
99 }
10- if ( node == null ) return null ;
11- if ( typeof node === 'number' || node instanceof Number ) return node ;
12- if ( typeof node === 'string' ) return node ;
13- if ( Array . isArray ( node ) ) return node . map ( item => this . transform ( item , context ) ) ;
1410
15- if ( typeof node === 'object' && node !== null ) {
16- const keys = Object . keys ( node ) ;
17- if ( keys . length === 1 ) {
18- const key = keys [ 0 ] ;
19- const value = ( node as any ) [ key ] ;
20- if ( typeof value === 'object' && value !== null && ! Array . isArray ( value ) ) {
21- try {
22- return this . visit ( node , context ) ;
23- } catch ( error ) {
24- throw new Error ( `Error transforming ${ key } : ${ ( error as Error ) . message } ` ) ;
25- }
26- }
27- }
28- return this . transformGenericNode ( node , context ) ;
11+ if ( typeof node === 'number' || node instanceof Number ) {
12+ return node ;
13+ }
14+
15+ if ( typeof node === 'string' ) {
16+ return node ;
17+ }
18+
19+ if ( Array . isArray ( node ) ) {
20+ return node . map ( item => this . transform ( item , context ) ) ;
21+ }
22+
23+ // Handle ParseResult objects specially
24+ if ( typeof node === 'object' && node !== null && 'version' in node && 'stmts' in node ) {
25+ return this . ParseResult ( node as PG13 . ParseResult , context ) ;
2926 }
3027
3128 try {
@@ -40,21 +37,29 @@ export class V13ToV14Transformer {
4037 if ( ! context . parentNodeTypes || ! Array . isArray ( context . parentNodeTypes ) ) {
4138 context = { ...context , parentNodeTypes : [ ] } ;
4239 }
40+
4341 const nodeType = this . getNodeType ( node ) ;
44- if ( ! nodeType ) return { } ;
42+
43+ // Handle empty objects
44+ if ( ! nodeType ) {
45+ return { } ;
46+ }
4547
4648 const nodeData = this . getNodeData ( node ) ;
49+
4750 const methodName = nodeType as keyof this;
48-
4951 if ( typeof this [ methodName ] === 'function' ) {
5052 const childContext : TransformerContext = {
5153 ...context ,
5254 parentNodeTypes : [ ...context . parentNodeTypes , nodeType ]
5355 } ;
54- return ( this [ methodName ] as any ) ( nodeData , childContext ) ;
56+ const result = ( this [ methodName ] as any ) ( nodeData , childContext ) ;
57+
58+ return result ;
5559 }
5660
57- return { [ nodeType ] : this . transformGenericNode ( nodeData , context ) } ;
61+ // If no specific method, return the node as-is
62+ return node ;
5863 }
5964
6065 private transformGenericNode ( node : any , context : TransformerContext ) : any {
@@ -325,7 +330,7 @@ export class V13ToV14Transformer {
325330 if ( funcCall . funcname && Array . isArray ( funcCall . funcname ) ) {
326331 const lastName = funcCall . funcname [ funcCall . funcname . length - 1 ] ;
327332 if ( lastName && typeof lastName === 'object' && 'String' in lastName ) {
328- return lastName . String . str ;
333+ return lastName . String . str || lastName . String . sval ;
329334 }
330335 }
331336 return null ;
@@ -347,20 +352,14 @@ export class V13ToV14Transformer {
347352 }
348353
349354 if ( node . mode !== undefined ) {
350- if ( node . mode === "FUNC_PARAM_IN" ) {
351- result . mode = "FUNC_PARAM_DEFAULT" ;
352- } else {
353- result . mode = node . mode ;
354- }
355- }
356-
357- if ( node . name !== undefined ) {
358- result . name = node . name ;
355+ result . mode = node . mode ;
359356 }
360357
361358 return { FunctionParameter : result } ;
362359 }
363360
361+
362+
364363 AlterFunctionStmt ( node : PG13 . AlterFunctionStmt , context : TransformerContext ) : any {
365364 const result : any = { } ;
366365
@@ -422,46 +421,40 @@ export class V13ToV14Transformer {
422421 }
423422
424423 AlterTableStmt ( node : PG13 . AlterTableStmt , context : TransformerContext ) : any {
425- const result : any = { } ;
426-
427- if ( 'relkind' in node ) {
428- result . objtype = node . relkind ;
429- }
424+ const result : any = { ...node } ;
430425
431- if ( node . relation !== undefined ) {
432- result . relation = this . transform ( node . relation as any , context ) ;
426+ if ( 'relkind' in result ) {
427+ result . objtype = result . relkind ;
428+ delete result . relkind ;
433429 }
434430
435- if ( node . cmds !== undefined ) {
436- result . cmds = Array . isArray ( node . cmds )
437- ? node . cmds . map ( item => this . transform ( item as any , context ) )
438- : this . transform ( node . cmds as any , context ) ;
431+ if ( result . relation !== undefined ) {
432+ result . relation = this . transform ( result . relation as any , context ) ;
439433 }
440434
441- if ( node . missing_ok !== undefined ) {
442- result . missing_ok = node . missing_ok ;
435+ if ( result . cmds !== undefined ) {
436+ result . cmds = Array . isArray ( result . cmds )
437+ ? result . cmds . map ( ( item : any ) => this . transform ( item as any , context ) )
438+ : this . transform ( result . cmds as any , context ) ;
443439 }
444440
445441 return { AlterTableStmt : result } ;
446442 }
447443
448444 CreateTableAsStmt ( node : PG13 . CreateTableAsStmt , context : TransformerContext ) : any {
449- const result : any = { } ;
450-
451- if ( 'relkind' in node ) {
452- result . objtype = node . relkind ;
453- }
445+ const result : any = { ...node } ;
454446
455- if ( node . query !== undefined ) {
456- result . query = this . transform ( node . query as any , context ) ;
447+ if ( 'relkind' in result ) {
448+ result . objtype = result . relkind ;
449+ delete result . relkind ;
457450 }
458451
459- if ( node . into !== undefined ) {
460- result . into = this . transform ( node . into as any , context ) ;
452+ if ( result . query !== undefined ) {
453+ result . query = this . transform ( result . query as any , context ) ;
461454 }
462455
463- if ( node . is_select_into !== undefined ) {
464- result . is_select_into = node . is_select_into ;
456+ if ( result . into !== undefined ) {
457+ result . into = this . transform ( result . into as any , context ) ;
465458 }
466459
467460 return { CreateTableAsStmt : result } ;
@@ -956,8 +949,12 @@ export class V13ToV14Transformer {
956949 }
957950
958951 if ( node . func !== undefined ) {
952+ const childContext : TransformerContext = {
953+ ...context ,
954+ parentNodeTypes : [ ...( context . parentNodeTypes || [ ] ) , 'CreateCastStmt' ]
955+ } ;
959956 const wrappedFunc = { ObjectWithArgs : node . func } ;
960- const transformedFunc = this . transform ( wrappedFunc as any , context ) ;
957+ const transformedFunc = this . transform ( wrappedFunc as any , childContext ) ;
961958 result . func = transformedFunc . ObjectWithArgs ;
962959 }
963960
@@ -997,33 +994,31 @@ export class V13ToV14Transformer {
997994 }
998995
999996 ObjectWithArgs ( node : PG13 . ObjectWithArgs , context : TransformerContext ) : any {
1000- const result : any = { } ;
997+ const result : any = { ... node } ;
1001998
1002- if ( node . objname !== undefined ) {
1003- result . objname = Array . isArray ( node . objname )
1004- ? node . objname . map ( item => this . transform ( item , context ) )
1005- : this . transform ( node . objname , context ) ;
999+ if ( result . objname !== undefined ) {
1000+ result . objname = Array . isArray ( result . objname )
1001+ ? result . objname . map ( ( item : any ) => this . transform ( item , context ) )
1002+ : this . transform ( result . objname , context ) ;
10061003 }
10071004
1008- if ( node . objargs !== undefined ) {
1009- result . objargs = Array . isArray ( node . objargs )
1010- ? node . objargs . map ( item => this . transform ( item , context ) )
1011- : [ this . transform ( node . objargs , context ) ] ;
1005+ if ( result . objargs !== undefined ) {
1006+ result . objargs = Array . isArray ( result . objargs )
1007+ ? result . objargs . map ( ( item : any ) => this . transform ( item , context ) )
1008+ : [ this . transform ( result . objargs , context ) ] ;
10121009 }
10131010
1014- if ( node . objfuncargs !== undefined ) {
1011+ if ( result . objfuncargs !== undefined ) {
10151012 const shouldPreserveObjfuncargs = this . shouldPreserveObjfuncargs ( context ) ;
10161013 if ( shouldPreserveObjfuncargs ) {
1017- result . objfuncargs = Array . isArray ( node . objfuncargs )
1018- ? node . objfuncargs . map ( item => this . transform ( item , context ) )
1019- : [ this . transform ( node . objfuncargs , context ) ] ;
1014+ result . objfuncargs = Array . isArray ( result . objfuncargs )
1015+ ? result . objfuncargs . map ( ( item : any ) => this . transform ( item , context ) )
1016+ : [ this . transform ( result . objfuncargs , context ) ] ;
1017+ } else {
1018+ delete result . objfuncargs ;
10201019 }
10211020 }
10221021
1023- if ( node . args_unspecified !== undefined ) {
1024- result . args_unspecified = node . args_unspecified ;
1025- }
1026-
10271022 return { ObjectWithArgs : result } ;
10281023 }
10291024
@@ -1032,42 +1027,18 @@ export class V13ToV14Transformer {
10321027 }
10331028
10341029 private shouldPreserveObjfuncargs ( context : TransformerContext ) : boolean {
1035- let currentContext = context ;
1036- while ( currentContext ) {
1037- if ( currentContext . currentNode && typeof currentContext . currentNode === 'object' ) {
1038- if ( 'CreateCastStmt' in currentContext . currentNode ) {
1039- return false ;
1040- }
1041- if ( 'AlterFunctionStmt' in currentContext . currentNode ) {
1042- return false ;
1043- }
1044- }
1045- currentContext = currentContext . parent ;
1030+ if ( ! context . parentNodeTypes || context . parentNodeTypes . length === 0 ) {
1031+ return true ;
10461032 }
10471033
1048- if ( context . rootNode && typeof context . rootNode === 'object' ) {
1049- function hasStmtRequiringObjfuncargsRemoval ( node : any ) : boolean {
1050- if ( ! node || typeof node !== 'object' ) return false ;
1051- if ( 'CreateCastStmt' in node || 'AlterFunctionStmt' in node ) {
1052- return true ;
1053- }
1054-
1055- for ( const value of Object . values ( node ) ) {
1056- if ( Array . isArray ( value ) ) {
1057- for ( const item of value ) {
1058- if ( hasStmtRequiringObjfuncargsRemoval ( item ) ) return true ;
1059- }
1060- } else if ( typeof value === 'object' ) {
1061- if ( hasStmtRequiringObjfuncargsRemoval ( value ) ) return true ;
1062- }
1063- }
1064- return false ;
1065- }
1066-
1067- if ( hasStmtRequiringObjfuncargsRemoval ( context . rootNode ) ) {
1068- return false ;
1069- }
1034+ if ( context . parentNodeTypes . includes ( 'CreateCastStmt' ) ) {
1035+ return false ;
1036+ }
1037+
1038+ if ( context . parentNodeTypes . includes ( 'AlterFunctionStmt' ) ) {
1039+ return false ;
10701040 }
1041+
10711042 return true ;
10721043 }
10731044
@@ -1090,9 +1061,7 @@ export class V13ToV14Transformer {
10901061 }
10911062
10921063 String ( node : PG13 . String , context : TransformerContext ) : any {
1093- const result : any = { ...node } ;
1094-
1095- return { String : result } ;
1064+ return { String : node } ;
10961065 }
10971066
10981067 BitString ( node : PG13 . BitString , context : TransformerContext ) : any {
0 commit comments