繁体   English   中英

PostgreSQL 的 libpq:ARRAY[]-data 二进制传输的编码?

[英]PostgreSQL's libpq: Encoding for binary transport of ARRAY[]-data?

经过数小时的文档/板/邮件列表但没有任何进展,我可能会问你:我如何“编码”我的数据以使用 libpq 的PQexecParams(.)将其用于二进制传输?

简单变量只是按大端顺序排列:

PGconn *conn;
PGresult *res;
char *paramValues[1];
int paramLengths[1];
int paramFormats[1];

conn = PQconnectdb(CONNINFO);

// -- (1) -- send a float value
float val_f = 0.12345678901234567890; // float precision: ~7 decimal digits
// alloc some memory & write float (in big endian) into
paramValues[0] = (char *) malloc(sizeof(val_f));
*((uint32_t*) paramValues[0]) = htobe32(*((uint32_t*) &val_f)); // host to big endian

paramLengths[0] = sizeof(val_f);
paramFormats[0] = 1; // binary

res = PQexecParams(conn, "SELECT $1::real ;", //
        1, // number parameters
        NULL, // let the backend deduce param type
        paramValues, //
        paramLengths, //
        paramFormats, //
        0); // return text
printf("sent float: %s \n", PQgetvalue(res, 0, 0));
// --> sent float: 0.123457

并且像这样也是双精度型、整数型等...

但是数组呢?

    float vals_f[] = {1.23, 9.87};
    // alloc some memory
    paramValues[0] = (char *) malloc(sizeof(float) * 2);

//  ???? paramValues[0] = ??????

    paramLengths[0] = sizeof(float) * 2;
    paramFormats[0] = 1; // binary


    res = PQexecParams(conn, "SELECT $1::real[] ;", //
            1, // number parameters
            NULL, // let the backend deduce param type
            paramValues, //
            paramLengths, //
            paramFormats, //
            0); // return text
    printf("sent float array: %s \n", PQgetvalue(res, 0, 0));

是否有任何以 PostgreSQL 的二进制格式传输 ARRAY 数据的工作示例? backend/utils/adt/中的代码对我帮助不大(除了我现在知道有一个 ARRAYTYPE,但不知道如何使用它们):-(

我只需要一个函数char* to_PQbin(float [] input, int length)来传递给paramValues[.] ...

非常感谢,特巴斯

PS:转换简单变量(而不是我的htobe32(.) )的建议方法是什么?

http://git.postgresql.org/gitweb?p=postgresql.git;a=blob;f=src/include/utils/array.h;h=7f7e744cb12bc872f628f90dad99dfdf074eb314;hb=master描述了 Postgres 的数组二进制格式。 使用 libpq 时,省略 vl_len_ 部分。 例如,一个包含 4 个整数的数组将如下所示:

0x00000001 0x00000000 0x00000017 0x00000004 0x00000001 0x00000004 0x00000004 0x00000004 0x0000000

这有 OID 1007 (INT4ARRAYOID)。 第一个整数是1维,第二个整数是没有NULL位图(所以数组的值都不是NULL),第三个整数是元素的OID(23,INT4OID),第四个整数是第一维有多大(4)、第五个整数是第一维的起始索引。 之后是原始数组数据,按顺序排列,每个元素以其长度为前缀(每个整数 4 个字节)。

正如ccuter已经提到的,您需要创建自己的 API。 以下代码提取int4的一维数组,忽略任何 NULL 值。

#define   INT4OID   23

/*! Structure of array header to determine array type */
struct array_int4 {
  int32_t ndim; /* Number of dimensions */
  int32_t _ign; /* offset for data, removed by libpq */
  Oid elemtype; /* type of element in the array */

  /* First dimension */
  int32_t size; /* Number of elements */
  int32_t index; /* Index of first element */
  int32_t first_value; /* Beginning of integer data */
};

static int extract_int4_array (char *raw_array, 
                               int32_t **values, 
                               int *num_values) {
  /* Array information header */
  struct array_int4 *array = (struct array_int4 *) raw_array; 
  /* Pointer to traverse int array */
  int32_t *p_value = &(array->first_value);
  /* int value in host byte order */
  int32_t hval;

  /* Check if we have a 1-dimensional INT4 array */
  if (ntohl(array->ndim) != 1 
  || ntohl(array->elemtype) != INT4OID) {
    return -1;
  }
  /* Number of elements including NULLs */
  int array_elements = ntohl (array->size);

  *num_values = 0;
  /* Get size of array */
  for (int i=0; i<array_elements; ++i) {
    /* Check size to see if this is a NULL value */
    hval = ntohl (*p_value);
    if (hval != -1) {
      ++p_value;
      (*num_values) += 1;
    } 

    ++p_value;
  }
  *values = malloc (*num_values * sizeof **values);

  /* Fill output int array. Skip every other value as it contains the size of 
   * the element */
  *num_values = 0; /* Use num_values as the index of the output array */
  p_value = &(array->first_value);
  for (int i=0; i<array_elements; ++i) {
    /* Check size to see if this is a NULL value */
    hval = ntohl (*p_value);
    if (hval != -1) {
      ++p_value;
  (*values)[*num_values] = ntohl (*p_value);
      (*num_values) += 1;
    } 

    ++p_value;
  }

  return 0;
}

似乎还有一个名为libpqtypes的库,它有助于这种转换。

这是我在 Node.js / TypeScript 中设法完成的工作:

function writeInt4Array(buffer: Buffer, values: number[], offset: number): number {
  offset = buffer.writeInt32BE(1, offset) // Number of dimensions
  offset = buffer.writeInt32BE(0, offset) // Has nulls?
  offset = buffer.writeInt32BE(ObjectId.Int4, offset) // Element type
  offset = buffer.writeInt32BE(values.length, offset) // Size of first dimension
  offset = buffer.writeInt32BE(1, offset) // Offset (starting index) of first dimension
  for (const v of values) {
    offset = buffer.writeInt32BE(4, offset)
    offset = buffer.writeInt32BE(v, offset)
  }
  return offset
}

暂无
暂无

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

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