本文整理汇总了C++中PQresultErrorField函数的典型用法代码示例。如果您正苦于以下问题:C++ PQresultErrorField函数的具体用法?C++ PQresultErrorField怎么用?C++ PQresultErrorField使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PQresultErrorField函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: pg_result_error
static void
pg_result_error(PGresult *pg_result)
{
const char *diag_sqlstate = PQresultErrorField(pg_result, PG_DIAG_SQLSTATE);
const char *diag_primary = PQresultErrorField(pg_result, PG_DIAG_MESSAGE_PRIMARY);
const char *diag_detail = PQresultErrorField(pg_result, PG_DIAG_MESSAGE_DETAIL);
const char *diag_context = PQresultErrorField(pg_result, PG_DIAG_CONTEXT);
const char *diag_hint = PQresultErrorField(pg_result, PG_DIAG_MESSAGE_HINT);
int sqlstate;
if (diag_sqlstate)
sqlstate = MAKE_SQLSTATE(diag_sqlstate[0],
diag_sqlstate[1],
diag_sqlstate[2],
diag_sqlstate[3],
diag_sqlstate[4]);
else
sqlstate = ERRCODE_CONNECTION_FAILURE;
PQclear(pg_result);
ereport(ERROR,
(errcode(sqlstate),
errmsg("Remote error: %s", diag_primary),
diag_detail ? errdetail("Remote detail: %s", diag_detail) : 0,
diag_hint ? errhint("Remote hint: %s", diag_hint) : 0,
diag_context ? errcontext("Remote context: %s", diag_context) : 0));
}
示例2: __db_insert
/*
* insert a row into the db
* this expects an escaped table name
*/
enum DB_RESULT __db_insert(const char *table_esc, unsigned long iid, struct timeval current_time, unsigned long long insert_val, double insert_rate) {
PGconn *pgsql = getpgsql();
char *query;
char *diag;
char now[20];
enum DB_RESULT status;
PGresult *result;
if (pgsql == NULL) {
debug(LOW, "No Postgres connection in db_insert\n");
return FALSE;
}
tv2iso8601(now, current_time);
/* INSERT INTO %s (iid,dtime,counter,rate) VALUES (%d, NOW(), %llu, %.6f) */
/* don't include the rate column if it's not needed */
if (insert_rate > 0) {
/* double columns have precision of at least 15 digits */
asprintf(&query,
"INSERT INTO \"%s\" (iid,dtime,counter,rate) VALUES (%lu,\'%s\',%llu,%.15f)",
table_esc, iid, now, insert_val, insert_rate);
} else {
asprintf(&query,
"INSERT INTO \"%s\" (iid,dtime,counter) VALUES (%lu,\'%s\',%llu)",
table_esc, iid, now, insert_val);
}
db_debug(HIGH, "Query = %s\n", query);
result = PQexec(pgsql, query);
if (PQresultStatus(result) == PGRES_COMMAND_OK) {
status = DB_OK;
} else {
/* Note that by libpq convention, a non-empty PQerrorMessage will include a trailing newline. */
/* also errors start with 'ERROR:' so we don't need to */
db_debug(LOW, "(error code %s) %s", PQresultErrorField(result, PG_DIAG_SQLSTATE), PQerrorMessage(pgsql));
db_debug(LOW, "%s\n", query);
diag = PQresultErrorField(result, PG_DIAG_SQLSTATE);
if (diag && strncmp(diag, "22003", 5) == 0) {
/* NUMERIC VALUE OUT OF RANGE */
/* this can happen because postgres doesn't have unsigned integers */
status = DB_OOR;
} else {
status = DB_RETRY;
}
}
/* free the result */
(void)PQclear(result);
free(query);
return(status);
}
示例3: plproxy_remote_error
/*
* Pass remote error/notice/warning through.
*/
void
plproxy_remote_error(ProxyFunction *func, ProxyConnection *conn, const PGresult *res, bool iserr)
{
const char *ss = PQresultErrorField(res, PG_DIAG_SQLSTATE);
const char *sev = PQresultErrorField(res, PG_DIAG_SEVERITY);
const char *msg = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
const char *det = PQresultErrorField(res, PG_DIAG_MESSAGE_DETAIL);
const char *hint = PQresultErrorField(res, PG_DIAG_MESSAGE_HINT);
const char *spos = PQresultErrorField(res, PG_DIAG_STATEMENT_POSITION);
const char *ipos = PQresultErrorField(res, PG_DIAG_INTERNAL_POSITION);
const char *iquery = PQresultErrorField(res, PG_DIAG_INTERNAL_QUERY);
const char *ctx = PQresultErrorField(res, PG_DIAG_CONTEXT);
int elevel;
/* libpq errors may not have sqlstate */
if (!ss)
ss = "XX000";
if (iserr)
/* must ignore remote level, as it may be FATAL/PANIC */
elevel = ERROR;
else
/* cannot look at sev here, as it may be localized */
elevel = !strncmp(ss, "00", 2) ? NOTICE : WARNING;
ereport(elevel, (
errcode(MAKE_SQLSTATE(ss[0], ss[1], ss[2], ss[3], ss[4])),
errmsg("%s(%d): [%s] REMOTE %s: %s", func->name, func->arg_count, PQdb(conn->cur->db), sev, msg),
det ? errdetail("Remote detail: %s", det) : 0,
hint ? errhint("Remote hint: %s", hint) : 0,
spos ? errposition(atoi(spos)) : 0,
ipos ? internalerrposition(atoi(ipos)) : 0,
iquery ? internalerrquery(iquery) : 0,
ctx ? errcontext("Remote context: %s", ctx) : 0));
}
示例4: ECPGnoticeReceiver
static void
ECPGnoticeReceiver(void *arg, const PGresult *result)
{
char *sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
char *message = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
struct sqlca_t *sqlca = ECPGget_sqlca();
int sqlcode;
if (sqlca == NULL)
{
ecpg_log("out of memory");
return;
}
(void) arg; /* keep the compiler quiet */
if (sqlstate == NULL)
sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
if (message == NULL) /* Shouldn't happen, but need to be sure */
message = ecpg_gettext("empty message text");
/* these are not warnings */
if (strncmp(sqlstate, "00", 2) == 0)
return;
ecpg_log("ECPGnoticeReceiver: %s\n", message);
/* map to SQLCODE for backward compatibility */
if (strcmp(sqlstate, ECPG_SQLSTATE_INVALID_CURSOR_NAME) == 0)
sqlcode = ECPG_WARNING_UNKNOWN_PORTAL;
else if (strcmp(sqlstate, ECPG_SQLSTATE_ACTIVE_SQL_TRANSACTION) == 0)
sqlcode = ECPG_WARNING_IN_TRANSACTION;
else if (strcmp(sqlstate, ECPG_SQLSTATE_NO_ACTIVE_SQL_TRANSACTION) == 0)
sqlcode = ECPG_WARNING_NO_TRANSACTION;
else if (strcmp(sqlstate, ECPG_SQLSTATE_DUPLICATE_CURSOR) == 0)
sqlcode = ECPG_WARNING_PORTAL_EXISTS;
else
sqlcode = 0;
strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
sqlca->sqlcode = sqlcode;
sqlca->sqlwarn[2] = 'W';
sqlca->sqlwarn[0] = 'W';
strncpy(sqlca->sqlerrm.sqlerrmc, message, sizeof(sqlca->sqlerrm.sqlerrmc));
sqlca->sqlerrm.sqlerrmc[sizeof(sqlca->sqlerrm.sqlerrmc) - 1] = 0;
sqlca->sqlerrm.sqlerrml = strlen(sqlca->sqlerrm.sqlerrmc);
ecpg_log("raising sqlcode %d\n", sqlcode);
}
示例5: SetResultError
PyObject* SetResultError(PGresult* r)
{
// Creates an exception from `result`.
//
// This function takes ownership of `result` and will clear it, even if an exception cannot be created.
//
// Always returns zero so it can be called using "return SetResultError(result);"
// TODO: Make a new exception class that always has SQLSTATE
ResultHolder result(r); // make sure `r` gets cleared no matter what
const char* szMessage = PQresultErrorMessage(result);
const char* szSQLSTATE = PQresultErrorField(result, PG_DIAG_SQLSTATE);
if (!szMessage || !szSQLSTATE)
return PyErr_NoMemory();
Object msg(PyUnicode_FromFormat("[%s] %s", szSQLSTATE, szMessage));
if (!msg)
return 0;
PyObject* error = PyObject_CallFunction(Error, (char*)"O", msg.Get());
if (!error)
return 0;
for (size_t i = 0; i < _countof(errorFields); i++)
{
const char* szValue = PQresultErrorField(result, errorFields[i].fieldcode);
Object value;
if (szValue == 0)
{
value.AttachAndIncrement(Py_None);
}
else
{
value.Attach(PyUnicode_FromString(szValue));
if (!value)
return 0;
}
if (PyObject_SetAttrString(error, errorFields[i].szAttr, value) == -1)
return 0;
}
PyErr_SetObject(Error, error);
return 0;
}
示例6: transfer_message
static void
transfer_message(void *arg, const PGresult *res)
{
int elevel;
int code;
const char *severity = PQresultErrorField(res, PG_DIAG_SEVERITY);
const char *state = PQresultErrorField(res, PG_DIAG_SQLSTATE);
const char *message = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
const char *detail = PQresultErrorField(res, PG_DIAG_MESSAGE_DETAIL);
if (detail && !detail[0])
detail = NULL;
switch (severity[0])
{
case 'D':
elevel = DEBUG2;
break;
case 'L':
elevel = LOG;
break;
case 'I':
elevel = INFO;
break;
case 'N':
elevel = NOTICE;
break;
case 'E':
case 'F':
elevel = ERROR;
break;
default:
elevel = WARNING;
break;
}
code = MAKE_SQLSTATE(state[0], state[1], state[2], state[3], state[4]);
if (elevel >= ERROR)
{
if (message)
message = pstrdup(message);
if (detail)
detail = pstrdup(detail);
PQclear((PGresult *) res);
}
ereport(elevel,
(errcode(code),
errmsg("%s", message),
(detail ? errdetail("%s", detail) : 0)));
}
示例7: pgfdw_report_error
/*
* Report an error we got from the remote server.
*
* elevel: error level to use (typically ERROR, but might be less)
* res: PGresult containing the error
* conn: connection we did the query on
* clear: if true, PQclear the result (otherwise caller will handle it)
* sql: NULL, or text of remote command we tried to execute
*
* Note: callers that choose not to throw ERROR for a remote error are
* responsible for making sure that the associated ConnCacheEntry gets
* marked with have_error = true.
*/
void
pgfdw_report_error(int elevel, PGresult *res, PGconn *conn,
bool clear, const char *sql)
{
/* If requested, PGresult must be released before leaving this function. */
PG_TRY();
{
char *diag_sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
char *message_primary = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
char *message_detail = PQresultErrorField(res, PG_DIAG_MESSAGE_DETAIL);
char *message_hint = PQresultErrorField(res, PG_DIAG_MESSAGE_HINT);
char *message_context = PQresultErrorField(res, PG_DIAG_CONTEXT);
int sqlstate;
if (diag_sqlstate)
sqlstate = MAKE_SQLSTATE(diag_sqlstate[0],
diag_sqlstate[1],
diag_sqlstate[2],
diag_sqlstate[3],
diag_sqlstate[4]);
else
sqlstate = ERRCODE_CONNECTION_FAILURE;
/*
* If we don't get a message from the PGresult, try the PGconn. This
* is needed because for connection-level failures, PQexec may just
* return NULL, not a PGresult at all.
*/
if (message_primary == NULL)
message_primary = PQerrorMessage(conn);
ereport(elevel,
(errcode(sqlstate),
message_primary ? errmsg_internal("%s", message_primary) :
errmsg("unknown error"),
message_detail ? errdetail_internal("%s", message_detail) : 0,
message_hint ? errhint("%s", message_hint) : 0,
message_context ? errcontext("%s", message_context) : 0,
sql ? errcontext("Remote SQL command: %s", sql) : 0));
}
PG_CATCH();
{
if (clear)
PQclear(res);
PG_RE_THROW();
}
PG_END_TRY();
if (clear)
PQclear(res);
}
示例8: wxLogInfo
void dlgDirectDbg::OnTargetComplete( wxCommandEvent &event )
{
// Extract the result set handle from the event and log the status info
PGresult *result = (PGresult *)event.GetClientData();
wxLogInfo( wxT( "OnTargetComplete() called\n" ));
wxLogInfo( wxT( "%s\n" ), wxString(PQresStatus( PQresultStatus( result )), wxConvUTF8).c_str());
// If the query failed, write the error message to the status line, otherwise, copy the result set into the grid
if(( PQresultStatus( result ) == PGRES_NONFATAL_ERROR ) || ( PQresultStatus( result ) == PGRES_FATAL_ERROR ))
{
wxString message( PQresultErrorMessage( result ), wxConvUTF8 );
message.Replace( wxT( "\n" ), wxT( " " ));
m_parent->getStatusBar()->SetStatusText( message, 1 );
char *state = PQresultErrorField(result, PG_DIAG_SQLSTATE);
// Don't bother telling the user that he aborted - he already knows!
// Depending on the stage, m_conn might not be set all! so check for
// that first
if (m_conn)
{
if (state != NULL && strcmp(state, "57014"))
wxLogError( wxT( "%s\n" ), wxString(PQerrorMessage(m_conn->getConnection()), wxConvUTF8).c_str());
else
wxLogInfo( wxT( "%s\n" ), wxString(PQerrorMessage(m_conn->getConnection()), wxConvUTF8).c_str());
}
}
else
{
wxString message( PQcmdStatus( result ), wxConvUTF8 );
message.Replace( wxT( "\r" ), wxT( "" ));
message.Replace( wxT( "\n" ), wxT( " " ));
m_parent->getStatusBar()->SetStatusText( message, 1 );
// If this result set has any columns, add a result grid to the code window so
// we can show the results to the user
if( m_codeWindow && PQnfields( result ))
{
m_codeWindow->OnResultSet( result );
}
}
if (m_codeWindow)
{
m_codeWindow->m_targetComplete = true;
m_codeWindow->disableTools( );
}
// Do not show if aborted
if ( m_codeWindow && m_codeWindow->m_targetAborted )
return;
this->Show( true );
}
示例9: raise_error
static void raise_error(VALUE self, PGresult *result, VALUE query) {
VALUE exception;
char *message;
char *sqlstate;
int postgres_errno;
message = PQresultErrorMessage(result);
sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
postgres_errno = MAKE_SQLSTATE(sqlstate[0], sqlstate[1], sqlstate[2], sqlstate[3], sqlstate[4]);
PQclear(result);
const char *exception_type = "SQLError";
struct errcodes *errs;
for (errs = errors; errs->error_name; errs++) {
if(errs->error_no == postgres_errno) {
exception_type = errs->exception;
break;
}
}
VALUE uri = rb_funcall(rb_iv_get(self, "@connection"), rb_intern("to_s"), 0);
exception = rb_funcall(CONST_GET(mDO, exception_type), ID_NEW, 5,
rb_str_new2(message),
INT2NUM(postgres_errno),
rb_str_new2(sqlstate),
query,
uri);
rb_exc_raise(exception);
}
示例10: Excecao
//-----------------------------------------------------------
void ConexaoBD::executarComandoDML(const QString &sql, Resultado &resultado)
{
Resultado *novo_res=NULL;
PGresult *res_sql=NULL;
//Dispara um erro caso o usuário tente reiniciar uma conexão não iniciada
if(!conexao)
throw Excecao(ERR_CONEXBD_OPRCONEXNAOALOC, __PRETTY_FUNCTION__, __FILE__, __LINE__);
//Aloca um novo resultado para receber o result-set vindo da execução do comando sql
res_sql=PQexec(conexao, sql.toStdString().c_str());
if(strlen(PQerrorMessage(conexao))>0)
{
throw Excecao(QString(Excecao::obterMensagemErro(ERR_CONEXBD_CMDSQLNAOEXECUTADO))
.arg(PQerrorMessage(conexao)),
ERR_CONEXBD_CMDSQLNAOEXECUTADO, __PRETTY_FUNCTION__, __FILE__, __LINE__, NULL,
QString(PQresultErrorField(res_sql, PG_DIAG_SQLSTATE)));
}
novo_res=new Resultado(res_sql);
//Copia o novo resultado para o resultado do parâmetro
resultado=*(novo_res);
//Desaloca o resultado criado
delete(novo_res);
}
示例11: GetQueryResult
/*
* GetQueryResult
*
* Process the query result. Returns true if there's no error, false
* otherwise -- but errors about trying to vacuum a missing relation are
* reported and subsequently ignored.
*/
static bool
GetQueryResult(PGconn *conn, const char *dbname, const char *progname)
{
PGresult *result;
SetCancelConn(conn);
while ((result = PQgetResult(conn)) != NULL)
{
/*
* If errors are found, report them. Errors about a missing table are
* harmless so we continue processing; but die for other errors.
*/
if (PQresultStatus(result) != PGRES_COMMAND_OK)
{
char *sqlState = PQresultErrorField(result, PG_DIAG_SQLSTATE);
fprintf(stderr, _("%s: vacuuming of database \"%s\" failed: %s"),
progname, dbname, PQerrorMessage(conn));
if (sqlState && strcmp(sqlState, ERRCODE_UNDEFINED_TABLE) != 0)
{
PQclear(result);
return false;
}
}
PQclear(result);
}
ResetCancelConn();
return true;
}
示例12: Exception
void DBConnection::executeDMLCommand(const QString &sql, ResultSet &result)
{
ResultSet *new_res=nullptr;
PGresult *sql_res=nullptr;
//Raise an error in case the user try to close a not opened connection
if(!connection)
throw Exception(ERR_OPR_NOT_ALOC_CONN, __PRETTY_FUNCTION__, __FILE__, __LINE__);
//Alocates a new result to receive the resultset returned by the sql command
sql_res=PQexec(connection, sql.toStdString().c_str());
//Raise an error in case the command sql execution is not sucessful
if(strlen(PQerrorMessage(connection))>0)
{
throw Exception(QString(Exception::getErrorMessage(ERR_CMD_SQL_NOT_EXECUTED))
.arg(PQerrorMessage(connection)),
ERR_CMD_SQL_NOT_EXECUTED, __PRETTY_FUNCTION__, __FILE__, __LINE__, nullptr,
QString(PQresultErrorField(sql_res, PG_DIAG_SQLSTATE)));
}
//Generates the resultset based on the sql result descriptor
new_res=new ResultSet(sql_res);
//Copy the new resultset to the parameter resultset
result=*(new_res);
//Deallocate the new resultset
delete(new_res);
}
示例13: pg_result_check
/*
* call-seq:
* res.check -> nil
*
* Raises appropriate exception if PG::Result is in a bad state.
*/
VALUE
pg_result_check( VALUE self )
{
VALUE error, exception, klass;
VALUE rb_pgconn = rb_iv_get( self, "@connection" );
PGconn *conn = pg_get_pgconn(rb_pgconn);
PGresult *result;
#ifdef M17N_SUPPORTED
rb_encoding *enc = pg_conn_enc_get( conn );
#endif
char * sqlstate;
Data_Get_Struct(self, PGresult, result);
if(result == NULL)
{
error = rb_str_new2( PQerrorMessage(conn) );
}
else
{
switch (PQresultStatus(result))
{
case PGRES_TUPLES_OK:
case PGRES_COPY_OUT:
case PGRES_COPY_IN:
#ifdef HAVE_CONST_PGRES_COPY_BOTH
case PGRES_COPY_BOTH:
#endif
#ifdef HAVE_CONST_PGRES_SINGLE_TUPLE
case PGRES_SINGLE_TUPLE:
#endif
case PGRES_EMPTY_QUERY:
case PGRES_COMMAND_OK:
return Qnil;
case PGRES_BAD_RESPONSE:
case PGRES_FATAL_ERROR:
case PGRES_NONFATAL_ERROR:
error = rb_str_new2( PQresultErrorMessage(result) );
break;
default:
error = rb_str_new2( "internal error : unknown result status." );
}
}
#ifdef M17N_SUPPORTED
rb_enc_set_index( error, rb_enc_to_index(enc) );
#endif
sqlstate = PQresultErrorField( result, PG_DIAG_SQLSTATE );
klass = lookup_error_class( sqlstate );
exception = rb_exc_new3( klass, error );
rb_iv_set( exception, "@connection", rb_pgconn );
rb_iv_set( exception, "@result", self );
rb_exc_raise( exception );
/* Not reached */
return Qnil;
}
示例14: do_postgres_raise_error
void do_postgres_raise_error(VALUE self, PGresult *result, VALUE query) {
const char *message = PQresultErrorMessage(result);
char *sql_state = PQresultErrorField(result, PG_DIAG_SQLSTATE);
int postgres_errno = MAKE_SQLSTATE(sql_state[0], sql_state[1], sql_state[2], sql_state[3], sql_state[4]);
PQclear(result);
data_objects_raise_error(self, do_postgres_errors, postgres_errno, message, query, rb_str_new2(sql_state));
}
示例15: ReportRemoteError
/*
* ReportRemoteError retrieves various error fields from the a remote result and
* produces an error report at the WARNING level.
*/
void
ReportRemoteError(PGconn *connection, PGresult *result)
{
char *sqlStateString = PQresultErrorField(result, PG_DIAG_SQLSTATE);
char *remoteMessage = PQresultErrorField(result, PG_DIAG_MESSAGE_PRIMARY);
char *nodeName = ConnectionGetOptionValue(connection, "host");
char *nodePort = ConnectionGetOptionValue(connection, "port");
char *errorPrefix = "Connection failed to";
int sqlState = ERRCODE_CONNECTION_FAILURE;
if (sqlStateString != NULL)
{
sqlState = MAKE_SQLSTATE(sqlStateString[0], sqlStateString[1], sqlStateString[2],
sqlStateString[3], sqlStateString[4]);
/* use more specific error prefix for result failures */
if (sqlState != ERRCODE_CONNECTION_FAILURE)
{
errorPrefix = "Bad result from";
}
}
/*
* If the PGresult did not contain a message, the connection may provide a
* suitable top level one. At worst, this is an empty string.
*/
if (remoteMessage == NULL)
{
char *lastNewlineIndex = NULL;
remoteMessage = PQerrorMessage(connection);
lastNewlineIndex = strrchr(remoteMessage, '\n');
/* trim trailing newline, if any */
if (lastNewlineIndex != NULL)
{
*lastNewlineIndex = '\0';
}
}
ereport(WARNING, (errcode(sqlState),
errmsg("%s %s:%s", errorPrefix, nodeName, nodePort),
errdetail("Remote message: %s", remoteMessage)));
}