diff --git a/pgadmin/ctl/ctlSQLResult.cpp b/pgadmin/ctl/ctlSQLResult.cpp index 3eacd5c..2b3759c 100644 --- a/pgadmin/ctl/ctlSQLResult.cpp +++ b/pgadmin/ctl/ctlSQLResult.cpp @@ -42,6 +42,16 @@ ctlSQLResult::ctlSQLResult(wxWindow *parent, pgConn *_conn, wxWindowID id, const ctlSQLResult::~ctlSQLResult() { Abort(); + + if (thread) + { + thread->CancelExecution(); + thread->Wait(); + + delete thread; + + thread = NULL; + } } @@ -120,6 +130,16 @@ int ctlSQLResult::Execute(const wxString &query, int resultToRetrieve, wxWindow colTypes.Empty(); colTypClasses.Empty(); + if (thread) + { + thread->CancelExecution(); + thread->Wait(); + + delete thread; + thread = NULL; + } + + thread = new pgQueryThread(conn, query, resultToRetrieve, caller, eventId, data); if (thread->Create() != wxTHREAD_NO_ERROR) @@ -143,10 +163,7 @@ int ctlSQLResult::Abort() thread->CancelExecution(); thread->Wait(); - - delete thread; } - thread = 0; return 0; } diff --git a/pgadmin/db/pgQueryThread.cpp b/pgadmin/db/pgQueryThread.cpp index 7a8a555..1abb979 100644 --- a/pgadmin/db/pgQueryThread.cpp +++ b/pgadmin/db/pgQueryThread.cpp @@ -47,7 +47,8 @@ pgQueryThread::pgQueryThread(pgConn *_conn, wxEvtHandler *_caller, PQnoticeProcessor _processor, void *_noticeHandler) : wxThread(wxTHREAD_JOINABLE), m_currIndex(-1), m_conn(_conn), m_cancelled(false), m_multiQueries(true), m_useCallable(false), - m_caller(_caller), m_processor(pgNoticeProcessor), m_noticeHandler(NULL) + m_caller(_caller), m_processor(pgNoticeProcessor), m_noticeHandler(NULL), + m_eventOnCancellation(true) { // check if we can really use the enterprisedb callable statement and // required @@ -96,7 +97,8 @@ pgQueryThread::pgQueryThread(pgConn *_conn, const wxString &_qry, int _resultToRetrieve, wxWindow *_caller, long _eventId, void *_data) : wxThread(wxTHREAD_JOINABLE), m_currIndex(-1), m_conn(_conn), m_cancelled(false), m_multiQueries(false), m_useCallable(false), - m_caller(NULL), m_processor(pgNoticeProcessor), m_noticeHandler(NULL) + m_caller(NULL), m_processor(pgNoticeProcessor), m_noticeHandler(NULL), + m_eventOnCancellation(true) { if (m_conn && m_conn->conn) { @@ -115,6 +117,10 @@ pgQueryThread::pgQueryThread(pgConn *_conn, const wxString &_qry, } } +void pgQueryThread::SetEventOnCancellation(bool eventOnCancelled) +{ + m_eventOnCancellation = eventOnCancelled; +} void pgQueryThread::AddQuery(const wxString &_qry, pgParamsArray *_params, long _eventId, void *_data, bool _useCallable, int _resultToRetrieve) @@ -146,7 +152,7 @@ wxString pgQueryThread::GetMessagesAndClear(int idx) { wxCriticalSectionLocker lock(m_criticalSection); msg = m_queries[idx]->m_message; - m_queries[idx]->m_message.empty(); + m_queries[idx]->m_message = wxT(""); } return msg; @@ -332,6 +338,7 @@ return_with_error: continue_without_error: int resultsRetrieved = 0; PGresult *lastResult = 0; + bool connExecutionCancelled = false; while (true) { @@ -340,16 +347,22 @@ continue_without_error: // Hence - it does not make sense to use the function 'testdestroy' here. // We introduced the 'CancelExecution' function for the same purpose. // - // Also, do not raise event when the query execution is cancelled to - // avoid the bugs introduced to handle events by the event handler, - // which is missing or being deleted. + // We will have to call the CancelExecution function of pgConn to + // inform the backend that the user has cancelled the execution. + // + // We will need to consume all the results before quiting from the thread. + // Otherwise - the connection object will become unusable, and throw + // error - because libpq connections expects application to consume all + // the result, before executing another query // - // It will be responsibility of the compononent, using the object of - // pgQueryThread, to take the required actions to take care of the - // issue. - if (m_cancelled) + if (m_cancelled && rc != pgQueryResultEvent::PGQ_EXECUTION_CANCELLED) { - m_conn->CancelExecution(); + // We shouldn't be calling cancellation multiple time + if (!connExecutionCancelled) + { + m_conn->CancelExecution(); + connExecutionCancelled = true; + } rc = pgQueryResultEvent::PGQ_EXECUTION_CANCELLED; err.msg_primary = _("Execution Cancelled"); @@ -359,14 +372,31 @@ continue_without_error: PQclear(lastResult); lastResult = NULL; } - AppendMessage(_("Query-thread execution cancelled...\nthe query is:")); - AppendMessage(query); - - return rc; } if ((rc = PQconsumeInput(m_conn->conn)) != 1) { + if (m_cancelled) + { + rc = pgQueryResultEvent::PGQ_EXECUTION_CANCELLED; + err.msg_primary = _("Execution Cancelled"); + + if (lastResult) + { + PQclear(lastResult); + lastResult = NULL; + } + // There is nothing more to consume. + // We can quit the thread now. + // + // Raise the event - if the component asked for it on + // execution cancellation. + if (m_eventOnCancellation) + RaiseEvent(rc); + + return rc; + } + if (rc == 0) { err.msg_primary = wxString(PQerrorMessage(m_conn->conn), conv); @@ -423,6 +453,11 @@ continue_without_error: if (result) PQclear(result); + result = NULL; + + if (m_eventOnCancellation) + RaiseEvent(rc); + return rc; } goto out_of_consume_input_loop; @@ -432,6 +467,7 @@ continue_without_error: { goto out_of_consume_input_loop; } + // Release the temporary results PQclear(res); res = NULL; @@ -475,6 +511,18 @@ continue_without_error: while((copyRc = PQgetCopyData(m_conn->conn, &buf, 1)) >= 0) { + // Ignore processing the query result, when it has already been + // cancelled by the user + if (m_cancelled) + { + if (!connExecutionCancelled) + { + m_conn->CancelExecution(); + connExecutionCancelled = true; + } + continue; + } + if (buf != NULL) { if (copyRows < 100) @@ -492,13 +540,6 @@ continue_without_error: if (copyRc > 0) copyRows++; - if (m_cancelled) - { - m_conn->CancelExecution(); - rc = pgQueryResultEvent::PGQ_EXECUTION_CANCELLED; - - return -1; - } if (lastCopyRc == 0 && copyRc == 0) { Yield(); @@ -508,6 +549,24 @@ continue_without_error: { if (!PQconsumeInput(m_conn->conn)) { + // It might be the case - it is a result of the + // execution cancellation. + if (m_cancelled) + { + rc = pgQueryResultEvent::PGQ_EXECUTION_CANCELLED; + + // Release the result as the query execution has been cancelled by the + // user + if (result) + PQclear(result); + + result = NULL; + + if (m_eventOnCancellation) + RaiseEvent(rc); + + return rc; + } if (PQstatus(m_conn->conn) == CONNECTION_BAD) { err.msg_primary = _("Connection to the database server lost"); @@ -532,7 +591,10 @@ continue_without_error: } resultsRetrieved++; - if (resultsRetrieved == resultToRetrieve) + + // Save the current result, as asked by the component + // But - only if the execution is not cancelled + if (!m_cancelled && resultsRetrieved == resultToRetrieve) { result = res; insertedOid = PQoidValue(res); @@ -546,14 +608,31 @@ continue_without_error: if (lastResult) { - if (PQntuples(lastResult)) + if (!m_cancelled && 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; } + out_of_consume_input_loop: + if (m_cancelled) + { + rc = pgQueryResultEvent::PGQ_EXECUTION_CANCELLED; + + // Release the result as the query execution has been cancelled by the + // user + if (result) + PQclear(result); + + result = NULL; + + if (m_eventOnCancellation) + RaiseEvent(rc); + + return rc; + } if (!result) result = lastResult; @@ -651,7 +730,7 @@ void *pgQueryThread::Entry() if (!m_multiQueries || m_cancelled) break; - wxThread::Sleep(20); + wxThread::Sleep(10); } while (true); diff --git a/pgadmin/debugger/dbgController.cpp b/pgadmin/debugger/dbgController.cpp index f3ece2a..abff784 100644 --- a/pgadmin/debugger/dbgController.cpp +++ b/pgadmin/debugger/dbgController.cpp @@ -201,6 +201,9 @@ const wxString dbgController::ms_cmdCreateListener( const wxString dbgController::ms_cmdWaitForTarget( wxT("SELECT * FROM pldbg_wait_for_target(%s)")); +const wxString dbgController::ms_cmdIsBackendRunning( + wxT("SELECT COUNT(*) FROM (SELECT pg_catalog.pg_stat_get_backend_idset() AS bid) AS s WHERE pg_catalog.pg_stat_get_backend_pid(s.bid) = '%d'::integer;")); + dbgController::dbgController(frmMain *main, pgObject *_obj, bool _directDebugging) : m_ver(DEBUGGER_UNKNOWN_API), m_sessionType(DBG_SESSION_TYPE_UNKNOWN), m_terminated(false), m_frm(NULL), m_dbgConn(NULL), m_dbgThread(NULL), @@ -405,6 +408,7 @@ bool dbgController::Start() LOCKMUTEX(m_dbgThreadLock); m_dbgThread = new pgQueryThread( m_dbgConn, this, &(dbgController::NoticeHandler), this); + m_dbgThread->SetEventOnCancellation(false); if (m_dbgThread->Create() != wxTHREAD_NO_ERROR) { @@ -446,6 +450,7 @@ bool dbgController::Start() // Declare that execution has been cancelled m_terminated = true; m_frm->EnableToolsAndMenus(false); + m_frm->CloseProgressBar(); return false; } @@ -453,6 +458,7 @@ bool dbgController::Start() m_execConnThread = new pgQueryThread( conn, this, &(dbgController::NoticeHandler), this); + m_execConnThread->SetEventOnCancellation(false); if (m_execConnThread->Create() != wxTHREAD_NO_ERROR) { diff --git a/pgadmin/debugger/dbgEvents.cpp b/pgadmin/debugger/dbgEvents.cpp index e297a40..dd27795 100644 --- a/pgadmin/debugger/dbgEvents.cpp +++ b/pgadmin/debugger/dbgEvents.cpp @@ -326,6 +326,7 @@ bool dbgController::HandleQuery(pgBatchQuery *_qry, const wxString &_err) return false; LOCKMUTEX(m_dbgThreadLock); + // It is possible that we found one error, while running the previous query // and, called Stop function from it, because of an error found. // That may have released the debugger thread @@ -372,7 +373,6 @@ bool dbgController::HandleQuery(pgBatchQuery *_qry, const wxString &_err) } m_frm->EnableToolsAndMenus(false); - m_frm->SetStatusText(_("Debugging aborting...")); wxTheApp->Yield(true); if (!m_dbgConn->IsAlive()) @@ -391,9 +391,43 @@ bool dbgController::HandleQuery(pgBatchQuery *_qry, const wxString &_err) UNLOCKMUTEX(m_dbgThreadLock); if (_qry->ReturnCode() == PGRES_FATAL_ERROR) - wxMessageBox(_("The calling connection was closed or lost."), _("Connection Lost"), wxICON_ERROR | wxOK); + { + // We will start start listening for new in-context session, if the + // current session is closed. On which, the query/target was + // running. + // + // This allows us to have the same behaviour as the old one. + // + if (m_sessionType == DBG_SESSION_TYPE_INCONTEXT && m_currTargetPid != wxT("")) + { + // Let's check if the target pid has stopped or exited after + // successful debugging, let's move on and wait for the next + // target to hit. + wxString isTargetRunning = m_dbgConn->ExecuteScalar(wxString::Format(ms_cmdIsBackendRunning, m_currTargetPid.c_str())); + + if (isTargetRunning == wxT("0")) + { + // Reset the current backend-pid of the target + m_currTargetPid = wxT(""); + m_dbgThread->AddQuery( + wxString::Format( + ms_cmdWaitForTarget, m_model->GetSession().c_str()), + NULL, RESULT_ID_TARGET_READY); + + m_frm->LaunchWaitingDialog(); + + return false; + } + } + else + { + wxMessageBox(_("The calling connection was closed or lost."), _("Connection Lost"), wxICON_ERROR | wxOK); + } + } else + { wxMessageBox(strErr, _("Execution Error"), wxICON_ERROR | wxOK); + } wxLogQuietError(strErr); } @@ -910,6 +944,13 @@ void dbgController::ResultTargetReady(pgQueryResultEvent &_ev) { bool goAhead = false; goAhead = (qry->ReturnCode() == PGRES_TUPLES_OK); + pgSet *set = qry->ResultSet(); + + // Save the current running target pid + m_currTargetPid = set->GetVal(0); + + // Next line release the actual result-set + set = NULL; // Release the result-set qry->Release(); diff --git a/pgadmin/debugger/dbgTargetInfo.cpp b/pgadmin/debugger/dbgTargetInfo.cpp index 42c9ea4..ddc4197 100644 --- a/pgadmin/debugger/dbgTargetInfo.cpp +++ b/pgadmin/debugger/dbgTargetInfo.cpp @@ -220,8 +220,8 @@ dbgTargetInfo::dbgTargetInfo(Oid _target, pgConn *_conn) m_pkgOid = set->GetLong(wxT("pkg")); m_pkgInitOid = set->GetLong(wxT("pkgconsoid")); m_schemaOid = set->GetLong(wxT("schema")); - m_fqName = m_schema + wxT(".") + - (m_pkgOid == 0 ? wxT("") : (m_package + wxT("."))) + m_name; + m_fqName = qtIdent(m_schema) + wxT(".") + + (m_pkgOid == 0 ? wxT("") : (qtIdent(m_package) + wxT("."))) + qtIdent(m_name); wxArrayString argModes, argNames, argTypes, argTypeOids, argDefVals, argBaseTypes; diff --git a/pgadmin/frm/frmQuery.cpp b/pgadmin/frm/frmQuery.cpp index becc77c..ce2688f 100644 --- a/pgadmin/frm/frmQuery.cpp +++ b/pgadmin/frm/frmQuery.cpp @@ -2482,6 +2482,10 @@ void frmQuery::OnQueryComplete(pgQueryResultEvent &ev) { showMessage(_("Empty query, no results.")); } + else if (ev.GetInt() == pgQueryResultEvent::PGQ_EXECUTION_CANCELLED) + { + showMessage(_("Execution Cancelled!")); + } else { wxString errMsg, errMsg2; diff --git a/pgadmin/include/db/pgQueryThread.h b/pgadmin/include/db/pgQueryThread.h index b21ed28..b1cb6b5 100644 --- a/pgadmin/include/db/pgQueryThread.h +++ b/pgadmin/include/db/pgQueryThread.h @@ -182,6 +182,8 @@ public: return m_useCallable; } + void SetEventOnCancellation(bool eventOnCancelled); + void AddQuery( const wxString &_qry, pgParamsArray *_params = NULL, long _eventId = 0, void *_data = NULL, bool _useCallable = false, @@ -262,6 +264,8 @@ private: pgConn *m_conn; // Execution cancelled? bool m_cancelled; + // Raise events even when cancelled the execution + bool m_eventOnCancellation; // Does this thread support multiple queries bool m_multiQueries; // Use EDB callable statement (if available and require) diff --git a/pgadmin/include/debugger/dbgController.h b/pgadmin/include/debugger/dbgController.h index aed181e..f3f7268 100644 --- a/pgadmin/include/debugger/dbgController.h +++ b/pgadmin/include/debugger/dbgController.h @@ -138,6 +138,7 @@ private: const static wxString ms_cmdGetTargetInfo; const static wxString ms_cmdCreateListener; const static wxString ms_cmdWaitForTarget; + const static wxString ms_cmdIsBackendRunning; private: // Debugger Version for the current server @@ -166,6 +167,9 @@ private: // Debugger Data Model dbgModel *m_model; + // In-direct Debugging on which target-pid + wxString m_currTargetPid; + DECLARE_EVENT_TABLE() };