diff --git a/pgadmin/frm/frmQuery.cpp b/pgadmin/frm/frmQuery.cpp index 9457882..10b69c8 100644 --- a/pgadmin/frm/frmQuery.cpp +++ b/pgadmin/frm/frmQuery.cpp @@ -9,6 +9,8 @@ // ////////////////////////////////////////////////////////////////////////// +#include "utils/PgadminScanner.h" + #include "pgAdmin3.h" // wxWindows headers @@ -79,6 +81,14 @@ #include "images/help.pngc" #include "images/gqbJoin.pngc" +#include +WX_DEFINE_OBJARRAY(Output_Pane_Objects_Array); + +#define VK_DEBUG +#ifdef VK_DEBUG +static int myCounter = 0; +#endif + #define CTRLID_CONNECTION 4200 #define CTRLID_DATABASELABEL 4201 @@ -165,6 +175,7 @@ BEGIN_EVENT_TABLE(frmQuery, pgFrame) EVT_SPLITTER_SASH_POS_CHANGED(GQB_HORZ_SASH, frmQuery::OnResizeHorizontally) EVT_BUTTON(CTL_DELETECURRENTBTN, frmQuery::OnDeleteCurrent) EVT_BUTTON(CTL_DELETEALLBTN, frmQuery::OnDeleteAll) + EVT_COMBOBOX(CTL_SQLQUERIESCBOX, frmQuery::OnComboBoxUpdate) END_EVENT_TABLE() class DnDFile : public wxFileDropTarget @@ -445,6 +456,10 @@ frmQuery::frmQuery(frmMain *form, const wxString &_title, pgConn *_conn, const w // This one will contain the label, the combobox, and the two buttons wxBoxSizer *boxHistory = new wxBoxSizer(wxHORIZONTAL); + // Create the inner box sizer + // This one will contain the label and the combobox + wxBoxSizer *boxQueries = new wxBoxSizer(wxHORIZONTAL); + // Label wxStaticText *label = new wxStaticText(pnlQuery, 0, _("Previous queries")); boxHistory->Add(label, 0, wxALL | wxALIGN_CENTER_VERTICAL, 1); @@ -481,6 +496,15 @@ frmQuery::frmQuery(frmMain *form, const wxString &_title, pgConn *_conn, const w boxQuery->Add(boxSQL, 1, wxEXPAND | wxRIGHT | wxLEFT | wxBOTTOM, 1); + output_pane_objects_array = NULL; + cbQueries = new wxComboBox(pnlQuery, CTL_SQLQUERIESCBOX, wxT(""), wxDefaultPosition, wxDefaultSize, wxArrayString(), wxCB_DROPDOWN | wxCB_READONLY); + cbQueries->SetToolTip(_("Current queries")); + wxStaticText *labelcq = new wxStaticText(pnlQuery, 0, _("Current queries ")); + boxQueries->Add(labelcq, 0, wxALL | wxALIGN_CENTER_VERTICAL, 1); + boxQueries->Add(cbQueries, 1, wxEXPAND | wxALL | wxALIGN_CENTER_VERTICAL, 1); + + boxQuery->Add(boxQueries, 0, wxEXPAND | wxALL, 1); + // Auto-sizing pnlQuery->SetSizer(boxQuery); boxQuery->Fit(pnlQuery); @@ -2163,6 +2187,86 @@ bool frmQuery::updateFromGqb(bool executing) return false; } +void frmQuery::myLogNotice(const wxChar *szFormat, ...) +{ + wxLogLevel savedlogLevel = sysLogger::logLevel; + sysLogger::logLevel = LOG_NOTICE; + va_list argptr; + va_start(argptr, szFormat); + wxVLogNotice(szFormat, argptr); + va_end(argptr); + sysLogger::logLevel = savedlogLevel; +} + +output_pane_objects_tag::~output_pane_objects_tag() +{ + if (index > 0) + { + if (sqlResult) {delete sqlResult; sqlResult = NULL;} + if (explainCanvas) {delete explainCanvas; explainCanvas = NULL;} + if (msgResult) {delete msgResult; msgResult = NULL;} + } + index = -1; +} + +void frmQuery::OnComboBoxUpdate(wxCommandEvent &event) +{ + if (!event.GetEventType() == wxEVT_COMMAND_COMBOBOX_SELECTED) + return; + + if (queries.Count() > 1) + { + current_output_pane = event.GetSelection(); +#ifdef VK_DEBUG + myLogNotice(wxT("frmQuery %d OnComboBoxUpdate current_output_pane=%d"), + ++myCounter, current_output_pane); +#endif + OnComboBoxUpdatePrivate(current_output_pane); + } +} + +void frmQuery::OnComboBoxUpdatePrivate(int current_output_pane) +{ + if (queries.Count() > 1) + { + if (current_output_pane > -1) + { + if (outputPane->GetPageCount() > 0) + { + outputPane->RemovePage(3); + outputPane->RemovePage(2); + outputPane->RemovePage(1); + outputPane->RemovePage(0); + } + output_pane_objects_tag struc = output_pane_objects_array->Item(current_output_pane); + sqlResult = struc.sqlResult; + explainCanvas = struc.explainCanvas; + msgResult = struc.msgResult; + + wxString x = wxString::Format(_("%d Data Output"), current_output_pane + 1); + outputPane->AddPage(sqlResult, x); + x = wxString::Format(_("%d Explain"), current_output_pane + 1); + outputPane->AddPage(explainCanvas, x); + x = wxString::Format(_("%d Messages"), current_output_pane + 1); + outputPane->AddPage(msgResult, x); + outputPane->AddPage(msgHistory, _("History")); + + //I don't know why we need SetSelection(1) ? + outputPane->SetSelection(1); + outputPane->SetSelection(0); + + sqlQuery->Connect(wxID_ANY, wxEVT_SET_FOCUS, wxFocusEventHandler(frmQuery::OnFocus)); + sqlQuery->Connect(wxID_ANY, wxEVT_KILL_FOCUS, wxFocusEventHandler(frmQuery::OnFocus)); + sqlResult->Connect(wxID_ANY, wxEVT_SET_FOCUS, wxFocusEventHandler(frmQuery::OnFocus)); + msgResult->Connect(wxID_ANY, wxEVT_SET_FOCUS, wxFocusEventHandler(frmQuery::OnFocus)); + + struc.index = -1; + + sqlQuery->SetFocus(); + } + } +} + void frmQuery::OnExecute(wxCommandEvent &event) { if(sqlNotebook->GetSelection() == 1) @@ -2178,8 +2282,129 @@ void frmQuery::OnExecute(wxCommandEvent &event) if (query.IsNull()) return; - execQuery(query); - sqlQuery->SetFocus(); + if (!saved_sqlResult) + { + saved_sqlResult = sqlResult; + saved_explainCanvas = explainCanvas; + saved_msgResult = msgResult; + } + firstQuery = true; + queries.Clear(); + cbQueries->Clear(); + cbQueries->SetValue(wxT("")); + wxArrayInt locs; + struct myScannerNode *head = scan_SQL_cpp(query.ToUTF8()); + if (head) + { + struct myScannerNode *result = get_all_commands_cpp(head); + struct myScannerNode *current = result; + if (current) + { + do + { + wxString x = wxString(current->data.val.str, wxConvUTF8); + queries.Add(x); + locs.Add(current->data.loc); + current = current->next; + } while (current != result); + destroylist(result); + } + destroylist(head); + } + if (queries.Count() > 1) + { +#ifdef VK_DEBUG + myLogNotice(wxT("frmQuery %d OnExecute \"%s\""), + ++myCounter, query.c_str()); +#endif + multiSQL = true; + outputPane->RemovePage(3); + outputPane->RemovePage(2); + outputPane->RemovePage(1); + outputPane->RemovePage(0); + if (output_pane_objects_array) + { + delete output_pane_objects_array; + } + output_pane_objects_array = new Output_Pane_Objects_Array(); + threadCounter = 0; + bool saveAutoRollback = settings->GetAutoRollback(); + settings->SetAutoRollback(false); + for (int i = 0; i < queries.Count(); i++) + { + wxString item = queries.Item(i); + cbQueries->Append(item); + output_pane_objects_tag *struc = new output_pane_objects_tag(); + struc->index = i; + struc->offset = locs.Item(i); + if (i == 0) + { + struc->sqlResult = saved_sqlResult; + struc->explainCanvas = saved_explainCanvas; + struc->msgResult = saved_msgResult; + } + else + { + struc->sqlResult = new ctlSQLResult( + outputPane, conn, CTL_SQLRESULT, wxDefaultPosition, wxDefaultSize); + struc->explainCanvas = new ExplainCanvas(outputPane); + struc->msgResult = new wxTextCtrl( + outputPane, CTL_MSGRESULT, wxT(""), wxDefaultPosition, wxDefaultSize, + wxTE_MULTILINE | wxTE_READONLY | wxTE_DONTWRAP); + struc->msgResult->SetFont(settings->GetSQLFont()); + } + output_pane_objects_array->Add(struc); + + sqlResult = struc->sqlResult; + explainCanvas = struc->explainCanvas; + msgResult = struc->msgResult; + + sqlResult->Connect(wxID_ANY, wxEVT_SET_FOCUS, wxFocusEventHandler(frmQuery::OnFocus)); + msgResult->Connect(wxID_ANY, wxEVT_SET_FOCUS, wxFocusEventHandler(frmQuery::OnFocus)); + + threadCounter++; + execQuery(item, 0, false, struc->offset); + while (threadCounter > 0) + { + wxTheApp->Yield(true); + } + } + settings->SetAutoRollback(saveAutoRollback); + + sqlResult = saved_sqlResult; + explainCanvas = saved_explainCanvas; + msgResult = saved_msgResult; + + cbQueries->SetSelection(0); + OnComboBoxUpdatePrivate(0); + manager.Update(); + + sqlQuery->SetFocus(); + } + else + { + sqlResult = saved_sqlResult; + explainCanvas = saved_explainCanvas; + msgResult = saved_msgResult; + + if (multiSQL) + { + outputPane->RemovePage(3); + outputPane->RemovePage(2); + outputPane->RemovePage(1); + outputPane->RemovePage(0); + + outputPane->AddPage(sqlResult, _("Data Output")); + outputPane->AddPage(explainCanvas, _("Explain")); + outputPane->AddPage(msgResult, _("Messages")); + outputPane->AddPage(msgHistory, _("History")); + + multiSQL = false; + } + + execQuery(query); + sqlQuery->SetFocus(); + } } @@ -2343,6 +2568,10 @@ void frmQuery::showMessage(const wxString &msg, const wxString &msgShort) void frmQuery::execQuery(const wxString &query, int resultToRetrieve, bool singleResult, const int queryOffset, bool toFile, bool explain, bool verbose) { +#ifdef VK_DEBUG + myLogNotice(wxT("frmQuery %d execQuery \"%s\""), + ++myCounter, query.c_str()); +#endif setTools(true); queryMenu->Enable(MNU_SAVEHISTORY, true); queryMenu->Enable(MNU_CLEARHISTORY, true); @@ -2396,7 +2625,12 @@ void frmQuery::execQuery(const wxString &query, int resultToRetrieve, bool singl startTimeQuery = wxGetLocalTimeMillis(); timer.Start(10); - if (sqlResult->Execute(query, resultToRetrieve, this, QUERY_COMPLETE, qi) >= 0) + int rc = sqlResult->Execute(query, resultToRetrieve, this, QUERY_COMPLETE, qi); +#ifdef VK_DEBUG + myLogNotice(wxT("frmQuery %d execQuery rc=%d"), + ++myCounter, rc); +#endif + if (rc >= 0) { // Return and wait for the result return; @@ -2409,6 +2643,10 @@ void frmQuery::execQuery(const wxString &query, int resultToRetrieve, bool singl // When the query completes, it raises an event which we process here. void frmQuery::OnQueryComplete(wxCommandEvent &ev) { +#ifdef VK_DEBUG + myLogNotice(wxT("frmQuery %d OnQueryComplete"), + ++myCounter); +#endif QueryExecInfo *qi = (QueryExecInfo *)ev.GetClientData(); bool done = false; @@ -2436,7 +2674,8 @@ void frmQuery::OnQueryComplete(wxCommandEvent &ev) if (sqlResult->RunStatus() != PGRES_TUPLES_OK) { - outputPane->SetSelection(2); + if (queries.Count() <= 1) + outputPane->SetSelection(2); if (sqlResult->RunStatus() == PGRES_COMMAND_OK) { done = true; @@ -2540,7 +2779,8 @@ void frmQuery::OnQueryComplete(wxCommandEvent &ev) else { done = true; - outputPane->SetSelection(0); + if (queries.Count() <= 1) + outputPane->SetSelection(0); long rowsTotal = sqlResult->NumRows(); if (qi->toFileExportForm) @@ -2594,7 +2834,7 @@ void frmQuery::OnQueryComplete(wxCommandEvent &ev) } } - if (sqlResult->RunStatus() == PGRES_TUPLES_OK || sqlResult->RunStatus() == PGRES_COMMAND_OK) + if (firstQuery && (sqlResult->RunStatus() == PGRES_TUPLES_OK || sqlResult->RunStatus() == PGRES_COMMAND_OK)) { // Get the executed query wxString executedQuery = sqlQuery->GetSelectedText(); @@ -2636,6 +2876,7 @@ void frmQuery::OnQueryComplete(wxCommandEvent &ev) histoQueries.RemoveAt(index); sqlQueries->Delete(index); } + firstQuery = false; } // Make sure only the maximum query number is enforced @@ -2708,6 +2949,10 @@ void frmQuery::writeScriptOutput() // Complete the processing of a query void frmQuery::completeQuery(bool done, bool explain, bool verbose) { +#ifdef VK_DEBUG + myLogNotice(wxT("frmQuery %d completeQuery start"), + ++myCounter); +#endif // Display async notifications pgNotification *notify; int notifies = 0; @@ -2783,12 +3028,18 @@ void frmQuery::completeQuery(bool done, bool explain, bool verbose) } } explainCanvas->SetExplainString(str); - outputPane->SetSelection(1); + if (queries.Count() <= 1) + outputPane->SetSelection(1); } updateMenu(); } + threadCounter--; sqlQuery->SetFocus(); +#ifdef VK_DEBUG + myLogNotice(wxT("frmQuery %d completeQuery end"), + ++myCounter); +#endif } diff --git a/pgadmin/include/frm/frmQuery.h b/pgadmin/include/frm/frmQuery.h index df0a922..647594e 100644 --- a/pgadmin/include/frm/frmQuery.h +++ b/pgadmin/include/frm/frmQuery.h @@ -71,6 +71,21 @@ public: bool verbose; }; +struct output_pane_objects_tag +{ + int index; + int offset; + ctlSQLResult *sqlResult; + ExplainCanvas *explainCanvas; + wxTextCtrl *msgResult; + output_pane_objects_tag() : index(-1), offset(0), sqlResult(NULL), explainCanvas(NULL), msgResult(NULL) + { + } + ~output_pane_objects_tag(); +}; + +WX_DECLARE_OBJARRAY(struct output_pane_objects_tag, Output_Pane_Objects_Array); + class frmQuery : public pgFrame { public: @@ -264,6 +279,21 @@ private: // Required because the pgScript parser isn't currently thread-safe :-( static bool ms_pgScriptRunning; + //SQL commands for the current execute query + wxArrayString queries; + wxComboBox *cbQueries; + Output_Pane_Objects_Array *output_pane_objects_array; + int current_output_pane; + void OnComboBoxUpdate(wxCommandEvent &event); + void OnComboBoxUpdatePrivate(int current_output_pane); + void myLogNotice(const wxChar *szFormat, ...); + int threadCounter; + bool firstQuery; + ctlSQLResult *saved_sqlResult; + ExplainCanvas *saved_explainCanvas; + wxTextCtrl *saved_msgResult; + bool multiSQL; + DECLARE_EVENT_TABLE() }; @@ -280,19 +310,20 @@ enum enum { - CTL_SQLQUERY = 331, - CTL_SQLRESULT, - CTL_MSGRESULT, - CTL_MSGHISTORY, - CTL_NTBKCENTER, - CTL_COLSGRID, - CTL_TIMERSIZES, - CTL_TIMERFRM, - CTL_NTBKGQB, - CTL_SQLQUERYCBOX, - CTL_DELETECURRENTBTN, - CTL_DELETEALLBTN, - CTL_SCRATCHPAD + CTL_SQLQUERY = 331, + CTL_SQLRESULT, + CTL_MSGRESULT, + CTL_MSGHISTORY, + CTL_NTBKCENTER, + CTL_COLSGRID, + CTL_TIMERSIZES, + CTL_TIMERFRM, + CTL_NTBKGQB, + CTL_SQLQUERYCBOX, + CTL_DELETECURRENTBTN, + CTL_DELETEALLBTN, + CTL_SCRATCHPAD, + CTL_SQLQUERIESCBOX }; ///////////////////////////////////////////////////////