printtup.c 6.7 KB
Newer Older
1 2 3
/*-------------------------------------------------------------------------
 *
 * printtup.c--
4 5
 *	  Routines to print out tuples to the destination (binary or non-binary
 *	  portals, frontend/interactive backend, etc.).
6 7 8 9 10
 *
 * Copyright (c) 1994, Regents of the University of California
 *
 *
 * IDENTIFICATION
11
 *	  $Header: /cvsroot/pgsql/src/backend/access/common/printtup.c,v 1.26 1998/02/11 19:09:25 momjian Exp $
12 13 14 15
 *
 *-------------------------------------------------------------------------
 */

16
#include <string.h>
M
Marc G. Fournier 已提交
17
#include <postgres.h>
18

19 20 21
#include <fmgr.h>
#include <access/heapam.h>
#include <access/printtup.h>
M
Marc G. Fournier 已提交
22 23 24
#include <catalog/pg_type.h>
#include <libpq/libpq.h>
#include <utils/syscache.h>
M
Marc G. Fournier 已提交
25

26
/* ----------------------------------------------------------------
27
 *		printtup / debugtup support
28 29 30 31
 * ----------------------------------------------------------------
 */

/* ----------------
32
 *		typtoout - used by printtup and debugtup
33 34 35 36 37
 * ----------------
 */
Oid
typtoout(Oid type)
{
38
	HeapTuple	typeTuple;
39 40 41 42 43 44 45 46 47

	typeTuple = SearchSysCacheTuple(TYPOID,
									ObjectIdGetDatum(type),
									0, 0, 0);

	if (HeapTupleIsValid(typeTuple))
		return ((Oid)
				((TypeTupleForm) GETSTRUCT(typeTuple))->typoutput);

48
	elog(ERROR, "typtoout: Cache lookup of type %d failed", type);
49
	return (InvalidOid);
50 51 52 53 54
}

Oid
gettypelem(Oid type)
{
55
	HeapTuple	typeTuple;
56 57 58 59 60 61 62 63 64

	typeTuple = SearchSysCacheTuple(TYPOID,
									ObjectIdGetDatum(type),
									0, 0, 0);

	if (HeapTupleIsValid(typeTuple))
		return ((Oid)
				((TypeTupleForm) GETSTRUCT(typeTuple))->typelem);

65
	elog(ERROR, "typtoout: Cache lookup of type %d failed", type);
66
	return (InvalidOid);
67 68 69
}

/* ----------------
70
 *		printtup
71 72 73 74 75
 * ----------------
 */
void
printtup(HeapTuple tuple, TupleDesc typeinfo)
{
76 77 78
	int			i,
				j,
				k;
79 80
	char	   *outputstr;
	Datum		attr;
81 82
	bool		isnull;
	Oid			typoutput;
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99

	/* ----------------
	 *	tell the frontend to expect new tuple data
	 * ----------------
	 */
	pq_putnchar("D", 1);

	/* ----------------
	 *	send a bitmap of which attributes are null
	 * ----------------
	 */
	j = 0;
	k = 1 << 7;
	for (i = 0; i < tuple->t_natts;)
	{
		i++;					/* heap_getattr is a macro, so no
								 * increment */
100
		attr = heap_getattr(tuple, i, typeinfo, &isnull);
101 102 103 104 105 106 107 108 109
		if (!isnull)
			j |= k;
		k >>= 1;
		if (!(i & 7))
		{
			pq_putint(j, 1);
			j = 0;
			k = 1 << 7;
		}
110
	}
111 112 113 114 115 116 117 118 119
	if (i & 7)
		pq_putint(j, 1);

	/* ----------------
	 *	send the attributes of this tuple
	 * ----------------
	 */
	for (i = 0; i < tuple->t_natts; ++i)
	{
120
		attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
121 122 123 124 125
		typoutput = typtoout((Oid) typeinfo->attrs[i]->atttypid);

		if (!isnull && OidIsValid(typoutput))
		{
			outputstr = fmgr(typoutput, attr,
126
							 gettypelem(typeinfo->attrs[i]->atttypid),
B
Bruce Momjian 已提交
127
										typeinfo->attrs[i]->atttypmod);
128
			pq_putint(strlen(outputstr) + VARHDRSZ, VARHDRSZ);
129 130 131
			pq_putnchar(outputstr, strlen(outputstr));
			pfree(outputstr);
		}
132 133 134 135
	}
}

/* ----------------
136
 *		printatt
137 138 139 140
 * ----------------
 */
static void
printatt(unsigned attributeId,
141 142
		 AttributeTupleForm attributeP,
		 char *value)
143
{
144 145 146 147 148 149 150 151 152
	printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, byval = %c)\n",
		   attributeId,
		   attributeP->attname.data,
		   value != NULL ? " = \"" : "",
		   value != NULL ? value : "",
		   value != NULL ? "\"" : "",
		   (unsigned int) (attributeP->atttypid),
		   attributeP->attlen,
		   attributeP->attbyval ? 't' : 'f');
153 154 155
}

/* ----------------
156
 *		showatts
157 158 159 160 161
 * ----------------
 */
