1 /*-------------------------------------------------------------------------
4 * code to support accessing and searching namespaces
6 * This is separate from pg_namespace.c, which contains the routines that
7 * directly manipulate the pg_namespace system catalog. This module
8 * provides routines associated with defining a "namespace search path"
9 * and implementing search-path-controlled searches.
12 * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
16 * $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.2 2002/03/29 19:06:01 tgl Exp $
18 *-------------------------------------------------------------------------
22 #include "catalog/namespace.h"
23 #include "catalog/pg_namespace.h"
24 #include "miscadmin.h"
25 #include "nodes/makefuncs.h"
26 #include "utils/lsyscache.h"
27 #include "utils/syscache.h"
32 * Given a RangeVar describing an existing relation,
33 * select the proper namespace and look up the relation OID.
35 * If the relation is not found, return InvalidOid if failOK = true,
36 * otherwise raise an error.
39 RangeVarGetRelid(const RangeVar *relation, bool failOK)
45 * We check the catalog name and then ignore it.
47 if (relation->catalogname)
49 if (strcmp(relation->catalogname, DatabaseName) != 0)
50 elog(ERROR, "Cross-database references are not implemented");
53 if (relation->schemaname)
55 namespaceId = GetSysCacheOid(NAMESPACENAME,
56 CStringGetDatum(relation->schemaname),
58 if (!OidIsValid(namespaceId))
59 elog(ERROR, "Namespace \"%s\" does not exist",
60 relation->schemaname);
61 relId = get_relname_relid(relation->relname, namespaceId);
65 relId = RelnameGetRelid(relation->relname);
68 if (!OidIsValid(relId) && !failOK)
70 if (relation->schemaname)
71 elog(ERROR, "Relation \"%s\".\"%s\" does not exist",
72 relation->schemaname, relation->relname);
74 elog(ERROR, "Relation \"%s\" does not exist",
81 * RangeVarGetCreationNamespace
82 * Given a RangeVar describing a to-be-created relation,
83 * choose which namespace to create it in.
86 RangeVarGetCreationNamespace(const RangeVar *newRelation)
91 * We check the catalog name and then ignore it.
93 if (newRelation->catalogname)
95 if (strcmp(newRelation->catalogname, DatabaseName) != 0)
96 elog(ERROR, "Cross-database references are not implemented");
99 if (newRelation->schemaname)
101 namespaceId = GetSysCacheOid(NAMESPACENAME,
102 CStringGetDatum(newRelation->schemaname),
104 if (!OidIsValid(namespaceId))
105 elog(ERROR, "Namespace \"%s\" does not exist",
106 newRelation->schemaname);
110 /* XXX Wrong! Need to get a default schema from somewhere */
111 namespaceId = PG_CATALOG_NAMESPACE;
119 * Try to resolve an unqualified relation name.
120 * Returns OID if relation found in search path, else InvalidOid.
123 RelnameGetRelid(const char *relname)
125 /* XXX Wrong! must search search path */
126 return get_relname_relid(relname, PG_CATALOG_NAMESPACE);
130 * QualifiedNameGetCreationNamespace
131 * Given a possibly-qualified name for an object (in List-of-Values
132 * format), determine what namespace the object should be created in.
133 * Also extract and return the object name (last component of list).
136 QualifiedNameGetCreationNamespace(List *names, char **objname_p)
139 char *schemaname = NULL;
140 char *objname = NULL;
143 /* deconstruct the name list */
144 switch (length(names))
147 objname = strVal(lfirst(names));
150 schemaname = strVal(lfirst(names));
151 objname = strVal(lsecond(names));
154 catalogname = strVal(lfirst(names));
155 schemaname = strVal(lsecond(names));
156 objname = strVal(lfirst(lnext(lnext(names))));
158 * We check the catalog name and then ignore it.
160 if (strcmp(catalogname, DatabaseName) != 0)
161 elog(ERROR, "Cross-database references are not implemented");
164 elog(ERROR, "Improper qualified name (too many dotted names)");
170 namespaceId = GetSysCacheOid(NAMESPACENAME,
171 CStringGetDatum(schemaname),
173 if (!OidIsValid(namespaceId))
174 elog(ERROR, "Namespace \"%s\" does not exist",
179 /* XXX Wrong! Need to get a default schema from somewhere */
180 namespaceId = PG_CATALOG_NAMESPACE;
183 *objname_p = objname;
188 * makeRangeVarFromNameList
189 * Utility routine to convert a qualified-name list into RangeVar form.
192 makeRangeVarFromNameList(List *names)
194 RangeVar *rel = makeRangeVar(NULL, NULL);
196 switch (length(names))
199 rel->relname = strVal(lfirst(names));
202 rel->schemaname = strVal(lfirst(names));
203 rel->relname = strVal(lsecond(names));
206 rel->catalogname = strVal(lfirst(names));
207 rel->schemaname = strVal(lsecond(names));
208 rel->relname = strVal(lfirst(lnext(lnext(names))));
211 elog(ERROR, "Improper relation name (too many dotted names)");