1 /*-------------------------------------------------------------------------
4 * conversion creation command support code
6 * Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/commands/conversioncmds.c,v 1.21 2005/07/14 21:46:29 tgl Exp $
13 *-------------------------------------------------------------------------
17 #include "catalog/pg_conversion.h"
18 #include "access/heapam.h"
19 #include "catalog/catalog.h"
20 #include "catalog/dependency.h"
21 #include "catalog/indexing.h"
22 #include "catalog/namespace.h"
23 #include "catalog/pg_type.h"
24 #include "mb/pg_wchar.h"
25 #include "commands/conversioncmds.h"
26 #include "miscadmin.h"
27 #include "parser/parse_func.h"
28 #include "utils/acl.h"
29 #include "utils/builtins.h"
30 #include "utils/lsyscache.h"
31 #include "utils/syscache.h"
38 CreateConversionCommand(CreateConversionStmt *stmt)
41 char *conversion_name;
46 const char *from_encoding_name = stmt->for_encoding_name;
47 const char *to_encoding_name = stmt->to_encoding_name;
48 List *func_name = stmt->func_name;
49 static Oid funcargs[] = {INT4OID, INT4OID, CSTRINGOID, INTERNALOID, INT4OID};
51 /* Convert list of names to a name and namespace */
52 namespaceId = QualifiedNameGetCreationNamespace(stmt->conversion_name,
55 /* Check we have creation rights in target namespace */
56 aclresult = pg_namespace_aclcheck(namespaceId, GetUserId(), ACL_CREATE);
57 if (aclresult != ACLCHECK_OK)
58 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
59 get_namespace_name(namespaceId));
61 /* Check the encoding names */
62 from_encoding = pg_char_to_encoding(from_encoding_name);
63 if (from_encoding < 0)
65 (errcode(ERRCODE_UNDEFINED_OBJECT),
66 errmsg("source encoding \"%s\" does not exist",
67 from_encoding_name)));
69 to_encoding = pg_char_to_encoding(to_encoding_name);
72 (errcode(ERRCODE_UNDEFINED_OBJECT),
73 errmsg("destination encoding \"%s\" does not exist",
77 * Check the existence of the conversion function. Function name could
78 * be a qualified name.
80 funcoid = LookupFuncName(func_name, sizeof(funcargs) / sizeof(Oid),
83 /* Check we have EXECUTE rights for the function */
84 aclresult = pg_proc_aclcheck(funcoid, GetUserId(), ACL_EXECUTE);
85 if (aclresult != ACLCHECK_OK)
86 aclcheck_error(aclresult, ACL_KIND_PROC,
87 NameListToString(func_name));
90 * All seem ok, go ahead (possible failure would be a duplicate
93 ConversionCreate(conversion_name, namespaceId, GetUserId(),
94 from_encoding, to_encoding, funcoid, stmt->def);
101 DropConversionCommand(List *name, DropBehavior behavior)
105 conversionOid = FindConversionByName(name);
106 if (!OidIsValid(conversionOid))
108 (errcode(ERRCODE_UNDEFINED_OBJECT),
109 errmsg("conversion \"%s\" does not exist",
110 NameListToString(name))));
112 ConversionDrop(conversionOid, behavior);
119 RenameConversion(List *name, const char *newname)
127 rel = heap_open(ConversionRelationId, RowExclusiveLock);
129 conversionOid = FindConversionByName(name);
130 if (!OidIsValid(conversionOid))
132 (errcode(ERRCODE_UNDEFINED_OBJECT),
133 errmsg("conversion \"%s\" does not exist",
134 NameListToString(name))));
136 tup = SearchSysCacheCopy(CONOID,
137 ObjectIdGetDatum(conversionOid),
139 if (!HeapTupleIsValid(tup)) /* should not happen */
140 elog(ERROR, "cache lookup failed for conversion %u", conversionOid);
142 namespaceOid = ((Form_pg_conversion) GETSTRUCT(tup))->connamespace;
144 /* make sure the new name doesn't exist */
145 if (SearchSysCacheExists(CONNAMENSP,
146 CStringGetDatum(newname),
147 ObjectIdGetDatum(namespaceOid),
150 (errcode(ERRCODE_DUPLICATE_OBJECT),
151 errmsg("conversion \"%s\" already exists in schema \"%s\"",
152 newname, get_namespace_name(namespaceOid))));
155 if (!pg_conversion_ownercheck(conversionOid,GetUserId()))
156 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
157 NameListToString(name));
159 /* must have CREATE privilege on namespace */
160 aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
161 if (aclresult != ACLCHECK_OK)
162 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
163 get_namespace_name(namespaceOid));
166 namestrcpy(&(((Form_pg_conversion) GETSTRUCT(tup))->conname), newname);
167 simple_heap_update(rel, &tup->t_self, tup);
168 CatalogUpdateIndexes(rel, tup);
170 heap_close(rel, NoLock);
175 * Change conversion owner
178 AlterConversionOwner(List *name, Oid newOwnerId)
183 Form_pg_conversion convForm;
186 rel = heap_open(ConversionRelationId, RowExclusiveLock);
188 conversionOid = FindConversionByName(name);
189 if (!OidIsValid(conversionOid))
191 (errcode(ERRCODE_UNDEFINED_OBJECT),
192 errmsg("conversion \"%s\" does not exist",
193 NameListToString(name))));
195 tup = SearchSysCacheCopy(CONOID,
196 ObjectIdGetDatum(conversionOid),
198 if (!HeapTupleIsValid(tup)) /* should not happen */
199 elog(ERROR, "cache lookup failed for conversion %u", conversionOid);
201 convForm = (Form_pg_conversion) GETSTRUCT(tup);
204 * If the new owner is the same as the existing owner, consider the
205 * command to have succeeded. This is for dump restoration purposes.
207 if (convForm->conowner != newOwnerId)
209 /* Otherwise, must be owner of the existing object */
210 if (!pg_conversion_ownercheck(HeapTupleGetOid(tup),GetUserId()))
211 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
212 NameListToString(name));
214 /* Must be able to become new owner */
215 check_is_member_of_role(GetUserId(), newOwnerId);
217 /* New owner must have CREATE privilege on namespace */
218 aclresult = pg_namespace_aclcheck(convForm->connamespace, newOwnerId,
220 if (aclresult != ACLCHECK_OK)
221 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
222 get_namespace_name(convForm->connamespace));
225 * Modify the owner --- okay to scribble on tup because it's a
228 convForm->conowner = newOwnerId;
230 simple_heap_update(rel, &tup->t_self, tup);
232 CatalogUpdateIndexes(rel, tup);
234 /* Update owner dependency reference */
235 changeDependencyOnOwner(ConversionRelationId, conversionOid,
239 heap_close(rel, NoLock);