0

I am using libpq for my C/C++ application to interface with PostgreSQL.

My queries are parameterised and in text (not binary) format (using PQexecParams), and they return data that might be a text, text[], int4range, or other complex types. However, libpq seems to format strings based on rules that don't seem to be well-documented.

For example, for a string array, libpq returns something like that:

{abc,def,"h,h","h\"h",h'h,"h h"}

when the array, in C style, would look like this:

{"abc","def","h,h","h\"h","h'h","h h"}

libpq seems to place double quotes and escape characters (\) where it deems fit.

For a string (not string array), libpq will instead not place double quotes at all.

As any printable character might possibly be stored in my strings, is there a way to force libpq or PostgreSQL to return strings and string arrays in a consistent format for easy consumption by my C/C++ application?

Bernard
  • 5,209
  • 1
  • 34
  • 64
  • 1
    This has nothing to do with libpq, that's the standard [output syntax](https://www.postgresql.org/docs/current/static/arrays.html#ARRAYS-IO) for PostgreSQL's arrays (and in fact, it is more complicated than that if you want to support arbitrary indexed arrays too). `libpqtypes` might does what you need: see [this question](http://stackoverflow.com/questions/29802669/inserting-integer-array-with-postgresql-in-c-libpq). – pozs Jan 16 '17 at 09:42
  • @pozs That makes sense, thanks! Eventually I used `array_to_string` to get PostgreSQL to send the data as a plain string. For the integer and timestamp types, I had already written an interface that looks like this: `pqclient.query("SELECT my_id, my_text FROM my_table WHERE data = $1;", make_tuple(12345), tuple(), [](optional&& error, vector>&& results){ ... });`, which solves simple types for now. – Bernard Jan 16 '17 at 12:06

0 Answers0