1 /*-------------------------------------------------------------------------
4 * handle type operations for parser
6 * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/parser/parse_type.c,v 1.29 2000/01/26 05:56:42 momjian Exp $
13 *-------------------------------------------------------------------------
17 #include "catalog/pg_type.h"
18 #include "parser/parse_type.h"
19 #include "utils/syscache.h"
22 /* check to see if a type id is valid,
23 * returns true if it is. By using this call before calling
24 * typeidType or typeidTypeName, more meaningful error messages
25 * can be produced because the caller typically has more context of
26 * what's going on - jolly
31 return (SearchSysCacheTuple(TYPEOID,
36 /* return a type name, given a typeid */
38 typeidTypeName(Oid id)
41 Form_pg_type typetuple;
43 if (!(tup = SearchSysCacheTuple(TYPEOID,
47 elog(ERROR, "Unable to locate type oid %u in catalog", id);
50 typetuple = (Form_pg_type) GETSTRUCT(tup);
51 return NameStr(typetuple->typname);
54 /* return a Type structure, given a type id */
60 if (!(tup = SearchSysCacheTuple(TYPEOID,
64 elog(ERROR, "Unable to locate type oid %u in catalog", id);
70 /* return a Type structure, given type name */
77 elog(ERROR, "type(): Null type");
79 if (!(tup = SearchSysCacheTuple(TYPENAME,
82 elog(ERROR, "Unable to locate type name '%s' in catalog", s);
86 /* given type, return the type OID */
91 elog(ERROR, "typeTypeId() called with NULL type struct");
92 return tp->t_data->t_oid;
95 /* given type (as type struct), return the length of type */
101 typ = (Form_pg_type) GETSTRUCT(t);
105 /* given type (as type struct), return the value of its 'byval' attribute.*/
111 typ = (Form_pg_type) GETSTRUCT(t);
112 return typ->typbyval;
115 /* given type (as type struct), return the name of type */
121 typ = (Form_pg_type) GETSTRUCT(t);
122 return NameStr(typ->typname);
125 /* given a type, return its typetype ('c' for 'c'atalog types) */
131 typ = (Form_pg_type) GETSTRUCT(t);
135 /* Given a type structure and a string, returns the internal form of
138 stringTypeDatum(Type tp, char *string, int32 atttypmod)
143 op = ((Form_pg_type) GETSTRUCT(tp))->typinput;
144 typelem = ((Form_pg_type) GETSTRUCT(tp))->typelem; /* XXX - used for
146 return (Datum) fmgr(op, string, typelem, atttypmod);
149 /* Given a type id, returns the out-conversion function of the type */
152 typeidOutfunc(Oid type_id)
158 typeTuple = SearchSysCacheTuple(TYPEOID,
159 ObjectIdGetDatum(type_id),
161 if (!HeapTupleIsValid(typeTuple))
162 elog(ERROR, "typeidOutfunc: Invalid type - oid = %u", type_id);
164 type = (Form_pg_type) GETSTRUCT(typeTuple);
165 outfunc = type->typoutput;
172 typeidTypeRelid(Oid type_id)
177 typeTuple = SearchSysCacheTuple(TYPEOID,
178 ObjectIdGetDatum(type_id),
180 if (!HeapTupleIsValid(typeTuple))
181 elog(ERROR, "typeidTypeRelid: Invalid type - oid = %u", type_id);
183 type = (Form_pg_type) GETSTRUCT(typeTuple);
184 return type->typrelid;
188 typeTypeRelid(Type typ)
192 typtup = (Form_pg_type) GETSTRUCT(typ);
194 return typtup->typrelid;
198 typeTypElem(Type typ)
202 typtup = (Form_pg_type) GETSTRUCT(typ);
204 return typtup->typelem;
207 /* Given the attribute type of an array return the attribute type of
208 an element of the array */
211 GetArrayElementType(Oid typearray)
213 HeapTuple type_tuple;
214 Form_pg_type type_struct_array;
216 type_tuple = SearchSysCacheTuple(TYPEOID,
217 ObjectIdGetDatum(typearray),
220 if (!HeapTupleIsValid(type_tuple))
221 elog(ERROR, "GetArrayElementType: Cache lookup failed for type %u",
224 /* get the array type struct from the type tuple */
225 type_struct_array = (Form_pg_type) GETSTRUCT(type_tuple);
227 if (type_struct_array->typelem == InvalidOid)
229 elog(ERROR, "GetArrayElementType: type %s is not an array",
230 NameStr(type_struct_array->typname));
233 return type_struct_array->typelem;
236 /* Given a type structure, return the in-conversion function of the type */
242 typtup = (Form_pg_type) GETSTRUCT(typ);
244 return typtup->typinput;
247 /* Given a type structure, return the out-conversion function of the type */
249 typeOutfunc(Type typ)
253 typtup = (Form_pg_type) GETSTRUCT(typ);
255 return typtup->typoutput;