PGresult * pgut_execute_elevel(PGconn* conn, const char *query, int nParams, const char **params, int elevel) { PGresult *res; pgutConn *c; CHECK_FOR_INTERRUPTS(); /* write query to elog if debug */ if (pgut_echo) echo_query(query, nParams, params); if (conn == NULL) { ereport(elevel, (errcode(E_PG_COMMAND), errmsg("not connected"))); return NULL; } /* find connection */ pgut_conn_lock(); for (c = pgut_connections; c; c = c->next) if (c->conn == conn) break; pgut_conn_unlock(); if (c) on_before_exec(c); if (nParams == 0) res = PQexec(conn, query); else res = PQexecParams(conn, query, nParams, NULL, params, NULL, NULL, 0); if (c) on_after_exec(c); switch (PQresultStatus(res)) { case PGRES_TUPLES_OK: case PGRES_COMMAND_OK: case PGRES_COPY_IN: break; default: ereport(elevel, (errcode(E_PG_COMMAND), errmsg("query failed: %s", PQerrorMessage(conn)), errdetail("query was: %s", query))); break; } return res; }
PGresult * pgut_execute(PGconn* conn, const char *query, int nParams, const char **params) { int i; PGresult *res; if (interrupted && !in_cleanup) ereport(FATAL, (errcode(ERROR_INTERRUPTED), errmsg("interrupted"))); /* write query to elog with DEBUG level */ if (strchr(query, '\n')) elog(DEBUG, "(query)\n%s", query); else elog(DEBUG, "(query) %s", query); for (i = 0; i < nParams; i++) elog(DEBUG, "\t(param:%d) = %s", i, params[i] ? params[i] : "(null)"); if (conn == NULL) { ereport(ERROR, (errcode(ERROR_PG_CONNECT), errmsg("not connected"))); return NULL; } on_before_exec(conn); if (nParams == 0) res = PQexec(conn, query); else res = PQexecParams(conn, query, nParams, NULL, params, NULL, NULL, 0); on_after_exec(); switch (PQresultStatus(res)) { case PGRES_TUPLES_OK: case PGRES_COMMAND_OK: case PGRES_COPY_IN: break; default: ereport(ERROR, (errcode(ERROR_PG_COMMAND), errmsg( "query failed: %squery was: %s", PQerrorMessage(conn), query))); break; } return res; }