How Can I Call PL/pgSQL Function From C++ Code

谁都会走 提交于 2019-12-11 13:45:09

问题


I am trying to call a function which is declared in PostgreSQL with PL/pgSQL. For that I write the code below. My function is working but after that I am taking a "PGRES_FATAL_ERROR". Also when I changed "select removestopwords()" with an sql query like "DELETE * FROM TABLE1" it's working successfully.

I am considering, that error can cause some big problem in future even if now working. How can I call a PL/pgSQL function without taking error?

void removeStopWordsDB(PGconn* conn) {

PGresult *res = PQexec(conn, "select removestopwords()");

if (PQresultStatus(res) != PGRES_COMMAND_OK) {
    printf("removestopwords failed");
    cout<<PQresultStatus(res);
    PQclear(res);
    exit_nicely(conn);
}

printf("removestopwords - OK\n");

PQclear(res);

}


回答1:


If you get PGRES_FATAL_ERROR from PQresultStatus you should use PQresultErrorField to get all the error data from the result set to provide a useful error message. This will allow you to determine what the actual error is here (quite likely an error being sent over from the server).

Consider creating a class to hold PostgreSQL error details that can be constructed from q PQresult pointer, e.g.:

PgError(const PGresult *rs)
{
  severity = GetErrorField(rs, PG_DIAG_SEVERITY);
  sqlstate = GetErrorField(rs, PG_DIAG_SQLSTATE);
  primary = GetErrorField(rs, PG_DIAG_MESSAGE_PRIMARY);
  // ...
}
static std::string GetErrorField(const PGresult *rs, int fieldCode)
{
  const char *message = PQresultErrorField(rs, fieldCode);
  if (message == NULL) return "";
  return std::string(message);
}

Then you can, for example, encapsulate dumping out the error to a stream in this object to provide details just like psql and friends do (although strictly speaking, you'd need the input SQL as well for all of that)




回答2:


PostgreSQL API doesn't support some flag like "ignore all errors". If you would to ignore result, then just don't check result in host environment. But it is bad strategy.



来源:https://stackoverflow.com/questions/13473977/how-can-i-call-pl-pgsql-function-from-c-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!