* PQputCopyEnd - send EOF indication to the backend during COPY IN * * After calling this, use PQgetResult() to check command completion status. * * Returns 1 if successful, 0 if data could not be sent (only possible * in nonblock mode), or -1 if an error occurs. */
| 2636 | * in nonblock mode), or -1 if an error occurs. |
| 2637 | */ |
| 2638 | int |
| 2639 | PQputCopyEnd(PGconn *conn, const char *errormsg) |
| 2640 | { |
| 2641 | if (!conn) |
| 2642 | return -1; |
| 2643 | if (conn->asyncStatus != PGASYNC_COPY_IN && |
| 2644 | conn->asyncStatus != PGASYNC_COPY_BOTH) |
| 2645 | { |
| 2646 | appendPQExpBufferStr(&conn->errorMessage, |
| 2647 | libpq_gettext("no COPY in progress\n")); |
| 2648 | return -1; |
| 2649 | } |
| 2650 | |
| 2651 | /* |
| 2652 | * Send the COPY END indicator. This is simple enough that we don't |
| 2653 | * bother delegating it to the fe-protocol files. |
| 2654 | */ |
| 2655 | if (errormsg) |
| 2656 | { |
| 2657 | /* Send COPY FAIL */ |
| 2658 | if (pqPutMsgStart('f', conn) < 0 || |
| 2659 | pqPuts(errormsg, conn) < 0 || |
| 2660 | pqPutMsgEnd(conn) < 0) |
| 2661 | return -1; |
| 2662 | } |
| 2663 | else |
| 2664 | { |
| 2665 | /* Send COPY DONE */ |
| 2666 | if (pqPutMsgStart('c', conn) < 0 || |
| 2667 | pqPutMsgEnd(conn) < 0) |
| 2668 | return -1; |
| 2669 | } |
| 2670 | |
| 2671 | /* |
| 2672 | * If we sent the COPY command in extended-query mode, we must issue a |
| 2673 | * Sync as well. |
| 2674 | */ |
| 2675 | if (conn->cmd_queue_head && |
| 2676 | conn->cmd_queue_head->queryclass != PGQUERY_SIMPLE) |
| 2677 | { |
| 2678 | if (pqPutMsgStart('S', conn) < 0 || |
| 2679 | pqPutMsgEnd(conn) < 0) |
| 2680 | return -1; |
| 2681 | } |
| 2682 | |
| 2683 | /* Return to active duty */ |
| 2684 | if (conn->asyncStatus == PGASYNC_COPY_BOTH) |
| 2685 | conn->asyncStatus = PGASYNC_COPY_OUT; |
| 2686 | else |
| 2687 | conn->asyncStatus = PGASYNC_BUSY; |
| 2688 | |
| 2689 | /* Try to flush data */ |
| 2690 | if (pqFlush(conn) < 0) |
| 2691 | return -1; |
| 2692 | |
| 2693 | return 1; |
| 2694 | } |
| 2695 |
no test coverage detected