printtup.c 7.3 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.36 1998/11/27 19:51:28 vadim 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
#ifdef MULTIBYTE
M
 
Marc G. Fournier 已提交
27
#include <mb/pg_wchar.h>
28 29
#endif

30
/* ----------------------------------------------------------------
31
 *		printtup / debugtup support
32 33 34 35
 * ----------------------------------------------------------------
 */

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

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

	if (HeapTupleIsValid(typeTuple))
49
		return (Oid) ((Form_pg_type) GETSTRUCT(typeTuple))->typoutput;
50

51
	elog(ERROR, "typtoout: Cache lookup of type %d failed", type);
52
	return InvalidOid;
53 54 55 56 57
}

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

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

	if (HeapTupleIsValid(typeTuple))
65
		return (Oid) ((Form_pg_type) GETSTRUCT(typeTuple))->typelem;
66

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

/* ----------------
72
 *		printtup
73 74 75 76 77
 * ----------------
 */
void
printtup(HeapTuple tuple, TupleDesc typeinfo)
{
78 79 80
	int			i,
				j,
				k;
81 82
	char	   *outputstr;
	Datum		attr;
83 84
	bool		isnull;
	Oid			typoutput;
85

86
#ifdef MULTIBYTE
87
	unsigned char *p;
88

89
#endif
90 91 92 93 94 95 96 97 98 99 100 101 102

	/* ----------------
	 *	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;
103
	for (i = 0; i < tuple->t_data->t_natts;)
104 105 106
	{
		i++;					/* heap_getattr is a macro, so no
								 * increment */
107
		attr = heap_getattr(tuple, i, typeinfo, &isnull);
108 109 110 111 112 113 114 115 116
		if (!isnull)
			j |= k;
		k >>= 1;
		if (!(i & 7))
		{
			pq_putint(j, 1);
			j = 0;
			k = 1 << 7;
		}
117
	}
118 119 120 121 122 123 124
	if (i & 7)
		pq_putint(j, 1);

	/* ----------------
	 *	send the attributes of this tuple
	 * ----------------
	 */
125
	for (i = 0; i < tuple->t_data->t_natts; ++i)
126
	{
127
		attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
M
 
Marc G. Fournier 已提交
128 129
		if (isnull)
			continue;
130

M
 
Marc G. Fournier 已提交
131 132
		typoutput = typtoout((Oid) typeinfo->attrs[i]->atttypid);
		if (OidIsValid(typoutput))
133 134
		{
			outputstr = fmgr(typoutput, attr,
135
							 gettypelem(typeinfo->attrs[i]->atttypid),
136
							 typeinfo->attrs[i]->atttypmod);
137
#ifdef MULTIBYTE
138 139 140 141
			p = pg_server_to_client(outputstr, strlen(outputstr));
			pq_putint(strlen(p) + VARHDRSZ, VARHDRSZ);
			pq_putnchar(p, strlen(p));
#else
142
			pq_putint(strlen(outputstr) + VARHDRSZ, VARHDRSZ);
143
			pq_putnchar(outputstr, strlen(outputstr));
144
#endif
145 146
			pfree(outputstr);
		}
M
 
Marc G. Fournier 已提交
147 148 149 150 151
		else
		{
			outputstr = "<unprintable>";
			pq_putint(strlen(outputstr) + VARHDRSZ, VARHDRSZ);
			pq_putnchar(outputstr, strlen(outputstr));
152
		}
153 154 155 156
	}
}

/* ----------------
157
 *		printatt
158 159 160 161
 * ----------------
 */
static void
printatt(unsigned attributeId,
162
		 Form_pg_attribute attributeP,
163
		 char *value)
164
{
B
Bruce Momjian 已提交
165
	printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, typmod = %d, byval = %c)\n",
166 167 168 169 170 171 172
		   attributeId,
		   attributeP->attname.data,
		   value != NULL ? " = \"" : "",
		   value != NULL ? value : "",
		   value != NULL ? "\"" : "",
		   (unsigned int) (attributeP->atttypid),
		   attributeP->attlen,
B
Bruce Momjian 已提交
173
		   attributeP->atttypmod,
174
		   attributeP->attbyval ? 't' : 'f');
175 176 177
}

/* ----------------
178
 *		showatts
179 180 181 182 183
 * ----------------
 */
