11import { BaseTransformer , TransformerContext } from '../visitors/base' ;
22import { Node as PG13Node } from '../13/types' ;
33import { Node as PG14Node } from '../14/types' ;
4+ import * as pg13RuntimeSchema from '../13/runtime-schema' ;
5+ import * as pg14RuntimeSchema from '../14/runtime-schema' ;
46
57export class V13ToV14Transformer extends BaseTransformer {
68 transform ( node : any , context ?: TransformerContext ) : any {
@@ -25,28 +27,49 @@ export class V13ToV14Transformer extends BaseTransformer {
2527 A_Const ( nodeData : any , context ?: TransformerContext ) : any {
2628 const transformedData : any = { ...nodeData } ;
2729
28- if ( nodeData . val ) {
29- if ( nodeData . val . String ) {
30- transformedData . sval = { sval : nodeData . val . String . str } ;
31- delete transformedData . val ;
32- } else if ( nodeData . val . Float ) {
33- transformedData . fval = { fval : nodeData . val . Float . str } ;
34- delete transformedData . val ;
35- } else if ( nodeData . val . BitString ) {
36- transformedData . bsval = { bsval : nodeData . val . BitString . str } ;
37- delete transformedData . val ;
38- } else if ( nodeData . val . Integer ) {
39- const intVal = nodeData . val . Integer . ival ;
40- if ( intVal === 0 ) {
41- transformedData . ival = { } ;
42- } else {
43- transformedData . ival = { ival : intVal } ;
44- }
45- delete transformedData . val ;
46- } else if ( nodeData . val . Boolean ) {
47- transformedData . boolval = nodeData . val . Boolean . boolval ;
48- delete transformedData . val ;
30+ if ( nodeData . ival !== undefined ) {
31+ if ( typeof nodeData . ival === 'object' && nodeData . ival . ival !== undefined ) {
32+ transformedData . val = { Integer : { ival : nodeData . ival . ival } } ;
33+ } else if ( nodeData . ival === 0 || ( typeof nodeData . ival === 'object' && Object . keys ( nodeData . ival ) . length === 0 ) ) {
34+ transformedData . val = { Integer : { ival : 0 } } ;
4935 }
36+ delete transformedData . ival ;
37+ } else if ( nodeData . fval !== undefined ) {
38+ const fvalStr = typeof nodeData . fval === 'object' ? nodeData . fval . fval : nodeData . fval ;
39+ transformedData . val = { Float : { str : fvalStr } } ;
40+ delete transformedData . fval ;
41+ } else if ( nodeData . sval !== undefined ) {
42+ const svalStr = typeof nodeData . sval === 'object' ? nodeData . sval . sval : nodeData . sval ;
43+ transformedData . val = { String : { str : svalStr } } ;
44+ delete transformedData . sval ;
45+ } else if ( nodeData . bsval !== undefined ) {
46+ const bsvalStr = typeof nodeData . bsval === 'object' ? nodeData . bsval . bsval : nodeData . bsval ;
47+ transformedData . val = { BitString : { str : bsvalStr } } ;
48+ delete transformedData . bsval ;
49+ } else if ( nodeData . boolval !== undefined ) {
50+ transformedData . val = { Boolean : { boolval : nodeData . boolval } } ;
51+ delete transformedData . boolval ;
52+ }
53+
54+ return transformedData ;
55+ }
56+
57+ FuncCall ( nodeData : any , context ?: TransformerContext ) : any {
58+ const defaultResult = super . transformDefault ( { FuncCall : nodeData } , 'FuncCall' , nodeData , context ) ;
59+ const transformedData = defaultResult . FuncCall ;
60+
61+ if ( ! ( 'funcformat' in transformedData ) ) {
62+ transformedData . funcformat = "COERCE_EXPLICIT_CALL" ;
63+ }
64+
65+ return transformedData ;
66+ }
67+
68+ FunctionParameter ( nodeData : any , context ?: TransformerContext ) : any {
69+ const transformedData : any = { ...nodeData } ;
70+
71+ if ( transformedData . mode === "FUNC_PARAM_IN" ) {
72+ transformedData . mode = "FUNC_PARAM_DEFAULT" ;
5073 }
5174
5275 return transformedData ;
@@ -112,6 +135,10 @@ export class V13ToV14Transformer extends BaseTransformer {
112135 transformedData . orderClause = transformedData . orderClause . map ( ( item : any ) => this . transform ( item , context ) ) ;
113136 }
114137
138+ if ( transformedData . sortClause && Array . isArray ( transformedData . sortClause ) ) {
139+ transformedData . sortClause = transformedData . sortClause . map ( ( item : any ) => this . transform ( item , context ) ) ;
140+ }
141+
115142 if ( transformedData . limitClause && typeof transformedData . limitClause === 'object' ) {
116143 transformedData . limitClause = this . transform ( transformedData . limitClause , context ) ;
117144 }
@@ -124,13 +151,52 @@ export class V13ToV14Transformer extends BaseTransformer {
124151 }
125152
126153 TypeName ( nodeData : any , context ?: TransformerContext ) : any {
154+ const defaultResult = super . transformDefault ( { TypeName : nodeData } , 'TypeName' , nodeData , context ) ;
155+ const processedData = defaultResult . TypeName ;
156+
157+ if ( ! ( 'typemod' in processedData ) ) {
158+ processedData . typemod = - 1 ;
159+ }
160+
161+ return processedData ;
162+ }
163+
164+ TypeCast ( nodeData : any , context ?: TransformerContext ) : any {
165+ const transformedData : any = { ...nodeData } ;
166+
167+ if ( transformedData . typeName && typeof transformedData . typeName === 'object' ) {
168+ transformedData . typeName = this . TypeName ( transformedData . typeName , context ) ;
169+ }
170+
171+ if ( transformedData . arg && typeof transformedData . arg === 'object' ) {
172+ transformedData . arg = this . transform ( transformedData . arg , context ) ;
173+ }
174+
175+ return transformedData ;
176+ }
177+
178+ ColumnDef ( nodeData : any , context ?: TransformerContext ) : any {
127179 const transformedData : any = { ...nodeData } ;
128180
129- if ( ! ( 'location' in transformedData ) ) {
130- transformedData . location = undefined ;
181+ if ( transformedData . typeName && typeof transformedData . typeName === 'object' ) {
182+ transformedData . typeName = this . TypeName ( transformedData . typeName , context ) ;
131183 }
132- if ( ! ( 'typemod' in transformedData ) ) {
133- transformedData . typemod = - 1 ;
184+
185+ if ( transformedData . constraints && Array . isArray ( transformedData . constraints ) ) {
186+ transformedData . constraints = transformedData . constraints . map ( ( constraint : any ) => this . transform ( constraint , context ) ) ;
187+ }
188+
189+ return transformedData ;
190+ }
191+
192+ Constraint ( nodeData : any , context ?: TransformerContext ) : any {
193+ const transformedData : any = { ...nodeData } ;
194+
195+ if ( transformedData . pktable && typeof transformedData . pktable === 'object' ) {
196+ transformedData . pktable = this . transform ( transformedData . pktable , context ) ;
197+ if ( ! ( 'inh' in transformedData . pktable ) ) {
198+ transformedData . pktable . inh = true ;
199+ }
134200 }
135201
136202 return transformedData ;
@@ -140,13 +206,17 @@ export class V13ToV14Transformer extends BaseTransformer {
140206 const result = super . transformDefault ( node , nodeType , nodeData , context ) ;
141207 const transformedData = result [ nodeType ] ;
142208
143- if ( nodeType === 'AlterTableStmt' && transformedData && 'relkind' in transformedData ) {
209+ if ( ( nodeType === 'AlterTableStmt' || nodeType === 'CreateTableAsStmt' ) && transformedData && 'relkind' in transformedData ) {
144210 transformedData . objtype = transformedData . relkind ;
145211 delete transformedData . relkind ;
146212 }
147213
214+ if ( nodeType === 'CreateTableAsStmt' && transformedData && transformedData . into && ! ( 'onCommit' in transformedData . into ) ) {
215+ transformedData . into . onCommit = "ONCOMMIT_NOOP" ;
216+ }
217+
148218 if ( transformedData && typeof transformedData === 'object' ) {
149- this . ensureTypeNameFields ( transformedData ) ;
219+ this . applyRuntimeSchemaDefaults ( nodeType , transformedData ) ;
150220 }
151221
152222 return { [ nodeType ] : transformedData } ;
@@ -155,25 +225,47 @@ export class V13ToV14Transformer extends BaseTransformer {
155225
156226
157227 private ensureTypeNameFields ( obj : any ) : void {
158- if ( ! obj || typeof obj !== 'object' ) return ;
228+ return ;
229+ }
230+
231+ private isFieldWrapped ( nodeType : string , fieldName : string , version : 13 | 14 ) : boolean {
232+ const schema = version === 13 ? pg13RuntimeSchema . runtimeSchema : pg14RuntimeSchema . runtimeSchema ;
233+ const nodeSpec = schema . find ( spec => spec . name === nodeType ) ;
234+ if ( ! nodeSpec ) return false ;
159235
160- if ( obj . typeName && typeof obj . typeName === 'object' ) {
161- if ( ! ( 'location' in obj . typeName ) ) {
162- obj . typeName . location = undefined ;
163- }
164- if ( ! ( 'typemod' in obj . typeName ) ) {
165- obj . typeName . typemod = - 1 ;
166- }
236+ const fieldSpec = nodeSpec . fields . find ( field => field . name === fieldName ) ;
237+ if ( ! fieldSpec ) return false ;
238+
239+ return fieldSpec . type === 'Node' ;
240+ }
241+
242+ private getFieldType ( nodeType : string , fieldName : string , version : 13 | 14 ) : string | null {
243+ const schema = version === 13 ? pg13RuntimeSchema . runtimeSchema : pg14RuntimeSchema . runtimeSchema ;
244+ const nodeSpec = schema . find ( spec => spec . name === nodeType ) ;
245+ if ( ! nodeSpec ) return null ;
246+
247+ const fieldSpec = nodeSpec . fields . find ( field => field . name === fieldName ) ;
248+ return fieldSpec ? fieldSpec . type : null ;
249+ }
250+
251+ CreateFunctionStmt ( nodeData : any , context ?: TransformerContext ) : any {
252+ const transformedData : any = { ...nodeData } ;
253+
254+ if ( transformedData . returnType && typeof transformedData . returnType === 'object' ) {
255+ transformedData . returnType = this . TypeName ( transformedData . returnType , context ) ;
167256 }
168257
169- if ( Array . isArray ( obj ) ) {
170- obj . forEach ( item => this . ensureTypeNameFields ( item ) ) ;
171- } else {
172- Object . values ( obj ) . forEach ( value => {
173- if ( value && typeof value === 'object' ) {
174- this . ensureTypeNameFields ( value ) ;
175- }
176- } ) ;
258+ if ( transformedData . parameters && Array . isArray ( transformedData . parameters ) ) {
259+ transformedData . parameters = transformedData . parameters . map ( ( param : any ) => this . transform ( param , context ) ) ;
177260 }
261+
262+ return transformedData ;
263+ }
264+
265+ private applyRuntimeSchemaDefaults ( nodeType : string , nodeData : any ) : void {
266+ }
267+
268+ protected ensureCriticalFields ( nodeData : any , nodeType : string ) : void {
269+ super . ensureCriticalFields ( nodeData , nodeType ) ;
178270 }
179271}
0 commit comments