Refactor the handling of the various DropStmt variants so that when multiple
[PostgreSQL.git] / src / backend / commands / conversioncmds.c
blob14f819761a51d6cef73070ab0e525172639b4634
1 /*-------------------------------------------------------------------------
3 * conversioncmds.c
4 * conversion creation command support code
6 * Portions Copyright (c) 1996-2008, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
10 * IDENTIFICATION
11 * $PostgreSQL$
13 *-------------------------------------------------------------------------
15 #include "postgres.h"
17 #include "access/heapam.h"
18 #include "catalog/dependency.h"
19 #include "catalog/indexing.h"
20 #include "catalog/pg_conversion.h"
21 #include "catalog/pg_conversion_fn.h"
22 #include "catalog/pg_type.h"
23 #include "commands/conversioncmds.h"
24 #include "mb/pg_wchar.h"
25 #include "miscadmin.h"
26 #include "parser/parse_func.h"
27 #include "utils/acl.h"
28 #include "utils/builtins.h"
29 #include "utils/lsyscache.h"
30 #include "utils/syscache.h"
32 static void AlterConversionOwner_internal(Relation rel, Oid conversionOid,
33 Oid newOwnerId);
36 * CREATE CONVERSION
38 void
39 CreateConversionCommand(CreateConversionStmt *stmt)
41 Oid namespaceId;
42 char *conversion_name;
43 AclResult aclresult;
44 int from_encoding;
45 int to_encoding;
46 Oid funcoid;
47 const char *from_encoding_name = stmt->for_encoding_name;
48 const char *to_encoding_name = stmt->to_encoding_name;
49 List *func_name = stmt->func_name;
50 static Oid funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID};
52 /* Convert list of names to a name and namespace */
53 namespaceId = QualifiedNameGetCreationNamespace(stmt->conversion_name,
54 &conversion_name);
56 /* Check we have creation rights in target namespace */
57 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE);
58 if (aclresult != ACLCHECK_OK)
59 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
60 get_namespace_name(namespaceId));
62 /* Check the encoding names */
63 from_encoding = pg_char_to_encoding(from_encoding_name);
64 if (from_encoding < 0)
65 ereport(ERROR,
66 (errcode(ERRCODE_UNDEFINED_OBJECT),
67 errmsg("source encoding \"%s\" does not exist",
68 from_encoding_name)));
70 to_encoding = pg_char_to_encoding(to_encoding_name);
71 if (to_encoding < 0)
72 ereport(ERROR,
73 (errcode(ERRCODE_UNDEFINED_OBJECT),
74 errmsg("destination encoding \"%s\" does not exist",
75 to_encoding_name)));
78 * Check the existence of the conversion function. Function name could be
79 * a qualified name.
81 funcoid = LookupFuncName(func_name, sizeof(funcargs) / sizeof(Oid),
82 funcargs, false);
84 /* Check we have EXECUTE rights for the function */
85 aclresult = pg_proc_aclcheck(funcoid, GetUserId(), ACL_EXECUTE);
86 if (aclresult != ACLCHECK_OK)
87 aclcheck_error(aclresult, ACL_KIND_PROC,
88 NameListToString(func_name));
91 * All seem ok, go ahead (possible failure would be a duplicate conversion
92 * name)
94 ConversionCreate(conversion_name, namespaceId, GetUserId(),
95 from_encoding, to_encoding, funcoid, stmt->def);
99 * DROP CONVERSION
101 void
102 DropConversionsCommand(DropStmt *drop)
104 ObjectAddresses *objects;
105 ListCell *cell;
108 * First we identify all the conversions, then we delete them in a single
109 * performMultipleDeletions() call. This is to avoid unwanted
110 * DROP RESTRICT errors if one of the conversions depends on another.
111 * (Not that that is very likely, but we may as well do this consistently.)
113 objects = new_object_addresses();
115 foreach(cell, drop->objects)
117 List *name = (List *) lfirst(cell);
118 Oid conversionOid;
119 HeapTuple tuple;
120 Form_pg_conversion con;
121 ObjectAddress object;
123 conversionOid = FindConversionByName(name);
125 if (!OidIsValid(conversionOid))
127 if (!drop->missing_ok)
129 ereport(ERROR,
130 (errcode(ERRCODE_UNDEFINED_OBJECT),
131 errmsg("conversion \"%s\" does not exist",
132 NameListToString(name))));
134 else
136 ereport(NOTICE,
137 (errmsg("conversion \"%s\" does not exist, skipping",
138 NameListToString(name))));
140 continue;
143 tuple = SearchSysCache(CONVOID,
144 ObjectIdGetDatum(conversionOid),
145 0, 0, 0);
146 if (!HeapTupleIsValid(tuple))
147 elog(ERROR, "cache lookup failed for conversion %u",
148 conversionOid);
149 con = (Form_pg_conversion) GETSTRUCT(tuple);
151 /* Permission check: must own conversion or its namespace */
152 if (!pg_conversion_ownercheck(conversionOid, GetUserId()) &&
153 !pg_namespace_ownercheck(con->connamespace, GetUserId()))
154 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
155 NameStr(con->conname));
157 object.classId = ConversionRelationId;
158 object.objectId = conversionOid;
159 object.objectSubId = 0;
161 add_exact_object_address(&object, objects);
163 ReleaseSysCache(tuple);
166 performMultipleDeletions(objects, drop->behavior);
168 free_object_addresses(objects);
172 * Rename conversion
174 void
175 RenameConversion(List *name, const char *newname)
177 Oid conversionOid;
178 Oid namespaceOid;
179 HeapTuple tup;
180 Relation rel;
181 AclResult aclresult;
183 rel = heap_open(ConversionRelationId, RowExclusiveLock);
185 conversionOid = FindConversionByName(name);
186 if (!OidIsValid(conversionOid))
187 ereport(ERROR,
188 (errcode(ERRCODE_UNDEFINED_OBJECT),
189 errmsg("conversion \"%s\" does not exist",
190 NameListToString(name))));
192 tup = SearchSysCacheCopy(CONVOID,
193 ObjectIdGetDatum(conversionOid),
194 0, 0, 0);
195 if (!HeapTupleIsValid(tup)) /* should not happen */
196 elog(ERROR, "cache lookup failed for conversion %u", conversionOid);
198 namespaceOid = ((Form_pg_conversion) GETSTRUCT(tup))->connamespace;
200 /* make sure the new name doesn't exist */
201 if (SearchSysCacheExists(CONNAMENSP,
202 CStringGetDatum(newname),
203 ObjectIdGetDatum(namespaceOid),
204 0, 0))
205 ereport(ERROR,
206 (errcode(ERRCODE_DUPLICATE_OBJECT),
207 errmsg("conversion \"%s\" already exists in schema \"%s\"",
208 newname, get_namespace_name(namespaceOid))));
210 /* must be owner */
211 if (!pg_conversion_ownercheck(conversionOid, GetUserId()))
212 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
213 NameListToString(name));
215 /* must have CREATE privilege on namespace */
216 aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
217 if (aclresult != ACLCHECK_OK)
218 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
219 get_namespace_name(namespaceOid));
221 /* rename */
222 namestrcpy(&(((Form_pg_conversion) GETSTRUCT(tup))->conname), newname);
223 simple_heap_update(rel, &tup->t_self, tup);
224 CatalogUpdateIndexes(rel, tup);
226 heap_close(rel, NoLock);
227 heap_freetuple(tup);
231 * Change conversion owner, by name
233 void
234 AlterConversionOwner(List *name, Oid newOwnerId)
236 Oid conversionOid;
237 Relation rel;
239 rel = heap_open(ConversionRelationId, RowExclusiveLock);
241 conversionOid = FindConversionByName(name);
242 if (!OidIsValid(conversionOid))
243 ereport(ERROR,
244 (errcode(ERRCODE_UNDEFINED_OBJECT),
245 errmsg("conversion \"%s\" does not exist",
246 NameListToString(name))));
248 AlterConversionOwner_internal(rel, conversionOid, newOwnerId);
250 heap_close(rel, NoLock);
254 * Change conversion owner, by oid
256 void
257 AlterConversionOwner_oid(Oid conversionOid, Oid newOwnerId)
259 Relation rel;
261 rel = heap_open(ConversionRelationId, RowExclusiveLock);
263 AlterConversionOwner_internal(rel, conversionOid, newOwnerId);
265 heap_close(rel, NoLock);
269 * AlterConversionOwner_internal
271 * Internal routine for changing the owner. rel must be pg_conversion, already
272 * open and suitably locked; it will not be closed.
274 static void
275 AlterConversionOwner_internal(Relation rel, Oid conversionOid, Oid newOwnerId)
277 Form_pg_conversion convForm;
278 HeapTuple tup;
280 Assert(RelationGetRelid(rel) == ConversionRelationId);
282 tup = SearchSysCacheCopy(CONVOID,
283 ObjectIdGetDatum(conversionOid),
284 0, 0, 0);
285 if (!HeapTupleIsValid(tup)) /* should not happen */
286 elog(ERROR, "cache lookup failed for conversion %u", conversionOid);
288 convForm = (Form_pg_conversion) GETSTRUCT(tup);
291 * If the new owner is the same as the existing owner, consider the
292 * command to have succeeded. This is for dump restoration purposes.
294 if (convForm->conowner != newOwnerId)
296 AclResult aclresult;
298 /* Superusers can always do it */
299 if (!superuser())
301 /* Otherwise, must be owner of the existing object */
302 if (!pg_conversion_ownercheck(HeapTupleGetOid(tup), GetUserId()))
303 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
304 NameStr(convForm->conname));
306 /* Must be able to become new owner */
307 check_is_member_of_role(GetUserId(), newOwnerId);
309 /* New owner must have CREATE privilege on namespace */
310 aclresult = pg_namespace_aclcheck(convForm->connamespace,
311 newOwnerId,
312 ACL_CREATE);
313 if (aclresult != ACLCHECK_OK)
314 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
315 get_namespace_name(convForm->connamespace));
319 * Modify the owner --- okay to scribble on tup because it's a copy
321 convForm->conowner = newOwnerId;
323 simple_heap_update(rel, &tup->t_self, tup);
325 CatalogUpdateIndexes(rel, tup);
327 /* Update owner dependency reference */
328 changeDependencyOnOwner(ConversionRelationId, conversionOid,
329 newOwnerId);
332 heap_freetuple(tup);