1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-08 11:42:09 +03:00

Refactor ALTER some-obj RENAME implementation

Remove duplicate implementations of catalog munging and miscellaneous
privilege checks.  Instead rely on already existing data in
objectaddress.c to do the work.

Author: KaiGai Kohei, changes by me
Reviewed by: Robert Haas, Álvaro Herrera, Dimitri Fontaine
This commit is contained in:
Alvaro Herrera
2013-01-21 12:06:41 -03:00
parent 8f0d8f481e
commit 765cbfdc92
19 changed files with 354 additions and 857 deletions

View File

@ -114,56 +114,3 @@ CreateConversionCommand(CreateConversionStmt *stmt)
return ConversionCreate(conversion_name, namespaceId, GetUserId(),
from_encoding, to_encoding, funcoid, stmt->def);
}
/*
* Rename conversion
*/
Oid
RenameConversion(List *name, const char *newname)
{
Oid conversionOid;
Oid namespaceOid;
HeapTuple tup;
Relation rel;
AclResult aclresult;
rel = heap_open(ConversionRelationId, RowExclusiveLock);
conversionOid = get_conversion_oid(name, false);
tup = SearchSysCacheCopy1(CONVOID, ObjectIdGetDatum(conversionOid));
if (!HeapTupleIsValid(tup)) /* should not happen */
elog(ERROR, "cache lookup failed for conversion %u", conversionOid);
namespaceOid = ((Form_pg_conversion) GETSTRUCT(tup))->connamespace;
/* make sure the new name doesn't exist */
if (SearchSysCacheExists2(CONNAMENSP,
CStringGetDatum(newname),
ObjectIdGetDatum(namespaceOid)))
ereport(ERROR,
(errcode(ERRCODE_DUPLICATE_OBJECT),
errmsg("conversion \"%s\" already exists in schema \"%s\"",
newname, get_namespace_name(namespaceOid))));
/* must be owner */
if (!pg_conversion_ownercheck(conversionOid, GetUserId()))
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
NameListToString(name));
/* must have CREATE privilege on namespace */
aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
if (aclresult != ACLCHECK_OK)
aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
get_namespace_name(namespaceOid));
/* rename */
namestrcpy(&(((Form_pg_conversion) GETSTRUCT(tup))->conname), newname);
simple_heap_update(rel, &tup->t_self, tup);
CatalogUpdateIndexes(rel, tup);
heap_close(rel, NoLock);
heap_freetuple(tup);
return conversionOid;
}