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.38 1999/01/24 05:40:47 tgl 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 37 38 39 40
 *		getTypeOutAndElem -- get both typoutput and typelem for a type
 *
 * We used to fetch these with two separate function calls,
 * typtoout() and gettypelem(), which each called SearchSysCacheTuple.
 * This way takes half the time.
41 42
 * ----------------
 */
43 44
int
getTypeOutAndElem(Oid type, Oid* typOutput, Oid* typElem)
45
{
46
	HeapTuple	typeTuple;
47 48 49 50 51 52

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

	if (HeapTupleIsValid(typeTuple))
53 54 55 56 57 58
	{
		Form_pg_type pt = (Form_pg_type) GETSTRUCT(typeTuple);
		*typOutput = (Oid) pt->typoutput;
		*typElem = (Oid) pt->typelem;
		return OidIsValid(*typOutput);
	}
59

60
	elog(ERROR, "getTypeOutAndElem: Cache lookup of type %d failed", type);
61

62 63 64
	*typOutput = InvalidOid;
	*typElem = InvalidOid;
	return 0;
65 66 67
}

/* ----------------
68
 *		printtup
69 70 71 72 73
 * ----------------
 */
void
printtup(HeapTuple tuple, TupleDesc typeinfo)
{
74 75
	int			i,
				j,
76 77
				k,
				outputlen;
78 79
	char	   *outputstr;
	Datum		attr;
80
	bool		isnull;
81 82
	Oid			typoutput,
				typelem;
83
#ifdef MULTIBYTE
84 85
	unsigned char *p;
#endif
86 87

	/* ----------------
88
	 *	tell the frontend to expect new tuple data (in ASCII style)
89 90 91 92 93 94 95 96 97 98
	 * ----------------
	 */
	pq_putnchar("D", 1);

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

	/* ----------------
	 *	send the attributes of this tuple
	 * ----------------
	 */
121
	for (i = 0; i < tuple->t_data->t_natts; ++i)
122
	{
123
		attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
M
 
Marc G. Fournier 已提交
124 125
		if (isnull)
			continue;
126 127
		if (getTypeOutAndElem((Oid) typeinfo->attrs[i]->atttypid,
							  &typoutput, &typelem))
128
		{
129
			outputstr = fmgr(typoutput, attr, typelem,
130
							 typeinfo->attrs[i]->atttypmod);
131
#ifdef MULTIBYTE
132
			p = pg_server_to_client(outputstr, strlen(outputstr));
133 134 135
			outputlen = strlen(p);
			pq_putint(outputlen + VARHDRSZ, VARHDRSZ);
			pq_putnchar(p, outputlen);
136
#else
137 138 139
			outputlen = strlen(outputstr);
			pq_putint(outputlen + VARHDRSZ, VARHDRSZ);
			pq_putnchar(outputstr, outputlen);
140
#endif
141 142
			pfree(outputstr);
		}
M
 
Marc G. Fournier 已提交
143 144 145
		else
		{
			outputstr = "<unprintable>";
146 147 148
			outputlen = strlen(outputstr);
			pq_putint(outputlen + VARHDRSZ, VARHDRSZ);
			pq_putnchar(outputstr, outputlen);
149
		}
150 151 152 153
	}
}

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

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

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

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

205
	for (i = 0; i < tuple->t_data->t_natts; ++i)
206
	{
207
		attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
208 209 210 211
		if (isnull)
			continue;
		if (getTypeOutAndElem((Oid) typeinfo->attrs[i]->atttypid,
							  &typoutput, &typelem))
212
		{
213
			value = fmgr(typoutput, attr, typelem,
214
						 typeinfo->attrs[i]->atttypmod);
215 216 217
			printatt((unsigned) i + 1, typeinfo->attrs[i], value);
			pfree(value);
		}
218
	}
219
	printf("\t----\n");
220 221 222
}

/* ----------------
223 224 225
 *		printtup_internal
 *		We use a different data prefix, e.g. 'B' instead of 'D' to
 *		indicate a tuple in internal (binary) form.
226
 *
227
 *		This is same as printtup, except we don't use the typout func.
228 229 230 231 232
 * ----------------
 */
void
printtup_internal(HeapTuple tuple, TupleDesc typeinfo)
{
233 234 235
	int			i,
				j,
				k;
236
	Datum		attr;
237
	bool		isnull;
238 239

	/* ----------------
240
	 *	tell the frontend to expect new tuple data (in binary style)
241 242 243 244 245 246 247 248 249 250
	 * ----------------
	 */
	pq_putnchar("B", 1);

	/* ----------------
	 *	send a bitmap of which attributes are null
	 * ----------------
	 */
	j = 0;
	k = 1 << 7;
251
	for (i = 0; i < tuple->t_data->t_natts;)
252 253 254
	{
		i++;					/* heap_getattr is a macro, so no
								 * increment */
255
		attr = heap_getattr(tuple, i, typeinfo, &isnull);
256 257 258 259 260 261 262 263 264
		if (!isnull)
			j |= k;
		k >>= 1;
		if (!(i & 7))
		{
			pq_putint(j, 1);
			j = 0;
			k = 1 << 7;
		}
265
	}
266 267 268 269 270 271 272
	if (i & 7)
		pq_putint(j, 1);

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

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

B
Bruce Momjian 已提交
289
				pq_putint(len, VARHDRSZ);
290
				pq_putnchar(VARDATA(attr), len);
291

292 293
#ifdef IPORTAL_DEBUG
				{
294
					char	   *d = VARDATA(attr);
295 296 297 298

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

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