MCPcopy Index your code
hub / github.com/adelsz/pgtyped / processSQLQueryIR

Function processSQLQueryIR

packages/runtime/src/preprocessor-sql.ts:15–173  ·  view source on GitHub ↗
(
  queryIR: SQLQueryIR,
  passedParams?: QueryParameters,
)

Source from the content-addressed store, hash-verified

13
14/* Processes query AST formed by new parser from pure SQL files */
15export const processSQLQueryIR = (
16 queryIR: SQLQueryIR,
17 passedParams?: QueryParameters,
18): InterpolatedQuery => {
19 const bindings: Scalar[] = [];
20 const paramMapping: QueryParameter[] = [];
21 const usedParams = queryIR.params.filter(
22 (p) => p.name in queryIR.usedParamSet,
23 );
24 let i = 1;
25 const intervals: { a: number; b: number; sub: string }[] = [];
26 for (const usedParam of usedParams) {
27 // Handle spread transform
28 if (usedParam.transform.type === TransformType.ArraySpread) {
29 let sub: string;
30 if (passedParams) {
31 const paramValue = passedParams[usedParam.name];
32 sub = (paramValue as Scalar[])
33 .map((val) => {
34 bindings.push(val);
35 return `$${i++}`;
36 })
37 .join(',');
38 } else {
39 const idx = i++;
40 paramMapping.push({
41 name: usedParam.name,
42 type: ParameterTransform.Spread,
43 assignedIndex: idx,
44 required: usedParam.required,
45 } as ScalarArrayParameter);
46 sub = `$${idx}`;
47 }
48 usedParam.locs.forEach((loc) =>
49 intervals.push({
50 ...loc,
51 sub: `(${sub})`,
52 }),
53 );
54 continue;
55 }
56
57 // Handle pick transform
58 if (usedParam.transform.type === TransformType.PickTuple) {
59 const dict: {
60 [key: string]: ScalarParameter;
61 } = {};
62 const sub = usedParam.transform.keys
63 .map(({ name, required }) => {
64 const idx = i++;
65 dict[name] = {
66 name,
67 required,
68 type: ParameterTransform.Scalar,
69 assignedIndex: idx,
70 } as ScalarParameter;
71 if (passedParams) {
72 const paramValue = passedParams[usedParam.name] as NestedParameters;

Callers 3

queryToTypeDeclarationsFunction · 0.90
constructorMethod · 0.85

Calls 2

assertFunction · 0.90
replaceIntervalsFunction · 0.85

Tested by

no test coverage detected