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()) { DatabaseLayerPtr pDbLayer = m_pEditedConnection->GetDbAdapter()->GetDatabaseLayer(wxT("")); wxString sql = m_pEditedConnection->GetDbAdapter()->GetCreateDatabaseSql(dbName); if(!sql.empty()) { pDbLayer->RunQuery(sql); pDbLayer->Close(); // 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) { DatabaseLayerPtr pDbLayer = pDb->GetDbAdapter()->GetDatabaseLayer(wxT("")); pDbLayer->RunQuery(dropSQL); pDbLayer->Close(); // 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) { Table* pTab = (Table*)wxDynamicCast(data->GetData(), Table); if(pTab) { wxString pagename; pagename = CreatePanelName(pTab, DbViewerPanel::Erd); ErdPanel* erdpanel = new ErdPanel(m_pNotebook, pTab->GetDbAdapter()->Clone(), m_pConnections, (Table*)pTab->Clone()); AddEditorPage(erdpanel, 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); } } } 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) { Table* pTab = (Table*)wxDynamicCast(data->GetData(), Table); if(pTab) { ClassGenerateDialog dlg( m_mgr->GetTheApp()->GetTopWindow(), pTab->GetDbAdapter(), (Table*)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) { Table* pTab = (Table*)wxDynamicCast(data->GetData(), Table); if(pTab) { wxMessageDialog dlg(this, wxString::Format(_("Remove table '%s'?"), pTab->GetName().c_str()), _("Drop table"), wxYES_NO); if(dlg.ShowModal() == wxID_YES) { DatabaseLayerPtr pDbLayer = pTab->GetDbAdapter()->GetDatabaseLayer(pTab->GetParentName()); pDbLayer->RunQuery(pTab->GetDbAdapter()->GetDropTableSql(pTab)); pDbLayer->Close(); wxMessageBox(_("Table dropped successfully")); RefreshDbView(); } } } } else if(evt.GetId() == XRCID("IDR_DBVIEWER_DROP_VIEW")) { DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); if(data) { View* pView = (View*)wxDynamicCast(data->GetData(), View); if(pView) { wxMessageDialog dlg(this, wxString::Format(_("Remove view '%s'?"), pView->GetName().c_str()), _("Drop view"), wxYES_NO); if(dlg.ShowModal() == wxID_YES) { DatabaseLayerPtr pDbLayer = pView->GetDbAdapter()->GetDatabaseLayer(pView->GetParentName()); pDbLayer->RunQuery(pView->GetDbAdapter()->GetDropViewSql(pView)); pDbLayer->Close(); wxMessageBox(_("View dropped successfully")); RefreshDbView(); } } } } else if(evt.GetId() == XRCID("IDR_DBVIEWER_SQL_TABLE")) { DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); if(data) { Table* pTab = (Table*)wxDynamicCast(data->GetData(), Table); if(pTab) { #ifdef __WXMSW__ clWindowUpdateLocker locker(m_mgr->GetEditorPaneNotebook()); #endif wxString pagename = CreatePanelName(pTab, DbViewerPanel::Sql); if(!DoSelectPage(pagename)) { SQLCommandPanel* sqlpage = new SQLCommandPanel( m_pNotebook, pTab->GetDbAdapter()->Clone(), pTab->GetParentName(), pTab->GetName()); AddEditorPage(sqlpage, pagename); } } } } else if(evt.GetId() == XRCID("IDR_DBVIEWER_SQL_VIEW")) { DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); if(data) { View* pView = (View*)wxDynamicCast(data->GetData(), View); if(pView) { #ifdef __WXMSW__ clWindowUpdateLocker locker(m_mgr->GetEditorPaneNotebook()); #endif wxString pagename = CreatePanelName(pView, DbViewerPanel::Sql); if(!DoSelectPage(pagename)) { SQLCommandPanel* sqlpage = new SQLCommandPanel( m_pNotebook, pView->GetDbAdapter()->Clone(), pView->GetParentName(), pView->GetName()); AddEditorPage(sqlpage, pagename); } } } } else if(evt.GetId() == XRCID("IDR_DBVIEWER_SQL_DATABASE")) { DbItem* data = (DbItem*)m_treeDatabases->GetItemData(m_selectedID); if(data) { Database* pDb = (Database*)wxDynamicCast(data->GetData(), Database); if(pDb) { wxString pagename = CreatePanelName(pDb, DbViewerPanel::Sql); if(!DoSelectPage(pagename)) { SQLCommandPanel* sqlpage = new SQLCommandPanel(m_pNotebook, pDb->GetDbAdapter()->Clone(), pDb->GetName(), wxT("")); #ifndef __WXMSW__ sqlpage->Show(); #endif AddEditorPage(sqlpage, pagename); } } } } 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) { Table* tab = wxDynamicCast(tabNode->GetData(), Table); 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) { Table* tab = wxDynamicCast(tabNode->GetData(), Table); 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(wxTextFileType_None, wxConvUTF8); 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 PostgreSqlDbAdapter::GetColumns(Table* pTab) { if (pTab) { // SetDatabase(pTab->GetParentName()); DatabaseLayerPtr dbLayer = this->GetDatabaseLayer(pTab->GetParentName()); if (!dbLayer->IsOpen()) return NULL; // loading columns //TODO:SQL: //DatabaseResultSet *database = dbLayer->RunQueryWithResults(wxString::Format(wxT("SHOW COLUMNS IN `%s`.`%s`"),pTab->getParentName().c_str(),pTab->getName().c_str())); DatabaseResultSet *database = dbLayer->RunQueryWithResults(wxString::Format(wxT("SELECT * FROM information_schema.columns WHERE table_name = '%s'"),pTab->GetName().c_str())); while (database->Next()) { IDbType* pType = parseTypeString(database->GetResultString(wxT("data_type"))); if (pType) { pType->SetSize(database->GetResultInt(wxT("numeric_precision"))); pType->SetSize2(database->GetResultInt(wxT("numeric_precision_radix"))); pType->SetNotNull(database->GetResultString(wxT("is_nullable")) == wxT("NO")); Column* pCol = new Column(database->GetResultString(wxT("column_name")),pTab->GetName(), pType); pTab->AddChild(pCol); } } dbLayer->CloseResultSet(database); //wxT("SELECT tc.constraint_name, tc.constraint_type, tc.table_name, kcu.column_name, tc.is_deferrable, tc.initially_deferred, rc.match_option AS match_type, rc.update_rule AS on_update, rc.delete_rule AS on_delete, ccu.table_name AS references_table, ccu.column_name AS references_field FROM information_schema.table_constraints tc LEFT JOIN information_schema.key_column_usage kcu ON tc.constraint_catalog = kcu.constraint_catalog AND tc.constraint_schema = kcu.constraint_schema AND tc.constraint_name = kcu.constraint_name LEFT JOIN information_schema.referential_constraints rc ON tc.constraint_catalog = rc.constraint_catalog AND tc.constraint_schema = rc.constraint_schema AND tc.constraint_name = rc.constraint_name LEFT JOIN information_schema.constraint_column_usage ccu ON rc.unique_constraint_catalog = ccu.constraint_catalog AND rc.unique_constraint_schema = ccu.constraint_schema AND rc.unique_constraint_name = ccu.constraint_name WHERE tc.table_name = '%s'"); //TODO:SQL: wxString constrSql = wxT("SELECT tc.constraint_name, tc.constraint_type, tc.table_name, kcu.column_name, tc.is_deferrable, tc.initially_deferred, rc.match_option AS match_type, rc.update_rule AS on_update, rc.delete_rule AS on_delete, ccu.table_name AS references_table, ccu.column_name AS references_field FROM information_schema.table_constraints tc LEFT JOIN information_schema.key_column_usage kcu ON tc.constraint_catalog = kcu.constraint_catalog AND tc.constraint_schema = kcu.constraint_schema AND tc.constraint_name = kcu.constraint_name LEFT JOIN information_schema.referential_constraints rc ON tc.constraint_catalog = rc.constraint_catalog AND tc.constraint_schema = rc.constraint_schema AND tc.constraint_name = rc.constraint_name LEFT JOIN information_schema.constraint_column_usage ccu ON rc.unique_constraint_catalog = ccu.constraint_catalog AND rc.unique_constraint_schema = ccu.constraint_schema AND rc.unique_constraint_name = ccu.constraint_name WHERE tc.table_name = '%s'"); database = dbLayer->RunQueryWithResults(wxString::Format(constrSql, pTab->GetName().c_str())); while (database->Next()) { if ((database->GetResultString(wxT("constraint_type")) == wxT("PRIMARY KEY"))||(database->GetResultString(wxT("constraint_type")) == wxT("FOREIGN KEY"))) { Constraint* constr = new Constraint(); constr->SetName(database->GetResultString(wxT("constraint_name"))); constr->SetLocalColumn(database->GetResultString(wxT("column_name"))); constr->SetType(Constraint::primaryKey); if (database->GetResultString(wxT("references_table")) != wxT("") ) { constr->SetType(Constraint::foreignKey); constr->SetRefTable(database->GetResultString(wxT("references_table"))); constr->SetRefCol(database->GetResultString(wxT("references_field"))); wxString onDelete = database->GetResultString(wxT("on_update")); if (onDelete == wxT("RESTRICT")) constr->SetOnUpdate(Constraint::restrict); if (onDelete == wxT("CASCADE")) constr->SetOnUpdate(Constraint::cascade); if (onDelete == wxT("SET NULL")) constr->SetOnUpdate(Constraint::setNull); if (onDelete == wxT("NO ACTION")) constr->SetOnUpdate(Constraint::noAction); wxString onUpdate = database->GetResultString(wxT("on_delete")); if (onUpdate == wxT("RESTRICT")) constr->SetOnDelete(Constraint::restrict); if (onUpdate == wxT("CASCADE")) constr->SetOnDelete(Constraint::cascade); if (onUpdate == wxT("SET NULL")) constr->SetOnDelete(Constraint::setNull); if (onUpdate == wxT("NO ACTION")) constr->SetOnDelete(Constraint::noAction); } pTab->AddChild(constr); } } dbLayer->CloseResultSet(database); dbLayer->Close(); } return true; }