13
13
* Portions Copyright (c) 1994, Regents of the University of California
14
14
*
15
15
* 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 $
17
17
*
18
18
*-------------------------------------------------------------------------
19
19
*/
@@ -164,18 +164,7 @@ RangeVarGetRelid(const RangeVar *relation, bool failOK)
164
164
if (relation -> schemaname )
165
165
{
166
166
/* 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 );
179
168
relId = get_relname_relid (relation -> relname , namespaceId );
180
169
}
181
170
else
@@ -239,6 +228,7 @@ RangeVarGetCreationNamespace(const RangeVar *newRelation)
239
228
if (!OidIsValid (namespaceId ))
240
229
elog (ERROR , "Namespace \"%s\" does not exist" ,
241
230
newRelation -> schemaname );
231
+ /* we do not check for USAGE rights here! */
242
232
}
243
233
else
244
234
{
@@ -431,53 +421,19 @@ FuncCandidateList
431
421
FuncnameGetCandidates (List * names , int nargs )
432
422
{
433
423
FuncCandidateList resultList = NULL ;
434
- char * catalogname ;
435
- char * schemaname = NULL ;
436
- char * funcname = NULL ;
424
+ char * schemaname ;
425
+ char * funcname ;
437
426
Oid namespaceId ;
438
427
CatCList * catlist ;
439
428
int i ;
440
429
441
430
/* 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 );
466
432
467
433
if (schemaname )
468
434
{
469
435
/* 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 );
481
437
}
482
438
else
483
439
{
@@ -684,53 +640,19 @@ FuncCandidateList
684
640
OpernameGetCandidates (List * names , char oprkind )
685
641
{
686
642
FuncCandidateList resultList = NULL ;
687
- char * catalogname ;
688
- char * schemaname = NULL ;
689
- char * opername = NULL ;
643
+ char * schemaname ;
644
+ char * opername ;
690
645
Oid namespaceId ;
691
646
CatCList * catlist ;
692
647
int i ;
693
648
694
649
/* 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 );
719
651
720
652
if (schemaname )
721
653
{
722
654
/* 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 );
734
656
}
735
657
else
736
658
{
@@ -1105,25 +1027,22 @@ OpclassIsVisible(Oid opcid)
1105
1027
return visible ;
1106
1028
}
1107
1029
1108
-
1109
1030
/*
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.
1114
1034
*
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.
1117
1036
*/
1118
- Oid
1119
- QualifiedNameGetCreationNamespace (List * names , char * * objname_p )
1037
+ void
1038
+ DeconstructQualifiedName (List * names ,
1039
+ char * * nspname_p ,
1040
+ char * * objname_p )
1120
1041
{
1121
1042
char * catalogname ;
1122
1043
char * schemaname = NULL ;
1123
1044
char * objname = NULL ;
1124
- Oid namespaceId ;
1125
1045
1126
- /* deconstruct the name list */
1127
1046
switch (length (names ))
1128
1047
{
1129
1048
case 1 :
@@ -1149,6 +1068,55 @@ QualifiedNameGetCreationNamespace(List *names, char **objname_p)
1149
1068
break ;
1150
1069
}
1151
1070
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
+
1152
1120
if (schemaname )
1153
1121
{
1154
1122
/* use exact schema given */
@@ -1158,6 +1126,7 @@ QualifiedNameGetCreationNamespace(List *names, char **objname_p)
1158
1126
if (!OidIsValid (namespaceId ))
1159
1127
elog (ERROR , "Namespace \"%s\" does not exist" ,
1160
1128
schemaname );
1129
+ /* we do not check for USAGE rights here! */
1161
1130
}
1162
1131
else
1163
1132
{
0 commit comments