postgresql/src/backend/access/common/printsimple.c

132 lines
3.1 KiB
C
Raw Normal View History

/*-------------------------------------------------------------------------
*
* printsimple.c
* Routines to print out tuples containing only a limited range of
* builtin types without catalog access. This is intended for
* backends that don't have catalog access because they are not bound
* to a specific database, such as some walsender processes. It
* doesn't handle standalone backends or protocol versions other than
* 3.0, because we don't need such handling for current applications.
*
* Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
* src/backend/access/common/printsimple.c
*
*-------------------------------------------------------------------------
*/
#include "postgres.h"
#include "access/printsimple.h"
#include "catalog/pg_type.h"
#include "fmgr.h"
#include "libpq/pqformat.h"
#include "utils/builtins.h"
/*
* At startup time, send a RowDescription message.
*/
void
printsimple_startup(DestReceiver *self, int operation, TupleDesc tupdesc)
{
StringInfoData buf;
int i;
pq_beginmessage(&buf, 'T'); /* RowDescription */
pq_sendint(&buf, tupdesc->natts, 2);
for (i = 0; i < tupdesc->natts; ++i)
{
Form_pg_attribute attr = tupdesc->attrs[i];
pq_sendstring(&buf, NameStr(attr->attname));
pq_sendint(&buf, 0, 4); /* table oid */
pq_sendint(&buf, 0, 2); /* attnum */
pq_sendint(&buf, (int) attr->atttypid, 4);
pq_sendint(&buf, attr->attlen, 2);
pq_sendint(&buf, attr->atttypmod, 4);
pq_sendint(&buf, 0, 2); /* format code */
}
pq_endmessage(&buf);
}
/*
* For each tuple, send a DataRow message.
*/
bool
printsimple(TupleTableSlot *slot, DestReceiver *self)
{
TupleDesc tupdesc = slot->tts_tupleDescriptor;
StringInfoData buf;
int i;
/* Make sure the tuple is fully deconstructed */
slot_getallattrs(slot);
/* Prepare and send message */
pq_beginmessage(&buf, 'D');
pq_sendint(&buf, tupdesc->natts, 2);
for (i = 0; i < tupdesc->natts; ++i)
{
Form_pg_attribute attr = tupdesc->attrs[i];
Datum value;
if (slot->tts_isnull[i])
{
pq_sendint(&buf, -1, 4);
continue;
}
value = slot->tts_values[i];
/*
* We can't call the regular type output functions here because we
* might not have catalog access. Instead, we must hard-wire
* knowledge of the required types.
*/
switch (attr->atttypid)
{
case TEXTOID:
{
text *t = DatumGetTextPP(value);
pq_sendcountedtext(&buf,
VARDATA_ANY(t),
VARSIZE_ANY_EXHDR(t),
false);
}
break;
case INT4OID:
{
int32 num = DatumGetInt32(value);
Phase 2 of pgindent updates. Change pg_bsd_indent to follow upstream rules for placement of comments to the right of code, and remove pgindent hack that caused comments following #endif to not obey the general rule. Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using the published version of pg_bsd_indent, but a hacked-up version that tried to minimize the amount of movement of comments to the right of code. The situation of interest is where such a comment has to be moved to the right of its default placement at column 33 because there's code there. BSD indent has always moved right in units of tab stops in such cases --- but in the previous incarnation, indent was working in 8-space tab stops, while now it knows we use 4-space tabs. So the net result is that in about half the cases, such comments are placed one tab stop left of before. This is better all around: it leaves more room on the line for comment text, and it means that in such cases the comment uniformly starts at the next 4-space tab stop after the code, rather than sometimes one and sometimes two tabs after. Also, ensure that comments following #endif are indented the same as comments following other preprocessor commands such as #else. That inconsistency turns out to have been self-inflicted damage from a poorly-thought-through post-indent "fixup" in pgindent. This patch is much less interesting than the first round of indent changes, but also bulkier, so I thought it best to separate the effects. Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
char str[12]; /* sign, 10 digits and '\0' */
pg_ltoa(num, str);
pq_sendcountedtext(&buf, str, strlen(str), false);
}
break;
case INT8OID:
{
int64 num = DatumGetInt64(value);
Phase 2 of pgindent updates. Change pg_bsd_indent to follow upstream rules for placement of comments to the right of code, and remove pgindent hack that caused comments following #endif to not obey the general rule. Commit e3860ffa4dd0dad0dd9eea4be9cc1412373a8c89 wasn't actually using the published version of pg_bsd_indent, but a hacked-up version that tried to minimize the amount of movement of comments to the right of code. The situation of interest is where such a comment has to be moved to the right of its default placement at column 33 because there's code there. BSD indent has always moved right in units of tab stops in such cases --- but in the previous incarnation, indent was working in 8-space tab stops, while now it knows we use 4-space tabs. So the net result is that in about half the cases, such comments are placed one tab stop left of before. This is better all around: it leaves more room on the line for comment text, and it means that in such cases the comment uniformly starts at the next 4-space tab stop after the code, rather than sometimes one and sometimes two tabs after. Also, ensure that comments following #endif are indented the same as comments following other preprocessor commands such as #else. That inconsistency turns out to have been self-inflicted damage from a poorly-thought-through post-indent "fixup" in pgindent. This patch is much less interesting than the first round of indent changes, but also bulkier, so I thought it best to separate the effects. Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:18:54 +02:00
char str[23]; /* sign, 21 digits and '\0' */
pg_lltoa(num, str);
pq_sendcountedtext(&buf, str, strlen(str), false);
}
break;
default:
elog(ERROR, "unsupported type OID: %u", attr->atttypid);
}
}
pq_endmessage(&buf);
return true;
}