static void handle_copy_out(PGresult *res) { bool copydone = false; char copybuf[COPYBUFSIZ]; int ret; if (!Silent) fprintf(stdout, "Copy command returns...\n"); while (!copydone) { ret = PQgetline(res->conn, copybuf, COPYBUFSIZ); if (copybuf[0] == '.' && copybuf[1] =='\0') { copydone = true; /* don't print this... */ } else { fputs(copybuf, stdout); switch (ret) { case EOF: copydone = true; /*FALLTHROUGH*/ case 0: fputc('\n', stdout); break; case 1: break; } } } fflush(stdout); PQendcopy(res->conn); }
/* ML type : pgconn_ -> string option */ EXTERNML value pq_getline(value conn) { int bufsize = INITIALSIZE; char* buf = (char*)(malloc(bufsize)); int status; value res; status = PQgetline(PGconn_val(conn), &buf[0], bufsize); if (status != EOF && buf[0] == '\\' && buf[1] == '.' && buf[2] == '\0') status = EOF; while (status == 1) { /* Not finished reading line; read more */ buf = realloc(buf, bufsize * 2); status = PQgetline(PGconn_val(conn), &buf[bufsize-1], bufsize+1); bufsize *= 2; } res = Val_stringornull(status == EOF ? NULL : buf); free(buf); return res; }
CAMLprim value PQgetline_stub( value v_conn, value v_buf, value v_pos, value v_len) { CAMLparam2(v_conn, v_buf); PGconn *conn = get_conn(v_conn); value v_res; size_t len = Long_val(v_len); char *buf = caml_stat_alloc(len); caml_enter_blocking_section(); v_res = Val_int(PQgetline(conn, buf, len)); caml_leave_blocking_section(); memcpy(String_val(v_buf) + Long_val(v_pos), buf, len); free(buf); CAMLreturn(v_res); }