Enable pgScript to use the pgAdmin UI for error
authordpage <dpage@a7884b65-44f6-0310-8a51-81a127f17b15>
Wed, 11 Mar 2009 19:35:51 +0000 (19:35 +0000)
committerdpage <dpage@a7884b65-44f6-0310-8a51-81a127f17b15>
Wed, 11 Mar 2009 19:35:51 +0000 (19:35 +0000)
output: when an error occurs while interpreting a script then the line
where the error occurred is signaled. [Mickael Deloison]

git-svn-id: svn://svn.pgadmin.org/trunk/pgadmin3@7679 a7884b65-44f6-0310-8a51-81a127f17b15

pgadmin/frm/frmQuery.cpp
pgadmin/include/pgscript/pgsApplication.h
pgadmin/include/pgscript/utilities/pgsThread.h
pgadmin/pgscript/pgsApplication.cpp
pgadmin/pgscript/statements/pgsStmtList.cpp
pgadmin/pgscript/utilities/pgsDriver.cpp
pgadmin/pgscript/utilities/pgsThread.cpp

index 811aa5f8e1989a4fc2f411ec22437a6ddac02594..f2280049c170f0bfe3780b54d4a858b3e54b2e3c 100644 (file)
@@ -1811,6 +1811,13 @@ void frmQuery::OnExecute(wxCommandEvent& event)
 
 void frmQuery::OnExecScript(wxCommandEvent& event)
 {
+    // Get the script
+    wxString query = sqlQuery->GetSelectedText();
+    if (query.IsNull())
+        query = sqlQuery->GetText();
+    if (query.IsNull())
+        return;
+
     // Clear markers and indicators
     sqlQuery->MarkerDeleteAll(0);
     sqlQuery->StartStyling(0, wxSTC_INDICS_MASK);
@@ -1844,7 +1851,7 @@ void frmQuery::OnExecScript(wxCommandEvent& event)
     pgScript->ClearSymbols();
 
     // Parse script
-    pgScript->ParseString(sqlQuery->GetText(), pgsOutput);
+    pgScript->ParseString(query, pgsOutput);
     pgsTimer->Start(20);
     aborted = false;
 }
@@ -2182,6 +2189,26 @@ void frmQuery::OnScriptComplete(wxCommandEvent &ev)
     SetStatusText(_("pgScript completed."), STATUSPOS_MSGS);
     wxString str = _("Total pgScript runtime: ") + elapsedQuery.ToString() + wxT(" ms.\n\n");
     msgHistory->AppendText(str);
+    
+    // Check whether there was an error/exception
+    if (pgScript->errorOccurred() && pgScript->errorLine() >= 1)
+    {
+        // Find out what the line number is
+        int selStart = sqlQuery->GetSelectionStart(), selEnd = sqlQuery->GetSelectionEnd();
+        if (selStart == selEnd)
+            selStart = 0;
+        SetStatusText(wxString() << selStart, STATUSPOS_MSGS);
+        int line = 0, maxLine = sqlQuery->GetLineCount();
+        while (line < maxLine && sqlQuery->GetLineEndPosition(line) < selStart)
+            line++;
+        line += pgScript->errorLine() - 1;
+        
+        // Mark the line where the error occurred
+        sqlQuery->MarkerAdd(line, 0);
+        
+        // Go to that line
+        sqlQuery->GotoPos(sqlQuery->GetLineEndPosition(line));
+    }
 }
 
 void frmQuery::writeScriptOutput()
index 6a22ed84f9b52e16347a72ac62a53815e99bec06..bbcaa3aa623956dad0487e9c89bbed2cb1a9c166 100644 (file)
@@ -47,6 +47,9 @@ private:
        /** pgAdmin specific: post this event when m_thread is done. */
        long m_event_id;
        
+       /** Location of the last error if there was one. */
+       int m_last_error_line;
+       
 public:
        
        /** Creates an application and creates a connection. */
@@ -103,6 +106,12 @@ public:
        /** Releases the lock on the output stream. */
        void UnlockOutput();
        
+       /** Was there an error? */
+       bool errorOccurred() const;
+       
+       /** Get the position (line) of the last error. */
+       int errorLine() const;
+       
 private:
        
        /** Common method for parse_file & parse_string: runs the thread. */
index 6bc42854e6daed31dd32aaa89a3b9cc4622a8014..978defff70ced4bca8c60f162373e4a043ac371f 100644 (file)
@@ -46,6 +46,9 @@ private:
        /** If set it is the encoding used in the file to parse. */
        wxMBConv * m_conv;
        
+       /** Location of the last error if there was one otherwise -1 */
+       int m_last_error_line;
+       
 public:
        
        /** Parses a file with the provided encoding. */
