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
|
1997-12-08 05:42:48 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/access/common/printtup.c,v 1.20 1997/12/08 04:42:43 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
|
|
|
|
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))
|
|
|
|
return ((Oid)
|
|
|
|
((TypeTupleForm) GETSTRUCT(typeTuple))->typoutput);
|
|
|
|
|
|
|
|
elog(WARN, "typtoout: Cache lookup of type %d failed", type);
|
|
|
|
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))
|
|
|
|
return ((Oid)
|
|
|
|
((TypeTupleForm) GETSTRUCT(typeTuple))->typelem);
|
|
|
|
|
|
|
|
elog(WARN, "typtoout: Cache lookup of type %d failed", type);
|
|
|
|
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;
|
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 */
|
|
|
|
attr = heap_getattr(tuple, InvalidBuffer, i, typeinfo, &isnull);
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
attr = heap_getattr(tuple, InvalidBuffer, i + 1, typeinfo, &isnull);
|
|
|
|
typoutput = typtoout((Oid) typeinfo->attrs[i]->atttypid);
|
|
|
|
|
|
|
|
if (!isnull && OidIsValid(typoutput))
|
|
|
|
{
|
|
|
|
outputstr = fmgr(typoutput, attr,
|
|
|
|
gettypelem(typeinfo->attrs[i]->atttypid));
|
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));
|
|
|
|
pfree(outputstr);
|
|
|
|
}
|
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,
|
1997-09-07 07:04:48 +02:00
|
|
|
AttributeTupleForm attributeP,
|
|
|
|
char *value)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-07 07:04:48 +02:00
|
|
|
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');
|
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;
|
1997-09-07 07:04:48 +02:00
|
|
|
AttributeTupleForm *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)
|
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
register int i;
|
1997-09-12 06:09:08 +02:00
|
|
|
Datum attr;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
attr = heap_getattr(tuple, InvalidBuffer, i + 1, typeinfo, &isnull);
|
|
|
|
typoutput = typtoout((Oid) typeinfo->attrs[i]->atttypid);
|
|
|
|
|
|
|
|
if (!isnull && OidIsValid(typoutput))
|
|
|
|
{
|
|
|
|
value = fmgr(typoutput, attr,
|
|
|
|
gettypelem(typeinfo->attrs[i]->atttypid));
|
|
|
|
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 */
|
|
|
|
attr = heap_getattr(tuple, InvalidBuffer, i, typeinfo, &isnull);
|
|
|
|
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
|
|
|
|
|
|
|
attr = heap_getattr(tuple, InvalidBuffer, i + 1, typeinfo, &isnull);
|
|
|
|
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;
|
|
|
|
|
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);
|
|
|
|
#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,
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|