bool pgConn::ExecuteVoid(const wxString &sql, bool reportError) { if (GetStatus() != PGCONN_OK) return false; // Execute the query and get the status. PGresult *qryRes; wxLogSql(wxT("Void query (%s:%d): %s"), this->GetHost().c_str(), this->GetPort(), sql.c_str()); SetConnCancel(); qryRes = PQexec(conn, sql.mb_str(*conv)); ResetConnCancel(); lastResultStatus = PQresultStatus(qryRes); SetLastResultError(qryRes); // Check for errors if (lastResultStatus != PGRES_TUPLES_OK && lastResultStatus != PGRES_COMMAND_OK) { LogError(!reportError); PQclear(qryRes); return false; } // Cleanup & exit PQclear(qryRes); return true; }
PGresult *dbgPgConn::waitForCommand( const wxString &command ) { wxLogSql(wxT("%s"), command.c_str()); PGresult *result = PQexec( m_pgConn, command.mb_str( wxConvUTF8 )); return( result ); }
wxString pgConn::ExecuteScalar(const wxString &sql, bool reportError) { wxString result; if (GetStatus() == PGCONN_OK) { // Execute the query and get the status. PGresult *qryRes; wxLogSql(wxT("Scalar query (%s:%d): %s"), this->GetHost().c_str(), this->GetPort(), sql.c_str()); SetConnCancel(); qryRes = PQexec(conn, sql.mb_str(*conv)); ResetConnCancel(); lastResultStatus = PQresultStatus(qryRes); SetLastResultError(qryRes); // Check for errors if (lastResultStatus != PGRES_TUPLES_OK && lastResultStatus != PGRES_COMMAND_OK) { LogError(!reportError); PQclear(qryRes); return wxEmptyString; } // Check for a returned row if (PQntuples(qryRes) < 1) { wxLogInfo(wxT("Query returned no tuples")); PQclear(qryRes); return wxEmptyString; } // Retrieve the query result and return it. result = wxString(PQgetvalue(qryRes, 0, 0), *conv); wxLogSql(wxT("Query result: %s"), result.c_str()); // Cleanup & exit PQclear(qryRes); } return result; }
pgQueryThread::pgQueryThread(pgConn *_conn, const wxString &qry, int _resultToRetrieve, wxWindow *_caller, long _eventId, void *_data) : wxThread(wxTHREAD_JOINABLE) { query = qry; conn = _conn; dataSet = 0; result = 0; resultToRetrieve = _resultToRetrieve; rc = -1; insertedOid = (OID) - 1; caller = _caller; eventId = _eventId; data = _data; wxLogSql(wxT("Thread query (%s:%d): %s"), conn->GetHost().c_str(), conn->GetPort(), qry.c_str()); conn->RegisterNoticeProcessor(pgNoticeProcessor, this); if (conn->conn) PQsetnonblocking(conn->conn, 1); }
void *pgQueryThread::Entry() { do { if (m_currIndex < (((int)m_queries.GetCount()) - 1)) { // Create the PGcancel object to enable cancelling the running // query m_conn->SetConnCancel(); m_currIndex++; m_queries[m_currIndex]->m_returnCode = -2; m_queries[m_currIndex]->m_rowsInserted = -1l; wxLogSql(wxT("Thread executing query (%d:%s:%d): %s"), m_currIndex + 1, m_conn->GetHost().c_str(), m_conn->GetPort(), m_queries[m_currIndex]->m_query.c_str()); // register the notice processor for the current query m_conn->RegisterNoticeProcessor(m_processor, m_noticeHandler); // execute the current query now Execute(); // remove the notice processor now m_conn->RegisterNoticeProcessor(0, 0); // reset the PGcancel object m_conn->ResetConnCancel(); } if (!m_multiQueries || m_cancelled) break; wxThread::Sleep(20); } while (true); return(NULL); }
bool pgConn::StartCopy(const wxString query) { if (GetStatus() != PGCONN_OK) return false; // Execute the query and get the status PGresult *qryRes; wxLogSql(wxT("COPY query (%s:%d): %s"), this->GetHost().c_str(), this->GetPort(), query.c_str()); qryRes = PQexec(conn, query.mb_str(*conv)); lastResultStatus = PQresultStatus(qryRes); SetLastResultError(qryRes); // Check for errors if (lastResultStatus != PGRES_COPY_IN) { LogError(false); PQclear(qryRes); return false; } // NO cleanup & exit return true; }
pgSet *pgConn::ExecuteSet(const wxString &sql, bool reportError) { // Execute the query and get the status. if (GetStatus() == PGCONN_OK) { PGresult *qryRes; wxLogSql(wxT("Set query (%s:%d): %s"), this->GetHost().c_str(), this->GetPort(), sql.c_str()); SetConnCancel(); qryRes = PQexec(conn, sql.mb_str(*conv)); ResetConnCancel(); lastResultStatus = PQresultStatus(qryRes); SetLastResultError(qryRes); if (lastResultStatus == PGRES_TUPLES_OK || lastResultStatus == PGRES_COMMAND_OK) { pgSet *set = new pgSet(qryRes, this, *conv, needColQuoting); if (!set) { if (reportError) wxLogError(_("Couldn't create a pgSet object!")); else wxLogQuietError(_("Couldn't create a pgSet object!")); PQclear(qryRes); } return set; } else { LogError(!reportError); PQclear(qryRes); } } return new pgSet(); }
void dbgPgConn::startCommand( const wxString &command, wxEvtHandler *caller, wxEventType eventType, dbgPgParams *params ) { wxLogSql(wxT("%s"), command.c_str()); m_workerThread->startCommand(command, caller, eventType, params); }