1996-07-09 08:22:35 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
1999-02-14 00:22:53 +01:00
|
|
|
* printtup.c
|
2002-02-27 20:36:13 +01:00
|
|
|
* Routines to print out tuples to the destination (both frontend
|
|
|
|
* clients and interactive backends are supported here).
|
|
|
|
*
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
2001-01-24 20:43:33 +01:00
|
|
|
* Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2002-02-27 20:36:13 +01:00
|
|
|
* $Header: /cvsroot/pgsql/src/backend/access/common/printtup.c,v 1.61 2002/02/27 19:34:09 tgl Exp $
|
1996-07-09 08:22:35 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
1999-04-25 05:19:27 +02:00
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "access/printtup.h"
|
|
|
|
#include "catalog/pg_type.h"
|
2002-02-27 20:36:13 +01:00
|
|
|
#include "libpq/libpq.h"
|
1999-04-25 05:19:27 +02:00
|
|
|
#include "libpq/pqformat.h"
|
|
|
|
#include "utils/syscache.h"
|
1996-10-21 00:04:49 +02:00
|
|
|
|
2002-02-27 20:36:13 +01:00
|
|
|
static void printtup_setup(DestReceiver *self, int operation,
|
|
|
|
const char *portalName, TupleDesc typeinfo);
|
1999-05-26 00:43:53 +02:00
|
|
|
static void printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self);
|
2000-12-01 23:10:31 +01:00
|
|
|
static void printtup_internal(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self);
|
1999-05-26 00:43:53 +02:00
|
|
|
static void printtup_cleanup(DestReceiver *self);
|
1999-01-27 01:36:28 +01: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
|
|
|
* ----------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* ----------------
|
2000-12-01 23:10:31 +01:00
|
|
|
* getTypeOutputInfo -- get info needed for printing values of a type
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
2000-12-01 23:10:31 +01:00
|
|
|
bool
|
|
|
|
getTypeOutputInfo(Oid type, Oid *typOutput, Oid *typElem,
|
|
|
|
bool *typIsVarlena)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
HeapTuple typeTuple;
|
2000-11-16 23:30:52 +01:00
|
|
|
Form_pg_type pt;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
typeTuple = SearchSysCache(TYPEOID,
|
|
|
|
ObjectIdGetDatum(type),
|
|
|
|
0, 0, 0);
|
|
|
|
if (!HeapTupleIsValid(typeTuple))
|
2000-12-01 23:10:31 +01:00
|
|
|
elog(ERROR, "getTypeOutputInfo: Cache lookup of type %u failed", type);
|
2000-11-16 23:30:52 +01:00
|
|
|
pt = (Form_pg_type) GETSTRUCT(typeTuple);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-11-16 23:30:52 +01:00
|
|
|
*typOutput = pt->typoutput;
|
|
|
|
*typElem = pt->typelem;
|
2001-03-22 05:01:46 +01:00
|
|
|
*typIsVarlena = (!pt->typbyval) && (pt->typlen == -1);
|
2000-11-16 23:30:52 +01:00
|
|
|
ReleaseSysCache(typeTuple);
|
|
|
|
return OidIsValid(*typOutput);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
/* ----------------
|
|
|
|
* Private state for a printtup destination object
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
typedef struct
|
|
|
|
{ /* Per-attribute information */
|
1999-01-27 01:36:28 +01:00
|
|
|
Oid typoutput; /* Oid for the attribute's type output fn */
|
|
|
|
Oid typelem; /* typelem value to pass to the output fn */
|
2000-12-01 23:10:31 +01:00
|
|
|
bool typisvarlena; /* is it varlena (ie possibly toastable)? */
|
1999-01-27 02:11:43 +01:00
|
|
|
FmgrInfo finfo; /* Precomputed call info for typoutput */
|
1999-05-26 00:43:53 +02:00
|
|
|
} PrinttupAttrInfo;
|
1999-01-27 01:36:28 +01:00
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
DestReceiver pub; /* publicly-known function pointers */
|
|
|
|
TupleDesc attrinfo; /* The attr info we are set up for */
|
|
|
|
int nattrs;
|
|
|
|
PrinttupAttrInfo *myinfo; /* Cached info about each attr */
|
1999-05-26 00:43:53 +02:00
|
|
|
} DR_printtup;
|
1999-01-27 01:36:28 +01:00
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* Initialize: create a DestReceiver for printtup
|
|
|
|
* ----------------
|
|
|
|
*/
|
1999-05-25 18:15:34 +02:00
|
|
|
DestReceiver *
|
2000-12-01 23:10:31 +01:00
|
|
|
printtup_create_DR(bool isBinary)
|
1999-01-27 01:36:28 +01:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
DR_printtup *self = (DR_printtup *) palloc(sizeof(DR_printtup));
|
1999-01-27 01:36:28 +01:00
|
|
|
|
2000-12-01 23:10:31 +01:00
|
|
|
self->pub.receiveTuple = isBinary ? printtup_internal : printtup;
|
1999-01-27 01:36:28 +01:00
|
|
|
self->pub.setup = printtup_setup;
|
|
|
|
self->pub.cleanup = printtup_cleanup;
|
|
|
|
|
|
|
|
self->attrinfo = NULL;
|
|
|
|
self->nattrs = 0;
|
|
|
|
self->myinfo = NULL;
|
|
|
|
|
1999-05-25 18:15:34 +02:00
|
|
|
return (DestReceiver *) self;
|
1999-01-27 01:36:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2002-02-27 20:36:13 +01:00
|
|
|
printtup_setup(DestReceiver *self, int operation,
|
|
|
|
const char *portalName, TupleDesc typeinfo)
|
1999-01-27 01:36:28 +01:00
|
|
|
{
|
2002-02-27 20:36:13 +01:00
|
|
|
/*
|
|
|
|
* Send portal name to frontend.
|
|
|
|
*
|
|
|
|
* If portal name not specified, use "blank" portal.
|
|
|
|
*/
|
|
|
|
if (portalName == NULL)
|
|
|
|
portalName = "blank";
|
|
|
|
|
|
|
|
pq_puttextmessage('P', portalName);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* if this is a retrieve, then we send back the tuple
|
|
|
|
* descriptor of the tuples.
|
|
|
|
*/
|
|
|
|
if (operation == CMD_SELECT)
|
|
|
|
{
|
|
|
|
Form_pg_attribute *attrs = typeinfo->attrs;
|
|
|
|
int natts = typeinfo->natts;
|
|
|
|
int i;
|
|
|
|
StringInfoData buf;
|
|
|
|
|
|
|
|
pq_beginmessage(&buf);
|
|
|
|
pq_sendbyte(&buf, 'T'); /* tuple descriptor message type */
|
|
|
|
pq_sendint(&buf, natts, 2); /* # of attrs in tuples */
|
|
|
|
|
|
|
|
for (i = 0; i < natts; ++i)
|
|
|
|
{
|
|
|
|
pq_sendstring(&buf, NameStr(attrs[i]->attname));
|
|
|
|
pq_sendint(&buf, (int) attrs[i]->atttypid,
|
|
|
|
sizeof(attrs[i]->atttypid));
|
|
|
|
pq_sendint(&buf, attrs[i]->attlen,
|
|
|
|
sizeof(attrs[i]->attlen));
|
|
|
|
if (PG_PROTOCOL_MAJOR(FrontendProtocol) >= 2)
|
|
|
|
pq_sendint(&buf, attrs[i]->atttypmod,
|
|
|
|
sizeof(attrs[i]->atttypmod));
|
|
|
|
}
|
|
|
|
pq_endmessage(&buf);
|
|
|
|
}
|
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
/* ----------------
|
|
|
|
* We could set up the derived attr info at this time, but we postpone it
|
2002-02-27 20:36:13 +01:00
|
|
|
* until the first call of printtup, for 2 reasons:
|
1999-01-27 01:36:28 +01:00
|
|
|
* 1. We don't waste time (compared to the old way) if there are no
|
1999-05-25 18:15:34 +02:00
|
|
|
* tuples at all to output.
|
1999-01-27 01:36:28 +01:00
|
|
|
* 2. Checking in printtup allows us to handle the case that the tuples
|
1999-05-25 18:15:34 +02:00
|
|
|
* change type midway through (although this probably can't happen in
|
|
|
|
* the current executor).
|
1999-01-27 01:36:28 +01:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
1999-05-26 00:43:53 +02:00
|
|
|
printtup_prepare_info(DR_printtup *myState, TupleDesc typeinfo, int numAttrs)
|
1999-01-27 01:36:28 +01:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
int i;
|
1999-01-27 01:36:28 +01:00
|
|
|
|
|
|
|
if (myState->myinfo)
|
1999-05-25 18:15:34 +02:00
|
|
|
pfree(myState->myinfo); /* get rid of any old data */
|
1999-01-27 01:36:28 +01:00
|
|
|
myState->myinfo = NULL;
|
|
|
|
myState->attrinfo = typeinfo;
|
|
|
|
myState->nattrs = numAttrs;
|
|
|
|
if (numAttrs <= 0)
|
|
|
|
return;
|
1999-05-25 18:15:34 +02:00
|
|
|
myState->myinfo = (PrinttupAttrInfo *)
|
1999-01-27 01:36:28 +01:00
|
|
|
palloc(numAttrs * sizeof(PrinttupAttrInfo));
|
|
|
|
for (i = 0; i < numAttrs; i++)
|
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
PrinttupAttrInfo *thisState = myState->myinfo + i;
|
|
|
|
|
2000-12-01 23:10:31 +01:00
|
|
|
if (getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
|
|
|
|
&thisState->typoutput, &thisState->typelem,
|
|
|
|
&thisState->typisvarlena))
|
1999-01-27 02:11:43 +01:00
|
|
|
fmgr_info(thisState->typoutput, &thisState->finfo);
|
1999-01-27 01:36:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
* ----------------
|
|
|
|
*/
|
1999-01-27 01:36:28 +01:00
|
|
|
static void
|
1999-05-26 00:43:53 +02:00
|
|
|
printtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
DR_printtup *myState = (DR_printtup *) self;
|
1999-04-25 05:19:27 +02:00
|
|
|
StringInfoData buf;
|
2000-12-01 23:10:31 +01:00
|
|
|
int natts = tuple->t_data->t_natts;
|
1997-09-08 04:41:22 +02:00
|
|
|
int i,
|
|
|
|
j,
|
1999-04-25 21:27:47 +02:00
|
|
|
k;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
/* Set or update my derived attribute info, if needed */
|
2000-12-01 23:10:31 +01:00
|
|
|
if (myState->attrinfo != typeinfo || myState->nattrs != natts)
|
|
|
|
printtup_prepare_info(myState, typeinfo, natts);
|
1999-01-27 01:36:28 +01:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* tell the frontend to expect new tuple data (in ASCII style)
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1999-04-25 05:19:27 +02:00
|
|
|
pq_beginmessage(&buf);
|
|
|
|
pq_sendbyte(&buf, 'D');
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* send a bitmap of which attributes are not null
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
j = 0;
|
|
|
|
k = 1 << 7;
|
2000-12-01 23:10:31 +01:00
|
|
|
for (i = 0; i < natts; ++i)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
if (!heap_attisnull(tuple, i + 1))
|
1999-01-24 23:50:58 +01:00
|
|
|
j |= k; /* set bit if not null */
|
1997-09-07 07:04:48 +02:00
|
|
|
k >>= 1;
|
1999-01-24 23:50:58 +01:00
|
|
|
if (k == 0) /* end of byte? */
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-04-25 05:19:27 +02:00
|
|
|
pq_sendint(&buf, j, 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
j = 0;
|
|
|
|
k = 1 << 7;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1999-01-24 23:50:58 +01:00
|
|
|
if (k != (1 << 7)) /* flush last partial byte */
|
1999-04-25 05:19:27 +02:00
|
|
|
pq_sendint(&buf, j, 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* send the attributes of this tuple
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
2000-12-01 23:10:31 +01:00
|
|
|
for (i = 0; i < natts; ++i)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
PrinttupAttrInfo *thisState = myState->myinfo + i;
|
2000-12-01 23:10:31 +01:00
|
|
|
Datum origattr,
|
|
|
|
attr;
|
|
|
|
bool isnull;
|
|
|
|
char *outputstr;
|
1999-05-25 18:15:34 +02:00
|
|
|
|
2000-12-01 23:10:31 +01:00
|
|
|
origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
|
1998-08-30 21:30:38 +02:00
|
|
|
if (isnull)
|
|
|
|
continue;
|
1999-01-27 02:11:43 +01:00
|
|
|
if (OidIsValid(thisState->typoutput))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-12-01 23:10:31 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* If we have a toasted datum, forcibly detoast it here to
|
|
|
|
* avoid memory leakage inside the type's output routine.
|
2000-12-01 23:10:31 +01:00
|
|
|
*/
|
|
|
|
if (thisState->typisvarlena)
|
|
|
|
attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
|
|
|
|
else
|
|
|
|
attr = origattr;
|
|
|
|
|
2000-05-30 06:25:00 +02:00
|
|
|
outputstr = DatumGetCString(FunctionCall3(&thisState->finfo,
|
2001-03-22 05:01:46 +01:00
|
|
|
attr,
|
|
|
|
ObjectIdGetDatum(thisState->typelem),
|
|
|
|
Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
|
2000-12-01 23:10:31 +01:00
|
|
|
|
1999-04-25 21:27:47 +02:00
|
|
|
pq_sendcountedtext(&buf, outputstr, strlen(outputstr));
|
2000-12-01 23:10:31 +01:00
|
|
|
|
|
|
|
/* Clean up detoasted copy, if any */
|
|
|
|
if (attr != origattr)
|
|
|
|
pfree(DatumGetPointer(attr));
|
1997-09-07 07:04:48 +02:00
|
|
|
pfree(outputstr);
|
|
|
|
}
|
1998-08-30 21:30:38 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
outputstr = "<unprintable>";
|
1999-04-25 21:27:47 +02:00
|
|
|
pq_sendcountedtext(&buf, outputstr, strlen(outputstr));
|
1998-09-01 06:40:42 +02:00
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1999-04-25 05:19:27 +02:00
|
|
|
|
|
|
|
pq_endmessage(&buf);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
/* ----------------
|
|
|
|
* printtup_cleanup
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
static void
|
1999-05-26 00:43:53 +02:00
|
|
|
printtup_cleanup(DestReceiver *self)
|
1999-01-27 01:36:28 +01:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
DR_printtup *myState = (DR_printtup *) self;
|
|
|
|
|
1999-01-27 01:36:28 +01:00
|
|
|
if (myState->myinfo)
|
|
|
|
pfree(myState->myinfo);
|
|
|
|
pfree(myState);
|
|
|
|
}
|
|
|
|
|
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,
|
1999-11-08 00:08:36 +01:00
|
|
|
NameStr(attributeP->attname),
|
1997-09-07 07:04:48 +02:00
|
|
|
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
|
|
|
* ----------------
|
|
|
|
*/
|
2002-02-27 20:36:13 +01:00
|
|
|
static void
|
|
|
|
showatts(const char *name, TupleDesc tupleDesc)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
1997-09-08 04:41:22 +02:00
|
|
|
int natts = tupleDesc->natts;
|
1998-09-01 05:29:17 +02:00
|
|
|
Form_pg_attribute *attinfo = tupleDesc->attrs;
|
2002-02-27 20:36:13 +01:00
|
|
|
int i;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
2002-02-27 20:36:13 +01:00
|
|
|
* debugSetup - prepare to print tuples for an interactive backend
|
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
debugSetup(DestReceiver *self, int operation,
|
|
|
|
const char *portalName, TupleDesc typeinfo)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* show the return type of the tuples
|
|
|
|
*/
|
|
|
|
if (portalName == NULL)
|
|
|
|
portalName = "blank";
|
|
|
|
|
|
|
|
showatts(portalName, typeinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ----------------
|
|
|
|
* debugtup - print one tuple for an interactive backend
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
|
|
|
void
|
1999-05-26 00:43:53 +02:00
|
|
|
debugtup(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-12-01 23:10:31 +01:00
|
|
|
int natts = tuple->t_data->t_natts;
|
1998-02-26 05:46:47 +01:00
|
|
|
int i;
|
2000-12-01 23:10:31 +01:00
|
|
|
Datum origattr,
|
|
|
|
attr;
|
1997-09-12 06:09:08 +02:00
|
|
|
char *value;
|
1997-09-08 04:41:22 +02:00
|
|
|
bool isnull;
|
1999-01-24 06:40:49 +01:00
|
|
|
Oid typoutput,
|
|
|
|
typelem;
|
2000-12-01 23:10:31 +01:00
|
|
|
bool typisvarlena;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-12-01 23:10:31 +01:00
|
|
|
for (i = 0; i < natts; ++i)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-12-01 23:10:31 +01:00
|
|
|
origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
|
1999-01-24 06:40:49 +01:00
|
|
|
if (isnull)
|
|
|
|
continue;
|
2000-12-01 23:10:31 +01:00
|
|
|
if (getTypeOutputInfo(typeinfo->attrs[i]->atttypid,
|
|
|
|
&typoutput, &typelem, &typisvarlena))
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-12-01 23:10:31 +01:00
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* If we have a toasted datum, forcibly detoast it here to
|
|
|
|
* avoid memory leakage inside the type's output routine.
|
2000-12-01 23:10:31 +01:00
|
|
|
*/
|
|
|
|
if (typisvarlena)
|
|
|
|
attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
|
|
|
|
else
|
|
|
|
attr = origattr;
|
|
|
|
|
2000-05-30 06:25:00 +02:00
|
|
|
value = DatumGetCString(OidFunctionCall3(typoutput,
|
2001-03-22 05:01:46 +01:00
|
|
|
attr,
|
|
|
|
ObjectIdGetDatum(typelem),
|
|
|
|
Int32GetDatum(typeinfo->attrs[i]->atttypmod)));
|
2000-12-01 23:10:31 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
printatt((unsigned) i + 1, typeinfo->attrs[i], value);
|
2000-12-01 23:10:31 +01:00
|
|
|
|
|
|
|
/* Clean up detoasted copy, if any */
|
|
|
|
if (attr != origattr)
|
|
|
|
pfree(DatumGetPointer(attr));
|
1997-09-07 07:04:48 +02:00
|
|
|
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
|
|
|
|
* 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
|
|
|
*
|
2000-12-01 23:10:31 +01:00
|
|
|
* This is largely same as printtup, except we don't use the typout func.
|
1996-07-09 08:22:35 +02:00
|
|
|
* ----------------
|
|
|
|
*/
|
2000-12-01 23:10:31 +01:00
|
|
|
static void
|
1999-05-26 00:43:53 +02:00
|
|
|
printtup_internal(HeapTuple tuple, TupleDesc typeinfo, DestReceiver *self)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-12-01 23:10:31 +01:00
|
|
|
DR_printtup *myState = (DR_printtup *) self;
|
1999-04-25 05:19:27 +02:00
|
|
|
StringInfoData buf;
|
2000-12-01 23:10:31 +01:00
|
|
|
int natts = tuple->t_data->t_natts;
|
1997-09-08 04:41:22 +02:00
|
|
|
int i,
|
|
|
|
j,
|
|
|
|
k;
|
2000-12-01 23:10:31 +01:00
|
|
|
|
|
|
|
/* Set or update my derived attribute info, if needed */
|
|
|
|
if (myState->attrinfo != typeinfo || myState->nattrs != natts)
|
|
|
|
printtup_prepare_info(myState, typeinfo, natts);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* tell the frontend to expect new tuple data (in binary style)
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1999-04-25 05:19:27 +02:00
|
|
|
pq_beginmessage(&buf);
|
|
|
|
pq_sendbyte(&buf, 'B');
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* send a bitmap of which attributes are not null
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
|
|
|
j = 0;
|
|
|
|
k = 1 << 7;
|
2000-12-01 23:10:31 +01:00
|
|
|
for (i = 0; i < natts; ++i)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-05-25 18:15:34 +02:00
|
|
|
if (!heap_attisnull(tuple, i + 1))
|
1999-01-24 23:50:58 +01:00
|
|
|
j |= k; /* set bit if not null */
|
1997-09-07 07:04:48 +02:00
|
|
|
k >>= 1;
|
1999-01-24 23:50:58 +01:00
|
|
|
if (k == 0) /* end of byte? */
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
1999-04-25 05:19:27 +02:00
|
|
|
pq_sendint(&buf, j, 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
j = 0;
|
|
|
|
k = 1 << 7;
|
|
|
|
}
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|
1999-01-24 23:50:58 +01:00
|
|
|
if (k != (1 << 7)) /* flush last partial byte */
|
1999-04-25 05:19:27 +02:00
|
|
|
pq_sendint(&buf, j, 1);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2001-03-22 07:16:21 +01:00
|
|
|
/*
|
|
|
|
* send the attributes of this tuple
|
1997-09-07 07:04:48 +02:00
|
|
|
*/
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef IPORTAL_DEBUG
|
2000-12-01 23:10:31 +01:00
|
|
|
fprintf(stderr, "sending tuple with %d atts\n", natts);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
2000-12-01 23:10:31 +01:00
|
|
|
|
|
|
|
for (i = 0; i < natts; ++i)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-12-01 23:10:31 +01:00
|
|
|
PrinttupAttrInfo *thisState = myState->myinfo + i;
|
|
|
|
Datum origattr,
|
|
|
|
attr;
|
|
|
|
bool isnull;
|
|
|
|
int32 len;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-12-01 23:10:31 +01:00
|
|
|
origattr = heap_getattr(tuple, i + 1, typeinfo, &isnull);
|
|
|
|
if (isnull)
|
|
|
|
continue;
|
|
|
|
/* send # of bytes, and opaque data */
|
|
|
|
if (thisState->typisvarlena)
|
1996-07-09 08:22:35 +02:00
|
|
|
{
|
2000-12-01 23:10:31 +01:00
|
|
|
/*
|
|
|
|
* If we have a toasted datum, must detoast before sending.
|
|
|
|
*/
|
|
|
|
attr = PointerGetDatum(PG_DETOAST_DATUM(origattr));
|
|
|
|
|
|
|
|
len = VARSIZE(attr) - VARHDRSZ;
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-12-01 23:10:31 +01:00
|
|
|
pq_sendint(&buf, len, VARHDRSZ);
|
|
|
|
pq_sendbytes(&buf, VARDATA(attr), len);
|
1998-12-12 23:04:09 +01:00
|
|
|
|
1997-09-07 07:04:48 +02:00
|
|
|
#ifdef IPORTAL_DEBUG
|
2000-12-01 23:10:31 +01:00
|
|
|
{
|
|
|
|
char *d = VARDATA(attr);
|
1997-09-07 07:04:48 +02:00
|
|
|
|
2000-12-01 23:10:31 +01:00
|
|
|
fprintf(stderr, "length %d data %x %x %x %x\n",
|
|
|
|
len, *d, *(d + 1), *(d + 2), *(d + 3));
|
1997-09-07 07:04:48 +02:00
|
|
|
}
|
2000-12-01 23:10:31 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Clean up detoasted copy, if any */
|
|
|
|
if (attr != origattr)
|
|
|
|
pfree(DatumGetPointer(attr));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* fixed size */
|
|
|
|
attr = origattr;
|
|
|
|
len = typeinfo->attrs[i]->attlen;
|
|
|
|
pq_sendint(&buf, len, sizeof(int32));
|
|
|
|
if (typeinfo->attrs[i]->attbyval)
|
1997-09-07 07:04:48 +02:00
|
|
|
{
|
2000-12-28 00:59:14 +01:00
|
|
|
Datum datumBuf;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need this horsing around because we don't know how
|
|
|
|
* shorter data values are aligned within a Datum.
|
|
|
|
*/
|
|
|
|
store_att_byval(&datumBuf, attr, len);
|
|
|
|
pq_sendbytes(&buf, (char *) &datumBuf, len);
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef IPORTAL_DEBUG
|
2000-12-28 00:59:14 +01:00
|
|
|
fprintf(stderr, "byval length %d data %ld\n", len,
|
|
|
|
(long) attr);
|
1996-07-09 08:22:35 +02:00
|
|
|
#endif
|
2000-12-01 23:10:31 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pq_sendbytes(&buf, DatumGetPointer(attr), len);
|
1996-07-09 08:22:35 +02:00
|
|
|
#ifdef IPORTAL_DEBUG
|
2000-12-01 23:10:31 +01:00
|
|
|
fprintf(stderr, "byref length %d data %p\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
|
|
|
}
|
|
|
|
}
|
1999-04-25 05:19:27 +02:00
|
|
|
|
|
|
|
pq_endmessage(&buf);
|
1996-07-09 08:22:35 +02:00
|
|
|
}
|