| 1 |
////////////////////////////////////////////////////////////////////////////// |
| 2 |
////////////////////////////////////////////////////////////////////////////// |
| 3 |
// |
| 4 |
// copyright : (C) 2014 Eran Ifrah |
| 5 |
// file name : DbViewerPanel.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 "DbViewerPanel.h" |
| 27 |
|
| 28 |
#include "AdapterSelectDlg.h" |
| 29 |
#include "DbExplorerFrame.h" |
| 30 |
#include "DbSettingDialog.h" |
| 31 |
#include "SqlCommandPanel.h" |
| 32 |
#include "SqliteDbAdapter.h" |
| 33 |
#include "editor_config.h" |
| 34 |
#include "event_notifier.h" |
| 35 |
#include "globals.h" |
| 36 |
#include "lexer_configuration.h" |
| 37 |
#include "window_locker.h" |
| 38 |
|
| 39 |
#include <algorithm> |
| 40 |
#include <wx/imaglist.h> |
| 41 |
#include <wx/msgdlg.h> |
| 42 |
#include <wx/wfstream.h> |
| 43 |
#include <wx/xrc/xmlres.h> |
| 44 |
|
| 45 |
DbViewerPanel::DbViewerPanel(wxWindow* parent, wxWindow* notebook, IManager* pManager) |
| 46 |
: _DbViewerPanel(parent) |
| 47 |
{ |
| 48 |
SetExtraStyle(wxWS_EX_BLOCK_EVENTS); |
| 49 |
|
| 50 |
// disable the sorting so the user will see the columns in the correct order |
| 51 |
m_treeDatabases->SetSortFunction(nullptr); |
| 52 |
|
| 53 |
m_pNotebook = notebook; |
| 54 |
m_pGlobalParent = parent; |
| 55 |
m_mgr = pManager; |
| 56 |
m_pPrevPanel = NULL; |
| 57 |
m_SuppressUpdate = false; |
| 58 |
|
| 59 |
MSWSetNativeTheme(m_treeDatabases); |
| 60 |
|
| 61 |
m_pDbAdapter = NULL; |
| 62 |
m_pConnections = new xsSerializable(); |
| 63 |
GetSizer()->Layout(); |
| 64 |
|
| 65 |
// replace the icons... |
| 66 |
m_toolbar = new clToolBar(this); |
| 67 |
GetSizer()->Insert(0, m_toolbar, 0, wxEXPAND); |
| 68 |
|
| 69 |
auto images = m_toolbar->GetBitmapsCreateIfNeeded(); |
| 70 |
m_toolbar->AddTool(XRCID("IDT_DBE_CONNECT"), _("Open connection"), images->Add("plus")); |
| 71 |
m_toolbar->AddTool(XRCID("IDT_DBE_CLOSE_CONNECTION"), _("Close selected connection"), images->Add("minus")); |
| 72 |
m_toolbar->AddTool(XRCID("IDT_DBE_REFRESH"), _("Refresh View"), images->Add("debugger_restart")); |
| 73 |
m_toolbar->Realize(); |
| 74 |
Layout(); |
| 75 |
|
| 76 |
m_toolbar->Bind(wxEVT_TOOL, &DbViewerPanel::OnConnectClick, this, XRCID("IDT_DBE_CONNECT")); |
| 77 |
m_toolbar->Bind(wxEVT_TOOL, &DbViewerPanel::OnToolCloseClick, this, XRCID("IDT_DBE_CLOSE_CONNECTION")); |
| 78 |
m_toolbar->Bind(wxEVT_UPDATE_UI, &DbViewerPanel::OnToolCloseUI, this, XRCID("IDT_DBE_CLOSE_CONNECTION")); |
| 79 |
m_toolbar->Bind(wxEVT_TOOL, &DbViewerPanel::OnRefreshClick, this, XRCID("IDT_DBE_REFRESH")); |
| 80 |
m_toolbar->Bind(wxEVT_UPDATE_UI, &DbViewerPanel::OnRefreshUI, this, XRCID("IDT_DBE_REFRESH")); |
| 81 |
} |
| 82 |
|
| 83 |
DbViewerPanel::~DbViewerPanel() |
| 84 |
{ |
| 85 |
std::for_each(m_frames.begin(), m_frames.end(), [&](DbExplorerFrame* frame) { wxDELETE(frame); }); |
| 86 |
|
| 87 |
m_toolbar->Unbind(wxEVT_TOOL, &DbViewerPanel::OnConnectClick, this, XRCID("IDT_DBE_CONNECT")); |
| 88 |
m_toolbar->Unbind(wxEVT_TOOL, &DbViewerPanel::OnToolCloseClick, this, XRCID("IDT_DBE_CLOSE_CONNECTION")); |
| 89 |
m_toolbar->Unbind(wxEVT_UPDATE_UI, &DbViewerPanel::OnToolCloseUI, this, XRCID("IDT_DBE_CLOSE_CONNECTION")); |
| 90 |
m_toolbar->Unbind(wxEVT_TOOL, &DbViewerPanel::OnRefreshClick, this, XRCID("IDT_DBE_REFRESH")); |
| 91 |
m_toolbar->Unbind(wxEVT_UPDATE_UI, &DbViewerPanel::OnRefreshUI, this, XRCID("IDT_DBE_REFRESH")); |
| 92 |
delete m_pDbAdapter; |
| 93 |
} |
| 94 |
|
| 95 |
void DbViewerPanel::OnConnectClick(wxCommandEvent& event) |
| 96 |
{ |
| 97 |
DbSettingDialog dlg(this, m_mgr->GetTheApp()->GetTopWindow()); |
| 98 |
dlg.ShowModal(); |
| 99 |
RefreshDbView(); |
| 100 |
} |
| 101 |
|
| 102 |
void DbViewerPanel::OnConnectUI(wxUpdateUIEvent& event) { event.Enable(!m_pConnections->HasChildren()); } |
| 103 |
|
| 104 |
void DbViewerPanel::OnItemActivate(wxTreeEvent& event) |
| 105 |
{ |
| 106 |
try { |
| 107 |
DbItem* item = (DbItem*)m_treeDatabases->GetItemData(event.GetItem()); |
| 108 |
if(item) { |
| 109 |
wxString pagename; |
| 110 |
if(Table* tab = wxDynamicCast(item->GetData(), Table)) { |
| 111 |
pagename = CreatePanelName(tab); |
| 112 |
SQLCommandPanel* sqlpage = new SQLCommandPanel(m_pNotebook, tab->GetDbAdapter()->Clone(), |
| 113 |
tab->GetParentName(), tab->GetName()); |
| 114 |
CallAfter(&DbViewerPanel::AddEditorPage, sqlpage, pagename); |
| 115 |
} |
| 116 |
|
| 117 |
if(View* pView = wxDynamicCast(item->GetData(), View)) { |
| 118 |
pagename = CreatePanelName(pView); |
| 119 |
SQLCommandPanel* sqlpage = new SQLCommandPanel(m_pNotebook, pView->GetDbAdapter()->Clone(), |
| 120 |
pView->GetParentName(), pView->GetName()); |
| 121 |
CallAfter(&DbViewerPanel::AddEditorPage, sqlpage, pagename); |
| 122 |
} |
| 123 |
|
| 124 |
if(Database* db = wxDynamicCast(item->GetData(), Database)) { |
| 125 |
pagename = CreatePanelName(db); |
| 126 |
SQLCommandPanel* sqlpage = |
| 127 |
new SQLCommandPanel(m_pNotebook, db->GetDbAdapter()->Clone(), db->GetName(), wxT("")); |
| 128 |
CallAfter(&DbViewerPanel::AddEditorPage, sqlpage, pagename); |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
} catch(DatabaseLayerException& e) { |
| 133 |
::wxMessageBox(wxString() << "Error occurred while opening SQL panel: " << e.GetErrorMessage(), |
| 134 |
"Database Explorer", wxOK | wxICON_ERROR | wxCENTER); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
void DbViewerPanel::OnRefreshClick(wxCommandEvent& event) |
| 139 |
{ |
| 140 |
// Refresh the view |
| 141 |
try { |
| 142 |
RefreshDbView(); |
| 143 |
|
| 144 |
} catch(DatabaseLayerException& e) { |
| 145 |
::wxMessageBox(e.GetErrorMessage(), "Database Explorer", wxOK | wxCENTER | wxICON_ERROR); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
void DbViewerPanel::OnRefreshUI(wxUpdateUIEvent& event) { event.Enable(m_pConnections->HasChildren()); } |
| 150 |
|
| 151 |
void DbViewerPanel::RefreshDbView() |
| 152 |
{ |
| 153 |
// Refresh all connections |
| 154 |
wxTreeItemId root = m_treeDatabases->GetRootItem(); |
| 155 |
if(root.IsOk()) { |
| 156 |
wxTreeItemIdValue cookie; |
| 157 |
wxTreeItemId child = m_treeDatabases->GetFirstChild(root, cookie); |
| 158 |
while(child.IsOk()) { |
| 159 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(child); |
| 160 |
if(data) { |
| 161 |
DbConnection* pCon = wxDynamicCast(data->GetData(), DbConnection); |
| 162 |
if(pCon) |
| 163 |
pCon->RefreshChildren(); |
| 164 |
Database* pDb = wxDynamicCast(data->GetData(), Database); |
| 165 |
if(pDb) |
| 166 |
pDb->RefreshChildren(false); |
| 167 |
Table* pTab = wxDynamicCast(data->GetData(), Table); |
| 168 |
if(pTab) |
| 169 |
pTab->RefreshChildren(); |
| 170 |
} |
| 171 |
child = m_treeDatabases->GetNextChild(root, cookie); |
| 172 |
} |
| 173 |
} |
| 174 |
// clear items from tree |
| 175 |
m_treeDatabases->DeleteAllItems(); |
| 176 |
// create imageList for icons |
| 177 |
BitmapLoader* loader = clGetManager()->GetStdIcons(); |
| 178 |
m_treeDatabases->SetBitmaps(loader->GetStandardMimeBitmapListPtr()); |
| 179 |
|
| 180 |
int img_folder = loader->GetImageIndex(FileExtManager::TypeFolder); |
| 181 |
int img_table = loader->GetImageIndex(BitmapLoader::kTable); |
| 182 |
int img_find = loader->GetImageIndex(BitmapLoader::kFind); |
| 183 |
int img_database = loader->GetImageIndex(BitmapLoader::kDatabase); |
| 184 |
int img_column = loader->GetImageIndex(BitmapLoader::kColumn); |
| 185 |
wxTreeItemId totalRootID = m_treeDatabases->AddRoot(wxString::Format(_("Databases")), wxNOT_FOUND); |
| 186 |
|
| 187 |
// ---------------- load connections ---------------------------- |
| 188 |
SerializableList::compatibility_iterator connectionNode = m_pConnections->GetFirstChildNode(); |
| 189 |
while(connectionNode) { |
| 190 |
DbConnection* pDbCon = (DbConnection*)wxDynamicCast(connectionNode->GetData(), DbConnection); |
| 191 |
if(pDbCon) { |
| 192 |
wxTreeItemId rootID = m_treeDatabases->AppendItem( |
| 193 |
totalRootID, wxString::Format(_("Databases (%s)"), pDbCon->GetServerName().c_str()), img_database, |
| 194 |
img_database, new DbItem(pDbCon)); |
| 195 |
|
| 196 |
// ----------------------- load databases ------------------------------- |
| 197 |
SerializableList::compatibility_iterator dbNode = pDbCon->GetFirstChildNode(); |
| 198 |
while(dbNode) { |
| 199 |
Database* pDatabase = wxDynamicCast(dbNode->GetData(), Database); |
| 200 |
if(pDatabase) { |
| 201 |
wxTreeItemId dbID = m_treeDatabases->AppendItem(rootID, pDatabase->GetName(), img_database, |
| 202 |
img_database, new DbItem(pDatabase)); |
| 203 |
m_treeDatabases->Expand(rootID); |
| 204 |
wxTreeItemId idFolder = |
| 205 |
m_treeDatabases->AppendItem(dbID, _("Tables"), img_folder, img_folder, NULL); |
| 206 |
|
| 207 |
// ----------------------------- load tables ---------------------------------- |
| 208 |
SerializableList::compatibility_iterator tabNode = pDatabase->GetFirstChildNode(); |
| 209 |
while(tabNode) { |
| 210 |
Table* pTable = wxDynamicCast(tabNode->GetData(), Table); |
| 211 |
if(pTable) { |
| 212 |
wxTreeItemId tabID = |
| 213 |
m_treeDatabases->AppendItem(idFolder, pTable->GetName(), img_table, img_table, |
| 214 |
new DbItem(pTable)); // NULL); |
| 215 |
|
| 216 |
/////////////////////////////////////////////////////////// |
| 217 |
// Add the columns |
| 218 |
/////////////////////////////////////////////////////////// |
| 219 |
SerializableList::compatibility_iterator columnNode = pTable->GetFirstChildNode(); |
| 220 |
while(columnNode) { |
| 221 |
Column* col = wxDynamicCast(columnNode->GetData(), Column); |
| 222 |
if(col) { |
| 223 |
m_treeDatabases->AppendItem(tabID, col->FormatName().c_str(), img_column, |
| 224 |
img_column, new DbItem(col)); |
| 225 |
} |
| 226 |
columnNode = columnNode->GetNext(); |
| 227 |
} |
| 228 |
} |
| 229 |
tabNode = tabNode->GetNext(); |
| 230 |
} |
| 231 |
// ---------------------------------------------------------------------------- |
| 232 |
|
| 233 |
idFolder = m_treeDatabases->AppendItem(dbID, _("Views"), img_folder, img_folder, NULL); |
| 234 |
|
| 235 |
// ----------------------------- load views ---------------------------------- |
| 236 |
tabNode = pDatabase->GetFirstChildNode(); |
| 237 |
while(tabNode) { |
| 238 |
View* pView = wxDynamicCast(tabNode->GetData(), View); |
| 239 |
if(pView) { |
| 240 |
m_treeDatabases->AppendItem(idFolder, pView->GetName(), img_find, img_find, |
| 241 |
new DbItem(pView)); // NULL); |
| 242 |
} |
| 243 |
tabNode = tabNode->GetNext(); |
| 244 |
} |
| 245 |
// ---------------------------------------------------------------------------- |
| 246 |
} |
| 247 |
dbNode = dbNode->GetNext(); |
| 248 |
} |
| 249 |
// ----------------------------------------------------------------------- |
| 250 |
} |
| 251 |
connectionNode = connectionNode->GetNext(); |
| 252 |
} |
| 253 |
} |
| 254 |
|
| 255 |
void DbViewerPanel::OnItemSelectionChange(wxTreeEvent& event) { wxUnusedVar(event); } |
| 256 |
|
| 257 |
void DbViewerPanel::OnDnDStart(wxTreeEvent& event) { event.Skip(); } |
| 258 |
|
| 259 |
void DbViewerPanel::OnItemRightClick(wxTreeEvent& event) |
| 260 |
{ |
| 261 |
event.Skip(); |
| 262 |
if(event.GetItem().IsOk()) { |
| 263 |
m_treeDatabases->SelectItem(event.GetItem()); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
void DbViewerPanel::OnToolCloseClick(wxCommandEvent& event) |
| 268 |
{ |
| 269 |
// Close the active connection (there is only one) |
| 270 |
// getting selected item data |
| 271 |
/*wxTreeItemIdValue cookie; |
| 272 |
DbItem* data = (DbItem*) m_treeDatabases->GetItemData(m_treeDatabases->GetFirstChild(m_treeDatabases->GetRootItem(), |
| 273 |
cookie));*/ |
| 274 |
|
| 275 |
wxTreeItemId itemId = m_treeDatabases->GetSelection(); |
| 276 |
|
| 277 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(itemId); |
| 278 |
if(data) { |
| 279 |
DbConnection* pCon = wxDynamicCast(data->GetData(), DbConnection); |
| 280 |
if(pCon) { |
| 281 |
wxMessageDialog dlg(this, _("Close connection?"), _("Close"), wxYES_NO | wxCANCEL | wxCANCEL_DEFAULT); |
| 282 |
if(dlg.ShowModal() == wxID_YES) { |
| 283 |
// m_pConnections->GetChildrenList().DeleteContents(true); |
| 284 |
m_pConnections->GetChildrenList().DeleteObject(pCon); |
| 285 |
m_treeDatabases->Delete(itemId); |
| 286 |
|
| 287 |
RefreshDbView(); |
| 288 |
} |
| 289 |
} |
| 290 |
/* else |
| 291 |
wxMessageBox( _("Please, select database connection in DbExplorer tree view."), _("Close"), wxOK | |
| 292 |
wxICON_WARNING ); */ |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
void DbViewerPanel::OnToolCloseUI(wxUpdateUIEvent& event) |
| 297 |
{ |
| 298 |
wxTreeItemId treeId = m_treeDatabases->GetSelection(); |
| 299 |
if(treeId.IsOk()) { |
| 300 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(treeId); |
| 301 |
event.Enable(data && wxDynamicCast(data->GetData(), DbConnection)); |
| 302 |
} else { |
| 303 |
event.Enable(false); |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
void DbViewerPanel::OnPopupClick(wxCommandEvent& evt) |
| 308 |
{ |
| 309 |
if(!m_selectedID.IsOk()) |
| 310 |
return; |
| 311 |
|
| 312 |
try { |
| 313 |
if(evt.GetId() == XRCID("IDR_DBVIEWER_COPY_COLUMN")) { |
| 314 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); |
| 315 |
if(data) { |
| 316 |
Column* col = wxDynamicCast(data->GetData(), Column); |
| 317 |
if(col) { |
| 318 |
wxString colname = col->GetName(); |
| 319 |
::CopyToClipboard(colname); |
| 320 |
} |
| 321 |
} |
| 322 |
} else if(evt.GetId() == XRCID("IDT_DBE_CLOSE_CONNECTION")) { |
| 323 |
OnToolCloseClick(evt); |
| 324 |
return; |
| 325 |
|
| 326 |
} else if(evt.GetId() == XRCID("IDT_DBE_REFRESH")) { |
| 327 |
OnRefreshClick(evt); |
| 328 |
return; |
| 329 |
|
| 330 |
} else if(evt.GetId() == XRCID("IDR_DBVIEWER_ADD_DATABASE")) { |
| 331 |
if(m_pEditedConnection) { |
| 332 |
// TODO:LANG: |
| 333 |
wxString dbName = wxGetTextFromUser(_("Database name"), _("Add database")); |
| 334 |
if(!dbName.IsEmpty()) { |
| 335 |
DatabaseLayerPtr pDbLayer = m_pEditedConnection->GetDbAdapter()->GetDatabaseLayer(wxT("")); |
| 336 |
wxString sql = m_pEditedConnection->GetDbAdapter()->GetCreateDatabaseSql(dbName); |
| 337 |
if(!sql.empty()) { |
| 338 |
|
| 339 |
pDbLayer->RunQuery(sql); |
| 340 |
pDbLayer->Close(); |
| 341 |
|
| 342 |
// TODO:LANG: |
| 343 |
wxMessageBox(_("Database created successfully")); |
| 344 |
|
| 345 |
RefreshDbView(); |
| 346 |
} else { |
| 347 |
// TODO:LANG: |
| 348 |
wxMessageDialog dlg(this, _("Can't create new db in this database engine!"), _("Error"), |
| 349 |
wxOK | wxICON_ERROR); |
| 350 |
dlg.ShowModal(); |
| 351 |
} |
| 352 |
} |
| 353 |
} |
| 354 |
} else if(evt.GetId() == XRCID("IDR_DBVIEWER_DROP_DATABASE")) { |
| 355 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); |
| 356 |
if(data) { |
| 357 |
Database* pDb = (Database*)wxDynamicCast(data->GetData(), Database); |
| 358 |
if(pDb) { |
| 359 |
wxString dropSQL = pDb->GetDbAdapter()->GetDropDatabaseSql(pDb); |
| 360 |
if(!dropSQL.IsEmpty()) { |
| 361 |
// TODO:LANG: |
| 362 |
wxMessageDialog dlg(this, wxString::Format(_("Remove database '%s'?"), pDb->GetName().c_str()), |
| 363 |
_("Drop database"), wxYES_NO); |
| 364 |
if(dlg.ShowModal() == wxID_YES) { |
| 365 |
DatabaseLayerPtr pDbLayer = pDb->GetDbAdapter()->GetDatabaseLayer(wxT("")); |
| 366 |
pDbLayer->RunQuery(dropSQL); |
| 367 |
pDbLayer->Close(); |
| 368 |
// TODO:LANG: |
| 369 |
wxMessageBox(_("Database dropped successfully")); |
| 370 |
RefreshDbView(); |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
} else if(evt.GetId() == XRCID("IDR_DBVIEWER_CLASS_DB")) { |
| 376 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); |
| 377 |
if(data) { |
| 378 |
Database* pDb = (Database*)wxDynamicCast(data->GetData(), Database); |
| 379 |
if(pDb) { |
| 380 |
pDb = (Database*)pDb->Clone(); |
| 381 |
// NOTE: the refresh functions must be here for propper code generation (they translate views into |
| 382 |
// tables) |
| 383 |
pDb->RefreshChildren(true); |
| 384 |
pDb->RefreshChildrenDetails(); |
| 385 |
ClassGenerateDialog dlg(m_mgr->GetTheApp()->GetTopWindow(), pDb->GetDbAdapter(), pDb, m_mgr); |
| 386 |
dlg.ShowModal(); |
| 387 |
delete pDb; |
| 388 |
} |
| 389 |
} |
| 390 |
} else if(evt.GetId() == XRCID("IDR_DBVIEWER_CLASS_TABLE")) { |
| 391 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); |
| 392 |
if(data) { |
| 393 |
Table* pTab = (Table*)wxDynamicCast(data->GetData(), Table); |
| 394 |
if(pTab) { |
| 395 |
ClassGenerateDialog dlg(m_mgr->GetTheApp()->GetTopWindow(), pTab->GetDbAdapter(), |
| 396 |
(Table*)pTab->Clone(), m_mgr); |
| 397 |
dlg.ShowModal(); |
| 398 |
} |
| 399 |
} |
| 400 |
} else if(evt.GetId() == XRCID("IDR_DBVIEWER_DROP_TABLE")) { |
| 401 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); |
| 402 |
if(data) { |
| 403 |
Table* pTab = (Table*)wxDynamicCast(data->GetData(), Table); |
| 404 |
if(pTab) { |
| 405 |
wxMessageDialog dlg(this, wxString::Format(_("Remove table '%s'?"), pTab->GetName().c_str()), |
| 406 |
_("Drop table"), wxYES_NO); |
| 407 |
if(dlg.ShowModal() == wxID_YES) { |
| 408 |
DatabaseLayerPtr pDbLayer = pTab->GetDbAdapter()->GetDatabaseLayer(pTab->GetParentName()); |
| 409 |
pDbLayer->RunQuery(pTab->GetDbAdapter()->GetDropTableSql(pTab)); |
| 410 |
pDbLayer->Close(); |
| 411 |
|
| 412 |
wxMessageBox(_("Table dropped successfully")); |
| 413 |
RefreshDbView(); |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
} else if(evt.GetId() == XRCID("IDR_DBVIEWER_DROP_VIEW")) { |
| 418 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); |
| 419 |
if(data) { |
| 420 |
View* pView = (View*)wxDynamicCast(data->GetData(), View); |
| 421 |
if(pView) { |
| 422 |
wxMessageDialog dlg(this, wxString::Format(_("Remove view '%s'?"), pView->GetName().c_str()), |
| 423 |
_("Drop view"), wxYES_NO); |
| 424 |
if(dlg.ShowModal() == wxID_YES) { |
| 425 |
DatabaseLayerPtr pDbLayer = pView->GetDbAdapter()->GetDatabaseLayer(pView->GetParentName()); |
| 426 |
pDbLayer->RunQuery(pView->GetDbAdapter()->GetDropViewSql(pView)); |
| 427 |
pDbLayer->Close(); |
| 428 |
|
| 429 |
wxMessageBox(_("View dropped successfully")); |
| 430 |
RefreshDbView(); |
| 431 |
} |
| 432 |
} |
| 433 |
} |
| 434 |
} else if(evt.GetId() == XRCID("IDR_DBVIEWER_SQL_TABLE")) { |
| 435 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); |
| 436 |
if(data) { |
| 437 |
Table* pTab = (Table*)wxDynamicCast(data->GetData(), Table); |
| 438 |
if(pTab) { |
| 439 |
#ifdef __WXMSW__ |
| 440 |
clWindowUpdateLocker locker(m_mgr->GetEditorPaneNotebook()); |
| 441 |
#endif |
| 442 |
wxString pagename = CreatePanelName(pTab); |
| 443 |
if(!DoSelectPage(pagename)) { |
| 444 |
SQLCommandPanel* sqlpage = new SQLCommandPanel(m_pNotebook, pTab->GetDbAdapter()->Clone(), |
| 445 |
pTab->GetParentName(), pTab->GetName()); |
| 446 |
AddEditorPage(sqlpage, pagename); |
| 447 |
} |
| 448 |
} |
| 449 |
} |
| 450 |
} else if(evt.GetId() == XRCID("IDR_DBVIEWER_SQL_VIEW")) { |
| 451 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); |
| 452 |
if(data) { |
| 453 |
View* pView = (View*)wxDynamicCast(data->GetData(), View); |
| 454 |
if(pView) { |
| 455 |
#ifdef __WXMSW__ |
| 456 |
clWindowUpdateLocker locker(m_mgr->GetEditorPaneNotebook()); |
| 457 |
#endif |
| 458 |
wxString pagename = CreatePanelName(pView); |
| 459 |
if(!DoSelectPage(pagename)) { |
| 460 |
SQLCommandPanel* sqlpage = new SQLCommandPanel(m_pNotebook, pView->GetDbAdapter()->Clone(), |
| 461 |
pView->GetParentName(), pView->GetName()); |
| 462 |
AddEditorPage(sqlpage, pagename); |
| 463 |
} |
| 464 |
} |
| 465 |
} |
| 466 |
} else if(evt.GetId() == XRCID("IDR_DBVIEWER_SQL_DATABASE")) { |
| 467 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); |
| 468 |
if(data) { |
| 469 |
Database* pDb = (Database*)wxDynamicCast(data->GetData(), Database); |
| 470 |
if(pDb) { |
| 471 |
wxString pagename = CreatePanelName(pDb); |
| 472 |
if(!DoSelectPage(pagename)) { |
| 473 |
SQLCommandPanel* sqlpage = |
| 474 |
new SQLCommandPanel(m_pNotebook, pDb->GetDbAdapter()->Clone(), pDb->GetName(), wxT("")); |
| 475 |
#ifndef __WXMSW__ |
| 476 |
sqlpage->Show(); |
| 477 |
#endif |
| 478 |
AddEditorPage(sqlpage, pagename); |
| 479 |
} |
| 480 |
} |
| 481 |
} |
| 482 |
} else if(evt.GetId() == XRCID("IDR_DBVIEWER_IMPORT_DATABASE")) { |
| 483 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); |
| 484 |
if(data) { |
| 485 |
Database* pDb = (Database*)wxDynamicCast(data->GetData(), Database); |
| 486 |
if(pDb) { |
| 487 |
// TODO:LANG: |
| 488 |
wxFileDialog dlg(this, _("Import database from SQL file ..."), wxGetCwd(), wxT(""), |
| 489 |
wxT("SQL Files (*.sql)|*.sql"), wxFD_OPEN | wxFD_FILE_MUST_EXIST); |
| 490 |
if(dlg.ShowModal() == wxID_OK) { |
| 491 |
ImportDb(dlg.GetPath(), pDb); |
| 492 |
} |
| 493 |
} |
| 494 |
} |
| 495 |
RefreshDbView(); |
| 496 |
} else if(evt.GetId() == XRCID("IDR_DBVIEWER_DUMP_DATABASE")) { |
| 497 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); |
| 498 |
if(data) { |
| 499 |
Database* pDb = (Database*)wxDynamicCast(data->GetData(), Database); |
| 500 |
if(pDb) { |
| 501 |
// TODO:LANG: |
| 502 |
wxFileDialog dlg(this, _("Dump data into file ..."), wxT(""), pDb->GetName() + wxT(".sql"), |
| 503 |
wxT("SQL files (*.sql)|*.sql"), wxFD_SAVE | wxFD_OVERWRITE_PROMPT); |
| 504 |
if(dlg.ShowModal() == wxID_OK) { |
| 505 |
DumpClass* dump = new DumpClass(pDb->GetDbAdapter(), pDb, dlg.GetPath()); |
| 506 |
dump->DumpData(); |
| 507 |
wxMessageBox(_("Data was saved to ") + dlg.GetPath()); |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
} else if(evt.GetId() == XRCID("IDR_DBVIEWER_EXPORT_DATABASE")) { |
| 512 |
DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); |
| 513 |
if(data) { |
| 514 |
Database* pDb = (Database*)wxDynamicCast(data->GetData(), Database); |
| 515 |
if(pDb) { |
| 516 |
wxFileDialog dlg(this, _("Export database..."), wxGetCwd(), wxT(""), wxT("SQL Files (*.sql)|*.sql"), |
| 517 |
wxFD_SAVE | wxFD_OVERWRITE_PROMPT); |
| 518 |
if(dlg.ShowModal() == wxID_OK) { |
| 519 |
// CreateStructure |
| 520 |
wxString retStr = wxT("-- SQL script created by wxDbExplorer\n\n "); |
| 521 |
SerializableList::compatibility_iterator tabNode = pDb->GetFirstChildNode(); |
| 522 |
while(tabNode) { |
| 523 |
Table* tab = wxDynamicCast(tabNode->GetData(), Table); |
| 524 |
if(tab) { |
| 525 |
retStr.append(pDb->GetDbAdapter()->GetCreateTableSql(tab, true)); |
| 526 |
} |
| 527 |
tabNode = tabNode->GetNext(); |
| 528 |
} |
| 529 |
|
| 530 |
tabNode = pDb->GetFirstChildNode(); |
| 531 |
while(tabNode) { |
| 532 |
View* view = wxDynamicCast(tabNode->GetData(), View); |
| 533 |
if(view) { |
| 534 |
retStr.append(pDb->GetDbAdapter()->GetCreateViewSql(view, true)); |
| 535 |
} |
| 536 |
tabNode = tabNode->GetNext(); |
| 537 |
} |
| 538 |
|
| 539 |
tabNode = pDb->GetFirstChildNode(); |
| 540 |
while(tabNode) { |
| 541 |
Table* tab = wxDynamicCast(tabNode->GetData(), Table); |
| 542 |
if(tab) { |
| 543 |
retStr.append(pDb->GetDbAdapter()->GetAlterTableConstraintSql(tab)); |
| 544 |
} |
| 545 |
tabNode = tabNode->GetNext(); |
| 546 |
} |
| 547 |
|
| 548 |
DumpClass dump(pDb->GetDbAdapter(), pDb, dlg.GetPath()); |
| 549 |
dump.DumpData(); |
| 550 |
|
| 551 |
wxTextFile file(dlg.GetPath()); |
| 552 |
if(!file.Exists()) |
| 553 |
file.Create(); |
| 554 |
file.Open(); |
| 555 |
if(file.IsOpened()) { |
| 556 |
file.InsertLine(retStr, 0); |
| 557 |
file.Write(wxTextFileType_None, wxConvUTF8); |
| 558 |
file.Close(); |
| 559 |
} |
| 560 |
|
| 561 |
wxMessageBox( |
| 562 |
wxString::Format(_("The database has been exported to '%s'."), dlg.GetPath().GetData()), |
| 563 |
_("wxDbExplorer")); |
| 564 |
} |
| 565 |
} |
| 566 |
} |
| 567 |
} else { |
| 568 |
evt.Skip(); |
| 569 |
} |
| 570 |
} catch(DatabaseLayerException& e) { |
| 571 |
wxString errorMessage = wxString::Format(_("Error (%d): %s"), e.GetErrorCode(), e.GetErrorMessage().c_str()); |
| 572 |
wxMessageDialog dlg(this, errorMessage, _("DB Error"), wxOK | wxCENTER | wxICON_ERROR); |
| 573 |
dlg.ShowModal(); |
| 574 |
} catch(...) { |
| 575 |
wxMessageDialog dlg(this, _("Unknown error."), _("DB Error"), wxOK | wxCENTER | wxICON_ERROR); |
| 576 |
dlg.ShowModal(); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
bool DbViewerPanel::ImportDb(const wxString& sqlFile, Database* pDb) |
| 581 |
{ |
| 582 |
DatabaseLayerPtr pDbLayer(NULL); |
| 583 |
LogDialog dialog(this); // TODO:Doresit parenta |
| 584 |
dialog.Show(); |
| 585 |
|
| 586 |
try { |
| 587 |
wxFileInputStream input(sqlFile); |
| 588 |
wxTextInputStream text(input); |
| 589 |
text.SetStringSeparators(wxT(";")); |
| 590 |
wxString command = wxT(""); |
| 591 |
|
| 592 |
pDbLayer = pDb->GetDbAdapter()->GetDatabaseLayer(pDb->GetName()); |
| 593 |
pDbLayer->BeginTransaction(); |
| 594 |
|
| 595 |
wxString useSql = pDb->GetDbAdapter()->GetUseDb(pDb->GetName()); |
| 596 |
if(!useSql.IsEmpty()) |
| 597 |
pDbLayer->RunQuery(wxString::Format(wxT("USE %s"), pDb->GetName().c_str())); |
| 598 |
|
| 599 |
while(!input.Eof()) { |
| 600 |
wxString line = text.ReadLine(); |
| 601 |
// dialog.AppendText(line); |
| 602 |
int index = line.Find(wxT("--")); |
| 603 |
if(index != wxNOT_FOUND) |
| 604 |
line = line.Mid(0, index); |
| 605 |
command.append(line); |
| 606 |
if(line.Find(wxT(";")) != wxNOT_FOUND) { |
| 607 |
dialog.AppendSeparator(); |
| 608 |
dialog.AppendComment(wxT("Run SQL command:")); |
| 609 |
dialog.AppendText(command); |
| 610 |
pDbLayer->RunQuery(command); |
| 611 |
dialog.AppendComment(_("Successful!")); |
| 612 |
command.clear(); |
| 613 |
} |
| 614 |
} |
| 615 |
pDbLayer->Commit(); |
| 616 |
pDbLayer->Close(); |
| 617 |
} catch(DatabaseLayerException& e) { |
| 618 |
if(pDbLayer) { |
| 619 |
pDbLayer->RollBack(); |
| 620 |
pDbLayer->Close(); |
| 621 |
} |
| 622 |
wxString errorMessage = wxString::Format(_("Error (%d): %s"), e.GetErrorCode(), e.GetErrorMessage().c_str()); |
| 623 |
|
| 624 |
dialog.AppendComment(_("Fail!")); |
| 625 |
dialog.AppendComment(errorMessage); |
| 626 |
wxMessageDialog dlg(this, errorMessage, _("DB Error"), wxOK | wxCENTER | wxICON_ERROR); |
| 627 |
dlg.ShowModal(); |
| 628 |
} catch(...) { |
| 629 |
if(pDbLayer) { |
| 630 |
pDbLayer->RollBack(); |
| 631 |
pDbLayer->Close(); |
| 632 |
} |
| 633 |
wxMessageDialog dlg(this, _("Unknown error."), _("DB Error"), wxOK | wxCENTER | wxICON_ERROR); |
| 634 |
dlg.ShowModal(); |
| 635 |
} |
| 636 |
dialog.EnableClose(true); |
| 637 |
dialog.ShowModal(); |
| 638 |
|
| 639 |
return false; |
| 640 |
} |
| 641 |
|
| 642 |
void DbViewerPanel::OnPageChanged(wxBookCtrlEvent& event) { event.Skip(); } |
| 643 |
|
| 644 |
void DbViewerPanel::OnPageClosing(wxBookCtrlEvent& event) { event.Skip(); } |
| 645 |
|
| 646 |
wxString DbViewerPanel::CreatePanelName(Database* d) { return wxT("SQL [") + d->GetName() + wxT("]"); } |
| 647 |
|
| 648 |
wxString DbViewerPanel::CreatePanelName(Table* t) |
| 649 |
{ |
| 650 |
return wxT("SQL [") + t->GetParentName() + wxT(":") + t->GetName() + wxT("]"); |
| 651 |
} |
| 652 |
|
| 653 |
wxString DbViewerPanel::CreatePanelName(View* v) |
| 654 |
{ |
| 655 |
return wxT("SQL [") + v->GetParentName() + wxT(":") + v->GetName() + wxT("]"); |
| 656 |
} |
| 657 |
|
| 658 |
void DbViewerPanel::InitStyledTextCtrl(wxStyledTextCtrl* sci) |
| 659 |
{ |
| 660 |
LexerConf::Ptr_t lexer = EditorConfigST::Get()->GetLexer("SQL"); |
| 661 |
if(lexer) { |
| 662 |
lexer->Apply(sci, true); |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
void DbViewerPanel::OnShowThumbnail(wxCommandEvent& e) { wxUnusedVar(e); } |
| 667 |
|
| 668 |
bool DbViewerPanel::DoSelectPage(const wxString& page) |
| 669 |
{ |
| 670 |
wxWindow* win = m_mgr->FindPage(page); |
| 671 |
if(win) { |
| 672 |
m_mgr->SelectPage(win); |
| 673 |
win->SetFocus(); |
| 674 |
|
| 675 |
// replace the query and execute it |
| 676 |
SQLCommandPanel* sqlPage = dynamic_cast<SQLCommandPanel*>(win); |
| 677 |
if(sqlPage) { |
| 678 |
sqlPage->SetDefaultSelect(); |
| 679 |
sqlPage->ExecuteSql(); |
| 680 |
} |
| 681 |
return true; |
| 682 |
} |
| 683 |
return false; |
| 684 |
} |
| 685 |
|
| 686 |
void DbViewerPanel::AddEditorPage(wxWindow* page, const wxString& name) |
| 687 |
{ |
| 688 |
m_SuppressUpdate = true; |
| 689 |
DbExplorerFrame* frame = new DbExplorerFrame(EventNotifier::Get()->TopFrame(), page, name, this); |
| 690 |
frame->Show(); |
| 691 |
m_frames.insert(frame); |
| 692 |
} |
| 693 |
|
| 694 |
void DbViewerPanel::OnContextMenu(wxTreeEvent& event) |
| 695 |
{ |
| 696 |
m_pEditedDatabase = NULL; |
| 697 |
m_pEditedConnection = NULL; |
| 698 |
m_selectedID = event.GetItem(); |
| 699 |
|
| 700 |
DbItem* item = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); |
| 701 |
wxMenu menu; |
| 702 |
int c = 0; |
| 703 |
if(item) { |
| 704 |
DbConnection* conn = wxDynamicCast(item->GetData(), DbConnection); |
| 705 |
if(conn) { |
| 706 |
// Connection node |
| 707 |
menu.Append(XRCID("IDT_DBE_CLOSE_CONNECTION"), _("Close Connection"), _("Close Connection")); |
| 708 |
menu.AppendSeparator(); |
| 709 |
menu.Append(XRCID("IDT_DBE_REFRESH"), _("Refresh Connection"), _("Refresh Connection")); |
| 710 |
c++; |
| 711 |
} |
| 712 |
Database* db = wxDynamicCast(item->GetData(), Database); |
| 713 |
if(db) { |
| 714 |
// menu.Append(XRCID("IDR_DBVIEWER_ADD_TABLE"),_("Add table"),_("Run SQL command for creating Table")); |
| 715 |
menu.Append(XRCID("IDR_DBVIEWER_SQL_DATABASE"), _("Open SQL panel"), |
| 716 |
_("Open SQL command panel for the database")); |
| 717 |
menu.AppendSeparator(); |
| 718 |
|
| 719 |
menu.Append(XRCID("IDR_DBVIEWER_DROP_DATABASE"), _("Drop database"), |
| 720 |
_("Run SQL command for deleting Database")); |
| 721 |
|
| 722 |
menu.AppendSeparator(); |
| 723 |
menu.Append(XRCID("IDR_DBVIEWER_CLASS_DB"), _("Create classes from DB"), |
| 724 |
_("Create C++ classes for the database")); |
| 725 |
|
| 726 |
menu.AppendSeparator(); |
| 727 |
menu.Append(XRCID("IDR_DBVIEWER_IMPORT_DATABASE"), _("Import database from file"), |
| 728 |
_("Run SQL commands stored in *.sql file")); |
| 729 |
menu.Append(XRCID("IDR_DBVIEWER_EXPORT_DATABASE"), _("Export database to file"), |
| 730 |
_("Export database CREATE SQL statements into *.sql file")); |
| 731 |
|
| 732 |
menu.AppendSeparator(); |
| 733 |
menu.Append(XRCID("IDR_DBVIEWER_DUMP_DATABASE"), _("Dump data to file"), |
| 734 |
_("Dump data from database into .sql file")); |
| 735 |
c++; |
| 736 |
|
| 737 |
m_pEditedDatabase = db; |
| 738 |
} |
| 739 |
Table* tab = wxDynamicCast(item->GetData(), Table); |
| 740 |
if(tab) { |
| 741 |
menu.Append(XRCID("IDR_DBVIEWER_SQL_TABLE"), _("Open SQL panel"), |
| 742 |
_("Open SQL command panel for the table")); |
| 743 |
menu.AppendSeparator(); |
| 744 |
|
| 745 |
menu.Append(XRCID("IDR_DBVIEWER_DROP_TABLE"), _("Drop table"), _("Run SQL command to delete the table")); |
| 746 |
menu.AppendSeparator(); |
| 747 |
menu.Append(XRCID("IDR_DBVIEWER_CLASS_TABLE"), _("Create classes from Table"), |
| 748 |
_("Create C++ classes for the table")); |
| 749 |
c++; |
| 750 |
} |
| 751 |
Column* col = wxDynamicCast(item->GetData(), Column); |
| 752 |
if(col) { |
| 753 |
menu.Append(XRCID("IDR_DBVIEWER_COPY_COLUMN"), _("Copy Column Name"), _("Copy Column Name")); |
| 754 |
c++; |
| 755 |
} |
| 756 |
|
| 757 |
View* vw = wxDynamicCast(item->GetData(), View); |
| 758 |
if(vw) { |
| 759 |
menu.Append(XRCID("IDR_DBVIEWER_SQL_VIEW"), _("Open SQL panel"), _("Open SQL command panel for the view")); |
| 760 |
menu.AppendSeparator(); |
| 761 |
|
| 762 |
menu.Append(XRCID("IDR_DBVIEWER_DROP_VIEW"), _("Drop view"), _("Run SQL command to delete the view")); |
| 763 |
c++; |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
if(c > 0) { |
| 768 |
menu.Connect(wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction)&DbViewerPanel::OnPopupClick, NULL, this); |
| 769 |
PopupMenu(&menu); |
| 770 |
} |
| 771 |
} |
| 772 |
|
| 773 |
void DbViewerPanel::RemoveFrame(DbExplorerFrame* frame) |
| 774 |
{ |
| 775 |
if(m_frames.count(frame)) { |
| 776 |
m_frames.erase(frame); |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
void DbViewerPanel::OpenSQLiteFile(const wxFileName& fileName, bool openDefaultSQLPanel) |
| 781 |
{ |
| 782 |
try { |
| 783 |
IDbAdapter* pAdapt = new SQLiteDbAdapter(fileName.GetFullPath()); |
| 784 |
wxString serverName = fileName.GetFullPath(); |
| 785 |
DbConnection* connection = new DbConnection(pAdapt, serverName); |
| 786 |
AddDbConnection(connection); |
| 787 |
SetServer(serverName); |
| 788 |
RefreshDbView(); |
| 789 |
if(openDefaultSQLPanel) { |
| 790 |
// Open an SQL panel view |
| 791 |
SQLCommandPanel* sqlpage = new SQLCommandPanel(m_pNotebook, pAdapt->Clone(), fileName.GetFullPath(), ""); |
| 792 |
CallAfter(&DbViewerPanel::AddEditorPage, sqlpage, fileName.GetFullPath()); |
| 793 |
} |
| 794 |
|
| 795 |
} catch(DatabaseLayerException& e) { |
| 796 |
wxString errorMessage = wxString::Format(_("Error (%d): %s"), e.GetErrorCode(), e.GetErrorMessage().c_str()); |
| 797 |
::wxMessageBox(errorMessage, "CodeLite", wxICON_ERROR | wxCENTER | wxOK, EventNotifier::Get()->TopFrame()); |
| 798 |
} catch(...) { |
| 799 |
::wxMessageBox(_("Unknown error."), "CodeLite", wxICON_ERROR | wxCENTER | wxOK, |
| 800 |
EventNotifier::Get()->TopFrame()); |
| 801 |
} |
| 802 |
} |