📄 comment.c
字号:
RelationGetRelationName(relation)); /* Now, fetch the attribute number from the system cache */ attnum = get_attnum(RelationGetRelid(relation), attrname); if (attnum == InvalidAttrNumber) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_COLUMN), errmsg("column \"%s\" of relation \"%s\" does not exist", attrname, RelationGetRelationName(relation)))); /* Create the comment using the relation's oid */ CreateComments(RelationGetRelid(relation), RelationRelationId, (int32) attnum, comment); /* Done, but hold lock until commit */ relation_close(relation, NoLock);}/* * CommentDatabase -- * * This routine is used to add/drop any user-comments a user might * have regarding the specified database. The routine will check * security for owner permissions, and, if successful, will then * attempt to find the oid of the database specified. Once found, * a comment is added/dropped using the CreateComments() routine. */static voidCommentDatabase(List *qualname, char *comment){ char *database; Oid oid; if (list_length(qualname) != 1) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("database name may not be qualified"))); database = strVal(linitial(qualname)); /* * We cannot currently support cross-database comments (since other DBs * cannot see pg_description of this database). So, we reject attempts to * comment on a database other than the current one. Someday this might be * improved, but it would take a redesigned infrastructure. * * When loading a dump, we may see a COMMENT ON DATABASE for the old name * of the database. Erroring out would prevent pg_restore from completing * (which is really pg_restore's fault, but for now we will work around * the problem here). Consensus is that the best fix is to treat wrong * database name as a WARNING not an ERROR. */ /* First get the database OID */ oid = get_database_oid(database); if (!OidIsValid(oid)) { ereport(WARNING, (errcode(ERRCODE_UNDEFINED_DATABASE), errmsg("database \"%s\" does not exist", database))); return; } /* Only allow comments on the current database */ if (oid != MyDatabaseId) { ereport(WARNING, /* throw just a warning so pg_restore doesn't * fail */ (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("database comments may only be applied to the current database"))); return; } /* Check object security */ if (!pg_database_ownercheck(oid, GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_DATABASE, database); /* Call CreateComments() to create/drop the comments */ CreateComments(oid, DatabaseRelationId, 0, comment);}/* * CommentNamespace -- * * This routine is used to add/drop any user-comments a user might * have regarding the specified namespace. The routine will check * security for owner permissions, and, if successful, will then * attempt to find the oid of the namespace specified. Once found, * a comment is added/dropped using the CreateComments() routine. */static voidCommentNamespace(List *qualname, char *comment){ Oid oid; char *namespace; if (list_length(qualname) != 1) ereport(ERROR, (errcode(ERRCODE_SYNTAX_ERROR), errmsg("schema name may not be qualified"))); namespace = strVal(linitial(qualname)); oid = GetSysCacheOid(NAMESPACENAME, CStringGetDatum(namespace), 0, 0, 0); if (!OidIsValid(oid)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_SCHEMA), errmsg("schema \"%s\" does not exist", namespace))); /* Check object security */ if (!pg_namespace_ownercheck(oid, GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_NAMESPACE, namespace); /* Call CreateComments() to create/drop the comments */ CreateComments(oid, NamespaceRelationId, 0, comment);}/* * CommentRule -- * * This routine is used to add/drop any user-comments a user might * have regarding a specified RULE. The rule for commenting is determined by * both its name and the relation to which it refers. The arguments to this * function are the rule name and relation name (merged into a qualified * name), and the comment to add/drop. * * Before PG 7.3, rules had unique names across the whole database, and so * the syntax was just COMMENT ON RULE rulename, with no relation name. * For purposes of backwards compatibility, we support that as long as there * is only one rule by the specified name in the database. */static voidCommentRule(List *qualname, char *comment){ int nnames; List *relname; char *rulename; RangeVar *rel; Relation relation; HeapTuple tuple; Oid reloid; Oid ruleoid; AclResult aclcheck; /* Separate relname and trig name */ nnames = list_length(qualname); if (nnames == 1) { /* Old-style: only a rule name is given */ Relation RewriteRelation; HeapScanDesc scanDesc; ScanKeyData scanKeyData; rulename = strVal(linitial(qualname)); /* Search pg_rewrite for such a rule */ ScanKeyInit(&scanKeyData, Anum_pg_rewrite_rulename, BTEqualStrategyNumber, F_NAMEEQ, PointerGetDatum(rulename)); RewriteRelation = heap_open(RewriteRelationId, AccessShareLock); scanDesc = heap_beginscan(RewriteRelation, SnapshotNow, 1, &scanKeyData); tuple = heap_getnext(scanDesc, ForwardScanDirection); if (HeapTupleIsValid(tuple)) { reloid = ((Form_pg_rewrite) GETSTRUCT(tuple))->ev_class; ruleoid = HeapTupleGetOid(tuple); } else { ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("rule \"%s\" does not exist", rulename))); reloid = ruleoid = 0; /* keep compiler quiet */ } if (HeapTupleIsValid(tuple = heap_getnext(scanDesc, ForwardScanDirection))) ereport(ERROR, (errcode(ERRCODE_DUPLICATE_OBJECT), errmsg("there are multiple rules named \"%s\"", rulename), errhint("Specify a relation name as well as a rule name."))); heap_endscan(scanDesc); heap_close(RewriteRelation, AccessShareLock); /* Open the owning relation to ensure it won't go away meanwhile */ relation = heap_open(reloid, AccessShareLock); } else { /* New-style: rule and relname both provided */ Assert(nnames >= 2); relname = list_truncate(list_copy(qualname), nnames - 1); rulename = strVal(lfirst(list_tail(qualname))); /* Open the owning relation to ensure it won't go away meanwhile */ rel = makeRangeVarFromNameList(relname); relation = heap_openrv(rel, AccessShareLock); reloid = RelationGetRelid(relation); /* Find the rule's pg_rewrite tuple, get its OID */ tuple = SearchSysCache(RULERELNAME, ObjectIdGetDatum(reloid), PointerGetDatum(rulename), 0, 0); if (!HeapTupleIsValid(tuple)) ereport(ERROR, (errcode(ERRCODE_UNDEFINED_OBJECT), errmsg("rule \"%s\" for relation \"%s\" does not exist", rulename, RelationGetRelationName(relation)))); Assert(reloid == ((Form_pg_rewrite) GETSTRUCT(tuple))->ev_class); ruleoid = HeapTupleGetOid(tuple); ReleaseSysCache(tuple); } /* Check object security */ aclcheck = pg_class_aclcheck(reloid, GetUserId(), ACL_RULE); if (aclcheck != ACLCHECK_OK) aclcheck_error(aclcheck, ACL_KIND_CLASS, get_rel_name(reloid)); /* Call CreateComments() to create/drop the comments */ CreateComments(ruleoid, RewriteRelationId, 0, comment); heap_close(relation, NoLock);}/* * CommentType -- * * This routine is used to add/drop any user-comments a user might * have regarding a TYPE. The type is specified by name * and, if found, and the user has appropriate permissions, a * comment will be added/dropped using the CreateComments() routine. * The type's name and the comments are the parameters to this routine. */static voidCommentType(List *typename, char *comment){ TypeName *tname; Oid oid; /* XXX a bit of a crock; should accept TypeName in COMMENT syntax */ tname = makeNode(TypeName); tname->names = typename; tname->typmod = -1; /* Find the type's oid */ oid = typenameTypeId(tname); /* Check object security */ if (!pg_type_ownercheck(oid, GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TYPE, TypeNameToString(tname)); /* Call CreateComments() to create/drop the comments */ CreateComments(oid, TypeRelationId, 0, comment);}/* * CommentAggregate -- * * This routine is used to allow a user to provide comments on an * aggregate function. The aggregate function is determined by both * its name and its argument type, which, with the comments are * the three parameters handed to this routine. */static voidCommentAggregate(List *aggregate, List *arguments, char *comment){ TypeName *aggtype = (TypeName *) linitial(arguments); Oid baseoid, oid; /* First, attempt to determine the base aggregate oid */ if (aggtype) baseoid = typenameTypeId(aggtype); else baseoid = ANYOID; /* Now, attempt to find the actual tuple in pg_proc */ oid = find_aggregate_func(aggregate, baseoid, false); /* Next, validate the user's attempt to comment */ if (!pg_proc_ownercheck(oid, GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC, NameListToString(aggregate)); /* Call CreateComments() to create/drop the comments */ CreateComments(oid, ProcedureRelationId, 0, comment);}/* * CommentProc -- * * This routine is used to allow a user to provide comments on an * procedure (function). The procedure is determined by both * its name and its argument list. The argument list is expected to * be a series of parsed nodes pointed to by a List object. If the * comments string is empty, the associated comment is dropped. */static voidCommentProc(List *function, List *arguments, char *comment){ Oid oid; /* Look up the procedure */ oid = LookupFuncNameTypeNames(function, arguments, false); /* Now, validate the user's ability to comment on this function */ if (!pg_proc_ownercheck(oid, GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_PROC, NameListToString(function)); /* Call CreateComments() to create/drop the comments */ CreateComments(oid, ProcedureRelationId, 0, comment);}/* * CommentOperator -- * * This routine is used to allow a user to provide comments on an * operator. The operator for commenting is determined by both * its name and its argument list which defines the left and right * hand types the operator will operate on. The argument list is * expected to be a couple of parse nodes pointed to be a List * object. */static voidCommentOperator(List *opername, List *arguments, char *comment){ TypeName *typenode1 = (TypeName *) linitial(arguments); TypeName *typenode2 = (TypeName *) lsecond(arguments); Oid oid; /* Look up the operator */ oid = LookupOperNameTypeNames(opername, typenode1, typenode2, false); /* Valid user's ability to comment on this operator */ if (!pg_oper_ownercheck(oid, GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_OPER, NameListToString(opername)); /* Call CreateComments() to create/drop the comments */ CreateComments(oid, OperatorRelationId, 0, comment);}/* * CommentTrigger -- * * This routine is used to allow a user to provide comments on a * trigger event. The trigger for commenting is determined by both * its name and the relation to which it refers. The arguments to this * function are the trigger name and relation name (merged into a qualified * name), and the comment to add/drop. */static voidCommentTrigger(List *qualname, char *comment){ int nnames; List *relname; char *trigname; RangeVar *rel; Relation pg_trigger, relation; HeapTuple triggertuple; SysScanDesc scan; ScanKeyData entry[2]; Oid oid; /* Separate relname and trig name */ nnames = list_length(qualname); if (nnames < 2) /* parser messed up */ elog(ERROR, "must specify relation and trigger"); relname = list_truncate(list_copy(qualname), nnames - 1); trigname = strVal(lfirst(list_tail(qualname))); /* Open the owning relation to ensure it won't go away meanwhile */ rel = makeRangeVarFromNameList(relname); relation = heap_openrv(rel, AccessShareLock); /* Check object security */ if (!pg_class_ownercheck(RelationGetRelid(relation), GetUserId())) aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CLASS, RelationGetRelationName(relation));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -