]> granicus.if.org Git - postgresql/blob - src/backend/utils/adt/pseudotypes.c
Allow binary I/O of type "void".
[postgresql] / src / backend / utils / adt / pseudotypes.c
1 /*-------------------------------------------------------------------------
2  *
3  * pseudotypes.c
4  *        Functions for the system pseudo-types.
5  *
6  * A pseudo-type isn't really a type and never has any operations, but
7  * we do need to supply input and output functions to satisfy the links
8  * in the pseudo-type's entry in pg_type.  In most cases the functions
9  * just throw an error if invoked.      (XXX the error messages here cover
10  * the most common case, but might be confusing in some contexts.  Can
11  * we do better?)
12  *
13  *
14  * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
15  * Portions Copyright (c) 1994, Regents of the University of California
16  *
17  *
18  * IDENTIFICATION
19  *        src/backend/utils/adt/pseudotypes.c
20  *
21  *-------------------------------------------------------------------------
22  */
23 #include "postgres.h"
24
25 #include "libpq/pqformat.h"
26 #include "utils/array.h"
27 #include "utils/builtins.h"
28
29
30 /*
31  * cstring_in           - input routine for pseudo-type CSTRING.
32  *
33  * We might as well allow this to support constructs like "foo_in('blah')".
34  */
35 Datum
36 cstring_in(PG_FUNCTION_ARGS)
37 {
38         char       *str = PG_GETARG_CSTRING(0);
39
40         PG_RETURN_CSTRING(pstrdup(str));
41 }
42
43 /*
44  * cstring_out          - output routine for pseudo-type CSTRING.
45  *
46  * We allow this mainly so that "SELECT some_output_function(...)" does
47  * what the user will expect.
48  */
49 Datum
50 cstring_out(PG_FUNCTION_ARGS)
51 {
52         char       *str = PG_GETARG_CSTRING(0);
53
54         PG_RETURN_CSTRING(pstrdup(str));
55 }
56
57 /*
58  * cstring_recv         - binary input routine for pseudo-type CSTRING.
59  */
60 Datum
61 cstring_recv(PG_FUNCTION_ARGS)
62 {
63         StringInfo      buf = (StringInfo) PG_GETARG_POINTER(0);
64         char       *str;
65         int                     nbytes;
66
67         str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes);
68         PG_RETURN_CSTRING(str);
69 }
70
71 /*
72  * cstring_send         - binary output routine for pseudo-type CSTRING.
73  */
74 Datum
75 cstring_send(PG_FUNCTION_ARGS)
76 {
77         char       *str = PG_GETARG_CSTRING(0);
78         StringInfoData buf;
79
80         pq_begintypsend(&buf);
81         pq_sendtext(&buf, str, strlen(str));
82         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
83 }
84
85
86 /*
87  * any_in               - input routine for pseudo-type ANY.
88  */
89 Datum
90 any_in(PG_FUNCTION_ARGS)
91 {
92         ereport(ERROR,
93                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
94                          errmsg("cannot accept a value of type any")));
95
96         PG_RETURN_VOID();                       /* keep compiler quiet */
97 }
98
99 /*
100  * any_out              - output routine for pseudo-type ANY.
101  */
102 Datum
103 any_out(PG_FUNCTION_ARGS)
104 {
105         ereport(ERROR,
106                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
107                          errmsg("cannot display a value of type any")));
108
109         PG_RETURN_VOID();                       /* keep compiler quiet */
110 }
111
112
113 /*
114  * anyarray_in          - input routine for pseudo-type ANYARRAY.
115  */
116 Datum
117 anyarray_in(PG_FUNCTION_ARGS)
118 {
119         ereport(ERROR,
120                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
121                          errmsg("cannot accept a value of type anyarray")));
122
123         PG_RETURN_VOID();                       /* keep compiler quiet */
124 }
125
126 /*
127  * anyarray_out         - output routine for pseudo-type ANYARRAY.
128  *
129  * We may as well allow this, since array_out will in fact work.
130  */
131 Datum
132 anyarray_out(PG_FUNCTION_ARGS)
133 {
134         return array_out(fcinfo);
135 }
136
137 /*
138  * anyarray_recv                - binary input routine for pseudo-type ANYARRAY.
139  *
140  * XXX this could actually be made to work, since the incoming array
141  * data will contain the element type OID.      Need to think through
142  * type-safety issues before allowing it, however.
143  */
144 Datum
145 anyarray_recv(PG_FUNCTION_ARGS)
146 {
147         ereport(ERROR,
148                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
149                          errmsg("cannot accept a value of type anyarray")));
150
151         PG_RETURN_VOID();                       /* keep compiler quiet */
152 }
153
154 /*
155  * anyarray_send                - binary output routine for pseudo-type ANYARRAY.
156  *
157  * We may as well allow this, since array_send will in fact work.
158  */
159 Datum
160 anyarray_send(PG_FUNCTION_ARGS)
161 {
162         return array_send(fcinfo);
163 }
164
165
166 /*
167  * anyenum_in           - input routine for pseudo-type ANYENUM.
168  */
169 Datum
170 anyenum_in(PG_FUNCTION_ARGS)
171 {
172         ereport(ERROR,
173                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
174                          errmsg("cannot accept a value of type anyenum")));
175
176         PG_RETURN_VOID();                       /* keep compiler quiet */
177 }
178
179 /*
180  * anyenum_out          - output routine for pseudo-type ANYENUM.
181  *
182  * We may as well allow this, since enum_out will in fact work.
183  */
184 Datum
185 anyenum_out(PG_FUNCTION_ARGS)
186 {
187         return enum_out(fcinfo);
188 }
189
190
191 /*
192  * void_in              - input routine for pseudo-type VOID.
193  *
194  * We allow this so that PL functions can return VOID without any special
195  * hack in the PL handler.      Whatever value the PL thinks it's returning
196  * will just be ignored.
197  */
198 Datum
199 void_in(PG_FUNCTION_ARGS)
200 {
201         PG_RETURN_VOID();                       /* you were expecting something different? */
202 }
203
204 /*
205  * void_out             - output routine for pseudo-type VOID.
206  *
207  * We allow this so that "SELECT function_returning_void(...)" works.
208  */
209 Datum
210 void_out(PG_FUNCTION_ARGS)
211 {
212         PG_RETURN_CSTRING(pstrdup(""));
213 }
214
215 /*
216  * void_recv    - binary input routine for pseudo-type VOID.
217  *
218  * Note that since we consume no bytes, an attempt to send anything but
219  * an empty string will result in an "invalid message format" error.
220  */
221 Datum
222 void_recv(PG_FUNCTION_ARGS)
223 {
224         PG_RETURN_VOID();
225 }
226
227 /*
228  * void_send    - binary output routine for pseudo-type VOID.
229  *
230  * We allow this so that "SELECT function_returning_void(...)" works
231  * even when binary output is requested.
232  */
233 Datum
234 void_send(PG_FUNCTION_ARGS)
235 {
236         StringInfoData buf;
237
238         /* send an empty string */
239         pq_begintypsend(&buf);
240         PG_RETURN_BYTEA_P(pq_endtypsend(&buf));
241 }
242
243
244 /*
245  * trigger_in           - input routine for pseudo-type TRIGGER.
246  */
247 Datum
248 trigger_in(PG_FUNCTION_ARGS)
249 {
250         ereport(ERROR,
251                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
252                          errmsg("cannot accept a value of type trigger")));
253
254         PG_RETURN_VOID();                       /* keep compiler quiet */
255 }
256
257 /*
258  * trigger_out          - output routine for pseudo-type TRIGGER.
259  */
260 Datum
261 trigger_out(PG_FUNCTION_ARGS)
262 {
263         ereport(ERROR,
264                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
265                          errmsg("cannot display a value of type trigger")));
266
267         PG_RETURN_VOID();                       /* keep compiler quiet */
268 }
269
270
271 /*
272  * language_handler_in          - input routine for pseudo-type LANGUAGE_HANDLER.
273  */
274 Datum
275 language_handler_in(PG_FUNCTION_ARGS)
276 {
277         ereport(ERROR,
278                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
279                          errmsg("cannot accept a value of type language_handler")));
280
281         PG_RETURN_VOID();                       /* keep compiler quiet */
282 }
283
284 /*
285  * language_handler_out         - output routine for pseudo-type LANGUAGE_HANDLER.
286  */
287 Datum
288 language_handler_out(PG_FUNCTION_ARGS)
289 {
290         ereport(ERROR,
291                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
292                          errmsg("cannot display a value of type language_handler")));
293
294         PG_RETURN_VOID();                       /* keep compiler quiet */
295 }
296
297
298 /*
299  * fdw_handler_in               - input routine for pseudo-type FDW_HANDLER.
300  */
301 Datum
302 fdw_handler_in(PG_FUNCTION_ARGS)
303 {
304         ereport(ERROR,
305                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
306                          errmsg("cannot accept a value of type fdw_handler")));
307
308         PG_RETURN_VOID();                       /* keep compiler quiet */
309 }
310
311 /*
312  * fdw_handler_out              - output routine for pseudo-type FDW_HANDLER.
313  */
314 Datum
315 fdw_handler_out(PG_FUNCTION_ARGS)
316 {
317         ereport(ERROR,
318                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
319                          errmsg("cannot display a value of type fdw_handler")));
320
321         PG_RETURN_VOID();                       /* keep compiler quiet */
322 }
323
324
325 /*
326  * internal_in          - input routine for pseudo-type INTERNAL.
327  */
328 Datum
329 internal_in(PG_FUNCTION_ARGS)
330 {
331         ereport(ERROR,
332                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
333                          errmsg("cannot accept a value of type internal")));
334
335         PG_RETURN_VOID();                       /* keep compiler quiet */
336 }
337
338 /*
339  * internal_out         - output routine for pseudo-type INTERNAL.
340  */
341 Datum
342 internal_out(PG_FUNCTION_ARGS)
343 {
344         ereport(ERROR,
345                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
346                          errmsg("cannot display a value of type internal")));
347
348         PG_RETURN_VOID();                       /* keep compiler quiet */
349 }
350
351
352 /*
353  * opaque_in            - input routine for pseudo-type OPAQUE.
354  */
355 Datum
356 opaque_in(PG_FUNCTION_ARGS)
357 {
358         ereport(ERROR,
359                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
360                          errmsg("cannot accept a value of type opaque")));
361
362         PG_RETURN_VOID();                       /* keep compiler quiet */
363 }
364
365 /*
366  * opaque_out           - output routine for pseudo-type OPAQUE.
367  */
368 Datum
369 opaque_out(PG_FUNCTION_ARGS)
370 {
371         ereport(ERROR,
372                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
373                          errmsg("cannot display a value of type opaque")));
374
375         PG_RETURN_VOID();                       /* keep compiler quiet */
376 }
377
378
379 /*
380  * anyelement_in                - input routine for pseudo-type ANYELEMENT.
381  */
382 Datum
383 anyelement_in(PG_FUNCTION_ARGS)
384 {
385         ereport(ERROR,
386                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
387                          errmsg("cannot accept a value of type anyelement")));
388
389         PG_RETURN_VOID();                       /* keep compiler quiet */
390 }
391
392 /*
393  * anyelement_out               - output routine for pseudo-type ANYELEMENT.
394  */
395 Datum
396 anyelement_out(PG_FUNCTION_ARGS)
397 {
398         ereport(ERROR,
399                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
400                          errmsg("cannot display a value of type anyelement")));
401
402         PG_RETURN_VOID();                       /* keep compiler quiet */
403 }
404
405 /*
406  * anynonarray_in               - input routine for pseudo-type ANYNONARRAY.
407  */
408 Datum
409 anynonarray_in(PG_FUNCTION_ARGS)
410 {
411         ereport(ERROR,
412                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
413                          errmsg("cannot accept a value of type anynonarray")));
414
415         PG_RETURN_VOID();                       /* keep compiler quiet */
416 }
417
418 /*
419  * anynonarray_out              - output routine for pseudo-type ANYNONARRAY.
420  */
421 Datum
422 anynonarray_out(PG_FUNCTION_ARGS)
423 {
424         ereport(ERROR,
425                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
426                          errmsg("cannot display a value of type anynonarray")));
427
428         PG_RETURN_VOID();                       /* keep compiler quiet */
429 }
430
431 /*
432  * shell_in             - input routine for "shell" types (those not yet filled in).
433  */
434 Datum
435 shell_in(PG_FUNCTION_ARGS)
436 {
437         ereport(ERROR,
438                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
439                          errmsg("cannot accept a value of a shell type")));
440
441         PG_RETURN_VOID();                       /* keep compiler quiet */
442 }
443
444 /*
445  * shell_out            - output routine for "shell" types.
446  */
447 Datum
448 shell_out(PG_FUNCTION_ARGS)
449 {
450         ereport(ERROR,
451                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
452                          errmsg("cannot display a value of a shell type")));
453
454         PG_RETURN_VOID();                       /* keep compiler quiet */
455 }
456
457
458 /*
459  * pg_node_tree_in              - input routine for type PG_NODE_TREE.
460  *
461  * pg_node_tree isn't really a pseudotype --- it's real enough to be a table
462  * column --- but it presently has no operations of its own, and disallows
463  * input too, so its I/O functions seem to fit here as much as anywhere.
464  */
465 Datum
466 pg_node_tree_in(PG_FUNCTION_ARGS)
467 {
468         /*
469          * We disallow input of pg_node_tree values because the SQL functions that
470          * operate on the type are not secure against malformed input.
471          */
472         ereport(ERROR,
473                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
474                          errmsg("cannot accept a value of type pg_node_tree")));
475
476         PG_RETURN_VOID();                       /* keep compiler quiet */
477 }
478
479 /*
480  * pg_node_tree_out             - output routine for type PG_NODE_TREE.
481  *
482  * The internal representation is the same as TEXT, so just pass it off.
483  */
484 Datum
485 pg_node_tree_out(PG_FUNCTION_ARGS)
486 {
487         return textout(fcinfo);
488 }
489
490 /*
491  * pg_node_tree_recv            - binary input routine for type PG_NODE_TREE.
492  */
493 Datum
494 pg_node_tree_recv(PG_FUNCTION_ARGS)
495 {
496         ereport(ERROR,
497                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
498                          errmsg("cannot accept a value of type pg_node_tree")));
499
500         PG_RETURN_VOID();                       /* keep compiler quiet */
501 }
502
503 /*
504  * pg_node_tree_send            - binary output routine for type PG_NODE_TREE.
505  */
506 Datum
507 pg_node_tree_send(PG_FUNCTION_ARGS)
508 {
509         return textsend(fcinfo);
510 }