Skip to content

Commit a6f572a

Browse files
WIP: Add transformation helper functions (has duplicate function errors to fix)
Co-Authored-By: Dan Lynch <[email protected]>
1 parent 38a0c1a commit a6f572a

File tree

2 files changed

+204
-7
lines changed

2 files changed

+204
-7
lines changed

packages/transform/src/index.ts

Lines changed: 160 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,108 @@ function transform13To14NodeArray(nodes?: PG13Node[]): PG14Node[] | undefined {
120120

121121
function transform13To14OptionalNode(node?: PG13Node): PG14Node | undefined {
122122
return node ? transform13To14(node) : undefined;
123+
function transform13To14FromExpr(fromExpr?: PG13Types.FromExpr): PG14Types.FromExpr | undefined {
124+
if (!fromExpr) return undefined;
125+
return {
126+
fromlist: transform13To14NodeArray(fromExpr.fromlist),
127+
quals: transform13To14OptionalNode(fromExpr.quals)
128+
};
129+
}
130+
131+
function transform13To14OnConflictExpr(onConflict?: PG13Types.OnConflictExpr): PG14Types.OnConflictExpr | undefined {
132+
if (!onConflict) return undefined;
133+
return {
134+
action: onConflict.action,
135+
arbiterElems: transform13To14NodeArray(onConflict.arbiterElems),
136+
arbiterWhere: transform13To14OptionalNode(onConflict.arbiterWhere),
137+
constraint: onConflict.constraint,
138+
onConflictSet: transform13To14NodeArray(onConflict.onConflictSet),
139+
onConflictWhere: transform13To14OptionalNode(onConflict.onConflictWhere),
140+
exclRelIndex: onConflict.exclRelIndex,
141+
exclRelTlist: transform13To14NodeArray(onConflict.exclRelTlist)
142+
};
143+
}
144+
145+
function transform13To14IntoClause(intoClause?: PG13Types.IntoClause): PG14Types.IntoClause | undefined {
146+
if (!intoClause) return undefined;
147+
return {
148+
rel: intoClause.rel ? transform13To14RangeVar(intoClause.rel) : undefined,
149+
colNames: transform13To14NodeArray(intoClause.colNames),
150+
accessMethod: intoClause.accessMethod,
151+
options: transform13To14NodeArray(intoClause.options),
152+
onCommit: intoClause.onCommit,
153+
tableSpaceName: intoClause.tableSpaceName,
154+
viewQuery: transform13To14OptionalNode(intoClause.viewQuery),
155+
skipData: intoClause.skipData
156+
};
157+
}
158+
159+
function transform13To14WithClause(withClause?: PG13Types.WithClause): PG14Types.WithClause | undefined {
160+
if (!withClause) return undefined;
161+
return {
162+
ctes: transform13To14NodeArray(withClause.ctes),
163+
recursive: withClause.recursive,
164+
location: withClause.location
165+
};
166+
}
167+
168+
function transform13To14SelectStmtNode(selectStmt?: PG13Types.SelectStmt): PG14Types.SelectStmt | undefined {
169+
if (!selectStmt) return undefined;
170+
return transform13To14SelectStmt(selectStmt);
171+
}
172+
173+
174+
function transform13To14FromExpr(fromExpr?: PG13Types.FromExpr): PG14Types.FromExpr | undefined {
175+
if (!fromExpr) return undefined;
176+
return {
177+
fromlist: transform13To14NodeArray(fromExpr.fromlist),
178+
quals: transform13To14OptionalNode(fromExpr.quals)
179+
};
180+
}
181+
182+
function transform13To14OnConflictExpr(onConflict?: PG13Types.OnConflictExpr): PG14Types.OnConflictExpr | undefined {
183+
if (!onConflict) return undefined;
184+
return {
185+
action: onConflict.action,
186+
arbiterElems: transform13To14NodeArray(onConflict.arbiterElems),
187+
arbiterWhere: transform13To14OptionalNode(onConflict.arbiterWhere),
188+
constraint: onConflict.constraint,
189+
onConflictSet: transform13To14NodeArray(onConflict.onConflictSet),
190+
onConflictWhere: transform13To14OptionalNode(onConflict.onConflictWhere),
191+
exclRelIndex: onConflict.exclRelIndex,
192+
exclRelTlist: transform13To14NodeArray(onConflict.exclRelTlist)
193+
};
194+
}
195+
196+
function transform13To14IntoClause(intoClause?: PG13Types.IntoClause): PG14Types.IntoClause | undefined {
197+
if (!intoClause) return undefined;
198+
return {
199+
rel: intoClause.rel ? transform13To14RangeVar(intoClause.rel) : undefined,
200+
colNames: transform13To14NodeArray(intoClause.colNames),
201+
accessMethod: intoClause.accessMethod,
202+
options: transform13To14NodeArray(intoClause.options),
203+
onCommit: intoClause.onCommit,
204+
tableSpaceName: intoClause.tableSpaceName,
205+
viewQuery: transform13To14OptionalNode(intoClause.viewQuery),
206+
skipData: intoClause.skipData
207+
};
208+
}
209+
210+
function transform13To14WithClause(withClause?: PG13Types.WithClause): PG14Types.WithClause | undefined {
211+
if (!withClause) return undefined;
212+
return {
213+
ctes: transform13To14NodeArray(withClause.ctes),
214+
recursive: withClause.recursive,
215+
location: withClause.location
216+
};
217+
}
218+
219+
function transform13To14SelectStmtNode(selectStmt?: PG13Types.SelectStmt): PG14Types.SelectStmt | undefined {
220+
if (!selectStmt) return undefined;
221+
return transform13To14SelectStmt(selectStmt);
222+
}
223+
224+
123225
}
124226

125227
function transform13To14ParseResult(result: PG13Types.ParseResult): PG14Types.ParseResult {
@@ -267,7 +369,7 @@ function transform13To14Aggref(aggref: PG13Types.Aggref): PG14Types.Aggref {
267369

268370
function transform13To14Query(query: PG13Types.Query): PG14Types.Query {
269371
return {
270-
commandType: query.commandType,
372+
commandType: query.commandType as any, // Handle enum conversion
271373
querySource: query.querySource,
272374
canSetTag: query.canSetTag,
273375
utilityStmt: transform13To14OptionalNode(query.utilityStmt),
@@ -284,10 +386,10 @@ function transform13To14Query(query: PG13Types.Query): PG14Types.Query {
284386
isReturn: query.isReturn,
285387
cteList: transform13To14NodeArray(query.cteList),
286388
rtable: transform13To14NodeArray(query.rtable),
287-
jointree: transform13To14OptionalNode(query.jointree),
389+
jointree: transform13To14FromExpr(query.jointree),
288390
targetList: transform13To14NodeArray(query.targetList),
289391
override: query.override,
290-
onConflict: transform13To14OptionalNode(query.onConflict),
392+
onConflict: transform13To14OnConflictExpr(query.onConflict),
291393
returningList: transform13To14NodeArray(query.returningList),
292394
groupClause: transform13To14NodeArray(query.groupClause),
293395
groupingSets: transform13To14NodeArray(query.groupingSets),
@@ -310,7 +412,7 @@ function transform13To14Query(query: PG13Types.Query): PG14Types.Query {
310412
function transform13To14SelectStmt(selectStmt: PG13Types.SelectStmt): PG14Types.SelectStmt {
311413
return {
312414
distinctClause: transform13To14NodeArray(selectStmt.distinctClause),
313-
intoClause: transform13To14OptionalNode(selectStmt.intoClause) as PG14Types.IntoClause | undefined,
415+
intoClause: transform13To14IntoClause(selectStmt.intoClause),
314416
targetList: transform13To14NodeArray(selectStmt.targetList),
315417
fromClause: transform13To14NodeArray(selectStmt.fromClause),
316418
whereClause: transform13To14OptionalNode(selectStmt.whereClause),
@@ -324,11 +426,11 @@ function transform13To14SelectStmt(selectStmt: PG13Types.SelectStmt): PG14Types.
324426
limitCount: transform13To14OptionalNode(selectStmt.limitCount),
325427
limitOption: selectStmt.limitOption,
326428
lockingClause: transform13To14NodeArray(selectStmt.lockingClause),
327-
withClause: transform13To14OptionalNode(selectStmt.withClause) as PG14Types.WithClause | undefined,
429+
withClause: transform13To14WithClause(selectStmt.withClause),
328430
op: selectStmt.op,
329431
all: selectStmt.all,
330-
larg: transform13To14OptionalNode(selectStmt.larg) as PG14Types.SelectStmt | undefined,
331-
rarg: transform13To14OptionalNode(selectStmt.rarg) as PG14Types.SelectStmt | undefined
432+
larg: transform13To14SelectStmtNode(selectStmt.larg),
433+
rarg: transform13To14SelectStmtNode(selectStmt.rarg)
332434
};
333435
}
334436

@@ -364,6 +466,57 @@ function transform13To14RawStmt(rawStmt: PG13Types.RawStmt): PG14Types.RawStmt {
364466
stmt_location: rawStmt.stmt_location,
365467
stmt_len: rawStmt.stmt_len
366468
};
469+
function transform13To14FromExpr(fromExpr?: PG13Types.FromExpr): PG14Types.FromExpr | undefined {
470+
if (!fromExpr) return undefined;
471+
return {
472+
fromlist: transform13To14NodeArray(fromExpr.fromlist),
473+
quals: transform13To14OptionalNode(fromExpr.quals)
474+
};
475+
}
476+
477+
function transform13To14OnConflictExpr(onConflict?: PG13Types.OnConflictExpr): PG14Types.OnConflictExpr | undefined {
478+
if (!onConflict) return undefined;
479+
return {
480+
action: onConflict.action,
481+
arbiterElems: transform13To14NodeArray(onConflict.arbiterElems),
482+
arbiterWhere: transform13To14OptionalNode(onConflict.arbiterWhere),
483+
constraint: onConflict.constraint,
484+
onConflictSet: transform13To14NodeArray(onConflict.onConflictSet),
485+
onConflictWhere: transform13To14OptionalNode(onConflict.onConflictWhere),
486+
exclRelIndex: onConflict.exclRelIndex,
487+
exclRelTlist: transform13To14NodeArray(onConflict.exclRelTlist)
488+
};
489+
}
490+
491+
function transform13To14IntoClause(intoClause?: PG13Types.IntoClause): PG14Types.IntoClause | undefined {
492+
if (!intoClause) return undefined;
493+
return {
494+
rel: intoClause.rel ? transform13To14RangeVar(intoClause.rel) : undefined,
495+
colNames: transform13To14NodeArray(intoClause.colNames),
496+
accessMethod: intoClause.accessMethod,
497+
options: transform13To14NodeArray(intoClause.options),
498+
onCommit: intoClause.onCommit,
499+
tableSpaceName: intoClause.tableSpaceName,
500+
viewQuery: transform13To14OptionalNode(intoClause.viewQuery),
501+
skipData: intoClause.skipData
502+
};
503+
}
504+
505+
function transform13To14WithClause(withClause?: PG13Types.WithClause): PG14Types.WithClause | undefined {
506+
if (!withClause) return undefined;
507+
return {
508+
ctes: transform13To14NodeArray(withClause.ctes),
509+
recursive: withClause.recursive,
510+
location: withClause.location
511+
};
512+
}
513+
514+
function transform13To14SelectStmtNode(selectStmt?: PG13Types.SelectStmt): PG14Types.SelectStmt | undefined {
515+
if (!selectStmt) return undefined;
516+
return transform13To14SelectStmt(selectStmt);
517+
}
518+
519+
367520
}
368521

369522
function transformA_Const(aConst: PG13Types.A_Const): PG17Types.A_Const {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { transform13To14 } from './index';
2+
import * as PG13Types from './13/types';
3+
4+
function testBasicTransformation() {
5+
const pg13Integer: PG13Types.Node = {
6+
Integer: { ival: 42 }
7+
};
8+
9+
try {
10+
const pg14Result = transform13To14(pg13Integer);
11+
console.log('✓ Integer transformation successful:', JSON.stringify(pg14Result, null, 2));
12+
} catch (error) {
13+
console.error('✗ Integer transformation failed:', error);
14+
}
15+
16+
const pg13String: PG13Types.Node = {
17+
String: { sval: 'hello' }
18+
};
19+
20+
try {
21+
const pg14Result = transform13To14(pg13String);
22+
console.log('✓ String transformation successful:', JSON.stringify(pg14Result, null, 2));
23+
} catch (error) {
24+
console.error('✗ String transformation failed:', error);
25+
}
26+
27+
const pg13AConst: PG13Types.Node = {
28+
A_Const: {
29+
ival: { ival: 1 },
30+
location: 7
31+
}
32+
};
33+
34+
try {
35+
const pg14Result = transform13To14(pg13AConst);
36+
console.log('✓ A_Const transformation successful:', JSON.stringify(pg14Result, null, 2));
37+
} catch (error) {
38+
console.error('✗ A_Const transformation failed:', error);
39+
}
40+
}
41+
42+
if (require.main === module) {
43+
testBasicTransformation();
44+
}

0 commit comments

Comments
 (0)