Debian Codegraph @

DatabaseExplorer/ErdCommitWizard.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : ErdCommitWizard.cpp
6 //
7 // -------------------------------------------------------------------------
8 // A
9 // _____ _ _ _ _
10 // / __ \ | | | | (_) |
11 // | / \/ ___ __| | ___| | _| |_ ___
12 // | | / _ \ / _ |/ _ \ | | | __/ _ )
13 // | \__/\ (_) | (_| | __/ |___| | || __/
14 // \____/\___/ \__,_|\___\_____/_|\__\___|
15 //
16 // F i l e
17 //
18 // This program is free software; you can redistribute it and/or modify
19 // it under the terms of the GNU General Public License as published by
20 // the Free Software Foundation; either version 2 of the License, or
21 // (at your option) any later version.
22 //
23 //////////////////////////////////////////////////////////////////////////////
24 //////////////////////////////////////////////////////////////////////////////
25
26 #include "ErdCommitWizard.h"
27
28 #include "res/gui/folder.xpm"
29 #include "res/gui/form_blue.xpm"
30 #include "res/gui/form_yellow.xpm"
31 #include "res/gui/wiztest.xpm"
32
33 ErdCommitWizard::ErdCommitWizard(wxWindow* parent, xsSerializable* pConnections, const wxString& createScript): wxWizard(parent,wxID_ANY,_T("ERD commit wizard"),
34 wxBitmap(wiztest_xpm)) {
35
36 m_createScript = createScript;
37 m_pConnections = pConnections;
38 m_pSelectedDatabase = NULL;
39 m_pFirstPage = new FirstPage(this);
40
41 DatabasePage* pDbPage = new DatabasePage(this,pConnections);
42 m_pFirstPage->SetNext(pDbPage);
43 pDbPage->SetPrev(m_pFirstPage);
44
45 BackupPage* pBackPage = new BackupPage(this);
46 pDbPage->SetNext(pBackPage);
47 pBackPage->SetPrev(pDbPage);
48
49 WriteStructurePage* pWritePage = new WriteStructurePage(this);
50 pBackPage->SetNext(pWritePage);
51 pWritePage->SetPrev(pBackPage);
52
53 RestorePage* pRestorePage = new RestorePage(this);
54 pWritePage->SetNext(pRestorePage);
55 pRestorePage->SetPrev(pWritePage);
56
57 LastPage* pLastPage = new LastPage(this);
58 pRestorePage->SetNext(pLastPage);
59 pLastPage->SetPrev(pRestorePage);
60
61 GetPageAreaSizer()->Add(m_pFirstPage);
62 }
63
64 ErdCommitWizard::~ErdCommitWizard() {
65 }
66
67 wxWizardPageSimple* ErdCommitWizard::GetFirstPage() {
68 return m_pFirstPage;
69 }
70
71
72 FirstPage::FirstPage(wxWizard* parent):wxWizardPageSimple(parent) {
73 wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
74 mainSizer->Add(
75 new wxStaticText(this, wxID_ANY, wxString(_("Wizard for creating db structure\n")) +
76 wxString(_("on ERD diagram base.\n\n")) +
77 wxString(_("All tables which really exist in\n")) +
78 wxString(_("the database, will be deleted during\n")) +
79 wxString(_("this process, but you can do a backup\n")) +
80 wxString(_("in the second step."))),
81 0,wxALL,5);
82 SetSizer(mainSizer);
83 mainSizer->Fit(this);
84 }
85
86 FirstPage::~FirstPage() {
87 }
88
89
90
91 DatabasePage::DatabasePage(ErdCommitWizard* parent, xsSerializable* pConnections):wxWizardPageSimple(parent) {
92 m_pParentWizard = parent;
93 m_pConnections = pConnections;
94 m_mainSizer = new wxFlexGridSizer( 2, 1, 0, 0 );
95 m_mainSizer->AddGrowableCol( 0 );
96 m_mainSizer->AddGrowableRow( 1 );
97 m_mainSizer->SetFlexibleDirection( wxBOTH );
98 m_mainSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
99 m_mainSizer->Add(new wxStaticText(this,wxID_ANY,_("Select target database:")));
100 m_treeDatabases = new wxTreeCtrl(this,wxID_ANY,wxDefaultPosition, wxDefaultSize, wxTR_DEFAULT_STYLE|wxTR_HIDE_ROOT);
101 m_mainSizer->Add(m_treeDatabases, 0, wxALL|wxEXPAND, 5 );
102
103
104 SetSizer(m_mainSizer);
105 m_mainSizer->Fit(this);
106 LoadDatabases();
107 }
108
109 DatabasePage::~DatabasePage() {
110
111 }
112
113 void DatabasePage::LoadDatabases() {
114 // clear items from tree
115 m_treeDatabases->DeleteAllItems();
116 // create imageList for icons
117 wxImageList *pImageList = new wxImageList(16,16,true,3);
118 pImageList->Add(wxIcon(folder_xpm)); // folder icon
119 pImageList->Add(wxIcon(form_blue_xpm)); // table icon
120 pImageList->Add(wxIcon(form_yellow_xpm)); // view icon
121 m_treeDatabases->SetImageList(pImageList);
122
123 wxTreeItemId totalRootID = m_treeDatabases->AddRoot(wxString::Format(_("Databases")),-1);
124
125 // ---------------- load connections ----------------------------
126 SerializableList::compatibility_iterator connectionNode = m_pConnections->GetFirstChildNode();
127 while(connectionNode) {
128 DbConnection* pDbCon = (DbConnection*) wxDynamicCast(connectionNode->GetData(),DbConnection);
129 if (pDbCon) {
130 wxTreeItemId rootID = m_treeDatabases->AppendItem(totalRootID,wxString::Format(_("Databases (%s)"),pDbCon->GetServerName().c_str()),-1,-1, new DbItem(pDbCon));
131 m_treeDatabases->Expand(rootID);
132 // ----------------------- load databases -------------------------------
133 SerializableList::compatibility_iterator dbNode = pDbCon->GetFirstChildNode();
134 while(dbNode) {
135 Database* pDatabase = wxDynamicCast(dbNode->GetData(), Database);
136 if (pDatabase) {
137 //wxTreeItemId dbID = m_treeDatabases->AppendItem(rootID,pDatabase->getName(),-1,-1, new DbItem(pDatabase,NULL));//new DbDatabase(db->getName()));
138 m_treeDatabases->AppendItem(rootID,pDatabase->GetName(),-1,-1, new DbItem(pDatabase));//new DbDatabase(db->getName()));
139
140 }
141 dbNode = dbNode->GetNext();
142 }
143 // -----------------------------------------------------------------------
144 }
145 connectionNode = connectionNode->GetNext();
146 }
147
148 }
149
150 bool DatabasePage::TransferDataFromWindow() {
151 DbItem* data = (DbItem*) m_treeDatabases->GetItemData(m_treeDatabases->GetSelection());
152 if (data) {
153 Database* pdb = wxDynamicCast(data->GetData(), Database);
154 if (pdb) {
155 m_pParentWizard->SetSelectedDatabase(pdb);
156 }
157 }
158 //TODO:LANG:
159 if (!m_pParentWizard->GetSelectedDatabase()) {
160 wxMessageBox(_("The first time you have to select a target database!"));
161 return false;
162 }
163 return true;
164 }
165 BackupPage::BackupPage(ErdCommitWizard* parent):wxWizardPageSimple(parent) {
166 m_pParentWizard = parent;
167 backuped = false;
168
169 m_mainSizer = new wxFlexGridSizer(8,1,0,0);
170 m_mainSizer->AddGrowableCol(0);
171 m_mainSizer->SetFlexibleDirection(wxBOTH);
172 m_mainSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
173
174 m_mainSizer->Add(new wxStaticText(this,wxID_ANY, _("Backup data file")));
175 m_pFileData = new wxFilePickerCtrl(this,wxID_ANY,_("data.sql"),_("Select file"), wxT("SQL file *.sql|*.sql"),wxDefaultPosition, wxDefaultSize, wxFLP_USE_TEXTCTRL|wxFLP_SAVE|wxFLP_OVERWRITE_PROMPT);
176 m_mainSizer->Add(m_pFileData, 0, wxALL|wxEXPAND, 5 );
177 m_mainSizer->Add(new wxStaticLine(this), 0, wxALL|wxEXPAND, 5 );
178 m_pCheckStructure = new wxCheckBox(this, wxID_ANY,_("Backup database structure"));
179 m_mainSizer->Add(m_pCheckStructure, 0, wxALL|wxEXPAND, 5 );
180 m_mainSizer->Add(new wxStaticText(this,wxID_ANY, _("Backup structure file")));
181 m_pFileStructure = new wxFilePickerCtrl(this,wxID_ANY,_("structure.sql"),_("Select file"), wxT("SQL file *.sql|*.sql"),wxDefaultPosition, wxDefaultSize, wxFLP_USE_TEXTCTRL|wxFLP_SAVE|wxFLP_OVERWRITE_PROMPT);
182 m_mainSizer->Add(m_pFileStructure, 0, wxALL|wxEXPAND, 5 );
183 m_mainSizer->Add(new wxStaticLine(this), 0, wxALL|wxEXPAND, 5 );
184
185 m_btnBackup = new wxButton(this, wxID_ANY, _("Backup!"));
186 m_mainSizer->Add(m_btnBackup,0,wxALIGN_CENTER_HORIZONTAL,0);
187
188 SetSizer(m_mainSizer);
189 m_mainSizer->Fit(this);
190
191 m_pFileStructure->Connect(wxEVT_UPDATE_UI, wxUpdateUIEventHandler(BackupPage::OnFileStructureUI),NULL,this);
192 m_btnBackup->Connect(wxEVT_UPDATE_UI,wxUpdateUIEventHandler(BackupPage::OnBtnBackupUI),NULL,this);
193 m_btnBackup->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(BackupPage::OnBtnBackupClick),NULL,this);
194
195 }
196
197 bool BackupPage::TransferDataFromWindow() {
198 if (!backuped) {
199 wxMessageDialog dlg(this, _("Backing up database data before changing the structure is really good idea. Do you want to continue without doing so?"),_("Backup"),wxYES_NO);
200 if (dlg.ShowModal() == wxID_YES) {
201 return true;
202 } else return false;
203 }
204 return true;
205 }
206
207 BackupPage::~BackupPage() {
208 }
209
210 void BackupPage::OnFileStructureUI(wxUpdateUIEvent& event) {
211 if (m_pCheckStructure) {
212 event.Enable(m_pCheckStructure->IsChecked());
213 } else event.Enable(false);
214 }
215
216 void BackupPage::OnBtnBackupClick(wxCommandEvent& event) {
217 m_pParentWizard->GetSelectedDatabase()->RefreshChildrenDetails();
218 wxString dumpResult = wxT("");
219 DumpClass* pDump = new DumpClass(m_pParentWizard->GetSelectedDatabase()->GetDbAdapter(), m_pParentWizard->GetSelectedDatabase(), m_pFileData->GetPath());
220 if (pDump) dumpResult = pDump->DumpData();
221
222 wxMessageBox(_("Data saved! ")+ dumpResult );
223
224 if (m_pCheckStructure->IsChecked()) {
225 wxString retStr;
226
227
228 SerializableList::compatibility_iterator tabNode = m_pParentWizard->GetSelectedDatabase()->GetFirstChildNode();
229 while(tabNode) {
230 Table* tab = wxDynamicCast(tabNode->GetData(),Table);
231 if (tab) {
232 retStr.append(m_pParentWizard->GetSelectedDatabase()->GetDbAdapter()->GetCreateTableSql(tab,true));
233 }
234 tabNode = tabNode->GetNext();
235 }
236
237 tabNode = m_pParentWizard->GetSelectedDatabase()->GetFirstChildNode();
238 while(tabNode) {
239 View* view = wxDynamicCast(tabNode->GetData(),View);
240 if (view) {
241 retStr.append(m_pParentWizard->GetSelectedDatabase()->GetDbAdapter()->GetCreateViewSql(view,true));
242 }
243 tabNode = tabNode->GetNext();
244 }
245
246 tabNode = m_pParentWizard->GetSelectedDatabase()->GetFirstChildNode();
247 while(tabNode) {
248 Table* tab = wxDynamicCast(tabNode->GetData(),Table);
249 if (tab) {
250 retStr.append(m_pParentWizard->GetSelectedDatabase()->GetDbAdapter()->GetAlterTableConstraintSql(tab));
251 }
252 tabNode = tabNode->GetNext();
253 }
254
255 wxTextFile pTextFile(m_pFileStructure->GetPath());
256
257 if (pTextFile.Exists()) {
258 pTextFile.Open();
259 pTextFile.Clear();
260 } else {
261 pTextFile.Create();
262 pTextFile.Open();
263 }
264 if (pTextFile.IsOpened()) {
265 pTextFile.AddLine(retStr);
266 pTextFile.Write(wxTextFileType_None, wxConvUTF8);
267 pTextFile.Close();
268 wxMessageBox(_("Structure saved!"));
269 }
270
271 }
272 backuped = true;
273 }
274
275 void BackupPage::OnBtnBackupUI(wxUpdateUIEvent& event) {
276 if (m_pCheckStructure->IsChecked()) {
277 event.Enable((!m_pFileData->GetPath().IsEmpty())&&(!m_pFileStructure->GetPath().IsEmpty()));
278 } else event.Enable(!m_pFileData->GetPath().IsEmpty());
279 }
280
281 bool BackupPage::TransferDataToWindow() {
282 backuped = false;
283 return true;
284 }
285
286 WriteStructurePage::WriteStructurePage(ErdCommitWizard* parent):wxWizardPageSimple(parent) {
287 m_pParentWizard = parent;
288 m_mainSizer = new wxFlexGridSizer(1,0,0);
289 m_mainSizer->AddGrowableCol(0);
290 m_mainSizer->AddGrowableRow(1);
291 m_mainSizer->SetFlexibleDirection(wxBOTH);
292 m_mainSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
293
294 m_mainSizer->Add(new wxStaticText(this, wxID_ANY,_("Write log:")));
295
296 m_txLog = new wxTextCtrl(this, wxID_ANY,wxT(""),wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY);
297 m_mainSizer->Add(m_txLog,0,wxEXPAND,5);
298 m_mainSizer->Add(new wxStaticLine(this),0, wxEXPAND,2);
299
300 wxBoxSizer* pSizer = new wxBoxSizer(wxHORIZONTAL);
301 m_btnWrite = new wxButton(this, wxID_ANY,_("Write !!"));
302 m_btnShowSql = new wxButton(this, wxID_ANY, _("Show SQL"));
303
304 pSizer->Add(m_btnWrite, wxALIGN_CENTER_HORIZONTAL);
305 pSizer->Add(m_btnShowSql, wxALIGN_CENTER_HORIZONTAL);
306
307 m_mainSizer->Add(pSizer,0,wxEXPAND,0);
308
309 SetSizer(m_mainSizer);
310 m_mainSizer->Fit(this);
311
312 m_btnWrite->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(WriteStructurePage::OnBtnWriteClick),NULL, this);
313 m_btnShowSql->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(WriteStructurePage::OnBtnShowSqlClick), NULL, this);
314
315
316 }
317
318 WriteStructurePage::~WriteStructurePage() {
319 }
320
321 void WriteStructurePage::OnBtnShowSqlClick(wxCommandEvent& event) {
322 CodePreviewDialog dlg(this, m_pParentWizard->GetCreateScript());
323 dlg.ShowModal();
324 }
325
326 void WriteStructurePage::OnBtnWriteClick(wxCommandEvent& event) {
327 DatabaseLayerPtr pDbLayer(NULL);
328 try {
329 pDbLayer = m_pParentWizard->GetSelectedDatabase()->GetDbAdapter()->GetDatabaseLayer(m_pParentWizard->GetSelectedDatabase()->GetName());
330 if (pDbLayer) {
331 pDbLayer->BeginTransaction();
332
333 wxString useSql = m_pParentWizard->GetSelectedDatabase()->GetDbAdapter()->GetUseDb(m_pParentWizard->GetSelectedDatabase()->GetName());
334 if (!useSql.IsEmpty()) pDbLayer->RunQuery(wxString::Format(wxT("USE %s"), m_pParentWizard->GetSelectedDatabase()->GetName().c_str()));
335
336
337 pDbLayer->RunQuery(m_pParentWizard->GetCreateScript());
338
339 pDbLayer->Commit();
340 pDbLayer->Close();
341 m_txLog->SetValue(_("Data structure written successfully!"));
342 commited = true;
343 }
344
345 } catch (DatabaseLayerException& e) {
346 if (pDbLayer) {
347 pDbLayer->RollBack();
348 pDbLayer->Close();
349 }
350 wxString errorMessage = wxString::Format(_("Error (%d): %s"), e.GetErrorCode(), e.GetErrorMessage().c_str());
351 m_txLog->SetValue(errorMessage);
352 } catch( ... ) {
353 if (pDbLayer) {
354 pDbLayer->RollBack();
355 pDbLayer->Close();
356 }
357 m_txLog->SetValue(_("Unknown error!"));
358 }
359 }
360
361 bool WriteStructurePage::TransferDataFromWindow() {
362 if (!commited) {
363 wxMessageDialog dlg(this, _("Would you like to continue without writing the database structure?"),_("Question"),wxYES_NO);
364 if (dlg.ShowModal() == wxID_YES) {
365 return true;
366 } else return false;
367 }
368 return true;
369 }
370
371 bool WriteStructurePage::TransferDataToWindow() {
372 commited = false;
373 return true;
374 }
375
376 LastPage::LastPage(wxWizard* parent):wxWizardPageSimple(parent) {
377 wxBoxSizer* mainSizer = new wxBoxSizer(wxVERTICAL);
378 mainSizer->Add(
379 new wxStaticText(this, wxID_ANY, _("Writing structure ended.\n")),
380 0,wxALL,5);
381 SetSizer(mainSizer);
382 mainSizer->Fit(this);
383 }
384
385 LastPage::~LastPage() {
386 }
387
388 RestorePage::RestorePage(ErdCommitWizard* parent):wxWizardPageSimple(parent) {
389 m_pParentWizard = parent;
390 m_mainSizer = new wxFlexGridSizer(1, 0,0);
391 m_mainSizer->SetFlexibleDirection(wxBOTH);
392 m_mainSizer->SetNonFlexibleGrowMode( wxFLEX_GROWMODE_SPECIFIED );
393 m_mainSizer->AddGrowableCol(0);
394 m_mainSizer->AddGrowableRow(4);
395
396
397 m_mainSizer->Add(new wxStaticText(this, wxID_ANY, _("File for data restore:")), 0, wxEXPAND, 2);
398
399 m_restoreFile = new wxFilePickerCtrl(this,wxID_ANY,wxT(""),_("Select file"), wxT("SQL file *.sql|*.sql"),wxDefaultPosition, wxDefaultSize, wxFLP_USE_TEXTCTRL|wxFLP_OPEN|wxFLP_FILE_MUST_EXIST);
400 m_mainSizer->Add(m_restoreFile, 0, wxEXPAND, 2);
401 m_mainSizer->Add(new wxStaticLine(this),0, wxEXPAND,2);
402
403 m_mainSizer->Add(new wxStaticText(this, wxID_ANY, _("Restore log:")), 0, wxEXPAND, 2);
404
405 m_txLog = new wxTextCtrl(this, wxID_ANY, wxT(""),wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE | wxTE_READONLY);
406 m_mainSizer->Add(m_txLog,0,wxEXPAND,2 );
407 m_mainSizer->Add(new wxStaticLine(this),0, wxEXPAND,2);
408
409 m_btnRestore = new wxButton(this,wxID_ANY,_("Restore"));
410 m_mainSizer->Add(m_btnRestore,0, wxALIGN_CENTER_HORIZONTAL, 5);
411
412 SetSizer(m_mainSizer);
413 m_mainSizer->Fit(this);
414
415 m_btnRestore->Connect(wxEVT_UPDATE_UI, wxUpdateUIEventHandler(RestorePage::OnBtnRestoreUI), NULL, this);
416 m_btnRestore->Connect(wxEVT_COMMAND_BUTTON_CLICKED, wxCommandEventHandler(RestorePage::OnBtnRestoreClick), NULL, this);
417
418 }
419
420 RestorePage::~RestorePage() {
421 }
422 void RestorePage::OnBtnRestoreClick(wxCommandEvent& event)
423 {
424 if (m_restoreFile->GetPath().IsEmpty()) return;
425
426 DatabaseLayerPtr pDbLayer(NULL);
427 Database* pDb = m_pParentWizard->GetSelectedDatabase();
428 Clear();
429 try {
430 wxFileInputStream input(m_restoreFile->GetPath());
431 wxTextInputStream text( input, wxT(";"), wxConvUTF8 );
432 wxString command = wxT("");
433
434 pDbLayer = pDb->GetDbAdapter()->GetDatabaseLayer(pDb->GetName());
435 pDbLayer->BeginTransaction();
436
437 wxString useSql = pDb->GetDbAdapter()->GetUseDb(pDb->GetName());
438 if (!useSql.IsEmpty()) pDbLayer->RunQuery(wxString::Format(wxT("USE %s"), pDb->GetName().c_str()));
439
440 while (!input.Eof()) {
441 wxString line = text.ReadLine();
442 //dialog.AppendText(line);
443 int index = line.Find(wxT("--"));
444 if (index != wxNOT_FOUND) line = line.Mid(0,index);
445 command += line;
446 if (line.Find(wxT(";")) != wxNOT_FOUND) {
447 AppendSeparator();
448 AppendComment(wxT("Run SQL command:"));
449 AppendText(command);
450 pDbLayer->RunQuery(command);
451 AppendComment(_("Successful!"));
452 command.Clear();
453 }
454 }
455 pDbLayer->Commit();
456 pDbLayer->Close();
457 } catch (DatabaseLayerException& e) {
458 if (pDbLayer) {
459 pDbLayer->RollBack();
460 pDbLayer->Close();
461 }
462 wxString errorMessage = wxString::Format(_("Error (%d): %s"), e.GetErrorCode(), e.GetErrorMessage().c_str());
463
464 AppendComment(_("Fail!"));
465 AppendComment(errorMessage);
466 wxMessageDialog dlg(this,errorMessage,_("DB Error"),wxOK | wxCENTER | wxICON_ERROR);
467 dlg.ShowModal();
468 } catch( ... ) {
469 if (pDbLayer) {
470 pDbLayer->RollBack();
471 pDbLayer->Close();
472 }
473 wxMessageDialog dlg(this,_("Unknown error."),_("DB Error"),wxOK | wxCENTER | wxICON_ERROR);
474 dlg.ShowModal();
475 }
476
477 }
478
479 void RestorePage::OnBtnRestoreUI(wxUpdateUIEvent& event)
480 {
481 event.Enable(!m_restoreFile->GetPath().IsEmpty());
482 }
483 void RestorePage::AppendText(const wxString& txt)
484 {
485 m_text.Append(txt + wxT("\n"));
486 m_txLog->SetValue(m_text);
487 }
488
489 void RestorePage::Clear()
490 {
491 m_text.clear();
492 m_txLog->SetValue(m_text);
493 }
494
495 void RestorePage::AppendSeparator()
496 {
497 m_text.Append(wxT("*********************************************************\n"));
498 m_txLog->SetValue(m_text);
499 }
500
501 void RestorePage::AppendComment(const wxString& txt)
502 {
503 m_text.Append(wxNow() + wxT(" ") + txt + wxT("\n"));
504 m_txLog->SetValue(m_text);
505 }