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.27 1998/02/26 04:29:20 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(ERROR, "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(ERROR, "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, i, typeinfo, &isnull);
115 * send the attributes of this tuple
118 for (i = 0; i < tuple->t_natts; ++i)
120 attr = heap_getattr(tuple, 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 typeinfo->attrs[i]->atttypmod);
128 pq_putint(strlen(outputstr) + VARHDRSZ, VARHDRSZ);
129 pq_putnchar(outputstr, strlen(outputstr));
140 printatt(unsigned attributeId,
141 AttributeTupleForm attributeP,
144 printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, byval = %c)\n",
146 attributeP->attname.data,
147 value != NULL ? " = \"" : "",
148 value != NULL ? value : "",
149 value != NULL ? "\"" : "",
150 (unsigned int) (attributeP->atttypid),
152 attributeP->attbyval ? 't' : 'f');
160 showatts(char *name, TupleDesc tupleDesc)
163 int natts = tupleDesc->natts;
164 AttributeTupleForm *attinfo = tupleDesc->attrs;
167 for (i = 0; i < natts; ++i)
168 printatt((unsigned) i + 1, attinfo[i], (char *) NULL);
177 debugtup(HeapTuple tuple, TupleDesc typeinfo)
185 for (i = 0; i < tuple->t_natts; ++i)
187 attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
188 typoutput = typtoout((Oid) typeinfo->attrs[i]->atttypid);
190 if (!isnull && OidIsValid(typoutput))
192 value = fmgr(typoutput, attr,
193 gettypelem(typeinfo->attrs[i]->atttypid),
194 typeinfo->attrs[i]->atttypmod);
195 printatt((unsigned) i + 1, typeinfo->attrs[i], value);
204 * Protocol expects either T, D, C, E, or N.
205 * We use a different data prefix, e.g. 'B' instead of 'D' to
206 * indicate a tuple in internal (binary) form.
208 * This is same as printtup, except we don't use the typout func.
212 printtup_internal(HeapTuple tuple, TupleDesc typeinfo)
221 * tell the frontend to expect new tuple data
227 * send a bitmap of which attributes are null
232 for (i = 0; i < tuple->t_natts;)
234 i++; /* heap_getattr is a macro, so no
236 attr = heap_getattr(tuple, i, typeinfo, &isnull);
251 * send the attributes of this tuple
255 fprintf(stderr, "sending tuple with %d atts\n", tuple->t_natts);
257 for (i = 0; i < tuple->t_natts; ++i)
259 int32 len = typeinfo->attrs[i]->attlen;
261 attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
264 /* # of bytes, and opaque data */
267 /* variable length, assume a varlena structure */
268 len = VARSIZE(attr) - VARHDRSZ;
270 pq_putint(len, VARHDRSZ);
271 pq_putnchar(VARDATA(attr), len);
274 char *d = VARDATA(attr);
276 fprintf(stderr, "length %d data %x%x%x%x\n",
277 len, *d, *(d + 1), *(d + 2), *(d + 3));
284 if (typeinfo->attrs[i]->attbyval)
290 pq_putint(len, sizeof(int32));
294 i8 = DatumGetChar(attr);
295 pq_putnchar((char *) &i8, len);
298 i16 = DatumGetInt16(attr);
299 pq_putnchar((char *) &i16, len);
302 i32 = DatumGetInt32(attr);
303 pq_putnchar((char *) &i32, len);
307 fprintf(stderr, "byval length %d data %d\n", len, attr);
312 pq_putint(len, sizeof(int32));
313 pq_putnchar(DatumGetPointer(attr), len);
315 fprintf(stderr, "byref length %d data %x\n", len,
316 DatumGetPointer(attr));