1313 * Portions Copyright (c) 1994, Regents of the University of California
1414 *
1515 * IDENTIFICATION
16- * $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.26 2002/07/20 05:16:56 momjian Exp $
16+ * $Header: /cvsroot/pgsql/src/backend/catalog/namespace.c,v 1.27 2002/07/29 23:46:35 tgl Exp $
1717 *
1818 *-------------------------------------------------------------------------
1919 */
@@ -164,18 +164,7 @@ RangeVarGetRelid(const RangeVar *relation, bool failOK)
164164 if (relation -> schemaname )
165165 {
166166 /* use exact schema given */
167- AclResult aclresult ;
168-
169- namespaceId = GetSysCacheOid (NAMESPACENAME ,
170- CStringGetDatum (relation -> schemaname ),
171- 0 , 0 , 0 );
172- if (!OidIsValid (namespaceId ))
173- elog (ERROR , "Namespace \"%s\" does not exist" ,
174- relation -> schemaname );
175- aclresult = pg_namespace_aclcheck (namespaceId , GetUserId (), ACL_USAGE );
176- if (aclresult != ACLCHECK_OK )
177- aclcheck_error (aclresult , relation -> schemaname );
178-
167+ namespaceId = LookupExplicitNamespace (relation -> schemaname );
179168 relId = get_relname_relid (relation -> relname , namespaceId );
180169 }
181170 else
@@ -239,6 +228,7 @@ RangeVarGetCreationNamespace(const RangeVar *newRelation)
239228 if (!OidIsValid (namespaceId ))
240229 elog (ERROR , "Namespace \"%s\" does not exist" ,
241230 newRelation -> schemaname );
231+ /* we do not check for USAGE rights here! */
242232 }
243233 else
244234 {
@@ -431,53 +421,19 @@ FuncCandidateList
431421FuncnameGetCandidates (List * names , int nargs )
432422{
433423 FuncCandidateList resultList = NULL ;
434- char * catalogname ;
435- char * schemaname = NULL ;
436- char * funcname = NULL ;
424+ char * schemaname ;
425+ char * funcname ;
437426 Oid namespaceId ;
438427 CatCList * catlist ;
439428 int i ;
440429
441430 /* deconstruct the name list */
442- switch (length (names ))
443- {
444- case 1 :
445- funcname = strVal (lfirst (names ));
446- break ;
447- case 2 :
448- schemaname = strVal (lfirst (names ));
449- funcname = strVal (lsecond (names ));
450- break ;
451- case 3 :
452- catalogname = strVal (lfirst (names ));
453- schemaname = strVal (lsecond (names ));
454- funcname = strVal (lfirst (lnext (lnext (names ))));
455- /*
456- * We check the catalog name and then ignore it.
457- */
458- if (strcmp (catalogname , DatabaseName ) != 0 )
459- elog (ERROR , "Cross-database references are not implemented" );
460- break ;
461- default :
462- elog (ERROR , "Improper qualified name (too many dotted names): %s" ,
463- NameListToString (names ));
464- break ;
465- }
431+ DeconstructQualifiedName (names , & schemaname , & funcname );
466432
467433 if (schemaname )
468434 {
469435 /* use exact schema given */
470- AclResult aclresult ;
471-
472- namespaceId = GetSysCacheOid (NAMESPACENAME ,
473- CStringGetDatum (schemaname ),
474- 0 , 0 , 0 );
475- if (!OidIsValid (namespaceId ))
476- elog (ERROR , "Namespace \"%s\" does not exist" ,
477- schemaname );
478- aclresult = pg_namespace_aclcheck (namespaceId , GetUserId (), ACL_USAGE );
479- if (aclresult != ACLCHECK_OK )
480- aclcheck_error (aclresult , schemaname );
436+ namespaceId = LookupExplicitNamespace (schemaname );
481437 }
482438 else
483439 {
@@ -684,53 +640,19 @@ FuncCandidateList
684640OpernameGetCandidates (List * names , char oprkind )
685641{
686642 FuncCandidateList resultList = NULL ;
687- char * catalogname ;
688- char * schemaname = NULL ;
689- char * opername = NULL ;
643+ char * schemaname ;
644+ char * opername ;
690645 Oid namespaceId ;
691646 CatCList * catlist ;
692647 int i ;
693648
694649 /* deconstruct the name list */
695- switch (length (names ))
696- {
697- case 1 :
698- opername = strVal (lfirst (names ));
699- break ;
700- case 2 :
701- schemaname = strVal (lfirst (names ));
702- opername = strVal (lsecond (names ));
703- break ;
704- case 3 :
705- catalogname = strVal (lfirst (names ));
706- schemaname = strVal (lsecond (names ));
707- opername = strVal (lfirst (lnext (lnext (names ))));
708- /*
709- * We check the catalog name and then ignore it.
710- */
711- if (strcmp (catalogname , DatabaseName ) != 0 )
712- elog (ERROR , "Cross-database references are not implemented" );
713- break ;
714- default :
715- elog (ERROR , "Improper qualified name (too many dotted names): %s" ,
716- NameListToString (names ));
717- break ;
718- }
650+ DeconstructQualifiedName (names , & schemaname , & opername );
719651
720652 if (schemaname )
721653 {
722654 /* use exact schema given */
723- AclResult aclresult ;
724-
725- namespaceId = GetSysCacheOid (NAMESPACENAME ,
726- CStringGetDatum (schemaname ),
727- 0 , 0 , 0 );
728- if (!OidIsValid (namespaceId ))
729- elog (ERROR , "Namespace \"%s\" does not exist" ,
730- schemaname );
731- aclresult = pg_namespace_aclcheck (namespaceId , GetUserId (), ACL_USAGE );
732- if (aclresult != ACLCHECK_OK )
733- aclcheck_error (aclresult , schemaname );
655+ namespaceId = LookupExplicitNamespace (schemaname );
734656 }
735657 else
736658 {
@@ -1105,25 +1027,22 @@ OpclassIsVisible(Oid opcid)
11051027 return visible ;
11061028}
11071029
1108-
11091030/*
1110- * QualifiedNameGetCreationNamespace
1111- * Given a possibly-qualified name for an object (in List-of-Values
1112- * format), determine what namespace the object should be created in.
1113- * Also extract and return the object name (last component of list).
1031+ * DeconstructQualifiedName
1032+ * Given a possibly-qualified name expressed as a list of String nodes,
1033+ * extract the schema name and object name.
11141034 *
1115- * This is *not* used for tables. Hence, the TEMP table namespace is
1116- * never selected as the creation target.
1035+ * *nspname_p is set to NULL if there is no explicit schema name.
11171036 */
1118- Oid
1119- QualifiedNameGetCreationNamespace (List * names , char * * objname_p )
1037+ void
1038+ DeconstructQualifiedName (List * names ,
1039+ char * * nspname_p ,
1040+ char * * objname_p )
11201041{
11211042 char * catalogname ;
11221043 char * schemaname = NULL ;
11231044 char * objname = NULL ;
1124- Oid namespaceId ;
11251045
1126- /* deconstruct the name list */
11271046 switch (length (names ))
11281047 {
11291048 case 1 :
@@ -1149,6 +1068,55 @@ QualifiedNameGetCreationNamespace(List *names, char **objname_p)
11491068 break ;
11501069 }
11511070
1071+ * nspname_p = schemaname ;
1072+ * objname_p = objname ;
1073+ }
1074+
1075+ /*
1076+ * LookupExplicitNamespace
1077+ * Process an explicitly-specified schema name: look up the schema
1078+ * and verify we have USAGE (lookup) rights in it.
1079+ *
1080+ * Returns the namespace OID. Raises elog if any problem.
1081+ */
1082+ Oid
1083+ LookupExplicitNamespace (char * nspname )
1084+ {
1085+ Oid namespaceId ;
1086+ AclResult aclresult ;
1087+
1088+ namespaceId = GetSysCacheOid (NAMESPACENAME ,
1089+ CStringGetDatum (nspname ),
1090+ 0 , 0 , 0 );
1091+ if (!OidIsValid (namespaceId ))
1092+ elog (ERROR , "Namespace \"%s\" does not exist" , nspname );
1093+
1094+ aclresult = pg_namespace_aclcheck (namespaceId , GetUserId (), ACL_USAGE );
1095+ if (aclresult != ACLCHECK_OK )
1096+ aclcheck_error (aclresult , nspname );
1097+
1098+ return namespaceId ;
1099+ }
1100+
1101+ /*
1102+ * QualifiedNameGetCreationNamespace
1103+ * Given a possibly-qualified name for an object (in List-of-Values
1104+ * format), determine what namespace the object should be created in.
1105+ * Also extract and return the object name (last component of list).
1106+ *
1107+ * This is *not* used for tables. Hence, the TEMP table namespace is
1108+ * never selected as the creation target.
1109+ */
1110+ Oid
1111+ QualifiedNameGetCreationNamespace (List * names , char * * objname_p )
1112+ {
1113+ char * schemaname ;
1114+ char * objname ;
1115+ Oid namespaceId ;
1116+
1117+ /* deconstruct the name list */
1118+ DeconstructQualifiedName (names , & schemaname , & objname );
1119+
11521120 if (schemaname )
11531121 {
11541122 /* use exact schema given */
@@ -1158,6 +1126,7 @@ QualifiedNameGetCreationNamespace(List *names, char **objname_p)
11581126 if (!OidIsValid (namespaceId ))
11591127 elog (ERROR , "Namespace \"%s\" does not exist" ,
11601128 schemaname );
1129+ /* we do not check for USAGE rights here! */
11611130 }
11621131 else
11631132 {
0 commit comments