OSDN Git Service

Mark functions as static and ifdef NOT_USED as appropriate.
[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.32 2000/06/08 22:37:18 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         /* pstrdup here because result may need to outlive the syscache entry */
52         return pstrdup(NameStr(typetuple->typname));
53 }
54
55 /* return a Type structure, given a type id */
56 Type
57 typeidType(Oid id)
58 {
59         HeapTuple       tup;
60
61         if (!(tup = SearchSysCacheTuple(TYPEOID,
62                                                                         ObjectIdGetDatum(id),
63                                                                         0, 0, 0)))
64         {
65                 elog(ERROR, "Unable to locate type oid %u in catalog", id);
66                 return NULL;
67         }
68         return (Type) tup;
69 }
70
71 /* return a Type structure, given type name */
72 Type
73 typenameType(char *s)
74 {
75         HeapTuple       tup;
76
77         if (s == NULL)
78                 elog(ERROR, "type(): Null type");
79
80         if (!(tup = SearchSysCacheTuple(TYPENAME,
81                                                                         PointerGetDatum(s),
82                                                                         0, 0, 0)))
83                 elog(ERROR, "Unable to locate type name '%s' in catalog", s);
84         return (Type) tup;
85 }
86
87 /* given type, return the type OID */
88 Oid
89 typeTypeId(Type tp)
90 {
91         if (tp == NULL)
92                 elog(ERROR, "typeTypeId() called with NULL type struct");
93         return tp->t_data->t_oid;
94 }
95
96 /* given type (as type struct), return the length of type */
97 int16
98 typeLen(Type t)
99 {
100         Form_pg_type typ;
101
102         typ = (Form_pg_type) GETSTRUCT(t);
103         return typ->typlen;
104 }
105
106 /* given type (as type struct), return the value of its 'byval' attribute.*/
107 bool
108 typeByVal(Type t)
109 {
110         Form_pg_type typ;
111
112         typ = (Form_pg_type) GETSTRUCT(t);
113         return typ->typbyval;
114 }
115
116 /* given type (as type struct), return the name of type */
117 char *
118 typeTypeName(Type t)
119 {
120         Form_pg_type typ;
121
122         typ = (Form_pg_type) GETSTRUCT(t);
123         /* pstrdup here because result may need to outlive the syscache entry */
124         return pstrdup(NameStr(typ->typname));
125 }
126
127 /* given a type, return its typetype ('c' for 'c'atalog types) */
128 char
129 typeTypeFlag(Type t)
130 {
131         Form_pg_type typ;
132
133         typ = (Form_pg_type) GETSTRUCT(t);
134         return typ->typtype;
135 }
136
137 /* Given a type structure and a string, returns the internal form of
138    that string */
139 Datum
140 stringTypeDatum(Type tp, char *string, int32 atttypmod)
141 {
142         Oid                     op;
143         Oid                     typelem;
144
145         op = ((Form_pg_type) GETSTRUCT(tp))->typinput;
146         typelem = ((Form_pg_type) GETSTRUCT(tp))->typelem;      /* XXX - used for
147                                                                                                                  * array_in */
148         return OidFunctionCall3(op,
149                                                         CStringGetDatum(string),
150                                                         ObjectIdGetDatum(typelem),
151                                                         Int32GetDatum(atttypmod));
152 }
153
154 /* Given a type id, returns the out-conversion function of the type */
155 #ifdef NOT_USED
156 Oid
157 typeidOutfunc(Oid type_id)
158 {
159         HeapTuple       typeTuple;
160         Form_pg_type type;
161         Oid                     outfunc;
162
163         typeTuple = SearchSysCacheTuple(TYPEOID,
164                                                                         ObjectIdGetDatum(type_id),
165                                                                         0, 0, 0);
166         if (!HeapTupleIsValid(typeTuple))
167                 elog(ERROR, "typeidOutfunc: Invalid type - oid = %u", type_id);
168
169         type = (Form_pg_type) GETSTRUCT(typeTuple);
170         outfunc = type->typoutput;
171         return outfunc;
172 }
173
174 #endif
175
176 Oid
177 typeidTypeRelid(Oid type_id)
178 {
179         HeapTuple       typeTuple;
180         Form_pg_type type;
181
182         typeTuple = SearchSysCacheTuple(TYPEOID,
183                                                                         ObjectIdGetDatum(type_id),
184                                                                         0, 0, 0);
185         if (!HeapTupleIsValid(typeTuple))
186                 elog(ERROR, "typeidTypeRelid: Invalid type - oid = %u", type_id);
187
188         type = (Form_pg_type) GETSTRUCT(typeTuple);
189         return type->typrelid;
190 }
191
192 Oid
193 typeTypeRelid(Type typ)
194 {
195         Form_pg_type typtup;
196
197         typtup = (Form_pg_type) GETSTRUCT(typ);
198
199         return typtup->typrelid;
200 }
201
202 #ifdef NOT_USED
203 Oid
204 typeTypElem(Type typ)
205 {
206         Form_pg_type typtup;
207
208         typtup = (Form_pg_type) GETSTRUCT(typ);
209
210         return typtup->typelem;
211 }
212 #endif
213
214 #ifdef NOT_USED
215 /* Given the attribute type of an array return the attribute type of
216    an element of the array */
217 Oid
218 GetArrayElementType(Oid typearray)
219 {
220         HeapTuple       type_tuple;
221         Form_pg_type type_struct_array;
222
223         type_tuple = SearchSysCacheTuple(TYPEOID,
224                                                                          ObjectIdGetDatum(typearray),
225                                                                          0, 0, 0);
226
227         if (!HeapTupleIsValid(type_tuple))
228                 elog(ERROR, "GetArrayElementType: Cache lookup failed for type %u",
229                          typearray);
230
231         /* get the array type struct from the type tuple */
232         type_struct_array = (Form_pg_type) GETSTRUCT(type_tuple);
233
234         if (type_struct_array->typelem == InvalidOid)
235         {
236                 elog(ERROR, "GetArrayElementType: type %s is not an array",
237                          NameStr(type_struct_array->typname));
238         }
239
240         return type_struct_array->typelem;
241 }
242 #endif
243
244 #ifdef NOT_USED
245 /* Given a type structure, return the in-conversion function of the type */
246 Oid
247 typeInfunc(Type typ)
248 {
249         Form_pg_type typtup;
250
251         typtup = (Form_pg_type) GETSTRUCT(typ);
252
253         return typtup->typinput;
254 }
255 #endif
256
257 #ifdef NOT_USED
258 /* Given a type structure, return the out-conversion function of the type */
259 Oid
260 typeOutfunc(Type typ)
261 {
262         Form_pg_type typtup;
263
264         typtup = (Form_pg_type) GETSTRUCT(typ);
265
266         return typtup->typoutput;
267 }
268 #endif