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

Consolidate DROP handling for some object types.

This gets rid of a significant amount of duplicative code.

KaiGai Kohei, reviewed in earlier versions by Dimitri Fontaine, with
further review and cleanup by me.
This commit is contained in:
Robert Haas
2011-10-19 23:25:20 -04:00
parent 3301c83536
commit 82a4a777d9
17 changed files with 402 additions and 828 deletions

View File

@ -278,65 +278,6 @@ DefineTSParser(List *names, List *parameters)
heap_close(prsRel, RowExclusiveLock);
}
/*
* DROP TEXT SEARCH PARSER
*/
void
RemoveTSParsers(DropStmt *drop)
{
ObjectAddresses *objects;
ListCell *cell;
if (!superuser())
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be superuser to drop text search parsers")));
/*
* First we identify all the objects, then we delete them in a single
* performMultipleDeletions() call. This is to avoid unwanted DROP
* RESTRICT errors if one of the objects depends on another.
*/
objects = new_object_addresses();
foreach(cell, drop->objects)
{
List *names = (List *) lfirst(cell);
Oid prsOid;
ObjectAddress object;
prsOid = get_ts_parser_oid(names, true);
if (!OidIsValid(prsOid))
{
if (!drop->missing_ok)
{
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("text search parser \"%s\" does not exist",
NameListToString(names))));
}
else
{
ereport(NOTICE,
(errmsg("text search parser \"%s\" does not exist, skipping",
NameListToString(names))));
}
continue;
}
object.classId = TSParserRelationId;
object.objectId = prsOid;
object.objectSubId = 0;
add_exact_object_address(&object, objects);
}
performMultipleDeletions(objects, drop->behavior);
free_object_addresses(objects);
}
/*
* Guts of TS parser deletion.
*/
@ -730,76 +671,6 @@ AlterTSDictionaryNamespace_oid(Oid dictId, Oid newNspOid)
return oldNspOid;
}
/*
* DROP TEXT SEARCH DICTIONARY
*/
void
RemoveTSDictionaries(DropStmt *drop)
{
ObjectAddresses *objects;
ListCell *cell;
/*
* First we identify all the objects, then we delete them in a single
* performMultipleDeletions() call. This is to avoid unwanted DROP
* RESTRICT errors if one of the objects depends on another.
*/
objects = new_object_addresses();
foreach(cell, drop->objects)
{
List *names = (List *) lfirst(cell);
Oid dictOid;
ObjectAddress object;
HeapTuple tup;
Oid namespaceId;
dictOid = get_ts_dict_oid(names, true);
if (!OidIsValid(dictOid))
{
if (!drop->missing_ok)
{
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("text search dictionary \"%s\" does not exist",
NameListToString(names))));
}
else
{
ereport(NOTICE,
(errmsg("text search dictionary \"%s\" does not exist, skipping",
NameListToString(names))));
}
continue;
}
tup = SearchSysCache1(TSDICTOID, ObjectIdGetDatum(dictOid));
if (!HeapTupleIsValid(tup)) /* should not happen */
elog(ERROR, "cache lookup failed for text search dictionary %u",
dictOid);
/* Permission check: must own dictionary or its namespace */
namespaceId = ((Form_pg_ts_dict) GETSTRUCT(tup))->dictnamespace;
if (!pg_ts_dict_ownercheck(dictOid, GetUserId()) &&
!pg_namespace_ownercheck(namespaceId, GetUserId()))
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TSDICTIONARY,
NameListToString(names));
object.classId = TSDictionaryRelationId;
object.objectId = dictOid;
object.objectSubId = 0;
add_exact_object_address(&object, objects);
ReleaseSysCache(tup);
}
performMultipleDeletions(objects, drop->behavior);
free_object_addresses(objects);
}
/*
* Guts of TS dictionary deletion.
*/
@ -1262,65 +1133,6 @@ AlterTSTemplateNamespace_oid(Oid tmplId, Oid newNspOid)
return oldNspOid;
}
/*
* DROP TEXT SEARCH TEMPLATE
*/
void
RemoveTSTemplates(DropStmt *drop)
{
ObjectAddresses *objects;
ListCell *cell;
if (!superuser())
ereport(ERROR,
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
errmsg("must be superuser to drop text search templates")));
/*
* First we identify all the objects, then we delete them in a single
* performMultipleDeletions() call. This is to avoid unwanted DROP
* RESTRICT errors if one of the objects depends on another.
*/
objects = new_object_addresses();
foreach(cell, drop->objects)
{
List *names = (List *) lfirst(cell);
Oid tmplOid;
ObjectAddress object;
tmplOid = get_ts_template_oid(names, true);
if (!OidIsValid(tmplOid))
{
if (!drop->missing_ok)
{
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("text search template \"%s\" does not exist",
NameListToString(names))));
}
else
{
ereport(NOTICE,
(errmsg("text search template \"%s\" does not exist, skipping",
NameListToString(names))));
}
continue;
}
object.classId = TSTemplateRelationId;
object.objectId = tmplOid;
object.objectSubId = 0;
add_exact_object_address(&object, objects);
}
performMultipleDeletions(objects, drop->behavior);
free_object_addresses(objects);
}
/*
* Guts of TS template deletion.
*/
@ -1714,72 +1526,6 @@ AlterTSConfigurationNamespace_oid(Oid cfgId, Oid newNspOid)
return oldNspOid;
}
/*
* DROP TEXT SEARCH CONFIGURATION
*/
void
RemoveTSConfigurations(DropStmt *drop)
{
ObjectAddresses *objects;
ListCell *cell;
/*
* First we identify all the objects, then we delete them in a single
* performMultipleDeletions() call. This is to avoid unwanted DROP
* RESTRICT errors if one of the objects depends on another.
*/
objects = new_object_addresses();
foreach(cell, drop->objects)
{
List *names = (List *) lfirst(cell);
Oid cfgOid;
Oid namespaceId;
ObjectAddress object;
HeapTuple tup;
tup = GetTSConfigTuple(names);
if (!HeapTupleIsValid(tup))
{
if (!drop->missing_ok)
{
ereport(ERROR,
(errcode(ERRCODE_UNDEFINED_OBJECT),
errmsg("text search configuration \"%s\" does not exist",
NameListToString(names))));
}
else
{
ereport(NOTICE,
(errmsg("text search configuration \"%s\" does not exist, skipping",
NameListToString(names))));
}
continue;
}
/* Permission check: must own configuration or its namespace */
cfgOid = HeapTupleGetOid(tup);
namespaceId = ((Form_pg_ts_config) GETSTRUCT(tup))->cfgnamespace;
if (!pg_ts_config_ownercheck(cfgOid, GetUserId()) &&
!pg_namespace_ownercheck(namespaceId, GetUserId()))
aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_TSCONFIGURATION,
NameListToString(names));
object.classId = TSConfigRelationId;
object.objectId = cfgOid;
object.objectSubId = 0;
add_exact_object_address(&object, objects);
ReleaseSysCache(tup);
}
performMultipleDeletions(objects, drop->behavior);
free_object_addresses(objects);
}
/*
* Guts of TS configuration deletion.
*/