void
showatts(char *name, TupleDesc tupleDesc)
{
162 163
	int			i;
	int			natts = tupleDesc->natts;
164
	AttributeTupleForm *attinfo = tupleDesc->attrs;
165

166 167 168 169
	puts(name);
	for (i = 0; i < natts; ++i)
		printatt((unsigned) i + 1, attinfo[i], (char *) NULL);
	printf("\t----\n");
170 171 172
}

/* ----------------
173
 *		debugtup
174 175 176 177 178
 * ----------------
 */
void
debugtup(HeapTuple tuple, TupleDesc typeinfo)
{
179
	int i;
180 181
	Datum	    attr;
	char	   *value;
182 183
	bool		isnull;
	Oid			typoutput;
184 185 186

	for (i = 0; i < tuple->t_natts; ++i)
	{
187
		attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
188 189 190 191 192
		typoutput = typtoout((Oid) typeinfo->attrs[i]->atttypid);

		if (!isnull && OidIsValid(typoutput))
		{
			value = fmgr(typoutput, attr,
193
						 gettypelem(typeinfo->attrs[i]->atttypid),
B
Bruce Momjian 已提交
194
									typeinfo->attrs[i]->atttypmod);
195 196 197
			printatt((unsigned) i + 1, typeinfo->attrs[i], value);
			pfree(value);
		}
198
	}
199
	printf("\t----\n");
200 201 202
}

/* ----------------
203 204 205 206
 *		printtup_internal
 *		Protocol expects either T, D, C, E, or N.
 *		We use a different data prefix, e.g. 'B' instead of 'D' to
 *		indicate a tuple in internal (binary) form.
207
 *
208
 *		This is same as printtup, except we don't use the typout func.
209 210 211 212 213
 * ----------------
 */
void
printtup_internal(HeapTuple tuple, TupleDesc typeinfo)
{
214 215 216
	int			i,
				j,
				k;
217
	Datum		attr;
218
	bool		isnull;
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

	/* ----------------
	 *	tell the frontend to expect new tuple data
	 * ----------------
	 */
	pq_putnchar("B", 1);

	/* ----------------
	 *	send a bitmap of which attributes are null
	 * ----------------
	 */
	j = 0;
	k = 1 << 7;
	for (i = 0; i < tuple->t_natts;)
	{
		i++;					/* heap_getattr is a macro, so no
								 * increment */
236
		attr = heap_getattr(tuple, i, typeinfo, &isnull);
237 238 239 240 241 242 243 244 245
		if (!isnull)
			j |= k;
		k >>= 1;
		if (!(i & 7))
		{
			pq_putint(j, 1);
			j = 0;
			k = 1 << 7;
		}
246
	}
247 248 249 250 251 252 253
	if (i & 7)
		pq_putint(j, 1);

	/* ----------------
	 *	send the attributes of this tuple
	 * ----------------
	 */
254
#ifdef IPORTAL_DEBUG
255
	fprintf(stderr, "sending tuple with %d atts\n", tuple->t_natts);
256
#endif
257 258
	for (i = 0; i < tuple->t_natts; ++i)
	{
259
		int32		len = typeinfo->attrs[i]->attlen;
260

261
		attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
262
		if (!isnull)
263
		{
264 265 266 267 268 269
			/* # of bytes, and opaque data */
			if (len == -1)
			{
				/* variable length, assume a varlena structure */
				len = VARSIZE(attr) - VARHDRSZ;

B
Bruce Momjian 已提交
270
				pq_putint(len, VARHDRSZ);
271 272 273
				pq_putnchar(VARDATA(attr), len);
#ifdef IPORTAL_DEBUG
				{
274
					char	   *d = VARDATA(attr);
275 276 277 278

					fprintf(stderr, "length %d data %x%x%x%x\n",
							len, *d, *(d + 1), *(d + 2), *(d + 3));
				}
279
#endif
280 281 282 283 284 285
			}
			else
			{
				/* fixed size */
				if (typeinfo->attrs[i]->attbyval)
				{
286 287 288
					int8		i8;
					int16		i16;
					int32		i32;
289 290 291 292

					pq_putint(len, sizeof(int32));
					switch (len)
					{
293 294 295 296 297 298 299 300 301 302 303 304
						case sizeof(int8):
							i8 = DatumGetChar(attr);
							pq_putnchar((char *) &i8, len);
							break;
						case sizeof(int16):
							i16 = DatumGetInt16(attr);
							pq_putnchar((char *) &i16, len);
							break;
						case sizeof(int32):
							i32 = DatumGetInt32(attr);
							pq_putnchar((char *) &i32, len);
							break;
305
					}
306
#ifdef IPORTAL_DEBUG
307
					fprintf(stderr, "byval length %d data %d\n", len, attr);
308
#endif
309 310 311 312
				}
				else
				{
					pq_putint(len, sizeof(int32));
313
					pq_putnchar(DatumGetPointer(attr), len);
314
#ifdef IPORTAL_DEBUG
315 316
					fprintf(stderr, "byref length %d data %x\n", len,
												DatumGetPointer(attr));
317
#endif
318 319
				}
			}
320 321 322
		}
	}
}