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?
libpqtypesmight does what you need: see this question.array_to_stringto 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<int64_t, string>(), [](optional<CommandError>&& error, vector<tuple<int64_t, string>>&& results){ ... });, which solves simple types for now.