88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/catalog/pg_conversion.c,v 1.6 2002/09/04 20:31:14 momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/catalog/pg_conversion.c,v 1.7 2002/11/02 02:33:03 tgl Exp $
1212 *
1313 *-------------------------------------------------------------------------
1414 */
3030#include "utils/acl.h"
3131#include "miscadmin.h"
3232
33- /* ----------------
33+ /*
3434 * ConversionCreate
3535 *
36- * Add a new tuple to pg_coversion.
37- * ---------------
36+ * Add a new tuple to pg_conversion.
3837 */
3938Oid
4039ConversionCreate (const char * conname ,Oid connamespace ,
4140int32 conowner ,
42- int4 conforencoding ,int4 contoencoding ,
41+ int32 conforencoding ,int32 contoencoding ,
4342Oid conproc ,bool def )
4443{
4544int i ;
@@ -58,7 +57,7 @@ ConversionCreate(const char *conname, Oid connamespace,
5857elog (ERROR ,"no conversion name supplied" );
5958
6059/* make sure there is no existing conversion of same name */
61- if (SearchSysCacheExists (CONNAMESP ,
60+ if (SearchSysCacheExists (CONNAMENSP ,
6261PointerGetDatum (conname ),
6362ObjectIdGetDatum (connamespace ),
64630 ,0 ))
@@ -74,7 +73,8 @@ ConversionCreate(const char *conname, Oid connamespace,
7473conforencoding ,
7574contoencoding ))
7675elog (ERROR ,"default conversion for %s to %s already exists" ,
77- pg_encoding_to_char (conforencoding ),pg_encoding_to_char (contoencoding ));
76+ pg_encoding_to_char (conforencoding ),
77+ pg_encoding_to_char (contoencoding ));
7878}
7979
8080/* open pg_conversion */
@@ -96,10 +96,7 @@ ConversionCreate(const char *conname, Oid connamespace,
9696values [Anum_pg_conversion_conforencoding - 1 ]= Int32GetDatum (conforencoding );
9797values [Anum_pg_conversion_contoencoding - 1 ]= Int32GetDatum (contoencoding );
9898values [Anum_pg_conversion_conproc - 1 ]= ObjectIdGetDatum (conproc );
99- if (def == true)
100- values [Anum_pg_conversion_condefault - 1 ]= BoolGetDatum (def );
101- else
102- nulls [Anum_pg_conversion_condefault - 1 ]= 'n' ;
99+ values [Anum_pg_conversion_condefault - 1 ]= BoolGetDatum (def );
103100
104101tup = heap_formtuple (tupDesc ,values ,nulls );
105102
@@ -110,11 +107,11 @@ ConversionCreate(const char *conname, Oid connamespace,
110107/* update the index if any */
111108CatalogUpdateIndexes (rel ,tup );
112109
113- myself .classId = get_system_catalog_relid ( ConversionRelationName );
110+ myself .classId = RelationGetRelid ( rel );
114111myself .objectId = HeapTupleGetOid (tup );
115112myself .objectSubId = 0 ;
116113
117- /* dependency on conversion procedure */
114+ /*create dependency on conversion procedure */
118115referenced .classId = RelOid_pg_proc ;
119116referenced .objectId = conproc ;
120117referenced .objectSubId = 0 ;
@@ -126,79 +123,46 @@ ConversionCreate(const char *conname, Oid connamespace,
126123return oid ;
127124}
128125
129- /* ----------------
126+ /*
130127 * ConversionDrop
131128 *
132- * Drop a conversion and do dependency check.
133- * ---------------
129+ * Drop a conversion after doing permission checks.
134130 */
135131void
136- ConversionDrop (const char * conname ,Oid connamespace ,
137- int32 conowner ,DropBehavior behavior )
132+ ConversionDrop (Oid conversionOid ,DropBehavior behavior )
138133{
139- Relation rel ;
140- TupleDesc tupDesc ;
141134HeapTuple tuple ;
142- HeapScanDesc scan ;
143- ScanKeyData scanKeyData ;
144- Form_pg_conversion body ;
145135ObjectAddress object ;
146- Oid myoid ;
147-
148- /* sanity checks */
149- if (!conname )
150- elog (ERROR ,"no conversion name supplied" );
151-
152- ScanKeyEntryInitialize (& scanKeyData ,
153- 0 ,
154- Anum_pg_conversion_connamespace ,
155- F_OIDEQ ,
156- ObjectIdGetDatum (connamespace ));
157-
158- /* open pg_conversion */
159- rel = heap_openr (ConversionRelationName ,AccessShareLock );
160- tupDesc = rel -> rd_att ;
161-
162- scan = heap_beginscan (rel ,SnapshotNow ,
163- 1 ,& scanKeyData );
164-
165- /* search for the target tuple */
166- while (HeapTupleIsValid (tuple = heap_getnext (scan ,ForwardScanDirection )))
167- {
168- body = (Form_pg_conversion )GETSTRUCT (tuple );
169- if (!strncmp (NameStr (body -> conname ),conname ,NAMEDATALEN ))
170- break ;
171- }
172136
137+ tuple = SearchSysCache (CONOID ,
138+ ObjectIdGetDatum (conversionOid ),
139+ 0 ,0 ,0 );
173140if (!HeapTupleIsValid (tuple ))
174- {
175- elog (ERROR ,"conversion %s not found" ,conname );
176- return ;
177- }
141+ elog (ERROR ,"Conversion %u search from syscache failed" ,
142+ conversionOid );
178143
179- if (!superuser ()&& ((Form_pg_conversion )GETSTRUCT (tuple ))-> conowner != GetUserId ())
144+ if (!superuser ()&&
145+ ((Form_pg_conversion )GETSTRUCT (tuple ))-> conowner != GetUserId ())
180146elog (ERROR ,"permission denied" );
181147
182- myoid = HeapTupleGetOid (tuple );
183- heap_endscan (scan );
184- heap_close (rel ,AccessShareLock );
148+ ReleaseSysCache (tuple );
185149
186150/*
187151 * Do the deletion
188152 */
189153object .classId = get_system_catalog_relid (ConversionRelationName );
190- object .objectId = myoid ;
154+ object .objectId = conversionOid ;
191155object .objectSubId = 0 ;
192156
193157performDeletion (& object ,behavior );
194158}
195159
196- /* ----------------
160+ /*
197161 * RemoveConversionById
198162 *
199- * Remove a tuple from pg_conversion by Oid. This function issoley
163+ * Remove a tuple from pg_conversion by Oid. This function issolely
200164 * called inside catalog/dependency.c
201- * --------------- * /
165+ */
202166void
203167RemoveConversionById (Oid conversionOid )
204168{
@@ -230,26 +194,24 @@ RemoveConversionById(Oid conversionOid)
230194heap_close (rel ,RowExclusiveLock );
231195}
232196
233- /* ----------------
197+ /*
234198 * FindDefaultConversion
235199 *
236- * Find "default" conversion proc by for_encoding and to_encoding in this name space.
237- * If found, returns the procedure's oid, otherwise InvalidOid.
238- * ---------------
200+ * Find "default" conversion proc by for_encoding and to_encoding in the
201+ * given namespace.
202+ *
203+ * If found, returns the procedure's oid, otherwise InvalidOid. Note that
204+ * you get the procedure's OID not the conversion's OID!
239205 */
240206Oid
241- FindDefaultConversion (Oid name_space ,int4 for_encoding ,int4 to_encoding )
207+ FindDefaultConversion (Oid name_space ,int32 for_encoding ,int32 to_encoding )
242208{
243209CatCList * catlist ;
244210HeapTuple tuple ;
245211Form_pg_conversion body ;
246212Oid proc = InvalidOid ;
247213int i ;
248214
249- /* Check we have usage rights in target namespace */
250- if (pg_namespace_aclcheck (name_space ,GetUserId (),ACL_USAGE )!= ACLCHECK_OK )
251- return proc ;
252-
253215catlist = SearchSysCacheList (CONDEFAULT ,3 ,
254216ObjectIdGetDatum (name_space ),
255217Int32GetDatum (for_encoding ),
@@ -260,7 +222,7 @@ FindDefaultConversion(Oid name_space, int4 for_encoding, int4 to_encoding)
260222{
261223tuple = & catlist -> members [i ]-> tuple ;
262224body = (Form_pg_conversion )GETSTRUCT (tuple );
263- if (body -> condefault == TRUE )
225+ if (body -> condefault )
264226{
265227proc = body -> conproc ;
266228break ;
@@ -270,12 +232,11 @@ FindDefaultConversion(Oid name_space, int4 for_encoding, int4 to_encoding)
270232return proc ;
271233}
272234
273- /* ----------------
274- *FindConversionByName
235+ /*
236+ *FindConversion
275237 *
276238 * Find conversion by namespace and conversion name.
277- * Returns conversion oid.
278- * ---------------
239+ * Returns conversion OID.
279240 */
280241Oid
281242FindConversion (const char * conname ,Oid connamespace )
@@ -286,13 +247,13 @@ FindConversion(const char *conname, Oid connamespace)
286247AclResult aclresult ;
287248
288249/* search pg_conversion by connamespace and conversion name */
289- tuple = SearchSysCache (CONNAMESP ,
250+ tuple = SearchSysCache (CONNAMENSP ,
290251PointerGetDatum (conname ),
291252ObjectIdGetDatum (connamespace ),
2922530 ,0 );
293-
294254if (!HeapTupleIsValid (tuple ))
295255return InvalidOid ;
256+
296257procoid = ((Form_pg_conversion )GETSTRUCT (tuple ))-> conproc ;
297258conoid = HeapTupleGetOid (tuple );
298259