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.32 2000/06/08 22:37:18 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 /* pstrdup here because result may need to outlive the syscache entry */
52 return pstrdup(NameStr(typetuple->typname));
55 /* return a Type structure, given a type id */
61 if (!(tup = SearchSysCacheTuple(TYPEOID,
65 elog(ERROR, "Unable to locate type oid %u in catalog", id);
71 /* return a Type structure, given type name */
78 elog(ERROR, "type(): Null type");
80 if (!(tup = SearchSysCacheTuple(TYPENAME,
83 elog(ERROR, "Unable to locate type name '%s' in catalog", s);
87 /* given type, return the type OID */
92 elog(ERROR, "typeTypeId() called with NULL type struct");
93 return tp->t_data->t_oid;
96 /* given type (as type struct), return the length of type */
102 typ = (Form_pg_type) GETSTRUCT(t);
106 /* given type (as type struct), return the value of its 'byval' attribute.*/
112 typ = (Form_pg_type) GETSTRUCT(t);
113 return typ->typbyval;
116 /* given type (as type struct), return the name of type */
122 typ = (Form_pg_type) GETSTRUCT(t);
123 /* pstrdup here because result may need to outlive the syscache entry */
124 return pstrdup(NameStr(typ->typname));
127 /* given a type, return its typetype ('c' for 'c'atalog types) */
133 typ = (Form_pg_type) GETSTRUCT(t);
137 /* Given a type structure and a string, returns the internal form of
140 stringTypeDatum(Type tp, char *string, int32 atttypmod)
145 op = ((Form_pg_type) GETSTRUCT(tp))->typinput;
146 typelem = ((Form_pg_type) GETSTRUCT(tp))->typelem; /* XXX - used for
148 return OidFunctionCall3(op,
149 CStringGetDatum(string),
150 ObjectIdGetDatum(typelem),
151 Int32GetDatum(atttypmod));
154 /* Given a type id, returns the out-conversion function of the type */
157 typeidOutfunc(Oid type_id)
163 typeTuple = SearchSysCacheTuple(TYPEOID,
164 ObjectIdGetDatum(type_id),
166 if (!HeapTupleIsValid(typeTuple))
167 elog(ERROR, "typeidOutfunc: Invalid type - oid = %u", type_id);
169 type = (Form_pg_type) GETSTRUCT(typeTuple);
170 outfunc = type->typoutput;
177 typeidTypeRelid(Oid type_id)
182 typeTuple = SearchSysCacheTuple(TYPEOID,
183 ObjectIdGetDatum(type_id),
185 if (!HeapTupleIsValid(typeTuple))
186 elog(ERROR, "typeidTypeRelid: Invalid type - oid = %u", type_id);
188 type = (Form_pg_type) GETSTRUCT(typeTuple);
189 return type->typrelid;
193 typeTypeRelid(Type typ)
197 typtup = (Form_pg_type) GETSTRUCT(typ);
199 return typtup->typrelid;
204 typeTypElem(Type typ)
208 typtup = (Form_pg_type) GETSTRUCT(typ);
210 return typtup->typelem;
215 /* Given the attribute type of an array return the attribute type of
216 an element of the array */
218 GetArrayElementType(Oid typearray)
220 HeapTuple type_tuple;
221 Form_pg_type type_struct_array;
223 type_tuple = SearchSysCacheTuple(TYPEOID,
224 ObjectIdGetDatum(typearray),
227 if (!HeapTupleIsValid(type_tuple))
228 elog(ERROR, "GetArrayElementType: Cache lookup failed for type %u",
231 /* get the array type struct from the type tuple */
232 type_struct_array = (Form_pg_type) GETSTRUCT(type_tuple);
234 if (type_struct_array->typelem == InvalidOid)
236 elog(ERROR, "GetArrayElementType: type %s is not an array",
237 NameStr(type_struct_array->typname));
240 return type_struct_array->typelem;
245 /* Given a type structure, return the in-conversion function of the type */
251 typtup = (Form_pg_type) GETSTRUCT(typ);
253 return typtup->typinput;
258 /* Given a type structure, return the out-conversion function of the type */
260 typeOutfunc(Type typ)
264 typtup = (Form_pg_type) GETSTRUCT(typ);
266 return typtup->typoutput;