]> granicus.if.org Git - postgresql/blob - src/backend/parser/parse_type.c
Move some system includes into c.h, and remove duplicates.
[postgresql] / src / backend / parser / parse_type.c
1 /*-------------------------------------------------------------------------
2  *
3  * parse_type.c
4  *              handle type operations for parser
5  *
6  * Copyright (c) 1994, Regents of the University of California
7  *
8  *
9  * IDENTIFICATION
10  *        $Header: /cvsroot/pgsql/src/backend/parser/parse_type.c,v 1.24 1999/07/17 20:17:26 momjian Exp $
11  *
12  *-------------------------------------------------------------------------
13  */
14 #include "postgres.h"
15
16 #include "catalog/pg_type.h"
17 #include "parser/parse_type.h"
18 #include "utils/syscache.h"
19
20
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
26  */
27 bool
28 typeidIsValid(Oid id)
29 {
30         return (SearchSysCacheTuple(TYPOID,
31                                                                 ObjectIdGetDatum(id),
32                                                                 0, 0, 0) != NULL);
33 }
34
35 /* return a type name, given a typeid */
36 char *
37 typeidTypeName(Oid id)
38 {
39         HeapTuple       tup;
40         Form_pg_type typetuple;
41
42         if (!(tup = SearchSysCacheTuple(TYPOID,
43                                                                         ObjectIdGetDatum(id),
44                                                                         0, 0, 0)))
45         {
46                 elog(ERROR, "Unable to locate type oid %u in catalog", id);
47                 return NULL;
48         }
49         typetuple = (Form_pg_type) GETSTRUCT(tup);
50         return (typetuple->typname).data;
51 }
52
53 /* return a Type structure, given a type id */
54 Type
55 typeidType(Oid id)
56 {
57         HeapTuple       tup;
58
59         if (!(tup = SearchSysCacheTuple(TYPOID,
60                                                                         ObjectIdGetDatum(id),
61                                                                         0, 0, 0)))
62         {
63                 elog(ERROR, "Unable to locate type oid %u in catalog", id);
64                 return NULL;
65         }
66         return (Type) tup;
67 }
68
69 /* return a Type structure, given type name */
70 Type
71 typenameType(char *s)
72 {
73         HeapTuple       tup;
74
75         if (s == NULL)
76                 elog(ERROR, "type(): Null type");
77
78         if (!(tup = SearchSysCacheTuple(TYPNAME,
79                                                                         PointerGetDatum(s),
80                                                                         0, 0, 0)))
81                 elog(ERROR, "Unable to locate type name '%s' in catalog", s);
82         return (Type) tup;
83 }
84
85 /* given type, return the type OID */
86 Oid
87 typeTypeId(Type tp)
88 {
89         if (tp == NULL)
90                 elog(ERROR, "typeTypeId() called with NULL type struct");
91         return tp->t_data->t_oid;
92 }
93
94 /* given type (as type struct), return the length of type */
95 int16
96 typeLen(Type t)
97 {
98         Form_pg_type typ;
99
100         typ = (Form_pg_type) GETSTRUCT(t);
101         return typ->typlen;
102 }
103
104 /* given type (as type struct), return the value of its 'byval' attribute.*/
105 bool
106 typeByVal(Type t)
107 {
108         Form_pg_type typ;
109
110         typ = (Form_pg_type) GETSTRUCT(t);
111         return typ->typbyval;
112 }
113
114 /* given type (as type struct), return the name of type */
115 char *
116 typeTypeName(Type t)
117 {
118         Form_pg_type typ;
119
120         typ = (Form_pg_type) GETSTRUCT(t);
121         return (typ->typname).data;
122 }
123
124 /* given a type, return its typetype ('c' for 'c'atalog types) */
125 char
126 typeTypeFlag(Type t)
127 {
128         Form_pg_type typ;
129
130         typ = (Form_pg_type) GETSTRUCT(t);
131         return typ->typtype;
132 }
133
134 /* Given a type structure and a string, returns the internal form of
135    that string */
136 char *
137 stringTypeString(Type tp, char *string, int32 atttypmod)
138 {
139         Oid                     op;
140         Oid                     typelem;
141
142         op = ((Form_pg_type) GETSTRUCT(tp))->typinput;
143         typelem = ((Form_pg_type) GETSTRUCT(tp))->typelem;      /* XXX - used for
144                                                                                                                  * array_in */
145         return (char *) fmgr(op, string, typelem, atttypmod);
146 }
147
148 /* Given a type id, returns the out-conversion function of the type */
149 #ifdef NOT_USED
150 Oid
151 typeidOutfunc(Oid type_id)
152 {
153         HeapTuple       typeTuple;
154         Form_pg_type type;
155         Oid                     outfunc;
156
157         typeTuple = SearchSysCacheTuple(TYPOID,
158                                                                         ObjectIdGetDatum(type_id),
159                                                                         0, 0, 0);
160         if (!HeapTupleIsValid(typeTuple))
161                 elog(ERROR, "typeidOutfunc: Invalid type - oid = %u", type_id);
162
163         type = (Form_pg_type) GETSTRUCT(typeTuple);
164         outfunc = type->typoutput;
165         return outfunc;
166 }
167
168 #endif
169
170 Oid
171 typeidTypeRelid(Oid type_id)
172 {
173         HeapTuple       typeTuple;
174         Form_pg_type type;
175
176         typeTuple = SearchSysCacheTuple(TYPOID,
177                                                                         ObjectIdGetDatum(type_id),
178                                                                         0, 0, 0);
179         if (!HeapTupleIsValid(typeTuple))
180                 elog(ERROR, "typeidTypeRelid: Invalid type - oid = %u", type_id);
181
182         type = (Form_pg_type) GETSTRUCT(typeTuple);
183         return type->typrelid;
184 }
185
186 Oid
187 typeTypeRelid(Type typ)
188 {
189         Form_pg_type typtup;
190
191         typtup = (Form_pg_type) GETSTRUCT(typ);
192
193         return typtup->typrelid;
194 }
195
196 Oid
197 typeTypElem(Type typ)
198 {
199         Form_pg_type typtup;
200
201         typtup = (Form_pg_type) GETSTRUCT(typ);
202
203         return typtup->typelem;
204 }
205
206 /* Given the attribute type of an array return the attribute type of
207    an element of the array */
208
209 Oid
210 GetArrayElementType(Oid typearray)
211 {
212         HeapTuple       type_tuple;
213         Form_pg_type type_struct_array;
214
215         type_tuple = SearchSysCacheTuple(TYPOID,
216                                                                          ObjectIdGetDatum(typearray),
217                                                                          0, 0, 0);
218
219         if (!HeapTupleIsValid(type_tuple))
220                 elog(ERROR, "GetArrayElementType: Cache lookup failed for type %u",
221                          typearray);
222
223         /* get the array type struct from the type tuple */
224         type_struct_array = (Form_pg_type) GETSTRUCT(type_tuple);
225
226         if (type_struct_array->typelem == InvalidOid)
227         {
228                 elog(ERROR, "GetArrayElementType: type %s is not an array",
229                          type_struct_array->typname);
230         }
231
232         return type_struct_array->typelem;
233 }
234
235 /* Given a type structure, return the in-conversion function of the type */
236 Oid
237 typeInfunc(Type typ)
238 {
239         Form_pg_type typtup;
240
241         typtup = (Form_pg_type) GETSTRUCT(typ);
242
243         return typtup->typinput;
244 }