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.20 1999/05/10 00:45:29 momjian Exp $
12 *-------------------------------------------------------------------------
18 #include "nodes/nodes.h"
19 #include "nodes/parsenodes.h"
20 #include "nodes/primnodes.h"
21 #include "parser/parse_node.h"
23 #include "catalog/pg_type.h"
24 #include "parser/parse_target.h"
25 #include "parser/parse_type.h"
26 #include "utils/syscache.h"
29 /* check to see if a type id is valid,
30 * returns true if it is. By using this call before calling
31 * typeidType or typeidTypeName, more meaningful error messages
32 * can be produced because the caller typically has more context of
33 * what's going on - jolly
38 return (SearchSysCacheTuple(TYPOID,
43 /* return a type name, given a typeid */
45 typeidTypeName(Oid id)
48 Form_pg_type typetuple;
50 if (!(tup = SearchSysCacheTuple(TYPOID,
54 elog(ERROR, "Unable to locate type oid %u in catalog", id);
57 typetuple = (Form_pg_type) GETSTRUCT(tup);
58 return (typetuple->typname).data;
61 /* return a Type structure, given an typid */
67 if (!(tup = SearchSysCacheTuple(TYPOID,
71 elog(ERROR, "Unable to locate type oid %u in catalog", id);
77 /* return a Type structure, given type name */
84 elog(ERROR, "type(): Null type");
86 if (!(tup = SearchSysCacheTuple(TYPNAME,
89 elog(ERROR, "Unable to locate type name '%s' in catalog", s);
93 /* given type, return the type OID */
98 elog(ERROR, "typeTypeId() called with NULL type struct");
99 return tp->t_data->t_oid;
102 /* given type (as type struct), return the length of type */
108 typ = (Form_pg_type) GETSTRUCT(t);
112 /* given type (as type struct), return the value of its 'byval' attribute.*/
118 typ = (Form_pg_type) GETSTRUCT(t);
119 return typ->typbyval;
122 /* given type (as type struct), return the name of type */
128 typ = (Form_pg_type) GETSTRUCT(t);
129 return (typ->typname).data;
132 /* given a type, return its typetype ('c' for 'c'atalog types) */
138 typ = (Form_pg_type) GETSTRUCT(t);
142 /* Given a type structure and a string, returns the internal form of
145 stringTypeString(Type tp, char *string, int32 atttypmod)
150 op = ((Form_pg_type) GETSTRUCT(tp))->typinput;
151 typelem = ((Form_pg_type) GETSTRUCT(tp))->typelem; /* XXX - used for
153 return (char *) fmgr(op, string, typelem, atttypmod);
156 /* Given a type id, returns the out-conversion function of the type */
159 typeidOutfunc(Oid type_id)
165 typeTuple = SearchSysCacheTuple(TYPOID,
166 ObjectIdGetDatum(type_id),
168 if (!HeapTupleIsValid(typeTuple))
169 elog(ERROR, "typeidOutfunc: Invalid type - oid = %u", type_id);
171 type = (Form_pg_type) GETSTRUCT(typeTuple);
172 outfunc = type->typoutput;
178 typeidTypeRelid(Oid type_id)
184 typeTuple = SearchSysCacheTuple(TYPOID,
185 ObjectIdGetDatum(type_id),
187 if (!HeapTupleIsValid(typeTuple))
188 elog(ERROR, "typeidTypeRelid: Invalid type - oid = %u", type_id);
190 type = (Form_pg_type) GETSTRUCT(typeTuple);
191 infunc = type->typrelid;
196 typeTypeRelid(Type typ)
200 typtup = (Form_pg_type) GETSTRUCT(typ);
202 return typtup->typrelid;
206 typeidTypElem(Oid type_id)
211 if (!(typeTuple = SearchSysCacheTuple(TYPOID,
212 ObjectIdGetDatum(type_id),
214 elog(ERROR, "type id lookup of %u failed", type_id);
215 type = (Form_pg_type) GETSTRUCT(typeTuple);
217 return type->typelem;
220 /* Given the attribute type of an array return the attribute type of
221 an element of the array */
224 GetArrayElementType(Oid typearray)
226 HeapTuple type_tuple;
227 Form_pg_type type_struct_array;
229 type_tuple = SearchSysCacheTuple(TYPOID,
230 ObjectIdGetDatum(typearray),
233 if (!HeapTupleIsValid(type_tuple))
234 elog(ERROR, "GetArrayElementType: Cache lookup failed for type %u",
237 /* get the array type struct from the type tuple */
238 type_struct_array = (Form_pg_type) GETSTRUCT(type_tuple);
240 if (type_struct_array->typelem == InvalidOid)
242 elog(ERROR, "GetArrayElementType: type %s is not an array",
243 type_struct_array->typname);
246 return type_struct_array->typelem;
249 /* Given a type id, returns the in-conversion function of the type */
251 typeidInfunc(Oid type_id)
257 typeTuple = SearchSysCacheTuple(TYPOID,
258 ObjectIdGetDatum(type_id),
260 if (!HeapTupleIsValid(typeTuple))
261 elog(ERROR, "typeidInfunc: Invalid type - oid = %u", type_id);
263 type = (Form_pg_type) GETSTRUCT(typeTuple);
264 infunc = type->typinput;