1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* printtup.c--
|
1997-09-07 07:04:48 +02:00
|
|
|
* Routines to print out tuples to the destination (binary or non-binary
|
|
|
|
* portals, frontend/interactive backend, etc.).
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
1998-09-01 06:40:42 +02:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/access/common/printtup.c,v 1.35 1998/09/01 04:26:40 momjian Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
1997-01-10 21:19:49 +01:00
|
|
|
#include <string.h>
|
1996-11-05 08:42:46 +01:00
|
|
|
#include <postgres.h>
|
1996-11-01 10:41:41 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#include <fmgr.h>
|
|
|
|
#include <access/heapam.h>
|
|
|
|
#include <access/printtup.h>
|
1996-11-05 08:42:46 +01:00
|
|
|
#include <catalog/pg_type.h>
|
|
|
|
#include <libpq/libpq.h>
|
|
|
|
#include <utils/syscache.h>
|
1996-10-21 00:04:49 +02:00
|
|
|
|
1998-07-18 20:34:34 +02:00
|
|
|
#ifdef MULTIBYTE
|
1998-07-26 06:31:41 +02:00
|
|
|
#include <mb/pg_wchar.h>
|
1998-06-16 09:29:54 +02:00
|
|
|
#endif
|
|
|
|
|
1996-07-09 08:22:35 +02:00
|
|
|
/* ----------------------------------------------------------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* printtup / debugtup support
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* typtoout - used by printtup and debugtup
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
Oid
|
|
|
|
typtoout(Oid type)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
HeapTuple typeTuple;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
typeTuple = SearchSysCacheTuple(TYPOID,
|
|
|
|
ObjectIdGetDatum(type),
|
|
|
|
0, 0, 0);
|
|
|
|
|
|
|
|
if (HeapTupleIsValid(typeTuple))
|
1998-09-01 05:29:17 +02:00
|
|
|
return (Oid) ((Form_pg_type) GETSTRUCT(typeTuple))->typoutput;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "typtoout: Cache lookup of type %d failed", type);
|
1998-09-01 05:29:17 +02:00
|
|
|
return InvalidOid;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Oid
|
|
|
|
gettypelem(Oid type)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
HeapTuple typeTuple;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
typeTuple = SearchSysCacheTuple(TYPOID,
|
|
|
|
ObjectIdGetDatum(type),
|
|
|
|
0, 0, 0);
|
|
|
|
|
|
|
|
if (HeapTupleIsValid(typeTuple))
|
1998-09-01 05:29:17 +02:00
|
|
|
return (Oid) ((Form_pg_type) GETSTRUCT(typeTuple))->typelem;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-01-07 22:07:04 +01:00
|
|
|
elog(ERROR, "typtoout: Cache lookup of type %d failed", type);
|
1998-09-01 05:29:17 +02:00
|
|
|
return InvalidOid;
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* printtup
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
printtup(HeapTuple tuple, TupleDesc typeinfo)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i,
|
|
|
|
j,
|
|
|
|
k;
|
1997-09-12 06:09:08 +02:00
|
|
|
char *outputstr;
|
|
|
|
Datum attr;
|
1997-09-08 04:41:22 +02:00
|
|
|
bool isnull;
|
|
|
|
Oid typoutput;
|
1998-09-01 06:40:42 +02:00
|
|
|
|
1998-07-18 20:34:34 +02:00
|
|
|
#ifdef MULTIBYTE
|
1998-06-16 09:29:54 +02:00
|
|
|
unsigned char *p;
|
1998-09-01 06:40:42 +02:00
|
|
|
|
1998-06-16 09:29:54 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* 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 */
|
1998-01-31 05:39:26 +01:00
|
|
|
attr = heap_getattr(tuple, i, typeinfo, &isnull);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!isnull)
|
|
|
|
j |= k;
|
|
|
|
k >>= 1;
|
|
|
|
if (!(i & 7))
|
|
|
|
{
|
|
|
|
pq_putint(j, 1);
|
|
|
|
j = 0;
|
|
|
|
k = 1 << 7;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
if (i & 7)
|
|
|
|
pq_putint(j, 1);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* send the attributes of this tuple
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
for (i = 0; i < tuple->t_natts; ++i)
|
|
|
|
{
|
1998-01-31 05:39:26 +01:00
|
|
|
attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
|
1998-08-30 21:30:38 +02:00
|
|
|
if (isnull)
|
|
|
|
continue;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-08-30 21:30:38 +02:00
|
|
|
typoutput = typtoout((Oid) typeinfo->attrs[i]->atttypid);
|
|
|
|
if (OidIsValid(typoutput))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
|
|
|
outputstr = fmgr(typoutput, attr,
|
1998-02-10 05:02:59 +01:00
|
|
|
gettypelem(typeinfo->attrs[i]->atttypid),
|
1998-02-26 05:46:47 +01:00
|
|
|
typeinfo->attrs[i]->atttypmod);
|
1998-07-18 20:34:34 +02:00
|
|
|
#ifdef MULTIBYTE
|
1998-06-16 09:29:54 +02:00
|
|
|
p = pg_server_to_client(outputstr, strlen(outputstr));
|
|
|
|
pq_putint(strlen(p) + VARHDRSZ, VARHDRSZ);
|
|
|
|
pq_putnchar(p, strlen(p));
|
|
|
|
#else
|
1997-12-06 23:57:36 +01:00
|
|
|
pq_putint(strlen(outputstr) + VARHDRSZ, VARHDRSZ);
|
1997-09-07 07:04:48 +02:00
|
|
|
pq_putnchar(outputstr, strlen(outputstr));
|
1998-06-16 09:29:54 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
pfree(outputstr);
|
|
|
|
}
|
1998-08-30 21:30:38 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
outputstr = "<unprintable>";
|
|
|
|
pq_putint(strlen(outputstr) + VARHDRSZ, VARHDRSZ);
|
|
|
|
pq_putnchar(outputstr, strlen(outputstr));
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* printatt
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
printatt(unsigned attributeId,
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_attribute attributeP,
|
1997-09-07 07:04:48 +02:00
|
|
|
char *value)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1998-05-14 19:18:14 +02:00
|
|
|
printf("\t%2d: %s%s%s%s\t(typeid = %u, len = %d, typmod = %d, byval = %c)\n",
|
1997-09-07 07:04:48 +02:00
|
|
|
attributeId,
|
|
|
|
attributeP->attname.data,
|
|
|
|
value != NULL ? " = \"" : "",
|
|
|
|
value != NULL ? value : "",
|
|
|
|
value != NULL ? "\"" : "",
|
|
|
|
(unsigned int) (attributeP->atttypid),
|
|
|
|
attributeP->attlen,
|
1998-05-14 19:18:14 +02:00
|
|
|
attributeP->atttypmod,
|
1997-09-07 07:04:48 +02:00
|
|
|
attributeP->attbyval ? 't' : 'f');
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* showatts
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
showatts(char *name, TupleDesc tupleDesc)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i;
|
|
|
|
int natts = tupleDesc->natts;
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_attribute *attinfo = tupleDesc->attrs;
|
1996-07-09 08:22:35 +02:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
puts(name);
|
|
|
|
for (i = 0; i < natts; ++i)
|
|
|
|
printatt((unsigned) i + 1, attinfo[i], (char *) NULL);
|
|
|
|
printf("\t----\n");
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* debugtup
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
debugtup(HeapTuple tuple, TupleDesc typeinfo)
|
|
|
|
{
|
1998-02-26 05:46:47 +01:00
|
|
|
int i;
|
|
|
|
Datum attr;
|
1997-09-12 06:09:08 +02:00
|
|
|
char *value;
|
1997-09-08 04:41:22 +02:00
|
|
|
bool isnull;
|
|
|
|
Oid typoutput;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
for (i = 0; i < tuple->t_natts; ++i)
|
|
|
|
{
|
1998-01-31 05:39:26 +01:00
|
|
|
attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
|
1997-09-07 07:04:48 +02:00
|
|
|
typoutput = typtoout((Oid) typeinfo->attrs[i]->atttypid);
|
|
|
|
|
|
|
|
if (!isnull && OidIsValid(typoutput))
|
|
|
|
{
|
|
|
|
value = fmgr(typoutput, attr,
|
1998-02-10 05:02:59 +01:00
|
|
|
gettypelem(typeinfo->attrs[i]->atttypid),
|
1998-02-26 05:46:47 +01:00
|
|
|
typeinfo->attrs[i]->atttypmod);
|
1997-09-07 07:04:48 +02:00
|
|
|
printatt((unsigned) i + 1, typeinfo->attrs[i], value);
|
|
|
|
pfree(value);
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
printf("\t----\n");
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
1997-09-07 07:04:48 +02:00
|
|
|
* 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.
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
1997-09-07 07:04:48 +02:00
|
|
|
* This is same as printtup, except we don't use the typout func.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
printtup_internal(HeapTuple tuple, TupleDesc typeinfo)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int i,
|
|
|
|
j,
|
|
|
|
k;
|
1997-09-12 06:09:08 +02:00
|
|
|
Datum attr;
|
1997-09-08 04:41:22 +02:00
|
|
|
bool isnull;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* 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 */
|
1998-01-31 05:39:26 +01:00
|
|
|
attr = heap_getattr(tuple, i, typeinfo, &isnull);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!isnull)
|
|
|
|
j |= k;
|
|
|
|
k >>= 1;
|
|
|
|
if (!(i & 7))
|
|
|
|
{
|
|
|
|
pq_putint(j, 1);
|
|
|
|
j = 0;
|
|
|
|
k = 1 << 7;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1997-09-07 07:04:48 +02:00
|
|
|
if (i & 7)
|
|
|
|
pq_putint(j, 1);
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* send the attributes of this tuple
|
|
|
|
* ----------------
|
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef IPORTAL_DEBUG
|
1997-09-07 07:04:48 +02:00
|
|
|
fprintf(stderr, "sending tuple with %d atts\n", tuple->t_natts);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
for (i = 0; i < tuple->t_natts; ++i)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int32 len = typeinfo->attrs[i]->attlen;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1998-01-31 05:39:26 +01:00
|
|
|
attr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
|
1997-09-07 07:04:48 +02:00
|
|
|
if (!isnull)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
/* # of bytes, and opaque data */
|
|
|
|
if (len == -1)
|
|
|
|
{
|
|
|
|
/* variable length, assume a varlena structure */
|
|
|
|
len = VARSIZE(attr) - VARHDRSZ;
|
|
|
|
|
1998-07-18 20:34:34 +02:00
|
|
|
#ifdef MULTIBYTE
|
1998-06-16 09:29:54 +02:00
|
|
|
pq_putncharlen(VARDATA(attr), len);
|
|
|
|
#else
|
1997-12-08 05:42:48 +01:00
|
|
|
pq_putint(len, VARHDRSZ);
|
1997-09-07 07:04:48 +02:00
|
|
|
pq_putnchar(VARDATA(attr), len);
|
1998-06-16 09:29:54 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
#ifdef IPORTAL_DEBUG
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
char *d = VARDATA(attr);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
fprintf(stderr, "length %d data %x%x%x%x\n",
|
|
|
|
len, *d, *(d + 1), *(d + 2), *(d + 3));
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* fixed size */
|
|
|
|
if (typeinfo->attrs[i]->attbyval)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int8 i8;
|
|
|
|
int16 i16;
|
|
|
|
int32 i32;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
|
|
|
pq_putint(len, sizeof(int32));
|
|
|
|
switch (len)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
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;
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef IPORTAL_DEBUG
|
1997-09-07 07:04:48 +02:00
|
|
|
fprintf(stderr, "byval length %d data %d\n", len, attr);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pq_putint(len, sizeof(int32));
|
1997-09-12 06:09:08 +02:00
|
|
|
pq_putnchar(DatumGetPointer(attr), len);
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef IPORTAL_DEBUG
|
1997-09-12 06:09:08 +02:00
|
|
|
fprintf(stderr, "byref length %d data %x\n", len,
|
1998-02-26 05:46:47 +01:00
|
|
|
DatumGetPointer(attr));
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|