* PQgetlineAsync - gets a COPY data row without blocking. * * See fe-exec.c for documentation. */
| 2002 | * See fe-exec.c for documentation. |
| 2003 | */ |
| 2004 | int |
| 2005 | pqGetlineAsync3(PGconn *conn, char *buffer, int bufsize) |
| 2006 | { |
| 2007 | int msgLength; |
| 2008 | int avail; |
| 2009 | |
| 2010 | if (conn->asyncStatus != PGASYNC_COPY_OUT |
| 2011 | && conn->asyncStatus != PGASYNC_COPY_BOTH) |
| 2012 | return -1; /* we are not doing a copy... */ |
| 2013 | |
| 2014 | /* |
| 2015 | * Recognize the next input message. To make life simpler for async |
| 2016 | * callers, we keep returning 0 until the next message is fully available |
| 2017 | * even if it is not Copy Data. This should keep PQendcopy from blocking. |
| 2018 | * (Note: unlike pqGetCopyData3, we do not change asyncStatus here.) |
| 2019 | */ |
| 2020 | msgLength = getCopyDataMessage(conn); |
| 2021 | if (msgLength < 0) |
| 2022 | return -1; /* end-of-copy or error */ |
| 2023 | if (msgLength == 0) |
| 2024 | return 0; /* no data yet */ |
| 2025 | |
| 2026 | /* |
| 2027 | * Move data from libpq's buffer to the caller's. In the case where a |
| 2028 | * prior call found the caller's buffer too small, we use |
| 2029 | * conn->copy_already_done to remember how much of the row was already |
| 2030 | * returned to the caller. |
| 2031 | */ |
| 2032 | conn->inCursor += conn->copy_already_done; |
| 2033 | avail = msgLength - 4 - conn->copy_already_done; |
| 2034 | if (avail <= bufsize) |
| 2035 | { |
| 2036 | /* Able to consume the whole message */ |
| 2037 | memcpy(buffer, &conn->inBuffer[conn->inCursor], avail); |
| 2038 | /* Mark message consumed */ |
| 2039 | conn->inStart = conn->inCursor + avail; |
| 2040 | /* Reset state for next time */ |
| 2041 | conn->copy_already_done = 0; |
| 2042 | return avail; |
| 2043 | } |
| 2044 | else |
| 2045 | { |
| 2046 | /* We must return a partial message */ |
| 2047 | memcpy(buffer, &conn->inBuffer[conn->inCursor], bufsize); |
| 2048 | /* The message is NOT consumed from libpq's buffer */ |
| 2049 | conn->copy_already_done += bufsize; |
| 2050 | return bufsize; |
| 2051 | } |
| 2052 | } |
| 2053 | |
| 2054 | /* |
| 2055 | * PQendcopy |
no test coverage detected