* PQputCopyData - send some data to the backend during COPY IN or COPY BOTH * * Returns 1 if successful, 0 if data could not be sent (only possible * in nonblock mode), or -1 if an error occurs. */
| 2580 | * in nonblock mode), or -1 if an error occurs. |
| 2581 | */ |
| 2582 | int |
| 2583 | PQputCopyData(PGconn *conn, const char *buffer, int nbytes) |
| 2584 | { |
| 2585 | if (!conn) |
| 2586 | return -1; |
| 2587 | if (conn->asyncStatus != PGASYNC_COPY_IN && |
| 2588 | conn->asyncStatus != PGASYNC_COPY_BOTH) |
| 2589 | { |
| 2590 | appendPQExpBufferStr(&conn->errorMessage, |
| 2591 | libpq_gettext("no COPY in progress\n")); |
| 2592 | return -1; |
| 2593 | } |
| 2594 | |
| 2595 | /* |
| 2596 | * Process any NOTICE or NOTIFY messages that might be pending in the |
| 2597 | * input buffer. Since the server might generate many notices during the |
| 2598 | * COPY, we want to clean those out reasonably promptly to prevent |
| 2599 | * indefinite expansion of the input buffer. (Note: the actual read of |
| 2600 | * input data into the input buffer happens down inside pqSendSome, but |
| 2601 | * it's not authorized to get rid of the data again.) |
| 2602 | */ |
| 2603 | parseInput(conn); |
| 2604 | |
| 2605 | if (nbytes > 0) |
| 2606 | { |
| 2607 | /* |
| 2608 | * Try to flush any previously sent data in preference to growing the |
| 2609 | * output buffer. If we can't enlarge the buffer enough to hold the |
| 2610 | * data, return 0 in the nonblock case, else hard error. (For |
| 2611 | * simplicity, always assume 5 bytes of overhead.) |
| 2612 | */ |
| 2613 | if ((conn->outBufSize - conn->outCount - 5) < nbytes) |
| 2614 | { |
| 2615 | if (pqFlush(conn) < 0) |
| 2616 | return -1; |
| 2617 | if (pqCheckOutBufferSpace(conn->outCount + 5 + (size_t) nbytes, |
| 2618 | conn)) |
| 2619 | return pqIsnonblocking(conn) ? 0 : -1; |
| 2620 | } |
| 2621 | /* Send the data (too simple to delegate to fe-protocol files) */ |
| 2622 | if (pqPutMsgStart('d', conn) < 0 || |
| 2623 | pqPutnchar(buffer, nbytes, conn) < 0 || |
| 2624 | pqPutMsgEnd(conn) < 0) |
| 2625 | return -1; |
| 2626 | } |
| 2627 | return 1; |
| 2628 | } |
| 2629 | |
| 2630 | /* |
| 2631 | * PQputCopyEnd - send EOF indication to the backend during COPY IN |
no test coverage detected