Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * printsimple.c
4 : * Routines to print out tuples containing only a limited range of
5 : * builtin types without catalog access. This is intended for
6 : * backends that don't have catalog access because they are not bound
7 : * to a specific database, such as some walsender processes. It
8 : * doesn't handle standalone backends or protocol versions other than
9 : * 3.0, because we don't need such handling for current applications.
10 : *
11 : * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
12 : * Portions Copyright (c) 1994, Regents of the University of California
13 : *
14 : * IDENTIFICATION
15 : * src/backend/access/common/printsimple.c
16 : *
17 : *-------------------------------------------------------------------------
18 : */
19 : #include "postgres.h"
20 :
21 : #include "access/printsimple.h"
22 : #include "catalog/pg_type.h"
23 : #include "fmgr.h"
24 : #include "libpq/pqformat.h"
25 : #include "utils/builtins.h"
26 :
27 : /*
28 : * At startup time, send a RowDescription message.
29 : */
30 : void
31 0 : printsimple_startup(DestReceiver *self, int operation, TupleDesc tupdesc)
32 : {
33 : StringInfoData buf;
34 : int i;
35 :
36 0 : pq_beginmessage(&buf, 'T'); /* RowDescription */
37 0 : pq_sendint(&buf, tupdesc->natts, 2);
38 :
39 0 : for (i = 0; i < tupdesc->natts; ++i)
40 : {
41 0 : Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
42 :
43 0 : pq_sendstring(&buf, NameStr(attr->attname));
44 0 : pq_sendint(&buf, 0, 4); /* table oid */
45 0 : pq_sendint(&buf, 0, 2); /* attnum */
46 0 : pq_sendint(&buf, (int) attr->atttypid, 4);
47 0 : pq_sendint(&buf, attr->attlen, 2);
48 0 : pq_sendint(&buf, attr->atttypmod, 4);
49 0 : pq_sendint(&buf, 0, 2); /* format code */
50 : }
51 :
52 0 : pq_endmessage(&buf);
53 0 : }
54 :
55 : /*
56 : * For each tuple, send a DataRow message.
57 : */
58 : bool
59 0 : printsimple(TupleTableSlot *slot, DestReceiver *self)
60 : {
61 0 : TupleDesc tupdesc = slot->tts_tupleDescriptor;
62 : StringInfoData buf;
63 : int i;
64 :
65 : /* Make sure the tuple is fully deconstructed */
66 0 : slot_getallattrs(slot);
67 :
68 : /* Prepare and send message */
69 0 : pq_beginmessage(&buf, 'D');
70 0 : pq_sendint(&buf, tupdesc->natts, 2);
71 :
72 0 : for (i = 0; i < tupdesc->natts; ++i)
73 : {
74 0 : Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
75 : Datum value;
76 :
77 0 : if (slot->tts_isnull[i])
78 : {
79 0 : pq_sendint(&buf, -1, 4);
80 0 : continue;
81 : }
82 :
83 0 : value = slot->tts_values[i];
84 :
85 : /*
86 : * We can't call the regular type output functions here because we
87 : * might not have catalog access. Instead, we must hard-wire
88 : * knowledge of the required types.
89 : */
90 0 : switch (attr->atttypid)
91 : {
92 : case TEXTOID:
93 : {
94 0 : text *t = DatumGetTextPP(value);
95 :
96 0 : pq_sendcountedtext(&buf,
97 0 : VARDATA_ANY(t),
98 0 : VARSIZE_ANY_EXHDR(t),
99 : false);
100 : }
101 0 : break;
102 :
103 : case INT4OID:
104 : {
105 0 : int32 num = DatumGetInt32(value);
106 : char str[12]; /* sign, 10 digits and '\0' */
107 :
108 0 : pg_ltoa(num, str);
109 0 : pq_sendcountedtext(&buf, str, strlen(str), false);
110 : }
111 0 : break;
112 :
113 : case INT8OID:
114 : {
115 0 : int64 num = DatumGetInt64(value);
116 : char str[23]; /* sign, 21 digits and '\0' */
117 :
118 0 : pg_lltoa(num, str);
119 0 : pq_sendcountedtext(&buf, str, strlen(str), false);
120 : }
121 0 : break;
122 :
123 : default:
124 0 : elog(ERROR, "unsupported type OID: %u", attr->atttypid);
125 : }
126 : }
127 :
128 0 : pq_endmessage(&buf);
129 :
130 0 : return true;
131 : }
|