void
showatts(char *name, TupleDesc tupleDesc)
{
184 185
	int			i;
	int			natts = tupleDesc->natts;
186
	Form_pg_attribute *attinfo = tupleDesc->attrs;
187

188 189 190 191
	puts(name);
	for (i = 0; i < natts; ++i)
		printatt((unsigned) i + 1, attinfo[i], (char *) NULL);
	printf("\t----\n");
192 193 194
}

/* ----------------
195
 *		debugtup
196 197 198 199 200
 * ----------------
 */
void
debugtup(HeapTuple tuple, TupleDesc typeinfo)
{
201 202
	int			i;
	Datum		attr;
203
	char	   *value;
204 205
	bool		isnull;
	Oid			typoutput;
206

207
	for (i = 0; i < tuple->t_data->t_natts; ++i)
208
	{
209
		attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
210 211 212 213 214
		typoutput = typtoout((Oid) typeinfo->attrs[i]->atttypid);

		if (!isnull && OidIsValid(typoutput))
		{
			value = fmgr(typoutput, attr,
215
						 gettypelem(typeinfo->attrs[i]->atttypid),
216
						 typeinfo->attrs[i]->atttypmod);
217 218 219
			printatt((unsigned) i + 1, typeinfo->attrs[i], value);
			pfree(value);
		}
220
	}
221
	printf("\t----\n");
222 223 224
}

/* ----------------
225 226 227 228
 *		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.
229
 *
230
 *		This is same as printtup, except we don't use the typout func.
231 232 233 234 235
 * ----------------
 */
void
printtup_internal(HeapTuple tuple, TupleDesc typeinfo)
{
236 237 238
	int			i,
				j,
				k;
239
	Datum		attr;
240
	bool		isnull;
241 242 243 244 245 246 247 248 249 250 251 252 253

	/* ----------------
	 *	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;
254
	for (i = 0; i < tuple->t_data->t_natts;)
255 256 257
	{
		i++;					/* heap_getattr is a macro, so no
								 * increment */
258
		attr = heap_getattr(tuple, i, typeinfo, &isnull);
259 260 261 262 263 264 265 266 267
		if (!isnull)
			j |= k;
		k >>= 1;
		if (!(i & 7))
		{
			pq_putint(j, 1);
			j = 0;
			k = 1 << 7;
		}
268
	}
269 270 271 272 273 274 275
	if (i & 7)
		pq_putint(j, 1);

	/* ----------------
	 *	send the attributes of this tuple
	 * ----------------
	 */
276
#ifdef IPORTAL_DEBUG
277
	fprintf(stderr, "sending tuple with %d atts\n", tuple->t_data->t_natts);
278
#endif
279
	for (i = 0; i < tuple->t_data->t_natts; ++i)
280
	{
281
		int32		len = typeinfo->attrs[i]->attlen;
282

283
		attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
284
		if (!isnull)
285
		{
286 287 288 289 290 291
			/* # of bytes, and opaque data */
			if (len == -1)
			{
				/* variable length, assume a varlena structure */
				len = VARSIZE(attr) - VARHDRSZ;

292
#ifdef MULTIBYTE
293 294
				pq_putncharlen(VARDATA(attr), len);
#else
B
Bruce Momjian 已提交
295
				pq_putint(len, VARHDRSZ);
296
				pq_putnchar(VARDATA(attr), len);
297
#endif
298 299
#ifdef IPORTAL_DEBUG
				{
300
					char	   *d = VARDATA(attr);
301 302 303 304

					fprintf(stderr, "length %d data %x%x%x%x\n",
							len, *d, *(d + 1), *(d + 2), *(d + 3));
				}
305
#endif
306 307 308 309 310 311
			}
			else
			{
				/* fixed size */
				if (typeinfo->attrs[i]->attbyval)
				{
312 313 314
					int8		i8;
					int16		i16;
					int32		i32;
315 316 317 318

					pq_putint(len, sizeof(int32));
					switch (len)
					{
319 320 321 322 323 324 325 326 327 328 329 330
						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;
331
					}
332
#ifdef IPORTAL_DEBUG
333
					fprintf(stderr, "byval length %d data %d\n", len, attr);
334
#endif
335 336 337 338
				}
				else
				{
					pq_putint(len, sizeof(int32));
339
					pq_putnchar(DatumGetPointer(attr), len);
340
#ifdef IPORTAL_DEBUG
341
					fprintf(stderr, "byref length %d data %x\n", len,
342
							DatumGetPointer(attr));
343
#endif
344 345
				}
			}
346 347 348
		}
	}
}