* refnameNamespaceItem * Given a possibly-qualified refname, look to see if it matches any visible * namespace item. If so, return a pointer to the nsitem; else return NULL. * * Optionally get nsitem's nesting depth (0 = current) into *sublevels_up. * If sublevels_up is NULL, only consider items at the current nesting * level. * * An unqualified refname (schemaname == NULL) can
| 112 | * peculiar, but it's what SQL says to do. |
| 113 | */ |
| 114 | ParseNamespaceItem * |
| 115 | refnameNamespaceItem(ParseState *pstate, |
| 116 | const char *schemaname, |
| 117 | const char *refname, |
| 118 | int location, |
| 119 | int *sublevels_up) |
| 120 | { |
| 121 | Oid relId = InvalidOid; |
| 122 | |
| 123 | if (sublevels_up) |
| 124 | *sublevels_up = 0; |
| 125 | |
| 126 | if (schemaname != NULL) |
| 127 | { |
| 128 | Oid namespaceId; |
| 129 | |
| 130 | /* |
| 131 | * We can use LookupNamespaceNoError() here because we are only |
| 132 | * interested in finding existing RTEs. Checking USAGE permission on |
| 133 | * the schema is unnecessary since it would have already been checked |
| 134 | * when the RTE was made. Furthermore, we want to report "RTE not |
| 135 | * found", not "no permissions for schema", if the name happens to |
| 136 | * match a schema name the user hasn't got access to. |
| 137 | */ |
| 138 | namespaceId = LookupNamespaceNoError(schemaname); |
| 139 | if (!OidIsValid(namespaceId)) |
| 140 | return NULL; |
| 141 | relId = get_relname_relid(refname, namespaceId); |
| 142 | if (!OidIsValid(relId)) |
| 143 | return NULL; |
| 144 | } |
| 145 | |
| 146 | while (pstate != NULL) |
| 147 | { |
| 148 | ParseNamespaceItem *result; |
| 149 | |
| 150 | if (OidIsValid(relId)) |
| 151 | result = scanNameSpaceForRelid(pstate, relId, location); |
| 152 | else |
| 153 | result = scanNameSpaceForRefname(pstate, refname, location); |
| 154 | |
| 155 | if (result) |
| 156 | return result; |
| 157 | |
| 158 | if (sublevels_up) |
| 159 | (*sublevels_up)++; |
| 160 | else |
| 161 | break; |
| 162 | |
| 163 | pstate = pstate->parentParseState; |
| 164 | } |
| 165 | return NULL; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Search the query's table namespace for an item matching the |
no test coverage detected