1 //////////////////////////////////////////////////////////////////////////
2 //
3 // pgScript - PostgreSQL Tools
4 //
5 // Copyright (C) 2002 - 2016, The pgAdmin Development Team
6 // This software is released under the PostgreSQL Licence
7 //
8 //////////////////////////////////////////////////////////////////////////
9 
10 
11 #include "pgAdmin3.h"
12 #include "pgscript/statements/pgsStmtList.h"
13 
14 #include <typeinfo>
15 #include "pgscript/exceptions/pgsBreakException.h"
16 #include "pgscript/exceptions/pgsContinueException.h"
17 #include "pgscript/exceptions/pgsInterruptException.h"
18 #include "pgscript/utilities/pgsThread.h"
19 #include "pgscript/utilities/pgsUtilities.h"
20 
21 #include <wx/listimpl.cpp>
22 WX_DEFINE_LIST(pgsListStmt);
23 
24 bool pgsStmtList::m_exception_thrown = false;
25 
pgsStmtList(pgsOutputStream & cout,pgsThread * app)26 pgsStmtList::pgsStmtList(pgsOutputStream &cout, pgsThread *app) :
27 	pgsStmt(app), m_cout(cout)
28 {
29 
30 }
31 
~pgsStmtList()32 pgsStmtList::~pgsStmtList()
33 {
34 	pgsListStmt::iterator it;
35 	for (it = m_stmt_list.begin(); it != m_stmt_list.end(); it++)
36 	{
37 		pdelete(*it);
38 	}
39 }
40 
eval(pgsVarMap & vars) const41 void pgsStmtList::eval(pgsVarMap &vars) const
42 {
43 	pgsListStmt::const_iterator it;
44 	for (it = m_stmt_list.begin(); it != m_stmt_list.end(); it++)
45 	{
46 		pgsStmt *current = *it;
47 
48 		try
49 		{
50 			current->eval(vars);
51 
52 			if (m_app != 0 && m_app->TestDestroy())
53 				throw pgsInterruptException();
54 		}
55 		catch (const pgsException &e)
56 		{
57 			if (!m_exception_thrown && (typeid(e) != typeid(pgsBreakException))
58 			        && (typeid(e) != typeid(pgsContinueException)))
59 			{
60 				if (m_app != 0)
61 				{
62 					m_app->LockOutput();
63 					m_app->last_error_line(current->line());
64 				}
65 
66 				m_cout << wx_static_cast(const wxString, e.message())
67 				       << wxT(" on line ") << current->line() << wxT("\n");
68 				m_exception_thrown = true;
69 
70 				if (m_app != 0)
71 				{
72 					m_app->UnlockOutput();
73 				}
74 			}
75 			throw;
76 		}
77 		catch (const std::exception &e)
78 		{
79 			if (!m_exception_thrown)
80 			{
81 				if (m_app != 0)
82 				{
83 					m_app->LockOutput();
84 					m_app->last_error_line(current->line());
85 				}
86 
87 				m_cout << PGSOUTERROR << _("Unknown exception:\n")
88 				       << wx_static_cast(const wxString,
89 				                         wxString(e.what(), wxConvUTF8));
90 				m_exception_thrown = true;
91 
92 				if (m_app != 0)
93 				{
94 					m_app->UnlockOutput();
95 				}
96 			}
97 			throw;
98 		}
99 
100 		if (m_app != 0)
101 		{
102 			m_app->Yield();
103 		}
104 	}
105 }
106 
insert_front(pgsStmt * stmt)107 void pgsStmtList::insert_front(pgsStmt *stmt)
108 {
109 	m_stmt_list.push_front(stmt);
110 }
111 
insert_back(pgsStmt * stmt)112 void pgsStmtList::insert_back(pgsStmt *stmt)
113 {
114 	m_stmt_list.push_back(stmt);
115 }
116