forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbViewerPanel.cpp
843 lines (740 loc) · 32.1 KB
/
DbViewerPanel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
#include "DbViewerPanel.h"
#include "DbSettingDialog.h"
#include "SqlCommandPanel.h"
#include "AdapterSelectDlg.h"
#include "window_locker.h"
#include "globals.h"
#include "ErdPanel.h"
#include <wx/wfstream.h>
#include <wx/imaglist.h>
#include <wx/xrc/xmlres.h>
DbViewerPanel::DbViewerPanel(wxWindow *parent, wxWindow* notebook, IManager* pManager)
: _DbViewerPanel(parent)
{
m_pNotebook = notebook;
m_pGlobalParent = parent;
m_mgr = pManager;
m_pPrevPanel = NULL;
m_SuppressUpdate = false;
MSWSetNativeTheme(m_treeDatabases);
m_pDbAdapter = NULL;
m_pConnections = new xsSerializable();
m_pThumbnail = new wxSFThumbnail(this);
m_pThumbnail->SetSizeHints(wxSize(-1, 200));
GetSizer()->Add(m_pThumbnail, 0, wxEXPAND);
GetSizer()->Hide(m_pThumbnail);
GetSizer()->Layout();
// replace the icons...
BitmapLoader *bmpLoader = pManager->GetStdIcons();
m_toolBar1->AddTool( XRCID("IDT_DBE_CONNECT"), _("Open connection"), bmpLoader->LoadBitmap(wxT("db-explorer/16/connect")), wxNullBitmap, wxITEM_NORMAL, _("Open new connection"), _("Open new connection"), NULL );
m_toolBar1->AddTool( XRCID("IDT_DBE_CLOSE_CONNECTION"), _("tool"), bmpLoader->LoadBitmap(wxT("db-explorer/16/disconnect")), wxNullBitmap, wxITEM_NORMAL, _("Close selected connection"), _("Close selected connection"), NULL );
m_toolBar1->AddTool( XRCID("IDT_DBE_REFRESH"), _("tool"), bmpLoader->LoadBitmap(wxT("db-explorer/16/database_refresh")), wxNullBitmap, wxITEM_NORMAL, _("Refresh View"), wxEmptyString, NULL );
m_toolBar1->AddTool( XRCID("IDT_DBE_ERD"), _("ERD"), bmpLoader->LoadBitmap(wxT("db-explorer/16/table")), wxNullBitmap, wxITEM_NORMAL, _("Open ERD View"), wxEmptyString, NULL );
m_toolBar1->AddTool( XRCID("IDT_DBE_PREVIEW"), _("Show ERD Thumbnail"), bmpLoader->LoadBitmap(wxT("db-explorer/16/thumbnail")), _("Show ERD Thumbnail"), wxITEM_CHECK);
m_toolBar1->Realize();
Layout();
m_mgr->GetEditorPaneNotebook()->Connect(wxEVT_COMMAND_BOOK_PAGE_CHANGED,NotebookEventHandler(DbViewerPanel::OnPageChanged), NULL, this);
m_mgr->GetEditorPaneNotebook()->Connect(wxEVT_COMMAND_BOOK_PAGE_CLOSING,NotebookEventHandler(DbViewerPanel::OnPageClosing), NULL, this);
this->Connect( XRCID("IDT_DBE_CONNECT"), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( DbViewerPanel::OnConnectClick ) );
//this->Connect( XRCID("IDT_DBE_CONNECT"), wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DbViewerPanel::OnConnectUI ) );
this->Connect( XRCID("IDT_DBE_CLOSE_CONNECTION"), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( DbViewerPanel::OnToolCloseClick ) );
this->Connect( XRCID("IDT_DBE_CLOSE_CONNECTION"), wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DbViewerPanel::OnToolCloseUI ) );
this->Connect( XRCID("IDT_DBE_REFRESH"), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( DbViewerPanel::OnRefreshClick ) );
this->Connect( XRCID("IDT_DBE_REFRESH"), wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DbViewerPanel::OnRefreshUI ) );
this->Connect( XRCID("IDT_DBE_ERD"), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( DbViewerPanel::OnERDClick ) );
this->Connect( XRCID("IDT_DBE_PREVIEW"), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( DbViewerPanel::OnShowThumbnail ) );
}
DbViewerPanel::~DbViewerPanel()
{
m_mgr->GetEditorPaneNotebook()->Disconnect(wxEVT_COMMAND_BOOK_PAGE_CHANGED,NotebookEventHandler(DbViewerPanel::OnPageChanged), NULL, this);
m_mgr->GetEditorPaneNotebook()->Disconnect(wxEVT_COMMAND_BOOK_PAGE_CLOSING,NotebookEventHandler(DbViewerPanel::OnPageClosing), NULL, this);
this->Disconnect( XRCID("IDT_DBE_CONNECT"), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( DbViewerPanel::OnConnectClick ) );
//this->Disconnect( XRCID("IDT_DBE_CONNECT"), wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DbViewerPanel::OnConnectUI ) );
this->Disconnect( XRCID("IDT_DBE_CLOSE_CONNECTION"), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( DbViewerPanel::OnToolCloseClick ) );
this->Disconnect( XRCID("IDT_DBE_CLOSE_CONNECTION"), wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DbViewerPanel::OnToolCloseUI ) );
this->Disconnect( XRCID("IDT_DBE_REFRESH"), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( DbViewerPanel::OnRefreshClick ) );
this->Disconnect( XRCID("IDT_DBE_REFRESH"), wxEVT_UPDATE_UI, wxUpdateUIEventHandler( DbViewerPanel::OnRefreshUI ) );
this->Disconnect( XRCID("IDT_DBE_ERD"), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( DbViewerPanel::OnERDClick ) );
this->Disconnect( XRCID("IDT_DBE_PREVIEW"), wxEVT_COMMAND_TOOL_CLICKED, wxCommandEventHandler( DbViewerPanel::OnShowThumbnail ) );
delete m_pDbAdapter;
}
void DbViewerPanel::OnConnectClick(wxCommandEvent& event)
{
DbSettingDialog dlg(this, m_mgr->GetTheApp()->GetTopWindow());
dlg.ShowModal();
RefreshDbView();
}
void DbViewerPanel::OnConnectUI(wxUpdateUIEvent& event)
{
event.Enable( !m_pConnections->HasChildren() );
}
void DbViewerPanel::OnItemActivate(wxTreeEvent& event)
{
DbItem* item = (DbItem*) m_treeDatabases->GetItemData(event.GetItem());
if (item) {
wxMouseState cState = wxGetMouseState();
wxString pagename;
if (DBETable* tab = wxDynamicCast(item->GetData(), DBETable)) {
if( cState.ControlDown() ) {
pagename = CreatePanelName(tab, DbViewerPanel::Erd);
ErdPanel *erdpanel = new ErdPanel(m_pNotebook,tab->GetDbAdapter()->Clone(),m_pConnections, (DBETable*) tab->Clone() );
AddEditorPage(erdpanel, pagename);
m_pagesAdded.Add(pagename);
} else {
#if defined (__WXMSW__)
clWindowUpdateLocker locker(m_mgr->GetEditorPaneNotebook());
#endif
pagename = CreatePanelName(tab, DbViewerPanel::Sql);
if(!DoSelectPage(pagename)) {
SQLCommandPanel *sqlpage = new SQLCommandPanel(m_pNotebook,tab->GetDbAdapter()->Clone(),tab->GetParentName(),tab->GetName());
AddEditorPage(sqlpage, pagename);
m_pagesAdded.Add(pagename);
}
}
}
if (View* pView = wxDynamicCast(item->GetData(), View)) {
pagename = CreatePanelName(pView, DbViewerPanel::Sql);
if(!DoSelectPage(pagename)) {
SQLCommandPanel *sqlpage = new SQLCommandPanel(m_pNotebook,pView->GetDbAdapter()->Clone(),pView->GetParentName(),pView->GetName());
AddEditorPage(sqlpage, pagename);
m_pagesAdded.Add(pagename);
}
}
if (Database* db = wxDynamicCast(item->GetData(), Database)) {
if( cState.ControlDown() ) {
pagename = CreatePanelName(db, DbViewerPanel::Erd);
ErdPanel *erdpanel = new ErdPanel(m_pNotebook,db->GetDbAdapter()->Clone(),m_pConnections,(Database*)db->Clone());
AddEditorPage(erdpanel, pagename);
} else {
pagename = CreatePanelName(db, DbViewerPanel::Sql);
if(!DoSelectPage(pagename)) {
SQLCommandPanel *sqlpage = new SQLCommandPanel(m_pNotebook,db->GetDbAdapter()->Clone(),db->GetName(),wxT(""));
#ifndef __WXMSW__
sqlpage->Show();
#endif
AddEditorPage(sqlpage, pagename);
m_pagesAdded.Add(pagename);
}
}
}
}
}
void DbViewerPanel::OnRefreshClick(wxCommandEvent& event)
{
// Refresh all connections
wxTreeItemId root = m_treeDatabases->GetRootItem();
if(root.IsOk()) {
wxTreeItemIdValue cookie;
wxTreeItemId child = m_treeDatabases->GetFirstChild(root, cookie);
while(child.IsOk()) {
DbItem* data = (DbItem*) m_treeDatabases->GetItemData(child);
if (data) {
DbConnection* pCon = wxDynamicCast(data->GetData(), DbConnection);
if (pCon) pCon->RefreshChildren();
Database* pDb = wxDynamicCast(data->GetData(),Database);
if (pDb) pDb->RefreshChildren(false);
DBETable* pTab = wxDynamicCast(data->GetData(), DBETable);
if (pTab) pTab->RefreshChildren();
}
child = m_treeDatabases->GetNextChild(root, cookie);
}
}
// Refresh the view
RefreshDbView();
}
void DbViewerPanel::OnRefreshUI(wxUpdateUIEvent& event)
{
event.Enable(m_pConnections->HasChildren());
}
void DbViewerPanel::RefreshDbView()
{
// clear items from tree
m_treeDatabases->DeleteAllItems();
// create imageList for icons
wxImageList *pImageList = new wxImageList(16, 16, true,3);
pImageList->Add(m_mgr->GetStdIcons()->LoadBitmap(wxT("toolbars/16/standard/file_open"))); // folder icon
pImageList->Add(m_mgr->GetStdIcons()->LoadBitmap(wxT("db-explorer/16/table"))); // table icon
pImageList->Add(m_mgr->GetStdIcons()->LoadBitmap(wxT("toolbars/16/search/find"))); // view icon
pImageList->Add(m_mgr->GetStdIcons()->LoadBitmap(wxT("db-explorer/16/database"))); // database
pImageList->Add(m_mgr->GetStdIcons()->LoadBitmap(wxT("db-explorer/16/column"))); // column
m_treeDatabases->AssignImageList(pImageList);
wxTreeItemId totalRootID = m_treeDatabases->AddRoot(wxString::Format(wxT("Databases")),-1);
// ---------------- load connections ----------------------------
SerializableList::compatibility_iterator connectionNode = m_pConnections->GetFirstChildNode();
while(connectionNode) {
DbConnection* pDbCon = (DbConnection*) wxDynamicCast(connectionNode->GetData(),DbConnection);
if (pDbCon) {
wxTreeItemId rootID = m_treeDatabases->AppendItem(totalRootID,
wxString::Format(wxT("Databases (%s)"),pDbCon->GetServerName().c_str()),
3,
3,
new DbItem(pDbCon));
// ----------------------- load databases -------------------------------
SerializableList::compatibility_iterator dbNode = pDbCon->GetFirstChildNode();
while(dbNode) {
Database* pDatabase = wxDynamicCast(dbNode->GetData(), Database);
if (pDatabase) {
wxTreeItemId dbID = m_treeDatabases->AppendItem(rootID,
pDatabase->GetName(),
3,
3,
new DbItem(pDatabase));
m_treeDatabases->Expand(rootID);
wxTreeItemId idFolder = m_treeDatabases->AppendItem(dbID, wxT("Tables"),0,0,NULL);
//m_treeDatabases->Expand(dbID);
// ----------------------------- load tables ----------------------------------
SerializableList::compatibility_iterator tabNode = pDatabase->GetFirstChildNode();
while(tabNode) {
DBETable* pTable = wxDynamicCast(tabNode->GetData(), DBETable);
if (pTable) {
//wxTreeItemId tabID = m_treeDatabases->AppendItem(idFolder,pTable->getName(),1,-1,new DbItem(NULL,pTable)); //NULL);
wxTreeItemId tabID = m_treeDatabases->AppendItem(idFolder,
pTable->GetName(),
1,
1,
new DbItem(pTable)); //NULL);
///////////////////////////////////////////////////////////
// Add the columns
///////////////////////////////////////////////////////////
SerializableList::compatibility_iterator columnNode = pTable->GetFirstChildNode();
while(columnNode) {
DBEColumn *col = wxDynamicCast(columnNode->GetData(), DBEColumn);
if(col) {
m_treeDatabases->AppendItem(tabID,
col->FormatName().c_str(),
4,
4,
new DbItem(col));
}
columnNode = columnNode->GetNext();
}
}
tabNode = tabNode->GetNext();
}
// ----------------------------------------------------------------------------
idFolder = m_treeDatabases->AppendItem(dbID, wxT("Views"),0,0,NULL);
//m_treeDatabases->Expand(dbID);
// ----------------------------- load views ----------------------------------
tabNode = pDatabase->GetFirstChildNode();
while(tabNode) {
View* pView = wxDynamicCast(tabNode->GetData(),View);
if (pView) {
//wxTreeItemId tabID = m_treeDatabases->AppendItem(idFolder,pTable->getName(),1,-1,new DbItem(NULL,pTable)); //NULL);
m_treeDatabases->AppendItem(idFolder,
pView->GetName(),
2,
2,
new DbItem(pView)); //NULL);
}
tabNode = tabNode->GetNext();
}
// ----------------------------------------------------------------------------
}
dbNode = dbNode->GetNext();
}
// -----------------------------------------------------------------------
}
connectionNode = connectionNode->GetNext();
}
}
void DbViewerPanel::OnItemSelectionChange(wxTreeEvent& event)
{
wxUnusedVar(event);
}
void DbViewerPanel::OnERDClick(wxCommandEvent& event)
{
AdapterSelectDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_pNotebook, m_mgr, m_pConnections);
dlg.ShowModal();
}
void DbViewerPanel::OnDnDStart(wxTreeEvent& event)
{
ShapeList lstDnD;
lstDnD.DeleteContents(true);
DbItem* item = (DbItem*) m_treeDatabases->GetItemData(event.GetItem());
ErdPanel* pPanel = wxDynamicCast(m_mgr->GetActivePage(),ErdPanel);
if (item != NULL) {
if (pPanel) {
DBETable* table = wxDynamicCast(item->GetData(),DBETable);
if (table) {
table = (DBETable*) table->Clone();
wxSFShapeBase *pShape = new dndTableShape(table);
lstDnD.Append(pShape);
pPanel->GetCanvas()->DoDragDrop(lstDnD);
}
View* view = wxDynamicCast(item->GetData(),View);
if (view) {
view = (View*) view->Clone();
wxSFShapeBase *pShape = new dndTableShape(view);
lstDnD.Append(pShape);
pPanel->GetCanvas()->DoDragDrop(lstDnD);
}
}
}
}
void DbViewerPanel::OnItemRightClick(wxTreeEvent& event)
{
m_pEditedDatabase = NULL;
m_pEditedConnection = NULL;
m_selectedID = event.GetItem();
DbItem* item = (DbItem*) m_treeDatabases->GetItemData(m_selectedID);
wxMenu menu;
int c = 0;
if (item) {
Database* db = wxDynamicCast(item->GetData(),Database);
if (db) {
// menu.Append(XRCID("IDR_DBVIEWER_ADD_TABLE"),_("Add table"),_("Run SQL command for creating Table"));
menu.Append(XRCID("IDR_DBVIEWER_DROP_DATABASE"), _("Drop database"), _("Run SQL command for deleting Database"));
c++;
c++;
menu.AppendSeparator();
menu.Append(XRCID("IDR_DBVIEWER_ERD_DB"), _("Create ERD from DB"),_("Create ERD diagram from database"));
menu.Append(XRCID("IDR_DBVIEWER_CLASS_DB"), _("Create classes from DB"), _("Create c++ classes for selected database"));
c++;
c++;
menu.AppendSeparator();
menu.Append(XRCID("IDR_DBVIEWER_IMPORT_DATABASE"), _("Import database from file"), _("Run SQL commands stored in *.sql file"));
menu.Append(XRCID("IDR_DBVIEWER_EXPORT_DATABASE"), _("Export database to file"), _("Export database CREATE SQL statements into *.sql file"));
c++;
c++;
menu.AppendSeparator();
menu.Append(XRCID("IDR_DBVIEWER_DUMP_DATABASE"), _("Dump data to file"), _("Dump data from database into .sql file"));
c++;
m_pEditedDatabase = db;
}
DBETable* tab = wxDynamicCast(item->GetData(), DBETable);
if (tab) {
menu.Append(XRCID("IDR_DBVIEWER_DROP_TABLE"),_("Drop table"),_("Run SQL command for deleting Table"));
c++;
menu.AppendSeparator();
menu.Append(XRCID("IDR_DBVIEWER_ERD_TABLE"), _("Create ERD from Table"),_("Create ERD diagram from table"));
menu.Append(XRCID("IDR_DBVIEWER_CLASS_TABLE"), _("Create classes from Table"), _("Create c++ classes for selected table"));
c++;
c++;
}
}
if ( c > 0 ) {
menu.Connect(wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&DbViewerPanel::OnPopupClick, NULL, this);
PopupMenu(&menu);
}
}
void DbViewerPanel::OnToolCloseClick(wxCommandEvent& event)
{
// Close the active connection (there is only one)
// getting selected item data
/*wxTreeItemIdValue cookie;
DbItem* data = (DbItem*) m_treeDatabases->GetItemData(m_treeDatabases->GetFirstChild(m_treeDatabases->GetRootItem(), cookie));*/
DbItem* data = (DbItem*) m_treeDatabases->GetItemData(m_treeDatabases->GetSelection());
if (data) {
DbConnection* pCon = wxDynamicCast(data->GetData(), DbConnection);
if (pCon) {
wxMessageDialog dlg(this,_("Close connection?"),_("Close"),wxYES_NO);
if (dlg.ShowModal() == wxID_YES) {
m_pConnections->GetChildrenList().DeleteContents(true);
m_pConnections->GetChildrenList().DeleteObject(pCon);
RefreshDbView();
// loop over the editor open pages and close all DbExplorer related
for(size_t i=0; i<m_pagesAdded.Count(); i++) {
m_mgr->ClosePage(m_pagesAdded.Item(i));
}
m_pagesAdded.Clear();
}
}
/* else
wxMessageBox( _("Please, select database connection in DbExplorer tree view."), _("Close"), wxOK | wxICON_WARNING ); */
}
}
void DbViewerPanel::OnToolCloseUI(wxUpdateUIEvent& event)
{
wxTreeItemId treeId = m_treeDatabases->GetSelection();
if( treeId.IsOk() )
{
DbItem* data = (DbItem*) m_treeDatabases->GetItemData( treeId );
event.Enable( data && wxDynamicCast(data->GetData(), DbConnection) );
}
else
event.Enable( false );
}
void DbViewerPanel::OnPopupClick(wxCommandEvent& evt)
{
if( !m_selectedID.IsOk() ) return;
try {
if (evt.GetId() == XRCID("IDR_DBVIEWER_ADD_DATABASE")) {
if (m_pEditedConnection) {
//TODO:LANG:
wxString dbName = wxGetTextFromUser(_("Database name"), _("Add database"));
if (!dbName.IsEmpty()) {
DatabaseLayer* pDbLayer = m_pEditedConnection->GetDbAdapter()->GetDatabaseLayer(wxT(""));
wxString sql = m_pEditedConnection->GetDbAdapter()->GetCreateDatabaseSql(dbName);
if (!sql.empty()) {
pDbLayer->RunQuery(sql);
pDbLayer->Close();
delete pDbLayer;
//TODO:LANG:
wxMessageBox(_("Database created successfully"));
RefreshDbView();
} else {
//TODO:LANG:
wxMessageDialog dlg(this, _("Can't create new db in this database engine!"), _("Error"),wxOK|wxICON_ERROR);
dlg.ShowModal();
}
}
}
} else if (evt.GetId() == XRCID("IDR_DBVIEWER_DROP_DATABASE")) {
DbItem* data = (DbItem*) m_treeDatabases->GetItemData(m_selectedID);
if (data) {
Database* pDb = (Database*) wxDynamicCast(data->GetData(),Database);
if (pDb) {
wxString dropSQL = pDb->GetDbAdapter()->GetDropDatabaseSql(pDb);
if (!dropSQL.IsEmpty()) {
//TODO:LANG:
wxMessageDialog dlg(this, wxString::Format(_("Remove database '%s'?"),pDb->GetName().c_str()),_("Drop database"),wxYES_NO);
if (dlg.ShowModal() == wxID_YES) {
DatabaseLayer* pDbLayer = pDb->GetDbAdapter()->GetDatabaseLayer(wxT(""));
pDbLayer->RunQuery(dropSQL);
pDbLayer->Close();
delete pDbLayer;
//TODO:LANG:
wxMessageBox(_("Database dropped successfully"));
RefreshDbView();
}
}
}
}
} else if (evt.GetId() == XRCID("IDR_DBVIEWER_ERD_TABLE")) {
DbItem* data = (DbItem*) m_treeDatabases->GetItemData(m_selectedID);
if (data) {
DBETable* pTab = (DBETable*) wxDynamicCast(data->GetData(),DBETable);
if (pTab) {
wxString pagename;
pagename = CreatePanelName(pTab, DbViewerPanel::Erd);
ErdPanel *erdpanel = new ErdPanel(m_pNotebook,pTab->GetDbAdapter()->Clone(),m_pConnections, (DBETable*) pTab->Clone() );
AddEditorPage(erdpanel, pagename);
m_pagesAdded.Add(pagename);
}
}
} else if (evt.GetId() == XRCID("IDR_DBVIEWER_ERD_DB")) {
DbItem* data = (DbItem*) m_treeDatabases->GetItemData(m_selectedID);
if (data) {
Database* pDb = (Database*) wxDynamicCast(data->GetData(),Database);
if (pDb) {
wxString pagename;
pagename = CreatePanelName(pDb, DbViewerPanel::Erd);
ErdPanel *erdpanel = new ErdPanel(m_pNotebook,pDb->GetDbAdapter()->Clone(),m_pConnections, (Database*) pDb->Clone() );
AddEditorPage(erdpanel, pagename);
m_pagesAdded.Add(pagename);
}
}
} else if (evt.GetId() == XRCID("IDR_DBVIEWER_CLASS_DB")) {
DbItem* data = (DbItem*) m_treeDatabases->GetItemData(m_selectedID);
if (data) {
Database* pDb = (Database*) wxDynamicCast(data->GetData(),Database);
if (pDb) {
pDb = (Database*) pDb->Clone();
// NOTE: the refresh functions must be here for propper code generation (they translate views into tables)
pDb->RefreshChildren(true);
pDb->RefreshChildrenDetails();
ClassGenerateDialog dlg(m_mgr->GetTheApp()->GetTopWindow(), pDb->GetDbAdapter(), pDb, m_mgr);
dlg.ShowModal();
delete pDb;
}
}
} else if (evt.GetId() == XRCID("IDR_DBVIEWER_CLASS_TABLE")) {
DbItem* data = (DbItem*) m_treeDatabases->GetItemData(m_selectedID);
if (data) {
DBETable* pTab = (DBETable*) wxDynamicCast(data->GetData(),DBETable);
if (pTab) {
ClassGenerateDialog dlg(GetParent(), pTab->GetDbAdapter(), (DBETable*) pTab->Clone(), m_mgr);
dlg.ShowModal();
}
}
} else if (evt.GetId() == XRCID("IDR_DBVIEWER_DROP_TABLE")) {
DbItem* data = (DbItem*) m_treeDatabases->GetItemData(m_selectedID);
if (data) {
DBETable* pTab = (DBETable*) wxDynamicCast(data->GetData(),DBETable);
if (pTab) {
//TODO:LANG:
wxMessageDialog dlg(this, wxString::Format(_("Remove table '%s'?"),pTab->GetName().c_str()),_("Drop table"),wxYES_NO);
if (dlg.ShowModal() == wxID_YES) {
DatabaseLayer* pDbLayer = pTab->GetDbAdapter()->GetDatabaseLayer(pTab->GetParentName());
pDbLayer->RunQuery(pTab->GetDbAdapter()->GetDropTableSql(pTab));
pDbLayer->Close();
delete pDbLayer;
//TODO:LANG:
wxMessageBox(_("Table dropped successfully"));
RefreshDbView();
}
}
}
} else if (evt.GetId() == XRCID("IDR_DBVIEWER_IMPORT_DATABASE")) {
DbItem* data = (DbItem*) m_treeDatabases->GetItemData(m_selectedID);
if (data) {
Database* pDb = (Database*) wxDynamicCast(data->GetData(),Database);
if (pDb) {
//TODO:LANG:
wxFileDialog dlg(this, _("Import database from SQL file ..."), wxGetCwd(), wxT(""), wxT("SQL Files (*.sql)|*.sql"), wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if(dlg.ShowModal() == wxID_OK) {
ImportDb(dlg.GetPath(), pDb);
}
}
}
RefreshDbView();
} else if (evt.GetId() == XRCID("IDR_DBVIEWER_DUMP_DATABASE")) {
DbItem* data = (DbItem*) m_treeDatabases->GetItemData(m_selectedID);
if (data) {
Database* pDb = (Database*) wxDynamicCast(data->GetData(),Database);
if (pDb) {
//TODO:LANG:
wxFileDialog dlg(this, _("Dump data into file ..."),wxT(""), pDb->GetName() + wxT(".sql"),wxT("SQL files (*.sql)|*.sql"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if( dlg.ShowModal() == wxID_OK ) {
DumpClass* dump = new DumpClass(pDb->GetDbAdapter(),pDb, dlg.GetPath());
dump->DumpData();
wxMessageBox(_("Data was saved to ") + dlg.GetPath() );
}
}
}
} else if (evt.GetId() == XRCID("IDR_DBVIEWER_EXPORT_DATABASE")) {
DbItem* data = (DbItem*) m_treeDatabases->GetItemData(m_selectedID);
if (data) {
Database* pDb = (Database*) wxDynamicCast(data->GetData(),Database);
if (pDb) {
wxFileDialog dlg(this, _("Export database..."), wxGetCwd(), wxT(""), wxT("SQL Files (*.sql)|*.sql"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT);
if(dlg.ShowModal() == wxID_OK) {
// CreateStructure
wxString retStr = wxT("-- SQL script created by wxDbExplorer\n\n ");
SerializableList::compatibility_iterator tabNode = pDb->GetFirstChildNode();
while(tabNode) {
DBETable* tab = wxDynamicCast(tabNode->GetData(),DBETable);
if (tab) {
retStr.append(pDb->GetDbAdapter()->GetCreateTableSql(tab,true));
}
tabNode = tabNode->GetNext();
}
tabNode = pDb->GetFirstChildNode();
while(tabNode) {
View* view = wxDynamicCast(tabNode->GetData(),View);
if (view) {
retStr.append(pDb->GetDbAdapter()->GetCreateViewSql(view,true));
}
tabNode = tabNode->GetNext();
}
tabNode = pDb->GetFirstChildNode();
while(tabNode) {
DBETable* tab = wxDynamicCast(tabNode->GetData(),DBETable);
if (tab) {
retStr.append(pDb->GetDbAdapter()->GetAlterTableConstraintSql(tab));
}
tabNode = tabNode->GetNext();
}
DumpClass dump(pDb->GetDbAdapter(), pDb, dlg.GetPath());
dump.DumpData();
wxTextFile file(dlg.GetPath());
if (!file.Exists()) file.Create();
file.Open();
if (file.IsOpened()) {
file.InsertLine(retStr,0);
file.Write();
file.Close();
}
wxMessageBox(wxString::Format(_("The database has been exported to '%s'."), dlg.GetPath().GetData()), _("wxDbExplorer"));
}
}
}
} else {
wxMessageBox(_("Sorry, requested feature isn't implemented yet. "),_("Sorry"));
}
} catch (DatabaseLayerException& e) {
wxString errorMessage = wxString::Format(_("Error (%d): %s"), e.GetErrorCode(), e.GetErrorMessage().c_str());
wxMessageDialog dlg(this,errorMessage,_("DB Error"),wxOK | wxCENTER | wxICON_ERROR);
dlg.ShowModal();
} catch( ... ) {
wxMessageDialog dlg(this,_("Unknown error."),_("DB Error"),wxOK | wxCENTER | wxICON_ERROR);
dlg.ShowModal();
}
}
bool DbViewerPanel::ImportDb(const wxString& sqlFile, Database* pDb)
{
DatabaseLayer* pDbLayer = NULL;
LogDialog dialog( this );//TODO:Doresit parenta
dialog.Show();
try {
wxFileInputStream input(sqlFile);
wxTextInputStream text( input );
text.SetStringSeparators(wxT(";"));
wxString command = wxT("");
pDbLayer = pDb->GetDbAdapter()->GetDatabaseLayer(pDb->GetName());
pDbLayer->BeginTransaction();
wxString useSql = pDb->GetDbAdapter()->GetUseDb(pDb->GetName());
if (!useSql.IsEmpty()) pDbLayer->RunQuery(wxString::Format(wxT("USE %s"), pDb->GetName().c_str()));
while (!input.Eof()) {
wxString line = text.ReadLine();
//dialog.AppendText(line);
int index = line.Find(wxT("--"));
if (index != wxNOT_FOUND) line = line.Mid(0,index);
command.append(line);
if (line.Find(wxT(";")) != wxNOT_FOUND) {
dialog.AppendSeparator();
dialog.AppendComment(wxT("Run SQL command:"));
dialog.AppendText(command);
pDbLayer->RunQuery(command);
dialog.AppendComment(_("Successful!"));
command.clear();
}
}
pDbLayer->Commit();
pDbLayer->Close();
} catch (DatabaseLayerException& e) {
if (pDbLayer) {
pDbLayer->RollBack();
pDbLayer->Close();
}
wxString errorMessage = wxString::Format(_("Error (%d): %s"), e.GetErrorCode(), e.GetErrorMessage().c_str());
dialog.AppendComment(_("Fail!"));
dialog.AppendComment(errorMessage);
wxMessageDialog dlg(this,errorMessage,_("DB Error"),wxOK | wxCENTER | wxICON_ERROR);
dlg.ShowModal();
} catch( ... ) {
if (pDbLayer) {
pDbLayer->RollBack();
pDbLayer->Close();
}
wxMessageDialog dlg(this,_("Unknown error."),_("DB Error"),wxOK | wxCENTER | wxICON_ERROR);
dlg.ShowModal();
}
dialog.EnableClose(true);
dialog.ShowModal();
return false;
}
void DbViewerPanel::OnPageChanged(NotebookEvent& event)
{
if( !m_SuppressUpdate )
{
ErdPanel* pPanel = wxDynamicCast(m_mgr->GetPage( event.GetSelection() ), ErdPanel);
if (pPanel) m_pThumbnail->SetCanvas( pPanel->GetCanvas() );
else
m_pThumbnail->SetCanvas(NULL);
}
else
m_SuppressUpdate = false;
event.Skip();
}
void DbViewerPanel::OnPageClosing(NotebookEvent& event)
{
m_SuppressUpdate = true;
m_pThumbnail->SetCanvas(NULL);
event.Skip();
}
wxString DbViewerPanel::CreatePanelName(Database* d, PanelType type)
{
if( type == DbViewerPanel::Sql ) {
return wxT("SQL [") + d->GetName() + wxT("]");
} else
return wxT("ERD [") + d->GetName() + wxT("]");
}
wxString DbViewerPanel::CreatePanelName(DBETable* t, PanelType type)
{
if( type == DbViewerPanel::Sql ) {
return wxT("SQL [") + t->GetParentName() + wxT(":") + t->GetName() + wxT("]");
} else
return wxT("ERD [") + t->GetParentName() + wxT(":") + t->GetName() + wxT("]");
}
wxString DbViewerPanel::CreatePanelName(View* v, PanelType type)
{
if( type == DbViewerPanel::Sql ) {
return wxT("SQL [") + v->GetParentName() + wxT(":") + v->GetName() + wxT("]");
} else
return wxT("ERD [") + v->GetParentName() + wxT(":") + v->GetName() + wxT("]");
}
void DbViewerPanel::InitStyledTextCtrl(wxScintilla *sci)
{
sci->SetLexer( wxSCI_LEX_SQL );
sci->SetKeyWords(0, wxT("select insert into delete update from drop create alter where values order by desc asc show table column tables columns limit as in exists view join left inner set") );
wxFont font = wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT);
font.SetFamily(wxTELETYPE);
for(int i=0; i<wxSCI_STYLE_MAX; i++)
sci->StyleSetFont( wxSCI_STYLE_MAX, font);
sci->StyleSetBold( wxSCI_C_WORD, true );
sci->StyleSetForeground( wxSCI_C_WORD, *wxBLUE );
sci->StyleSetForeground( wxSCI_C_STRING, *wxRED );
sci->StyleSetForeground( wxSCI_C_STRINGEOL, *wxRED );
sci->StyleSetForeground( wxSCI_C_PREPROCESSOR, wxColour( 49, 106, 197 ) );
sci->StyleSetForeground( wxSCI_C_COMMENT, wxColour( 0, 128, 0 ) );
sci->StyleSetForeground( wxSCI_C_COMMENTLINE, wxColour( 0, 128, 0 ) );
sci->StyleSetForeground( wxSCI_C_COMMENTDOC, wxColour( 0, 128, 0 ) );
sci->StyleSetForeground( wxSCI_C_COMMENTLINEDOC, wxColour( 0, 128, 0 ) );
sci->StyleSetForeground( wxSCI_C_NUMBER, *wxGREEN );
sci->SetSelBackground( true, wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHT ) );
sci->SetSelForeground( true, wxSystemSettings::GetColour( wxSYS_COLOUR_HIGHLIGHTTEXT ) );
sci->SetCaretWidth( 2 );
sci->SetTabIndents(true);
sci->SetBackSpaceUnIndents(true);
sci->SetUseTabs( false );
sci->SetTabWidth( 4 );
sci->SetIndent( 4 );
// markers
sci->MarkerDefine(wxSCI_MARKNUM_FOLDER, wxSCI_MARK_BOXPLUS);
sci->MarkerSetBackground( wxSCI_MARKNUM_FOLDER, wxColour( wxT("DARK GREY") ) );
sci->MarkerSetForeground( wxSCI_MARKNUM_FOLDER, wxColour( wxT("WHITE") ) );
sci->MarkerDefine(wxSCI_MARKNUM_FOLDEROPEN, wxSCI_MARK_BOXMINUS);
sci->MarkerSetBackground( wxSCI_MARKNUM_FOLDEROPEN, wxColour( wxT("DARK GREY") ) );
sci->MarkerSetForeground( wxSCI_MARKNUM_FOLDEROPEN, wxColour( wxT("WHITE") ) );
sci->MarkerDefine(wxSCI_MARKNUM_FOLDERSUB, wxSCI_MARK_VLINE);
sci->MarkerSetBackground( wxSCI_MARKNUM_FOLDERSUB, wxColour( wxT("DARK GREY") ) );
sci->MarkerSetForeground( wxSCI_MARKNUM_FOLDERSUB, wxColour( wxT("WHITE") ) );
sci->MarkerDefine(wxSCI_MARKNUM_FOLDEREND, wxSCI_MARK_EMPTY);
sci->MarkerSetBackground( wxSCI_MARKNUM_FOLDEREND, wxColour( wxT("DARK GREY") ) );
sci->MarkerSetForeground( wxSCI_MARKNUM_FOLDEREND, wxColour( wxT("WHITE") ) );
sci->MarkerDefine(wxSCI_MARKNUM_FOLDEROPENMID, wxSCI_MARK_BOXMINUS);
sci->MarkerSetBackground( wxSCI_MARKNUM_FOLDEROPENMID, wxColour( wxT("DARK GREY") ) );
sci->MarkerSetForeground( wxSCI_MARKNUM_FOLDEROPENMID, wxColour( wxT("WHITE") ) );
sci->MarkerDefine(wxSCI_MARKNUM_FOLDERMIDTAIL, wxSCI_MARK_TCORNER);
sci->MarkerSetBackground( wxSCI_MARKNUM_FOLDERMIDTAIL, wxColour( wxT("DARK GREY") ) );
sci->MarkerSetForeground( wxSCI_MARKNUM_FOLDERMIDTAIL, wxColour( wxT("WHITE") ) );
sci->MarkerDefine(wxSCI_MARKNUM_FOLDERTAIL, wxSCI_MARK_LCORNER);
sci->MarkerSetBackground( wxSCI_MARKNUM_FOLDERTAIL, wxColour( wxT("DARK GREY") ) );
sci->MarkerSetForeground( wxSCI_MARKNUM_FOLDERTAIL, wxColour( wxT("WHITE") ) );
sci->SetFoldFlags( wxSCI_FOLDFLAG_LINEAFTER_CONTRACTED );
}
void DbViewerPanel::OnShowThumbnail(wxCommandEvent& e)
{
if(e.IsChecked()) {
ErdPanel *pPanel = wxDynamicCast( m_mgr->GetActivePage(), ErdPanel );
if( pPanel ) m_pThumbnail->SetCanvas( pPanel->GetCanvas() );
GetSizer()->Show(m_pThumbnail);
} else {
GetSizer()->Hide(m_pThumbnail);
}
GetSizer()->Layout();
}
bool DbViewerPanel::DoSelectPage(const wxString& page)
{
wxWindow *win = m_mgr->FindPage(page);
if(win) {
m_mgr->SelectPage(win);
win->SetFocus();
// replace the query and execute it
SQLCommandPanel *sqlPage = dynamic_cast<SQLCommandPanel*>(win);
if(sqlPage) {
sqlPage->SetDefaultSelect();
sqlPage->ExecuteSql();
}
return true;
}
return false;
}
void DbViewerPanel::AddEditorPage(wxWindow* page, const wxString& name)
{
m_SuppressUpdate = true;
m_mgr->AddEditorPage( page, name );
ErdPanel *erd = wxDynamicCast( page, ErdPanel );
if( erd )
{
m_pThumbnail->SetCanvas( erd->GetCanvas() );
erd->GetCanvas()->SaveCanvasState();
erd->GetCanvas()->SetFocus();
}
else
{
m_pThumbnail->SetCanvas( NULL );
page->SetFocus();
}
}