MCPcopy Create free account
hub / github.com/andywer/postguard / resolveColumnReference

Function resolveColumnReference

src/postgres/resolve-references.ts:34–104  ·  view source on GitHub ↗
(
  path: QueryNodePath<QueryParser.ColumnRef>,
  relationRefs: Array<QueryNodePath<QueryParser.RelationRef>>
)

Source from the content-addressed store, hash-verified

32}
33
34export function resolveColumnReference(
35 path: QueryNodePath<QueryParser.ColumnRef>,
36 relationRefs: Array<QueryNodePath<QueryParser.RelationRef>>
37): ColumnReference | null {
38 const { fields } = path.node.ColumnRef
39
40 if (fields.length === 1) {
41 const [columnNode] = fields
42 if (isPgString(columnNode)) {
43 const tableRefsInScope: TableReference[] = relationRefs.map(ref => ({
44 as: ref.node.RangeVar.alias ? ref.node.RangeVar.alias.Alias.aliasname : undefined,
45 tableName: ref.node.RangeVar.relname,
46 path
47 }))
48 return {
49 tableRefsInScope: filterDuplicateTableRefs(tableRefsInScope),
50 columnName: columnNode.String.str,
51 path
52 }
53 } else if (isStar(columnNode)) {
54 if (relationRefs.length !== 1) {
55 throw new Error(
56 `Can only have unqualified * selector if only one table is referenced in the (sub-)query.\n` +
57 `Tables in scope: ${
58 relationRefs.length === 0
59 ? "(none)"
60 : relationRefs.map(ref => ref.node.RangeVar.relname)
61 }`
62 )
63 }
64 const tableRef = relationRefs[0]
65 return {
66 tableName: tableRef.node.RangeVar.alias
67 ? tableRef.node.RangeVar.alias.Alias.aliasname
68 : tableRef.node.RangeVar.relname,
69 columnName: "*",
70 path
71 }
72 }
73 } else if (fields.length === 2) {
74 const [tableNode, columnNode] = fields
75 if (!isPgString(tableNode)) {
76 throw new Error(
77 `Expected first identifier in column reference to be a string. Got ${getNodeType(
78 tableNode
79 )}`
80 )
81 }
82 if (isPgString(columnNode)) {
83 return {
84 tableName: resolveTableName(tableNode.String.str, relationRefs),
85 columnName: columnNode.String.str,
86 path
87 }
88 } else if (isStar(columnNode)) {
89 return {
90 tableName: resolveTableName(tableNode.String.str, relationRefs),
91 columnName: "*",

Callers 2

getReferencedColumnsFunction · 0.90
resolveResTargetFunction · 0.85

Calls 5

isPgStringFunction · 0.90
isStarFunction · 0.90
getNodeTypeFunction · 0.90
filterDuplicateTableRefsFunction · 0.85
resolveTableNameFunction · 0.85

Tested by

no test coverage detected