OSDN Git Service

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