* PQgetResult * Get the next PGresult produced by a query. Returns NULL if no * query work remains or an error has occurred (e.g. out of * memory). * * In pipeline mode, once all the result of a query have been returned, * PQgetResult returns NULL to let the user know that the next * query is being processed. At the end of the pipeline, returns a * result with PQresultStatu
| 2014 | * result with PQresultStatus(result) == PGRES_PIPELINE_SYNC. |
| 2015 | */ |
| 2016 | PGresult * |
| 2017 | PQgetResult(PGconn *conn) |
| 2018 | { |
| 2019 | PGresult *res; |
| 2020 | |
| 2021 | if (!conn) |
| 2022 | return NULL; |
| 2023 | |
| 2024 | /* Parse any available data, if our state permits. */ |
| 2025 | parseInput(conn); |
| 2026 | |
| 2027 | /* If not ready to return something, block until we are. */ |
| 2028 | while (conn->asyncStatus == PGASYNC_BUSY) |
| 2029 | { |
| 2030 | int flushResult; |
| 2031 | |
| 2032 | /* |
| 2033 | * If data remains unsent, send it. Else we might be waiting for the |
| 2034 | * result of a command the backend hasn't even got yet. |
| 2035 | */ |
| 2036 | while ((flushResult = pqFlush(conn)) > 0) |
| 2037 | { |
| 2038 | if (pqWait(false, true, conn)) |
| 2039 | { |
| 2040 | flushResult = -1; |
| 2041 | break; |
| 2042 | } |
| 2043 | } |
| 2044 | |
| 2045 | /* |
| 2046 | * Wait for some more data, and load it. (Note: if the connection has |
| 2047 | * been lost, pqWait should return immediately because the socket |
| 2048 | * should be read-ready, either with the last server data or with an |
| 2049 | * EOF indication. We expect therefore that this won't result in any |
| 2050 | * undue delay in reporting a previous write failure.) |
| 2051 | */ |
| 2052 | if (flushResult || |
| 2053 | pqWait(true, false, conn) || |
| 2054 | pqReadData(conn) < 0) |
| 2055 | { |
| 2056 | /* |
| 2057 | * conn->errorMessage has been set by pqWait or pqReadData. We |
| 2058 | * want to append it to any already-received error message. |
| 2059 | */ |
| 2060 | pqSaveErrorResult(conn); |
| 2061 | conn->asyncStatus = PGASYNC_IDLE; |
| 2062 | return pqPrepareAsyncResult(conn); |
| 2063 | } |
| 2064 | |
| 2065 | /* Parse it. */ |
| 2066 | parseInput(conn); |
| 2067 | |
| 2068 | /* |
| 2069 | * If we had a write error, but nothing above obtained a query result |
| 2070 | * or detected a read error, report the write error. |
| 2071 | */ |
| 2072 | if (conn->write_failed && conn->asyncStatus == PGASYNC_BUSY) |
| 2073 | { |