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.10 1996/11/05 05:26:32 scrappy Exp $
13 *-------------------------------------------------------------------------
19 #include "access/heaptuple.h"
20 #include "catalog/pg_type.h"
21 #include "libpq/libpq.h"
22 #include "utils/syscache.h"
24 /* ----------------------------------------------------------------
25 * printtup / debugtup support
26 * ----------------------------------------------------------------
30 * typtoout - used by printtup and debugtup
38 typeTuple = SearchSysCacheTuple(TYPOID,
39 ObjectIdGetDatum(type),
42 if (HeapTupleIsValid(typeTuple))
44 ((TypeTupleForm) GETSTRUCT(typeTuple))->typoutput);
46 elog(WARN, "typtoout: Cache lookup of type %d failed", type);
55 typeTuple = SearchSysCacheTuple(TYPOID,
56 ObjectIdGetDatum(type),
59 if (HeapTupleIsValid(typeTuple))
61 ((TypeTupleForm) GETSTRUCT(typeTuple))->typelem);
63 elog(WARN, "typtoout: Cache lookup of type %d failed", type);
72 printtup(HeapTuple tuple, TupleDesc typeinfo)
75 char *outputstr, *attr;
80 * tell the frontend to expect new tuple data
86 * send a bitmap of which attributes are null
91 for (i = 0; i < tuple->t_natts; ) {
92 attr = heap_getattr(tuple, InvalidBuffer, ++i, typeinfo, &isnull);
106 * send the attributes of this tuple
109 for (i = 0; i < tuple->t_natts; ++i) {
110 attr = heap_getattr(tuple, InvalidBuffer, i+1, typeinfo, &isnull);
111 typoutput = typtoout((Oid) typeinfo->attrs[i]->atttypid);
113 if (!isnull && OidIsValid(typoutput)) {
114 outputstr = fmgr(typoutput, attr,
115 gettypelem(typeinfo->attrs[i]->atttypid));
116 pq_putint(strlen(outputstr)+4, 4);
117 pq_putnchar(outputstr, strlen(outputstr));
128 printatt(unsigned attributeId,
129 AttributeTupleForm attributeP,
132 printf("\t%2d: %.*s%s%s%s\t(typeid = %u, len = %d, byval = %c)\n",
134 NAMEDATALEN, /* attname is a char16 */
135 attributeP->attname.data,
136 value != NULL ? " = \"" : "",
137 value != NULL ? value : "",
138 value != NULL ? "\"" : "",
139 (unsigned int) (attributeP->atttypid),
141 attributeP->attbyval ? 't' : 'f');
149 showatts(char *name, TupleDesc tupleDesc)
152 int natts = tupleDesc->natts;
153 AttributeTupleForm *attinfo = tupleDesc->attrs;
156 for (i = 0; i < natts; ++i)
157 printatt((unsigned) i+1, attinfo[i], (char *) NULL);
166 debugtup(HeapTuple tuple, TupleDesc typeinfo)
173 for (i = 0; i < tuple->t_natts; ++i) {
174 attr = heap_getattr(tuple, InvalidBuffer, i+1, typeinfo, &isnull);
175 typoutput = typtoout((Oid) typeinfo->attrs[i]->atttypid);
177 if (!isnull && OidIsValid(typoutput)) {
178 value = fmgr(typoutput, attr,
179 gettypelem(typeinfo->attrs[i]->atttypid));
180 printatt((unsigned) i+1, typeinfo->attrs[i], value);
189 * Protocol expects either T, D, C, E, or N.
190 * We use a different data prefix, e.g. 'B' instead of 'D' to
191 * indicate a tuple in internal (binary) form.
193 * This is same as printtup, except we don't use the typout func.
197 printtup_internal(HeapTuple tuple, TupleDesc typeinfo)
204 * tell the frontend to expect new tuple data
210 * send a bitmap of which attributes are null
215 for (i = 0; i < tuple->t_natts; ) {
216 attr = heap_getattr(tuple, InvalidBuffer, ++i, typeinfo, &isnull);
230 * send the attributes of this tuple
234 fprintf(stderr, "sending tuple with %d atts\n", tuple->t_natts);
236 for (i = 0; i < tuple->t_natts; ++i) {
237 int32 len = typeinfo->attrs[i]->attlen;
239 attr = heap_getattr(tuple, InvalidBuffer, i+1, typeinfo, &isnull);
241 /* # of bytes, and opaque data */
243 /* variable length, assume a varlena structure */
244 len = VARSIZE(attr) - VARHDRSZ;
246 pq_putint(len, sizeof(int32));
247 pq_putnchar(VARDATA(attr), len);
250 char *d = VARDATA(attr);
252 fprintf(stderr, "length %d data %x%x%x%x\n",
253 len, *d, *(d+1), *(d+2), *(d+3));
258 if (typeinfo->attrs[i]->attbyval) {
263 pq_putint(len, sizeof(int32));
266 i8 = DatumGetChar(attr);
267 pq_putnchar((char *) &i8, len);
270 i16 = DatumGetInt16(attr);
271 pq_putnchar((char *) &i16, len);
274 i32 = DatumGetInt32(attr);
275 pq_putnchar((char *) &i32, len);
279 fprintf(stderr, "byval length %d data %d\n", len, attr);
282 pq_putint(len, sizeof(int32));
283 pq_putnchar(attr, len);
285 fprintf(stderr, "byref length %d data %x\n", len, attr);