1 /*-------------------------------------------------------------------------
4 * Routines to print out tuples to the destination (binary or non-binary
5 * portals, frontend/interactive backend, etc.).
7 * Copyright (c) 1994, Regents of the University of California
11 * $Header: /cvsroot/pgsql/src/backend/access/common/printtup.c,v 1.18 1997/09/12 04:07:12 momjian Exp $
13 *-------------------------------------------------------------------------
20 #include <access/heapam.h>
21 #include <access/printtup.h>
22 #include <catalog/pg_type.h>
23 #include <libpq/libpq.h>
24 #include <utils/syscache.h>
26 /* ----------------------------------------------------------------
27 * printtup / debugtup support
28 * ----------------------------------------------------------------
32 * typtoout - used by printtup and debugtup
40 typeTuple = SearchSysCacheTuple(TYPOID,
41 ObjectIdGetDatum(type),
44 if (HeapTupleIsValid(typeTuple))
46 ((TypeTupleForm) GETSTRUCT(typeTuple))->typoutput);
48 elog(WARN, "typtoout: Cache lookup of type %d failed", type);
57 typeTuple = SearchSysCacheTuple(TYPOID,
58 ObjectIdGetDatum(type),
61 if (HeapTupleIsValid(typeTuple))
63 ((TypeTupleForm) GETSTRUCT(typeTuple))->typelem);
65 elog(WARN, "typtoout: Cache lookup of type %d failed", type);
74 printtup(HeapTuple tuple, TupleDesc typeinfo)
85 * tell the frontend to expect new tuple data
91 * send a bitmap of which attributes are null
96 for (i = 0; i < tuple->t_natts;)
98 i++; /* heap_getattr is a macro, so no
100 attr = heap_getattr(tuple, InvalidBuffer, i, typeinfo, &isnull);
115 * send the attributes of this tuple
118 for (i = 0; i < tuple->t_natts; ++i)
120 attr = heap_getattr(tuple, InvalidBuffer, i + 1, typeinfo, &isnull);
121 typoutput = typtoout((Oid) typeinfo->attrs[i]->atttypid);
123 if (!isnull && OidIsValid(typoutput))
125 outputstr = fmgr(typoutput, attr,
126 gettypelem(typeinfo->attrs[i]->atttypid));
127 pq_putint(strlen(outputstr) + 4, 4);
128 pq_putnchar(outputstr, strlen(outputstr));
139 printatt(unsigned attributeId,
140 AttributeTupleForm attributeP,
143 printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, byval = %c)\n",
145 attributeP->attname.data,
146 value != NULL ? " = \"" : "",
147 value != NULL ? value : "",
148 value != NULL ? "\"" : "",
149 (unsigned int) (attributeP->atttypid),
151 attributeP->attbyval ? 't' : 'f');
159 showatts(char *name, TupleDesc tupleDesc)
162 int natts = tupleDesc->natts;
163 AttributeTupleForm *attinfo = tupleDesc->attrs;
166 for (i = 0; i < natts; ++i)
167 printatt((unsigned) i + 1, attinfo[i], (char *) NULL);
176 debugtup(HeapTuple tuple, TupleDesc typeinfo)
184 for (i = 0; i < tuple->t_natts; ++i)
186 attr = heap_getattr(tuple, InvalidBuffer, i + 1, typeinfo, &isnull);
187 typoutput = typtoout((Oid) typeinfo->attrs[i]->atttypid);
189 if (!isnull && OidIsValid(typoutput))
191 value = fmgr(typoutput, attr,
192 gettypelem(typeinfo->attrs[i]->atttypid));
193 printatt((unsigned) i + 1, typeinfo->attrs[i], value);
202 * Protocol expects either T, D, C, E, or N.
203 * We use a different data prefix, e.g. 'B' instead of 'D' to
204 * indicate a tuple in internal (binary) form.
206 * This is same as printtup, except we don't use the typout func.
210 printtup_internal(HeapTuple tuple, TupleDesc typeinfo)
219 * tell the frontend to expect new tuple data
225 * send a bitmap of which attributes are null
230 for (i = 0; i < tuple->t_natts;)
232 i++; /* heap_getattr is a macro, so no
234 attr = heap_getattr(tuple, InvalidBuffer, i, typeinfo, &isnull);
249 * send the attributes of this tuple
253 fprintf(stderr, "sending tuple with %d atts\n", tuple->t_natts);
255 for (i = 0; i < tuple->t_natts; ++i)
257 int32 len = typeinfo->attrs[i]->attlen;
259 attr = heap_getattr(tuple, InvalidBuffer, i + 1, typeinfo, &isnull);
262 /* # of bytes, and opaque data */
265 /* variable length, assume a varlena structure */
266 len = VARSIZE(attr) - VARHDRSZ;
268 pq_putint(len, sizeof(int32));
269 pq_putnchar(VARDATA(attr), len);
272 char *d = VARDATA(attr);
274 fprintf(stderr, "length %d data %x%x%x%x\n",
275 len, *d, *(d + 1), *(d + 2), *(d + 3));
282 if (typeinfo->attrs[i]->attbyval)
288 pq_putint(len, sizeof(int32));
292 i8 = DatumGetChar(attr);
293 pq_putnchar((char *) &i8, len);
296 i16 = DatumGetInt16(attr);
297 pq_putnchar((char *) &i16, len);
300 i32 = DatumGetInt32(attr);
301 pq_putnchar((char *) &i32, len);
305 fprintf(stderr, "byval length %d data %d\n", len, attr);
310 pq_putint(len, sizeof(int32));
311 pq_putnchar(DatumGetPointer(attr), len);
313 fprintf(stderr, "byref length %d data %x\n", len,
314 DatumGetPointer(attr));