1 //////////////////////////////////////////////////////////////////////////
2 //
3 // pgAdmin III - PostgreSQL Tools
4 //
5 // Copyright (C) 2002 - 2016, The pgAdmin Development Team
6 // This software is released under the PostgreSQL Licence
7 //
8 // frmMaintenance.cpp - Maintenance options selection dialogue
9 //
10 //////////////////////////////////////////////////////////////////////////
11 
12 // wxWindows headers
13 #include <wx/wx.h>
14 #include <wx/settings.h>
15 #include <wx/xrc/xmlres.h>
16 
17 
18 // App headers
19 #include "pgAdmin3.h"
20 #include "ctl/ctlMenuToolbar.h"
21 #include "frm/frmHint.h"
22 #include "frm/frmMaintenance.h"
23 #include "frm/frmMain.h"
24 #include "utils/sysLogger.h"
25 #include "schema/pgIndex.h"
26 
27 // Icons
28 #include "images/vacuum.pngc"
29 
30 
BEGIN_EVENT_TABLE(frmMaintenance,ExecutionDialog)31 BEGIN_EVENT_TABLE(frmMaintenance, ExecutionDialog)
32 	EVT_RADIOBOX(XRCID("rbxAction"),    frmMaintenance::OnAction)
33 END_EVENT_TABLE()
34 
35 #define nbNotebook              CTRL_NOTEBOOK("nbNotebook")
36 #define rbxAction               CTRL_RADIOBOX("rbxAction")
37 #define chkFull                 CTRL_CHECKBOX("chkFull")
38 #define chkFreeze               CTRL_CHECKBOX("chkFreeze")
39 #define chkAnalyze              CTRL_CHECKBOX("chkAnalyze")
40 #define chkVerbose              CTRL_CHECKBOX("chkVerbose")
41 
42 #define stBitmap                CTRL("stBitmap", wxStaticBitmap)
43 
44 
45 
46 frmMaintenance::frmMaintenance(frmMain *form, pgObject *obj) : ExecutionDialog(form, obj)
47 {
48 	SetFont(settings->GetSystemFont());
49 	LoadResource(form, wxT("frmMaintenance"));
50 	RestorePosition();
51 
52 	SetTitle(object->GetTranslatedMessage(MAINTENANCEDIALOGTITLE));
53 
54 	txtMessages = CTRL_TEXT("txtMessages");
55 
56 	// Icon
57 	SetIcon(*vacuum_png_ico);
58 
59 	// Note that under GTK+, SetMaxLength() function may only be used with single line text controls.
60 	// (see http://docs.wxwidgets.org/2.8/wx_wxtextctrl.html#wxtextctrlsetmaxlength)
61 #ifndef __WXGTK__
62 	txtMessages->SetMaxLength(0L);
63 #endif
64 
65 	if (object->GetMetaType() == PGM_INDEX || object->GetMetaType() == PGM_PRIMARYKEY || object->GetMetaType() == PGM_UNIQUE)
66 	{
67 		rbxAction->SetSelection(2);
68 		rbxAction->Enable(0, false);
69 		rbxAction->Enable(1, false);
70 	}
71 	wxCommandEvent ev;
72 	OnAction(ev);
73 }
74 
75 
~frmMaintenance()76 frmMaintenance::~frmMaintenance()
77 {
78 	SavePosition();
79 	Abort();
80 }
81 
82 
GetHelpPage() const83 wxString frmMaintenance::GetHelpPage() const
84 {
85 	wxString page;
86 	switch ((XRCCTRL(*(frmMaintenance *)this, "rbxAction", wxRadioBox))->GetSelection())
87 	{
88 		case 0:
89 			page = wxT("pg/sql-vacuum");
90 			break;
91 		case 1:
92 			page = wxT("pg/sql-analyze");
93 			break;
94 		case 2:
95 			page = wxT("pg/sql-reindex");
96 			break;
97 		case 3:
98 			page = wxT("pg/sql-cluster");
99 			break;
100 	}
101 	return page;
102 }
103 
104 
105 
OnAction(wxCommandEvent & ev)106 void frmMaintenance::OnAction(wxCommandEvent &ev)
107 {
108 	bool isVacuum = (rbxAction->GetSelection() == 0);
109 	chkFull->Enable(isVacuum);
110 	chkFreeze->Enable(isVacuum);
111 	chkAnalyze->Enable(isVacuum);
112 
113 	bool isReindex = (rbxAction->GetSelection() == 2);
114 	bool isCluster = (rbxAction->GetSelection() == 3);
115 	if (isReindex || (isCluster && !conn->BackendMinimumVersion(8, 4)))
116 	{
117 		chkVerbose->SetValue(false);
118 		chkVerbose->Enable(false);
119 	}
120 	else
121 	{
122 		chkVerbose->SetValue(true);
123 		chkVerbose->Enable(true);
124 	}
125 }
126 
127 
128 
GetSql()129 wxString frmMaintenance::GetSql()
130 {
131 	wxString sql;
132 
133 	switch (rbxAction->GetSelection())
134 	{
135 		case 0:
136 		{
137 			/* Warn about VACUUM FULL on < 9.0 */
138 			if (chkFull->GetValue() &&
139 			        !conn->BackendMinimumVersion(9, 0))
140 			{
141 				if (frmHint::ShowHint(this, HINT_VACUUM_FULL) == wxID_CANCEL)
142 					return wxEmptyString;
143 			}
144 			sql = wxT("VACUUM ");
145 
146 			if (chkFull->GetValue())
147 				sql += wxT("FULL ");
148 			if (chkFreeze->GetValue())
149 				sql += wxT("FREEZE ");
150 			if (chkVerbose->GetValue())
151 				sql += wxT("VERBOSE ");
152 			if (chkAnalyze->GetValue())
153 				sql += wxT("ANALYZE ");
154 
155 			if (object->GetMetaType() != PGM_DATABASE)
156 				sql += object->GetQuotedFullIdentifier();
157 
158 			break;
159 		}
160 		case 1:
161 		{
162 			sql = wxT("ANALYZE ");
163 			if (chkVerbose->GetValue())
164 				sql += wxT("VERBOSE ");
165 
166 			if (object->GetMetaType() != PGM_DATABASE)
167 				sql += object->GetQuotedFullIdentifier();
168 
169 			break;
170 		}
171 		case 2:
172 		{
173 			if (object->GetMetaType() == PGM_UNIQUE || object->GetMetaType() == PGM_PRIMARYKEY)
174 			{
175 				sql = wxT("REINDEX INDEX ") + object->GetQuotedFullIdentifier();
176 			}
177 			else // Database, Tables, and Index (but not Constraintes ones)
178 			{
179 				sql = wxT("REINDEX ") + object->GetTypeName().Upper()
180 				      + wxT(" ") + object->GetQuotedFullIdentifier();
181 			}
182 			break;
183 		}
184 		case 3:
185 		{
186 			sql = wxT("CLUSTER ");
187 
188 			if (chkVerbose->GetValue())
189 				sql += wxT("VERBOSE ");
190 			if (object->GetMetaType() == PGM_TABLE)
191 				sql += object->GetQuotedFullIdentifier();
192 			if (object->GetMetaType() == PGM_INDEX || object->GetMetaType() == PGM_UNIQUE
193 			        || object->GetMetaType() == PGM_PRIMARYKEY)
194 			{
195 				sql += object->GetSchema()->GetQuotedFullIdentifier();
196 				if (conn->BackendMinimumVersion(8, 4))
197 				{
198 					sql += wxT(" USING ") + object->GetQuotedIdentifier();
199 				}
200 				else
201 				{
202 					sql += wxT(" ON ") + object->GetQuotedIdentifier();
203 				}
204 			}
205 		}
206 	}
207 
208 	return sql;
209 }
210 
211 
212 
Go()213 void frmMaintenance::Go()
214 {
215 	chkFull->SetFocus();
216 	Show(true);
217 }
218 
219 
220 
maintenanceFactory(menuFactoryList * list,wxMenu * mnu,ctlMenuToolbar * toolbar)221 maintenanceFactory::maintenanceFactory(menuFactoryList *list, wxMenu *mnu, ctlMenuToolbar *toolbar) : contextActionFactory(list)
222 {
223 	mnu->Append(id, _("&Maintenance..."), _("Maintain the current database or table."));
224 	toolbar->AddTool(id, wxEmptyString, *vacuum_png_bmp, _("Maintain the current database or table."), wxITEM_NORMAL);
225 }
226 
227 
StartDialog(frmMain * form,pgObject * obj)228 wxWindow *maintenanceFactory::StartDialog(frmMain *form, pgObject *obj)
229 {
230 	frmMaintenance *frm = new frmMaintenance(form, obj);
231 	frm->Go();
232 	return 0;
233 }
234 
235 
CheckEnable(pgObject * obj)236 bool maintenanceFactory::CheckEnable(pgObject *obj)
237 {
238 	return obj && obj->CanMaintenance();
239 }
240