1 /*-------------------------------------------------------------------------
4 * handle type operations for parser
6 * Copyright (c) 1994, Regents of the University of California
10 * $Header: /cvsroot/pgsql/src/backend/parser/parse_type.c,v 1.24 1999/07/17 20:17:26 momjian Exp $
12 *-------------------------------------------------------------------------
16 #include "catalog/pg_type.h"
17 #include "parser/parse_type.h"
18 #include "utils/syscache.h"
21 /* check to see if a type id is valid,
22 * returns true if it is. By using this call before calling
23 * typeidType or typeidTypeName, more meaningful error messages
24 * can be produced because the caller typically has more context of
25 * what's going on - jolly
30 return (SearchSysCacheTuple(TYPOID,
35 /* return a type name, given a typeid */
37 typeidTypeName(Oid id)
40 Form_pg_type typetuple;
42 if (!(tup = SearchSysCacheTuple(TYPOID,
46 elog(ERROR, "Unable to locate type oid %u in catalog", id);
49 typetuple = (Form_pg_type) GETSTRUCT(tup);
50 return (typetuple->typname).data;
53 /* return a Type structure, given a type id */
59 if (!(tup = SearchSysCacheTuple(TYPOID,
63 elog(ERROR, "Unable to locate type oid %u in catalog", id);
69 /* return a Type structure, given type name */
76 elog(ERROR, "type(): Null type");
78 if (!(tup = SearchSysCacheTuple(TYPNAME,
81 elog(ERROR, "Unable to locate type name '%s' in catalog", s);
85 /* given type, return the type OID */
90 elog(ERROR, "typeTypeId() called with NULL type struct");
91 return tp->t_data->t_oid;
94 /* given type (as type struct), return the length of type */
100 typ = (Form_pg_type) GETSTRUCT(t);
104 /* given type (as type struct), return the value of its 'byval' attribute.*/
110 typ = (Form_pg_type) GETSTRUCT(t);
111 return typ->typbyval;
114 /* given type (as type struct), return the name of type */
120 typ = (Form_pg_type) GETSTRUCT(t);
121 return (typ->typname).data;
124 /* given a type, return its typetype ('c' for 'c'atalog types) */
130 typ = (Form_pg_type) GETSTRUCT(t);
134 /* Given a type structure and a string, returns the internal form of
137 stringTypeString(Type tp, char *string, int32 atttypmod)
142 op = ((Form_pg_type) GETSTRUCT(tp))->typinput;
143 typelem = ((Form_pg_type) GETSTRUCT(tp))->typelem; /* XXX - used for
145 return (char *) fmgr(op, string, typelem, atttypmod);
148 /* Given a type id, returns the out-conversion function of the type */
151 typeidOutfunc(Oid type_id)
157 typeTuple = SearchSysCacheTuple(TYPOID,
158 ObjectIdGetDatum(type_id),
160 if (!HeapTupleIsValid(typeTuple))
161 elog(ERROR, "typeidOutfunc: Invalid type - oid = %u", type_id);
163 type = (Form_pg_type) GETSTRUCT(typeTuple);
164 outfunc = type->typoutput;
171 typeidTypeRelid(Oid type_id)
176 typeTuple = SearchSysCacheTuple(TYPOID,
177 ObjectIdGetDatum(type_id),
179 if (!HeapTupleIsValid(typeTuple))
180 elog(ERROR, "typeidTypeRelid: Invalid type - oid = %u", type_id);
182 type = (Form_pg_type) GETSTRUCT(typeTuple);
183 return type->typrelid;
187 typeTypeRelid(Type typ)
191 typtup = (Form_pg_type) GETSTRUCT(typ);
193 return typtup->typrelid;
197 typeTypElem(Type typ)
201 typtup = (Form_pg_type) GETSTRUCT(typ);
203 return typtup->typelem;
206 /* Given the attribute type of an array return the attribute type of
207 an element of the array */
210 GetArrayElementType(Oid typearray)
212 HeapTuple type_tuple;
213 Form_pg_type type_struct_array;
215 type_tuple = SearchSysCacheTuple(TYPOID,
216 ObjectIdGetDatum(typearray),
219 if (!HeapTupleIsValid(type_tuple))
220 elog(ERROR, "GetArrayElementType: Cache lookup failed for type %u",
223 /* get the array type struct from the type tuple */
224 type_struct_array = (Form_pg_type) GETSTRUCT(type_tuple);
226 if (type_struct_array->typelem == InvalidOid)
228 elog(ERROR, "GetArrayElementType: type %s is not an array",
229 type_struct_array->typname);
232 return type_struct_array->typelem;
235 /* Given a type structure, return the in-conversion function of the type */
241 typtup = (Form_pg_type) GETSTRUCT(typ);
243 return typtup->typinput;