1 /*-------------------------------------------------------------------------
4 * routines to support manipulation of the pg_conversion relation
6 * Portions Copyright (c) 1996-2006, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/catalog/pg_conversion.c,v 1.30 2006/05/30 13:36:30 momjian Exp $
13 *-------------------------------------------------------------------------
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_namespace.h"
22 #include "catalog/pg_proc.h"
23 #include "catalog/namespace.h"
24 #include "utils/builtins.h"
25 #include "utils/lsyscache.h"
26 #include "utils/syscache.h"
27 #include "utils/catcache.h"
28 #include "mb/pg_wchar.h"
29 #include "utils/fmgroids.h"
30 #include "utils/acl.h"
31 #include "miscadmin.h"
36 * Add a new tuple to pg_conversion.
39 ConversionCreate(const char *conname, Oid connamespace,
41 int32 conforencoding, int32 contoencoding,
42 Oid conproc, bool def)
48 char nulls[Natts_pg_conversion];
49 Datum values[Natts_pg_conversion];
57 elog(ERROR, "no conversion name supplied");
59 /* make sure there is no existing conversion of same name */
60 if (SearchSysCacheExists(CONNAMENSP,
61 PointerGetDatum(conname),
62 ObjectIdGetDatum(connamespace),
65 (errcode(ERRCODE_DUPLICATE_OBJECT),
66 errmsg("conversion \"%s\" already exists", conname)));
71 * make sure there is no existing default <for encoding><to encoding>
72 * pair in this name space
74 if (FindDefaultConversion(connamespace,
78 (errcode(ERRCODE_DUPLICATE_OBJECT),
79 errmsg("default conversion for %s to %s already exists",
80 pg_encoding_to_char(conforencoding),
81 pg_encoding_to_char(contoencoding))));
84 /* open pg_conversion */
85 rel = heap_open(ConversionRelationId, RowExclusiveLock);
86 tupDesc = rel->rd_att;
88 /* initialize nulls and values */
89 for (i = 0; i < Natts_pg_conversion; i++)
92 values[i] = (Datum) NULL;
96 namestrcpy(&cname, conname);
97 values[Anum_pg_conversion_conname - 1] = NameGetDatum(&cname);
98 values[Anum_pg_conversion_connamespace - 1] = ObjectIdGetDatum(connamespace);
99 values[Anum_pg_conversion_conowner - 1] = ObjectIdGetDatum(conowner);
100 values[Anum_pg_conversion_conforencoding - 1] = Int32GetDatum(conforencoding);
101 values[Anum_pg_conversion_contoencoding - 1] = Int32GetDatum(contoencoding);
102 values[Anum_pg_conversion_conproc - 1] = ObjectIdGetDatum(conproc);
103 values[Anum_pg_conversion_condefault - 1] = BoolGetDatum(def);
105 tup = heap_formtuple(tupDesc, values, nulls);
107 /* insert a new tuple */
108 oid = simple_heap_insert(rel, tup);
109 Assert(OidIsValid(oid));
111 /* update the index if any */
112 CatalogUpdateIndexes(rel, tup);
114 myself.classId = ConversionRelationId;
115 myself.objectId = HeapTupleGetOid(tup);
116 myself.objectSubId = 0;
118 /* create dependency on conversion procedure */
119 referenced.classId = ProcedureRelationId;
120 referenced.objectId = conproc;
121 referenced.objectSubId = 0;
122 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
124 /* create dependency on owner */
125 recordDependencyOnOwner(ConversionRelationId, HeapTupleGetOid(tup),
128 /* create dependency on namespace */
129 myself.classId = ConversionRelationId;
130 referenced.classId = NamespaceRelationId;
131 referenced.objectId = connamespace;
132 recordDependencyOn(&myself, &referenced, DEPENDENCY_NORMAL);
135 heap_close(rel, RowExclusiveLock);
143 * Drop a conversion after doing permission checks.
146 ConversionDrop(Oid conversionOid, DropBehavior behavior)
149 ObjectAddress object;
151 tuple = SearchSysCache(CONOID,
152 ObjectIdGetDatum(conversionOid),
154 if (!HeapTupleIsValid(tuple))
155 elog(ERROR, "cache lookup failed for conversion %u", conversionOid);
158 ((Form_pg_conversion) GETSTRUCT(tuple))->conowner != GetUserId())
159 aclcheck_error(ACLCHECK_NOT_OWNER, ACL_KIND_CONVERSION,
160 NameStr(((Form_pg_conversion) GETSTRUCT(tuple))->conname));
162 ReleaseSysCache(tuple);
167 object.classId = ConversionRelationId;
168 object.objectId = conversionOid;
169 object.objectSubId = 0;
171 performDeletion(&object, behavior);
175 * RemoveConversionById
177 * Remove a tuple from pg_conversion by Oid. This function is solely
178 * called inside catalog/dependency.c
181 RemoveConversionById(Oid conversionOid)
186 ScanKeyData scanKeyData;
188 ScanKeyInit(&scanKeyData,
189 ObjectIdAttributeNumber,
190 BTEqualStrategyNumber, F_OIDEQ,
191 ObjectIdGetDatum(conversionOid));
193 /* open pg_conversion */
194 rel = heap_open(ConversionRelationId, RowExclusiveLock);
196 scan = heap_beginscan(rel, SnapshotNow,
199 /* search for the target tuple */
200 if (HeapTupleIsValid(tuple = heap_getnext(scan, ForwardScanDirection)))
201 simple_heap_delete(rel, &tuple->t_self);
203 elog(ERROR, "could not find tuple for conversion %u", conversionOid);
205 heap_close(rel, RowExclusiveLock);
209 * FindDefaultConversion
211 * Find "default" conversion proc by for_encoding and to_encoding in the
214 * If found, returns the procedure's oid, otherwise InvalidOid. Note that
215 * you get the procedure's OID not the conversion's OID!
218 FindDefaultConversion(Oid name_space, int32 for_encoding, int32 to_encoding)
222 Form_pg_conversion body;
223 Oid proc = InvalidOid;
226 catlist = SearchSysCacheList(CONDEFAULT, 3,
227 ObjectIdGetDatum(name_space),
228 Int32GetDatum(for_encoding),
229 Int32GetDatum(to_encoding),
232 for (i = 0; i < catlist->n_members; i++)
234 tuple = &catlist->members[i]->tuple;
235 body = (Form_pg_conversion) GETSTRUCT(tuple);
236 if (body->condefault)
238 proc = body->conproc;
242 ReleaseSysCacheList(catlist);
249 * Find conversion by namespace and conversion name.
250 * Returns conversion OID.
253 FindConversion(const char *conname, Oid connamespace)
260 /* search pg_conversion by connamespace and conversion name */
261 tuple = SearchSysCache(CONNAMENSP,
262 PointerGetDatum(conname),
263 ObjectIdGetDatum(connamespace),
265 if (!HeapTupleIsValid(tuple))
268 procoid = ((Form_pg_conversion) GETSTRUCT(tuple))->conproc;
269 conoid = HeapTupleGetOid(tuple);
271 ReleaseSysCache(tuple);
273 /* Check we have execute rights for the function */
274 aclresult = pg_proc_aclcheck(procoid, GetUserId(), ACL_EXECUTE);
275 if (aclresult != ACLCHECK_OK)
282 * Execute SQL99's CONVERT function.
284 * CONVERT <left paren> <character value expression>
285 * USING <form-of-use conversion name> <right paren>
287 * TEXT convert_using(TEXT string, TEXT conversion_name)
290 pg_convert_using(PG_FUNCTION_ARGS)
292 text *string = PG_GETARG_TEXT_P(0);
293 text *conv_name = PG_GETARG_TEXT_P(1);
298 Form_pg_conversion body;
303 /* Convert input string to null-terminated form */
304 len = VARSIZE(string) - VARHDRSZ;
305 str = palloc(len + 1);
306 memcpy(str, VARDATA(string), len);
309 /* Look up the conversion name */
310 parsed_name = textToQualifiedNameList(conv_name);
311 convoid = FindConversionByName(parsed_name);
312 if (!OidIsValid(convoid))
314 (errcode(ERRCODE_UNDEFINED_OBJECT),
315 errmsg("conversion \"%s\" does not exist",
316 NameListToString(parsed_name))));
318 tuple = SearchSysCache(CONOID,
319 ObjectIdGetDatum(convoid),
321 if (!HeapTupleIsValid(tuple))
322 elog(ERROR, "cache lookup failed for conversion %u", convoid);
323 body = (Form_pg_conversion) GETSTRUCT(tuple);
325 /* Temporary result area should be more than big enough */
326 result = palloc(len * 4 + 1);
328 OidFunctionCall5(body->conproc,
329 Int32GetDatum(body->conforencoding),
330 Int32GetDatum(body->contoencoding),
331 CStringGetDatum(str),
332 CStringGetDatum(result),
335 ReleaseSysCache(tuple);
338 * build text result structure. we cannot use textin() here, since textin
339 * assumes that input string encoding is same as database encoding.
341 len = strlen(result) + VARHDRSZ;
342 retval = palloc(len);
343 VARATT_SIZEP(retval) = len;
344 memcpy(VARDATA(retval), result, len - VARHDRSZ);
349 PG_RETURN_TEXT_P(retval);