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?

2
  • 1
    This has nothing to do with libpq, that's the standard output syntax 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. Commented Jan 16, 2017 at 9: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<int64_t, string>(), [](optional<CommandError>&& error, vector<tuple<int64_t, string>>&& results){ ... });, which solves simple types for now. Commented Jan 16, 2017 at 12:06

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.