OSDN Git Service

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