@@ -72,6 +75,12 @@ public:
        /** Releases the lock on the output stream. */
        void UnlockOutput();
        
+       /** Set the position (line) of the last error. */
+       void last_error_line(int line);
+       
+       /** Get the position (line) of the last error. */
+       int last_error_line() const;
+       
 private:
        
        pgsThread(const pgsThread & that);
index 4915483c51811d07426466ca5f659b4f22bea8c0..f6e5600ddace04486bdd017efdbd524a2644f30c 100644 (file)
@@ -54,6 +54,7 @@ bool pgsApplication::ParseFile(const wxString & file, pgsOutputStream & out,
 {
        if (!IsRunning())
        {
+               m_last_error_line = -1;
                m_thread = new pgsThread(m_vars, m_mutex, m_connection,
                                file, out, *this, conv);
                return RunThread();
@@ -69,6 +70,7 @@ bool pgsApplication::ParseString(const wxString & string,
 {
        if (!IsRunning())
        {
+               m_last_error_line = -1;
                m_thread = new pgsThread(m_vars, m_mutex, m_connection,
                                string, out, *this);
                return RunThread();
@@ -147,6 +149,10 @@ void pgsApplication::Complete()
                m_caller->AddPendingEvent(resultEvent);
     }
 #endif // PGSCLI
+
+       // If last_error_line() == -1 then there was no error
+       // Else get the line number where the error occurred
+       m_last_error_line = m_thread->last_error_line();
        
        wxLogScript(wxT("Execution completed"));
 }
@@ -192,3 +198,13 @@ void pgsApplication::UnlockOutput()
 {
        m_stream.Post();
 }
+
+bool pgsApplication::errorOccurred() const
+{
+       return (m_last_error_line != -1);
+}
+
+int pgsApplication::errorLine() const
+{
+       return m_last_error_line;
+}
index d9a7ff81591142a95d3e4b3b99695de488695fd9..05f654df974af3b3792b8df6de1860bc1375acef 100644 (file)
@@ -60,6 +60,7 @@ void pgsStmtList::eval(pgsVarMap & vars) const
                                if (m_app != 0)
                                {
                                        m_app->LockOutput();
+                                       m_app->last_error_line(current->line());
                                }
                                
                                m_cout << wx_static_cast(const wxString, e.message())
@@ -80,6 +81,7 @@ void pgsStmtList::eval(pgsVarMap & vars) const
                                if (m_app != 0)
                                {
                                        m_app->LockOutput();
+                                       m_app->last_error_line(current->line());
                                }
                                
                                m_cout << PGSOUTERROR << _("Unknown exception:\n")
index ae13428a58506128ce3ffaf8188c06e6b3230500..7d4d58eef6afef78d926d68b35e0e7da41f01abd 100644 (file)
@@ -70,6 +70,7 @@ void pgsDriver::error(const class location & l, const wxString & m)
 {
        std::ostringstream oss;
        oss << l;
+       thread.last_error_line(l.begin.line);
        thread.LockOutput();
        context.m_cout << wx_static_cast(const wxString, wxString(oss.str()
                        .c_str(), wxConvUTF8)) << wxT(": ") << m << wxT("\n");
index ee961f6ca74438e1206e1e48965afe9aa15d97f8..90b781931987f0df406712744b9bfb8940509619 100644 (file)
@@ -21,7 +21,7 @@ pgsThread::pgsThread(pgsVarMap & vars, wxSemaphore & mutex,
                pgsApplication & app, wxMBConv * conv) :
        wxThread(wxTHREAD_DETACHED), m_vars(vars), m_mutex(mutex),
                        m_connection(connection), m_data(file), m_out(out),
-                       m_app(app), m_conv(conv)
+                       m_app(app), m_conv(conv), m_last_error_line(-1)
 {
        wxLogScript(wxT("Starting thread"));
        m_mutex.Wait();
@@ -32,7 +32,7 @@ pgsThread::pgsThread(pgsVarMap & vars, wxSemaphore & mutex,
                pgsApplication & app) :
        wxThread(wxTHREAD_DETACHED), m_vars(vars), m_mutex(mutex),
                        m_connection(connection), m_data(string), m_out(out),
-                       m_app(app), m_conv(0)
+                       m_app(app), m_conv(0), m_last_error_line(-1)
 {
        wxLogScript(wxT("Starting thread"));
        m_mutex.Wait();
@@ -82,3 +82,13 @@ void pgsThread::UnlockOutput()
 {
        m_app.UnlockOutput();
 }
+
+void pgsThread::last_error_line(int line)
+{
+       m_last_error_line = line;
+}
+
+int pgsThread::last_error_line() const
+{
+       return m_last_error_line;
+}