diff options
Diffstat (limited to 'libpqpp')
-rw-r--r-- | libpqpp/connection.cpp | 38 | ||||
-rw-r--r-- | libpqpp/connection.h | 4 |
2 files changed, 42 insertions, 0 deletions
diff --git a/libpqpp/connection.cpp b/libpqpp/connection.cpp index 8bfc2ff..d07b9d2 100644 --- a/libpqpp/connection.cpp +++ b/libpqpp/connection.cpp @@ -130,3 +130,41 @@ PQ::Connection::checkResultFree(PGresult * res, int expected, int alt) const PQclear(res); } +void +PQ::Connection::beginBulkUpload(const char * table, const char * extra) const +{ + char buf[BUFSIZ]; + snprintf(buf, BUFSIZ, "COPY %s FROM STDIN %s", table, extra); + checkResultFree(PQexec(conn, buf), PGRES_COPY_IN); +} + +void +PQ::Connection::endBulkUpload(const char * msg) const +{ + switch (PQputCopyEnd(conn, msg)) { + case 0:// block + sleep(1); + endBulkUpload(msg); + return; + case 1:// success + checkResultFree(PQgetResult(conn), PGRES_COMMAND_OK); + return; + default:// -1 is error + throw Error(PQerrorMessage(conn)); + } +} + +size_t +PQ::Connection::bulkUploadData(const char * data, size_t len) const +{ + switch (PQputCopyData(conn, data, len)) { + case 0:// block + sleep(1); + return bulkUploadData(data, len); + case 1:// success + return len; + default:// -1 is error + throw Error(PQerrorMessage(conn)); + } +} + diff --git a/libpqpp/connection.h b/libpqpp/connection.h index d4cf60a..59c5858 100644 --- a/libpqpp/connection.h +++ b/libpqpp/connection.h @@ -22,6 +22,10 @@ namespace PQ { DB::SelectCommand * newSelectCommand(const std::string & sql) const; DB::ModifyCommand * newModifyCommand(const std::string & sql) const; + void beginBulkUpload(const char *, const char *) const; + void endBulkUpload(const char *) const; + size_t bulkUploadData(const char *, size_t) const; + PGresult * checkResult(PGresult * res, int expected, int alternative = -1) const; void checkResultFree(PGresult * res, int expected, int alternative = -1) const; |