简体   繁体   English

使用libpq库从PostgreSQL的查询结果中获取(integer [])数组值

[英]Get (integer[]) array value from the query result of PostgreSQL using libpq library

after executing query I run below functions but don't know how to convert value to c integer array. 执行查询后,我在下面的函数中运行,但不知道如何将值转换为C整数数组。

how to get integer array from pptRawValue where COLUMN_VALUE_IN_BINARY_FORMAT when I looked at memory i can see the value are present in pptRawValue but not able to get that values in integer array in c code? 当我查看内存时,如何从pptRawValue所在的COLUMN_VALUE_IN_BINARY_FORMAT中获取整数数组,我可以看到pptRawValue中存在该值,但无法在C代码中获取该整数数组中的值?

please help. 请帮忙。

long lColumnNum = PQfnumber(ptQueryRes, pstrColumnName);
long lFormat = PQfformat(ptQueryRes, lColumnNum);
*pptRawValue = PQgetvalue(ptQueryRes, lRowNum, lColumnNum);
*plValueLength = PQgetlength(ptQueryRes, lRowNum, lColumnNum);
int iarray[];

You can either request text mode output, then an integer[] would look like {1,2,42} , or you can request binary mode, but then you'd have to deal with the internal array representation of the PostgreSQL server. 您可以请求文本模式输出,然后使integer[]看起来像{1,2,42} ,或者可以请求二进制模式,但是随后必须处理PostgreSQL服务器的内部数组表示形式。

About binary mode for integer[] : 关于integer[]二进制模式:

Integers themselves are represented in network byte order , so endianness shouldn't be a problem. 整数本身以网络字节顺序表示 ,因此字节顺序不成问题。

You'll have to read the PostgreSQL source to understand the structure of an ArrayType . 您必须阅读PostgreSQL源代码才能理解ArrayType的结构。

Binary more could be a problem if client and server have a different architecture, because the alignment might differ. 如果客户端和服务器具有不同的体系结构,那么二进制化可能会成为问题,因为对齐方式可能会有所不同。

Found the way to extract values from received binary array 找到了从接收到的二进制数组中提取值的方法

The received binary array has below format. 接收到的二进制数组具有以下格式。 I have actually verified it. 我已经验证过了。

This didn't quite agree with what I found but it gave me a starting point. 这与我的发现并不完全一致,但它为我提供了一个起点。 Here is what I found and hope experts will correct me if I am wrong: 这是我的发现,希望如果我错了,专家会纠正我:

The first 20 bytes seem to have the following information about the array: 前20个字节似乎具有有关数组的以下信息:

--first 4 bytes don't know what it is but it is always 1 -前4个字节不知道它是什么,但始终为1
This is may be format number as Laurenz Albe mentioned above. 这可能是上面提到的Laurenz Albe的格式编号。
Reference: https://www.postgresql.org/docs/current/static/protocol-overview.html#PROTOCOL-FORMAT-CODES 参考: https : //www.postgresql.org/docs/current/static/protocol-overview.html#PROTOCOL-FORMAT-CODES
--second 4 bytes " " " " " " " " " 0 -秒4字节“”“”“”“” 0
--third 4 bytes oid of the datatype in the array -数组中数据类型的第三个4个字节的oid
--4th 4 bytes number of elements in the array --4th 4字节数组中的元素数
--5th 4 bytes dimension of the array 数组的-5th 4字节维

Form here we have the actual data. 在这里,我们有实际数据。 Each datum is preceeded with a 4 byte integer indicating the number of bytes the next element occupies. 每个数据前面都带有一个4字节的整数,该整数指示下一个元素占用的字节数。 Knowing the number of elements one can parse the memory and get access to the elements. 知道元素的数量可以解析内存并获得对元素的访问。

Please fallow the link: https://www.postgresql.org/message-id/attachment/13504/arrayAccess.txt 请放置链接: https : //www.postgresql.org/message-id/attachment/13504/arrayAccess.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM