PortalSetResultFormat

本文详细解析了PortalSetResultFormat函数的功能与实现细节,该函数用于设置门户输出格式,适用于DestRemote或DestRemoteExecute类型的输出目的地。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章目录


#函数原型

/*
 * PortalSetResultFormat
 *	Select the format codes for a portal's output.
 *
 * This must be run after PortalStart for a portal that will be read by
 * a DestRemote or DestRemoteExecute destination.  
 * It is not presently needed for other destination types.
 *
 * formats[] is the client format request, as per Bind message conventions.
 */
void
PortalSetResultFormat(Portal portal, int nFormats, int16 *formats)
{
	int			natts;
	int			i;

	/*如果没有tuple返回的话,直接返回*/
	if (portal->tupDesc == NULL)
		return;
	/*natts代表一个表中的属性值个数,也就是列的个数*/
	natts = portal->tupDesc->natts;
	/*在指定的内存块中分配内存*/
	portal->formats = (int16 *)
		MemoryContextAlloc(PortalGetHeapMemory(portal),
						   natts * sizeof(int16));
	/*判断nFormats的个数,由调用函数传入*/
	if (nFormats > 1)
	{
		/* format specified for each column */
		if (nFormats != natts)
			ereport(ERROR,
					(errcode(ERRCODE_PROTOCOL_VIOLATION),
					 errmsg("bind message has %d result formats but query has %d columns",
							nFormats, natts)));
		/*将formats带进来的格式拷贝到结构体portal中的formats指向的内存块中
		*也就是上面分配的内存块(format为1的时候代表是text,为0时代表二进制)
		*/
		memcpy(portal->formats, formats, natts * sizeof(int16));
	}
	else if (nFormats > 0)
	{
		/* single format specified, use for all columns */
		int16		format1 = formats[0];

		for (i = 0; i < natts; i++)
			portal->formats[i] = format1;
	}
	else
	{
		/* use default format for all columns */
		for (i = 0; i < natts; i++)
			portal->formats[i] = 0;
	}
}

综上所述,次函数的作用就是将参数formats数组的值,拷贝到portal内部formats指向的内存块中
#解释

DestRemote : 结果发送到前端程序
DestRemoteExecute:执行命令的结果发送到前端

两者可以认为没什么区别

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值