1 /*-------------------------------------------------------------------------
4 * conversion creation command support code
6 * Portions Copyright (c) 1996-2003, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/commands/conversioncmds.c,v 1.11 2003/08/04 02:39:58 momjian Exp $
13 *-------------------------------------------------------------------------
17 #include "catalog/pg_conversion.h"
18 #include "access/heapam.h"
19 #include "catalog/catalog.h"
20 #include "catalog/catname.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, CSTRINGOID, 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_openr(ConversionRelationName, 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))));
156 ((Form_pg_conversion) GETSTRUCT(tup))->conowner != GetUserId())
157 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
158 NameListToString(name));
160 /* must have CREATE privilege on namespace */
161 aclresult = pg_namespace_aclcheck(namespaceOid, GetUserId(), ACL_CREATE);
162 if (aclresult != ACLCHECK_OK)
163 aclcheck_error(aclresult, ACL_KIND_NAMESPACE,
164 get_namespace_name(namespaceOid));
167 namestrcpy(&(((Form_pg_conversion) GETSTRUCT(tup))->conname), newname);
168 simple_heap_update(rel, &tup->t_self, tup);
169 CatalogUpdateIndexes(rel, tup);
171 heap_close(rel, NoLock);