Reputation: 91
For basic data types I have no problems parsing data using a format like:
if (FALSE == PQgetisnull(res, rowNumber, columnIndex))
{
retVal.m_assignedSocket = atol(PQgetvalue(res, rowNumber, columnIndex));
} else
{
retVal.m_assignedSocket = 0;
}
I would like to have an array field in one of my tables that is an array of bigints. I believe I can insert the data like this:
// array elements are separated by curly braces
insertQuery += "{";
for (size_t i = 0; i < settings.m_stepVector.size(); i++)
{
if (snprintf(buffer, sizeof(buffer), ", %llu", settings.m_stepVector[i]) > (int) sizeof(buffer))
{
LOG_SYSTEM_WARNING("Unexpected string truncation\n");
}
insertQuery += buffer;
}
insertQuery += "}";
The problem comes with parsing the row. I have not found any examples or documentation stating the format of the returned array column. My assumption is that it is a string with curly braces on each end. What is the format between the curly braces? It doesn't seem like this is something new but I cannot find an answer anywhere. Could be I don't know the right keywords to search under. I tried postgresql, array, PQgetvalue, and parsing.
Upvotes: 4
Views: 1248
Reputation: 91
Final experiments show this to be working:
retVal.m_rules.clear();
pqxx::array_parser parser = result[columnIndex].as_array();
arrayObject obj = parser.get_next();
while (obj.first != pqxx::array_parser::done)
{
if (obj.first == pqxx::array_parser::string_value)
{
// arrays return a string value so atoll needed to convert to number
retVal.m_rules.push_back(atoll(obj.second.c_str()));
}
obj = parser.get_next();
}
columnIndex++;
Result is defined as pqxx::result::iterator &result.
Upvotes: 2