本文整理汇总了C++中PQresultStatus函数的典型用法代码示例。如果您正苦于以下问题:C++ PQresultStatus函数的具体用法?C++ PQresultStatus怎么用?C++ PQresultStatus使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了PQresultStatus函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: zbx_db_vexecute
//.........这里部分代码省略.........
if (OCI_SUCCESS == err)
{
err = OCIStmtExecute(oracle.svchp, stmthp, oracle.errhp, (ub4)1, (ub4)0,
(CONST OCISnapshot *)NULL, (OCISnapshot *)NULL, OCI_COMMIT_ON_SUCCESS);
if (OCI_SUCCESS == err)
{
ub4 nrows = 0;
err = OCIAttrGet((void *)stmthp, OCI_HTYPE_STMT, (ub4 *)&nrows,
(ub4 *)0, OCI_ATTR_ROW_COUNT, oracle.errhp);
ret = nrows;
}
}
if (OCI_SUCCESS != err)
{
zabbix_errlog(ERR_Z3005, err, zbx_oci_error(err), sql);
ret = (OCI_SERVER_NORMAL == OCI_DBserver_status() ? ZBX_DB_FAIL : ZBX_DB_DOWN);
}
if (NULL != stmthp)
{
(void)OCIHandleFree((dvoid *)stmthp, OCI_HTYPE_STMT);
stmthp = NULL;
}
#elif defined(HAVE_POSTGRESQL)
result = PQexec(conn,sql);
if (NULL == result)
{
zabbix_errlog(ERR_Z3005, 0, "result is NULL", sql);
ret = (CONNECTION_OK == PQstatus(conn) ? ZBX_DB_FAIL : ZBX_DB_DOWN);
}
else if (PGRES_COMMAND_OK != PQresultStatus(result))
{
error = zbx_dsprintf(error, "%s:%s",
PQresStatus(PQresultStatus(result)),
PQresultErrorMessage(result));
zabbix_errlog(ERR_Z3005, 0, error, sql);
zbx_free(error);
ret = (CONNECTION_OK == PQstatus(conn) ? ZBX_DB_FAIL : ZBX_DB_DOWN);
}
if (ZBX_DB_OK == ret)
ret = atoi(PQcmdTuples(result));
PQclear(result);
#elif defined(HAVE_SQLITE3)
if (0 == txn_level && PHP_MUTEX_OK != php_sem_acquire(&sqlite_access))
{
zabbix_log(LOG_LEVEL_CRIT, "ERROR: unable to create lock on SQLite database");
exit(FAIL);
}
lbl_exec:
if (SQLITE_OK != (err = sqlite3_exec(conn, sql, NULL, 0, &error)))
{
if (SQLITE_BUSY == err)
goto lbl_exec;
zabbix_errlog(ERR_Z3005, 0, error, sql);
sqlite3_free(error);
switch (err)
{
case SQLITE_ERROR: /* SQL error or missing database; assuming SQL error, because if we
are this far into execution, zbx_db_connect() was successful */
case SQLITE_NOMEM: /* A malloc() failed */
case SQLITE_TOOBIG: /* String or BLOB exceeds size limit */
case SQLITE_CONSTRAINT: /* Abort due to constraint violation */
case SQLITE_MISMATCH: /* Data type mismatch */
ret = ZBX_DB_FAIL;
break;
default:
ret = ZBX_DB_DOWN;
break;
}
}
if (ZBX_DB_OK == ret)
ret = sqlite3_changes(conn);
if (0 == txn_level)
php_sem_release(&sqlite_access);
#endif /* HAVE_SQLITE3 */
if (CONFIG_LOG_SLOW_QUERIES)
{
sec = zbx_time() - sec;
if (sec > (double)CONFIG_LOG_SLOW_QUERIES / 1000.0)
zabbix_log(LOG_LEVEL_WARNING, "slow query: " ZBX_FS_DBL " sec, \"%s\"", sec, sql);
}
return ret;
}
示例2: hb_main
int
hb_main(const char* conninfo)
{
PGconn *conn;
PGresult *res;
PGnotify *notify;
int nnotifies;
int checked;
int result;
char number[5];
char notify_buf[1024];
srand((unsigned)time(NULL));
/* Make a connection to the database */
conn = PQconnectdb(conninfo);
/* Check to see that the backend connection was successfully made */
if (PQstatus(conn) != CONNECTION_OK)
{
elog(WARNING, "Connection to database failed: %s",
PQerrorMessage(conn));
exit_nicely(conn);
}
/*
* Issue LISTEN command to enable notifications from the rule's NOTIFY.
*/
res = PQexec(conn, "LISTEN HB_SV");
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
elog(WARNING, "LISTEN command failed: %s", PQerrorMessage(conn));
PQclear(res);
exit_nicely(conn);
}
/*
* should PQclear PGresult whenever it is no longer needed to avoid memory
* leaks
*/
PQclear(res);
/* Set Secret Number */
memset(number, 0x00, 5);
create_random_number(number);
elog(LOG , "hb_worker: set secret number=%s\n", number);
/* Quit after four notifies are received. */
nnotifies = 0;
while (1)
{
/*
* Sleep until something happens on the connection. We use select(2)
* to wait for input, but you could also use poll() or similar
* facilities.
*/
int sock;
fd_set input_mask;
sock = PQsocket(conn);
if (sock < 0)
break; /* shouldn't happen */
FD_ZERO(&input_mask);
FD_SET(sock, &input_mask);
if (select(sock + 1, &input_mask, NULL, NULL, NULL) < 0)
{
elog(WARNING, "select() failed: %s\n", strerror(errno));
exit_nicely(conn);
}
/* Now check for input */
PQconsumeInput(conn);
while ((notify = PQnotifies(conn)) != NULL)
{
checked = check_number(notify->extra);
switch (checked) {
case NUMBER_COMMAND:
result = compare_numbers(number, notify->extra);
if (GET_HITS(result) == 4) {
// Notify Game Clear, and Set new number.
elog(LOG, "hb_worker: NOTIFY HB_CL,'4 Hit! Conguratulatoins!, next new game.'\n");
strcpy(notify_buf, "NOTIFY HB_CL,'4 Hit! Conguratulatoins!, next new game.'");
PQexec(conn, notify_buf);
create_random_number(number);
elog(LOG, "hb_worker: set secret number=%s\n", number);
} else {
// Notify Hit&blow
elog(LOG, "NOTIFY HB_CL,'%d Hit / %d Blow.'",
GET_HITS(result), GET_BLOWS(result));
sprintf(notify_buf, "NOTIFY HB_CL,'%d Hit / %d Blow.'",
GET_HITS(result), GET_BLOWS(result));
PQexec(conn, notify_buf);
}
break;
case START_COMMAND:
//.........这里部分代码省略.........
示例3: pgsql_query_checkstatus
/* Complex checking of the query status */
int
pgsql_query_checkstatus(PGresult *res)
{
int status;
int nFields;
long long nTuples;
/*
For successful queries, there are two options: either the query returns
results or not. If it does not return data, but successfully completed,
then the status will be set to
PGRES_COMMAND_OK.
If success and returns tuples, then it will be set to
PGRES_TUPLES_OK
*/
status = PQresultStatus(res);
if (PQresultStatus(res) == PGRES_COMMAND_OK)
{
/* Success but no results */
IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, PQcmdStatus(res));
return(MYPG_NO_RESULT);
}
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO,
PQresultErrorMessage(res));
switch (status)
{
/* No results */
case PGRES_COPY_IN: return(MYPG_NO_RESULT);
case PGRES_COPY_OUT: return(MYPG_NO_RESULT);
case PGRES_BAD_RESPONSE: return(MYPG_NO_RESULT);
case PGRES_NONFATAL_ERROR: return(MYPG_NO_RESULT);
/* An error */
case PGRES_FATAL_ERROR: return(MYPG_FATAL_ERROR);
default: break;
}
}
/* How many fields and rows did we return? */
nFields = PQnfields(res);
nTuples = PQntuples(res);
/* Result is empty, either an error or this query returns no data */
if (nTuples == 0)
{
if (nFields == 0)
{
/* Query returns no data. Try to print a message to clarify */
IDL_Message(IDL_M_NAMED_GENERIC, IDL_MSG_INFO, PQcmdStatus(res));
return(MYPG_NO_RESULT);
}
else
/* Probably nothing matched the query */
return(MYPG_NO_RESULT);
}
return(MYPG_SUCCESS);
}
示例4: libpqrcv_connect
/*
* Establish the connection to the primary server for XLOG streaming
*/
static bool
libpqrcv_connect(char *conninfo, XLogRecPtr startpoint)
{
char conninfo_repl[MAXCONNINFO + 18];
char *primary_sysid;
char standby_sysid[32];
TimeLineID primary_tli;
TimeLineID standby_tli;
PGresult *res;
char cmd[64];
/* Connect using deliberately undocumented parameter: replication */
snprintf(conninfo_repl, sizeof(conninfo_repl), "%s replication=true", conninfo);
streamConn = PQconnectdb(conninfo_repl);
if (PQstatus(streamConn) != CONNECTION_OK)
ereport(ERROR,
(errmsg("could not connect to the primary server: %s",
PQerrorMessage(streamConn))));
/*
* Get the system identifier and timeline ID as a DataRow message from the
* primary server.
*/
res = libpqrcv_PQexec("IDENTIFY_SYSTEM");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
PQclear(res);
ereport(ERROR,
(errmsg("could not receive database system identifier and timeline ID from "
"the primary server: %s",
PQerrorMessage(streamConn))));
}
if (PQnfields(res) != 2 || PQntuples(res) != 1)
{
int ntuples = PQntuples(res);
int nfields = PQnfields(res);
PQclear(res);
ereport(ERROR,
(errmsg("invalid response from primary server"),
errdetail("Expected 1 tuple with 2 fields, got %d tuples with %d fields.",
ntuples, nfields)));
}
primary_sysid = PQgetvalue(res, 0, 0);
primary_tli = pg_atoi(PQgetvalue(res, 0, 1), 4, 0);
/*
* Confirm that the system identifier of the primary is the same as ours.
*/
snprintf(standby_sysid, sizeof(standby_sysid), UINT64_FORMAT,
GetSystemIdentifier());
if (strcmp(primary_sysid, standby_sysid) != 0)
{
PQclear(res);
ereport(ERROR,
(errmsg("database system identifier differs between the primary and standby"),
errdetail("The primary's identifier is %s, the standby's identifier is %s.",
primary_sysid, standby_sysid)));
}
/*
* Confirm that the current timeline of the primary is the same as the
* recovery target timeline.
*/
standby_tli = GetRecoveryTargetTLI();
PQclear(res);
if (primary_tli != standby_tli)
ereport(ERROR,
(errmsg("timeline %u of the primary does not match recovery target timeline %u",
primary_tli, standby_tli)));
ThisTimeLineID = primary_tli;
/* Start streaming from the point requested by startup process */
snprintf(cmd, sizeof(cmd), "START_REPLICATION %X/%X",
startpoint.xlogid, startpoint.xrecoff);
res = libpqrcv_PQexec(cmd);
if (PQresultStatus(res) != PGRES_COPY_OUT)
{
PQclear(res);
ereport(ERROR,
(errmsg("could not start WAL streaming: %s",
PQerrorMessage(streamConn))));
}
PQclear(res);
justconnected = true;
ereport(LOG,
(errmsg("streaming replication successfully connected to primary")));
return true;
}
示例5: run_permutation
/*
* Run one permutation
*/
static void
run_permutation(TestSpec *testspec, int nsteps, Step **steps)
{
PGresult *res;
int i;
printf("\nstarting permutation:");
for (i = 0; i < nsteps; i++)
printf(" %s", steps[i]->name);
printf("\n");
/* Perform setup */
if (testspec->setupsql)
{
res = PQexec(conns[0], testspec->setupsql);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "setup failed: %s", PQerrorMessage(conns[0]));
exit_nicely();
}
PQclear(res);
}
/* Perform per-session setup */
for (i = 0; i < testspec->nsessions; i++)
{
if (testspec->sessions[i]->setupsql)
{
res = PQexec(conns[i], testspec->sessions[i]->setupsql);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "setup of session %s failed: %s",
testspec->sessions[i]->name,
PQerrorMessage(conns[0]));
exit_nicely();
}
PQclear(res);
}
}
/* Perform steps */
for (i = 0; i < nsteps; i++)
{
Step *step = steps[i];
printf("step %s: %s\n", step->name, step->sql);
res = PQexec(conns[step->session], step->sql);
switch(PQresultStatus(res))
{
case PGRES_COMMAND_OK:
break;
case PGRES_TUPLES_OK:
printResultSet(res);
break;
case PGRES_FATAL_ERROR:
/* Detail may contain xid values, so just show primary. */
printf("%s: %s\n", PQresultErrorField(res, PG_DIAG_SEVERITY),
PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY));
break;
default:
printf("unexpected result status: %s\n",
PQresStatus(PQresultStatus(res)));
}
PQclear(res);
}
/* Perform per-session teardown */
for (i = 0; i < testspec->nsessions; i++)
{
if (testspec->sessions[i]->teardownsql)
{
res = PQexec(conns[i], testspec->sessions[i]->teardownsql);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "teardown of session %s failed: %s",
testspec->sessions[i]->name,
PQerrorMessage(conns[0]));
/* don't exit on teardown failure */
}
PQclear(res);
}
}
/* Perform teardown */
if (testspec->teardownsql)
{
res = PQexec(conns[0], testspec->teardownsql);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
fprintf(stderr, "teardown failed: %s",
PQerrorMessage(conns[0]));
/* don't exit on teardown failure */
}
//.........这里部分代码省略.........
示例6: ows_psql_gml_to_sql
/*
* Transform a GML geometry to PostGIS EWKT
* Return NULL on error
*/
buffer * ows_psql_gml_to_sql(ows * o, xmlNodePtr n, int srid)
{
PGresult *res;
xmlNodePtr g;
buffer *result, *sql, *gml;
assert(o);
assert(n);
g = ows_psql_recursive_parse_gml(o, n, NULL);
if (!g) return NULL; /* No Geometry founded in GML doc */
/* Retrieve the sub doc and launch GML parse via PostGIS */
gml = buffer_init();
cgi_add_xml_into_buffer(gml, g);
sql = buffer_init();
buffer_add_str(sql, "SELECT ST_GeomFromGML('");
buffer_add_str(sql, gml->buf);
if (ows_version_get(o->postgis_version) >= 200) {
buffer_add_str(sql, "',");
buffer_add_int(sql, srid);
buffer_add_str(sql, ")");
} else {
/* Means PostGIS 1.5 */
buffer_add_str(sql, "')");
}
res = ows_psql_exec(o, sql->buf);
buffer_free(gml);
/* GML Parse errors cases */
if (PQresultStatus(res) != PGRES_TUPLES_OK || PQntuples(res) != 1) {
buffer_free(sql);
PQclear(res);
return NULL;
}
result = buffer_init();
buffer_add_str(result, PQgetvalue(res, 0, 0));
PQclear(res);
/* Check if geometry is valid */
if (o->check_valid_geom) {
buffer_empty(sql);
buffer_add_str(sql, "SELECT ST_IsValid('");
buffer_add_str(sql, result->buf);
buffer_add_str(sql, "')");
res = ows_psql_exec(o, sql->buf);
if ( PQresultStatus(res) != PGRES_TUPLES_OK
|| PQntuples(res) != 1
|| (char) PQgetvalue(res, 0, 0)[0] != 't') {
buffer_free(sql);
buffer_free(result);
PQclear(res);
return NULL;
}
PQclear(res);
}
buffer_free(sql);
return result;
}
示例7: GDALGetDataTypeSize
/*****************************************************
* \brief Read a natural block of raster band data
*****************************************************/
CPLErr PostGISRasterTileRasterBand::IReadBlock(CPL_UNUSED int nBlockXOff,
CPL_UNUSED int nBlockYOff,
void * pImage)
{
CPLString osCommand;
PGresult * poResult = NULL;
int nWKBLength = 0;
int nPixelSize = GDALGetDataTypeSize(eDataType)/8;
PostGISRasterTileDataset * poRTDS =
(PostGISRasterTileDataset *)poDS;
// Get by PKID
if (poRTDS->poRDS->pszPrimaryKeyName) {
//osCommand.Printf("select ST_AsBinary(st_band(%s, %d),TRUE) from %s.%s where "
osCommand.Printf("select st_band(%s, %d) from %s.%s where "
"%s = '%s'", poRTDS->poRDS->pszColumn, nBand, poRTDS->poRDS->pszSchema, poRTDS->poRDS->pszTable,
poRTDS->poRDS->pszPrimaryKeyName, poRTDS->pszPKID);
}
// Get by upperleft
else {
osCommand.Printf("select st_band(%s, %d) from %s.%s where "
"abs(ST_UpperLeftX(%s) - %.8f) < 1e-8 and abs(ST_UpperLeftY(%s) - %.8f) < 1e-8",
poRTDS->poRDS->pszColumn, nBand, poRTDS->poRDS->pszSchema, poRTDS->poRDS->pszTable, poRTDS->poRDS->pszColumn,
poRTDS->adfGeoTransform[GEOTRSFRM_TOPLEFT_X], poRTDS->poRDS->pszColumn,
poRTDS->adfGeoTransform[GEOTRSFRM_TOPLEFT_Y]);
}
poResult = PQexec(poRTDS->poRDS->poConn, osCommand.c_str());
#ifdef DEBUG_QUERY
CPLDebug("PostGIS_Raster", "PostGISRasterTileRasterBand::IReadBlock(): "
"Query = \"%s\" --> number of rows = %d",
osCommand.c_str(), poResult ? PQntuples(poResult) : 0 );
#endif
if (poResult == NULL ||
PQresultStatus(poResult) != PGRES_TUPLES_OK ||
PQntuples(poResult) <= 0) {
if (poResult)
PQclear(poResult);
ReportError(CE_Failure, CPLE_AppDefined,
"Error getting block of data (upperpixel = %f, %f)",
poRTDS->adfGeoTransform[GEOTRSFRM_TOPLEFT_X],
poRTDS->adfGeoTransform[GEOTRSFRM_TOPLEFT_Y]);
return CE_Failure;
}
// TODO: Check this
if (bIsOffline) {
CPLError(CE_Failure, CPLE_AppDefined, "This raster has outdb "
"storage. This feature isn't still available");
PQclear(poResult);
return CE_Failure;
}
/* Copy only data size, without payload */
int nExpectedDataSize =
nBlockXSize * nBlockYSize * nPixelSize;
GByte * pbyData = CPLHexToBinary(PQgetvalue(poResult, 0, 0),
&nWKBLength);
int nExpectedWKBLength = RASTER_HEADER_SIZE + BAND_SIZE(nPixelSize, nExpectedDataSize);
CPLErr eRet = CE_None;
if( nWKBLength != nExpectedWKBLength )
{
CPLDebug("PostGIS_Raster", "nWKBLength=%d, nExpectedWKBLength=%d", nWKBLength, nExpectedWKBLength );
eRet = CE_Failure;
}
else
{
GByte * pbyDataToRead =
(GByte*)GET_BAND_DATA(pbyData,1, nPixelSize,
nExpectedDataSize);
// Do byte-swapping if necessary */
int bIsLittleEndian = (pbyData[0] == 1);
#ifdef CPL_LSB
int bSwap = !bIsLittleEndian;
#else
int bSwap = bIsLittleEndian;
#endif
if( bSwap && nPixelSize > 1 )
{
GDALSwapWords( pbyDataToRead, nPixelSize,
nBlockXSize * nBlockYSize,
nPixelSize );
}
//.........这里部分代码省略.........
示例8: return
int pgQueryThread::execute()
{
rowsInserted = -1L;
if (!conn->conn)
return(raiseEvent(0));
wxCharBuffer queryBuf = query.mb_str(*conn->conv);
if (!queryBuf && !query.IsEmpty())
{
conn->SetLastResultError(NULL, _("The query could not be converted to the required encoding."));
return(raiseEvent(0));
}
if (!PQsendQuery(conn->conn, queryBuf))
{
conn->SetLastResultError(NULL);
conn->IsAlive();
return(raiseEvent(0));
}
int resultsRetrieved = 0;
PGresult *lastResult = 0;
while (true)
{
if (TestDestroy())
{
if (rc != -3)
{
if (!PQrequestCancel(conn->conn)) // could not abort; abort failed.
return(raiseEvent(-1));
rc = -3;
}
}
if (!PQconsumeInput(conn->conn))
return(raiseEvent(0));
if (PQisBusy(conn->conn))
{
Yield();
this->Sleep(10);
continue;
}
// If resultToRetrieve is given, the nth result will be returned,
// otherwise the last result set will be returned.
// all others are discarded
PGresult *res = PQgetResult(conn->conn);
if (!res)
break;
resultsRetrieved++;
if (resultsRetrieved == resultToRetrieve)
{
result = res;
insertedOid = PQoidValue(res);
if (insertedOid && insertedOid != (OID) - 1)
appendMessage(wxString::Format(_("Query inserted one row with OID %d.\n"), insertedOid));
else
appendMessage(wxString::Format(wxPLURAL("Query result with %d row will be returned.\n", "Query result with %d rows will be returned.\n",
PQntuples(result)), PQntuples(result)));
continue;
}
if (lastResult)
{
if (PQntuples(lastResult))
appendMessage(wxString::Format(wxPLURAL("Query result with %d row discarded.\n", "Query result with %d rows discarded.\n",
PQntuples(lastResult)), PQntuples(lastResult)));
PQclear(lastResult);
}
lastResult = res;
}
if (!result)
result = lastResult;
conn->SetLastResultError(result);
appendMessage(wxT("\n"));
rc = PQresultStatus(result);
insertedOid = PQoidValue(result);
if (insertedOid == (OID) - 1)
insertedOid = 0;
if (rc == PGRES_TUPLES_OK)
{
dataSet = new pgSet(result, conn, *conn->conv, conn->needColQuoting);
dataSet->MoveFirst();
}
else if (rc == PGRES_COMMAND_OK)
{
char *s = PQcmdTuples(result);
if (*s)
rowsInserted = atol(s);
}
else if (rc == PGRES_FATAL_ERROR)
{
appendMessage(conn->GetLastError() + wxT("\n"));
}
return(raiseEvent(1));
//.........这里部分代码省略.........
示例9: guard
KCSQLResult* PgSQLConnection::ExecuteQueryEx(
const char* queryStr,
long iTimeout,
int type
)
{
CMutexGuard guard(m_MutexQuery);
if (m_iAutoConnect)
{
if (m_conn)
{
if ( PQstatus(m_conn) == CONNECTION_BAD )
{
PQreset(m_conn);
// 重试一次不行,就关闭该连接
if (PQstatus(m_conn) == CONNECTION_BAD)
{
PQfinish(m_conn);
m_conn = NULL;
}
}
}
if (m_conn == NULL)
{
if ( !Connect(iTimeout) )
{
return new PgSQLResult(-1, "连接出错");
}
}
}
if (m_conn == NULL)
{
return new PgSQLResult(-1, "无数据库连接");
}
PGresult* result = PQexec(m_conn, queryStr);
if (result == NULL)
{
// 重试
Connect(iTimeout);
result = PQexec(m_conn, queryStr);
}
if (result == NULL)
{
return NULL;
}
ExecStatusType resultInfo = PQresultStatus(result);
m_errorCode = resultInfo;
switch (resultInfo)
{
case PGRES_COMMAND_OK:
// 仅记录影响行数
return new PgSQLResult( PQcmdTuples(result) ? atoi(PQcmdTuples(result)) : 0 );
break;
case PGRES_TUPLES_OK:
return new PgSQLResult(this, result);
break;
default:
return new PgSQLResult(resultInfo, PQresultErrorMessage(result));
break;
}
}
示例10: PQconnectdb
void Plugin::saveValue(const char *tagname, const char *tablename, const char *valstring)
{
#ifdef WITHDB
PGconn *conn = NULL;
TObject *dummy=getMemoryObject("Runnumber");
int runnumber=htonl(*(int*) &dummy); //ugly but works
// Make a connection to the database
conn = PQconnectdb("user=runinfo password=runinfo dbname=runinfo host=CookerWorkerNodeLNS00");
// Check to see that the backend connection was successfully made
if (PQstatus(conn) != CONNECTION_OK)
{
debug(0,"Connection to database failed\n");
PQfinish(conn);
return ;
}
const char *params[]={tagname,(char *) &runnumber,valstring};
int lengths[3]={strlen(tagname),sizeof(runnumber),strlen(valstring)};
int binary[3]={0,1,0};
debug(100,"Connection to database - OK\n");
PGresult *res=PQexecParams(conn, "select tagid from tagnames where tagname=$1::varchar;",1,NULL,params,lengths,binary,0);
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
debug(0, "select tagid command failed: %s\n", PQerrorMessage(conn));
PQclear(res);
PQfinish(conn);
return;
}
debug(100,"Found %i\n",PQntuples(res));
if (PQntuples(res)==0)
{
PQclear(res);
debug(100,"Trying to insert\n");
res=PQexecParams(conn, "insert into tagnames (tagname) values ($1::varchar);",1,NULL,params,lengths,binary,0);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
debug(0, "insert into tagid command failed: %s\n", PQerrorMessage(conn));
PQclear(res);
PQfinish(conn);
return;
}
}
PQclear(res);
debug(100,"Trying to insert\n");
char command[1000];
sprintf(command,"insert into %s (timestamp,runid,tagid,value) select now(),$2::int4,tagid,$3::double precision from tagnames where tagname=$1::varchar;",tablename);
res=PQexecParams(conn, command,3,NULL,params,lengths,binary,0);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
debug(100, "insert into rundata command failed: %s\n Trying update", PQerrorMessage(conn));
PQclear(res);
sprintf(command,"update %s set timestamp=now(), value=$3::double precision where runid=$2::int4 and tagid=(select tagid from tagnames where tagname=$1::varchar);",tablename);
res=PQexecParams(conn, command,3,NULL,params,lengths,binary,0);
if (PQresultStatus(res) != PGRES_COMMAND_OK)
{
debug(100, "update failed too: %s\n", PQerrorMessage(conn));
PQclear(res);
PQfinish(conn);
return;
}
}
PQclear(res);
PQfinish(conn);
#else
debug(10000,"DB not compiled in\n");
#endif
}
示例11: zbx_db_vselect
//.........这里部分代码省略.........
}
col_width++;
result->values[counter - 1] = zbx_malloc(NULL, col_width);
memset(result->values[counter - 1], 0, col_width);
if (OCI_SUCCESS == err)
{
/* represent any data as characters */
err = OCIDefineByPos(result->stmthp, &defnp, oracle.errhp, counter,
(dvoid *)result->values[counter - 1], col_width, SQLT_STR,
(dvoid *)0, (ub2 *)0, (ub2 *)0, OCI_DEFAULT);
}
/* free cell descriptor */
OCIDescriptorFree(parmdp, OCI_DTYPE_PARAM);
parmdp = NULL;
}
error:
if (OCI_SUCCESS != err)
{
zabbix_errlog(ERR_Z3005, err, zbx_oci_error(err), sql);
OCI_DBfree_result(result);
result = (OCI_SERVER_NORMAL == OCI_DBserver_status() ? NULL : (DB_RESULT)ZBX_DB_DOWN);
}
#elif defined(HAVE_POSTGRESQL)
result = zbx_malloc(NULL, sizeof(ZBX_PG_DB_RESULT));
result->pg_result = PQexec(conn, sql);
result->values = NULL;
result->cursor = 0;
result->row_num = 0;
if (NULL == result->pg_result)
zabbix_errlog(ERR_Z3005, 0, "result is NULL", sql);
if (PGRES_TUPLES_OK != PQresultStatus(result->pg_result))
{
error = zbx_dsprintf(error, "%s:%s",
PQresStatus(PQresultStatus(result->pg_result)),
PQresultErrorMessage(result->pg_result));
zabbix_errlog(ERR_Z3005, 0, error, sql);
zbx_free(error);
PG_DBfree_result(result);
result = (CONNECTION_OK == PQstatus(conn) ? NULL : (DB_RESULT)ZBX_DB_DOWN);
}
else /* init rownum */
result->row_num = PQntuples(result->pg_result);
#elif defined(HAVE_SQLITE3)
if (0 == txn_level && PHP_MUTEX_OK != php_sem_acquire(&sqlite_access))
{
zabbix_log(LOG_LEVEL_CRIT, "ERROR: unable to create lock on SQLite database");
exit(FAIL);
}
result = zbx_malloc(NULL, sizeof(ZBX_SQ_DB_RESULT));
result->curow = 0;
lbl_get_table:
if (SQLITE_OK != (ret = sqlite3_get_table(conn,sql, &result->data, &result->nrow, &result->ncolumn, &error)))
{
if (SQLITE_BUSY == ret)
goto lbl_get_table;
zabbix_errlog(ERR_Z3005, 0, error, sql);
sqlite3_free(error);
SQ_DBfree_result(result);
switch (ret)
{
case SQLITE_ERROR: /* SQL error or missing database; assuming SQL error, because if we
are this far into execution, zbx_db_connect() was successful */
case SQLITE_NOMEM: /* a malloc() failed */
case SQLITE_MISMATCH: /* data type mismatch */
result = NULL;
break;
default:
result = (DB_RESULT)ZBX_DB_DOWN;
break;
}
}
if (0 == txn_level)
php_sem_release(&sqlite_access);
#endif /* HAVE_SQLITE3 */
if (CONFIG_LOG_SLOW_QUERIES)
{
sec = zbx_time() - sec;
if (sec > (double)CONFIG_LOG_SLOW_QUERIES / 1000.0)
zabbix_log(LOG_LEVEL_WARNING, "slow query: " ZBX_FS_DBL " sec, \"%s\"", sec, sql);
}
zbx_free(sql);
return result;
}
示例12: sql_query
/*************************************************************************
*
* Function: sql_query
*
* Purpose: Issue a query to the database
*
*************************************************************************/
static sql_rcode_t sql_query(rlm_sql_handle_t * handle, UNUSED rlm_sql_config_t *config, char const *query)
{
rlm_sql_postgres_conn_t *conn = handle->conn;
int numfields = 0;
char *errorcode;
char *errormsg;
if (!conn->db) {
ERROR("rlm_sql_postgresql: Socket not connected");
return RLM_SQL_RECONNECT;
}
conn->result = PQexec(conn->db, query);
/*
* Returns a PGresult pointer or possibly a null pointer.
* A non-null pointer will generally be returned except in
* out-of-memory conditions or serious errors such as inability
* to send the command to the server. If a null pointer is
* returned, it should be treated like a PGRES_FATAL_ERROR
* result.
*/
if (!conn->result)
{
ERROR("rlm_sql_postgresql: PostgreSQL Query failed Error: %s",
PQerrorMessage(conn->db));
/* As this error COULD be a connection error OR an out-of-memory
* condition return value WILL be wrong SOME of the time regardless!
* Pick your poison....
*/
return RLM_SQL_RECONNECT;
} else {
ExecStatusType status = PQresultStatus(conn->result);
DEBUG("rlm_sql_postgresql: Status: %s", PQresStatus(status));
switch (status){
case PGRES_COMMAND_OK:
/*Successful completion of a command returning no data.*/
/*affected_rows function only returns
the number of affected rows of a command
returning no data...
*/
conn->affected_rows = affected_rows(conn->result);
DEBUG("rlm_sql_postgresql: query affected rows = %i", conn->affected_rows);
return 0;
break;
case PGRES_TUPLES_OK:
/*Successful completion of a command returning data (such as a SELECT or SHOW).*/
conn->cur_row = 0;
conn->affected_rows = PQntuples(conn->result);
numfields = PQnfields(conn->result); /*Check row storing functions..*/
DEBUG("rlm_sql_postgresql: query affected rows = %i , fields = %i", conn->affected_rows, numfields);
return 0;
break;
case PGRES_BAD_RESPONSE:
/*The server's response was not understood.*/
DEBUG("rlm_sql_postgresql: Bad Response From Server!!");
return -1;
break;
case PGRES_NONFATAL_ERROR:
/*A nonfatal error (a notice or warning) occurred. Possibly never returns*/
return -1;
break;
case PGRES_FATAL_ERROR:
#if defined(PG_DIAG_SQLSTATE) && defined(PG_DIAG_MESSAGE_PRIMARY)
/*A fatal error occurred.*/
errorcode = PQresultErrorField(conn->result, PG_DIAG_SQLSTATE);
errormsg = PQresultErrorField(conn->result, PG_DIAG_MESSAGE_PRIMARY);
DEBUG("rlm_sql_postgresql: Error %s", errormsg);
return check_fatal_error(errorcode);
#endif
break;
default:
/* FIXME: An unhandled error occurred.*/
/* PGRES_EMPTY_QUERY PGRES_COPY_OUT PGRES_COPY_IN */
return -1;
//.........这里部分代码省略.........
示例13: ExecQueryUsingCursor
/*
* ExecQueryUsingCursor: run a SELECT-like query using a cursor
*
* This feature allows result sets larger than RAM to be dealt with.
*
* Returns true if the query executed successfully, false otherwise.
*
* If pset.timing is on, total query time (exclusive of result-printing) is
* stored into *elapsed_msec.
*/
static bool
ExecQueryUsingCursor(const char *query, double *elapsed_msec)
{
bool OK = true;
PGresult *results;
PQExpBufferData buf;
printQueryOpt my_popt = pset.popt;
FILE *queryFout_copy = pset.queryFout;
bool queryFoutPipe_copy = pset.queryFoutPipe;
bool started_txn = false;
bool did_pager = false;
int ntuples;
int fetch_count;
char fetch_cmd[64];
instr_time before,
after;
int flush_error;
*elapsed_msec = 0;
/* initialize print options for partial table output */
my_popt.topt.start_table = true;
my_popt.topt.stop_table = false;
my_popt.topt.prior_records = 0;
if (pset.timing)
INSTR_TIME_SET_CURRENT(before);
/* if we're not in a transaction, start one */
if (PQtransactionStatus(pset.db) == PQTRANS_IDLE)
{
results = PQexec(pset.db, "BEGIN");
OK = AcceptResult(results) &&
(PQresultStatus(results) == PGRES_COMMAND_OK);
PQclear(results);
if (!OK)
return false;
started_txn = true;
}
/* Send DECLARE CURSOR */
initPQExpBuffer(&buf);
appendPQExpBuffer(&buf, "DECLARE _psql_cursor NO SCROLL CURSOR FOR\n%s",
query);
results = PQexec(pset.db, buf.data);
OK = AcceptResult(results) &&
(PQresultStatus(results) == PGRES_COMMAND_OK);
PQclear(results);
termPQExpBuffer(&buf);
if (!OK)
goto cleanup;
if (pset.timing)
{
INSTR_TIME_SET_CURRENT(after);
INSTR_TIME_SUBTRACT(after, before);
*elapsed_msec += INSTR_TIME_GET_MILLISEC(after);
}
/*
* In \gset mode, we force the fetch count to be 2, so that we will throw
* the appropriate error if the query returns more than one row.
*/
if (pset.gset_prefix)
fetch_count = 2;
else
fetch_count = pset.fetch_count;
snprintf(fetch_cmd, sizeof(fetch_cmd),
"FETCH FORWARD %d FROM _psql_cursor",
fetch_count);
/* prepare to write output to \g argument, if any */
if (pset.gfname)
{
/* keep this code in sync with PrintQueryTuples */
pset.queryFout = stdout; /* so it doesn't get closed */
/* open file/pipe */
if (!setQFout(pset.gfname))
{
pset.queryFout = queryFout_copy;
pset.queryFoutPipe = queryFoutPipe_copy;
OK = false;
goto cleanup;
}
}
/* clear any pre-existing error indication on the output stream */
//.........这里部分代码省略.........
示例14: assert
/*
* Generate a new buffer id supposed to be unique for a given layer name
*/
buffer *ows_psql_generate_id(ows * o, buffer * layer_name)
{
ows_layer_node *ln;
buffer * id, *sql_id;
FILE *fp;
PGresult * res;
int i, seed_len;
char * seed = NULL;
assert(o);
assert(o->layers);
assert(layer_name);
/* Retrieve layer node pointer */
for (ln = o->layers->first ; ln ; ln = ln->next) {
if (ln->layer->name && ln->layer->storage
&& !strcmp(ln->layer->name->buf, layer_name->buf)) break;
}
assert(ln);
id = buffer_init();
/* If PK have a sequence in PostgreSQL database,
* retrieve next available sequence value
*/
if (ln->layer->storage->pkey_sequence) {
sql_id = buffer_init();
buffer_add_str(sql_id, "SELECT nextval('");
buffer_copy(sql_id, ln->layer->storage->pkey_sequence);
buffer_add_str(sql_id, "');");
res = ows_psql_exec(o, sql_id->buf);
buffer_free(sql_id);
if (PQresultStatus(res) == PGRES_TUPLES_OK && PQntuples(res) == 1) {
buffer_add_str(id, (char *) PQgetvalue(res, 0, 0));
PQclear(res);
return id;
}
/* FIXME: Shouldn't we return an error there instead ? */
PQclear(res);
}
/*
* If we don't have a PostgreSQL Sequence, we will try to
* generate a pseudo random keystring using /dev/urandom
* Will so work only on somes/commons Unix system
*/
seed_len = 6;
seed = malloc(sizeof(char) * (seed_len * 3 + 1)); /* multiply by 3 to be able to deal
with hex2dec conversion */
assert(seed);
seed[0] = '\0';
fp = fopen("/dev/urandom","r");
if (fp) {
for (i=0 ; i<seed_len ; i++)
sprintf(seed,"%s%03d", seed, fgetc(fp));
fclose(fp);
buffer_add_str(id, seed);
free(seed);
return id;
}
free(seed);
/* Case where we not using PostgreSQL sequence,
* and OS don't have a /dev/urandom support
* This case don't prevent to produce ID collision
* Don't use it unless really no others choices !!!
*/
srand((int) (time(NULL) ^ rand() % 1000) + 42);
srand((rand() % 1000 ^ rand() % 1000) + 42);
buffer_add_int(id, rand());
return id;
}
示例15: PSQLexecWatch
/*
* PSQLexecWatch
*
* This function is used for \watch command to send the query to
* the server and print out the results.
*
* Returns 1 if the query executed successfully, 0 if it cannot be repeated,
* e.g., because of the interrupt, -1 on error.
*/
int
PSQLexecWatch(const char *query, const printQueryOpt *opt)
{
PGresult *res;
double elapsed_msec = 0;
instr_time before;
instr_time after;
if (!pset.db)
{
psql_error("You are currently not connected to a database.\n");
return 0;
}
SetCancelConn();
if (pset.timing)
INSTR_TIME_SET_CURRENT(before);
res = PQexec(pset.db, query);
ResetCancelConn();
if (!AcceptResult(res))
{
PQclear(res);
return 0;
}
if (pset.timing)
{
INSTR_TIME_SET_CURRENT(after);
INSTR_TIME_SUBTRACT(after, before);
elapsed_msec = INSTR_TIME_GET_MILLISEC(after);
}
/*
* If SIGINT is sent while the query is processing, the interrupt
* will be consumed. The user's intention, though, is to cancel
* the entire watch process, so detect a sent cancellation request and
* exit in this case.
*/
if (cancel_pressed)
{
PQclear(res);
return 0;
}
switch (PQresultStatus(res))
{
case PGRES_TUPLES_OK:
printQuery(res, opt, pset.queryFout, pset.logfile);
break;
case PGRES_COMMAND_OK:
fprintf(pset.queryFout, "%s\n%s\n\n", opt->title, PQcmdStatus(res));
break;
case PGRES_EMPTY_QUERY:
psql_error(_("\\watch cannot be used with an empty query\n"));
PQclear(res);
return -1;
case PGRES_COPY_OUT:
case PGRES_COPY_IN:
case PGRES_COPY_BOTH:
psql_error(_("\\watch cannot be used with COPY\n"));
PQclear(res);
return -1;
default:
psql_error(_("unexpected result status for \\watch\n"));
PQclear(res);
return -1;
}
PQclear(res);
fflush(pset.queryFout);
/* Possible microtiming output */
if (pset.timing)
printf(_("Time: %.3f ms\n"), elapsed_msec);
return 1;
}