Debian Codegraph @

DatabaseExplorer/CreateForeignKey.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : CreateForeignKey.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 "CreateForeignKey.h"
27
28 CreateForeignKey::CreateForeignKey(wxWindow* parent, ErdTable* pSourceTable, ErdTable* pDestTable, const wxString& srcColName, const wxString& dstColName):_CreateForeignKey(parent)
29 {
30 m_pSrcTable = pSourceTable;
31 m_pDstTable = pDestTable;
32 m_srcColName = srcColName;
33 m_dstColName = dstColName;
34
35 m_cmbDstCol->SetStringSelection(dstColName);
36 m_cmbSrcCol->SetStringSelection(srcColName);
37 m_txSrcTable->SetValue(pSourceTable->GetTable()->GetName());
38 m_txDstTable->SetValue(pDestTable->GetTable()->GetName());
39 SerializableList::compatibility_iterator node = pSourceTable->GetTable()->GetFirstChildNode();
40 while( node ) {
41 if( node->GetData()->IsKindOf( CLASSINFO(Column)) ) m_cmbSrcCol->AppendString(wxString::Format(wxT("%s"),((Column*) node->GetData())->GetName().c_str()));
42 node = node->GetNext();
43 }
44 node = pDestTable->GetTable()->GetFirstChildNode();
45 while( node ) {
46 if( node->GetData()->IsKindOf( CLASSINFO(Column)) ) m_cmbDstCol->AppendString(wxString::Format(wxT("%s"),((Column*) node->GetData())->GetName().c_str()));
47 node = node->GetNext();
48 }
49 }
50
51 CreateForeignKey::~CreateForeignKey()
52 {
53 }
54
55 void CreateForeignKey::OnCancelClick(wxCommandEvent& event)
56 {
57 EndModal(wxID_CANCEL);
58 }
59
60 void CreateForeignKey::OnOKClick(wxCommandEvent& event)
61 {
62 wxString srcColName;
63 wxString srcLocColName;
64 wxString dstColName;
65 wxString dstLocColName;
66
67 if (m_radioRelation->GetSelection() == 0){
68 Table* pTable = m_pSrcTable->GetTable();
69 Constraint* pConstr = new Constraint();
70 pConstr->SetName(wxString::Format(wxT("FK_%s_%s"), pTable->GetName().c_str(),m_pDstTable->GetTable()->GetName().c_str()));
71 pConstr->SetLocalColumn(m_cmbSrcCol->GetStringSelection());
72 pConstr->SetRefCol(m_cmbDstCol->GetStringSelection());
73 pConstr->SetRefTable(m_txDstTable->GetValue());
74 pConstr->SetType(Constraint::foreignKey);
75 pConstr->SetOnDelete((Constraint::constraintAction) m_radioOnDelete->GetSelection());
76 pConstr->SetOnUpdate((Constraint::constraintAction) m_radioOnUpdate->GetSelection());
77 pTable->AddChild(pConstr);
78 m_pSrcTable->UpdateColumns();
79 m_pSrcTable->Refresh();
80 }
81
82 if (m_radioRelation->GetSelection() == 1){
83 // Get old tables -------------------------------------------------------------
84 Table* pSrcTable = m_pSrcTable->GetTable();
85 Table* pDstTable = m_pDstTable->GetTable();
86 // Create new table -------------------------------------------------------------
87 //wxString sr1 = pSrcTable->getName();
88 //wxString sr2 = pDstTable->getName();
89 //DBETable* newTab = new DBETable();//new DBETable(pSrcTable->GetDbAdapter(),wxString::Format(wxT("FKT_%s_%s"),pSrcTable->getName().c_str(),pDstTable->getName().c_str() ), wxT(""),0);
90 Table* newTab = new Table();
91 newTab->SetName(wxString::Format(wxT("FKT_%s_%s"),pSrcTable->GetName().c_str(),pDstTable->GetName().c_str() ));
92 // Copy selected columns --------------------------------------------------------
93 SerializableList::compatibility_iterator node = pSrcTable->GetFirstChildNode();
94 while( node ) {
95 if( node->GetData()->IsKindOf( CLASSINFO(Column)) ){
96 Column* col = wxDynamicCast(node->GetData(), Column);
97 if (col->GetName() == m_cmbSrcCol->GetStringSelection()) {
98 Column* colNew = (Column*) col->Clone();
99 srcColName = col->GetName();
100 srcLocColName = wxString::Format(wxT("%s_%s"), pSrcTable->GetName().c_str(), col->GetName().c_str());
101 colNew->SetName(srcLocColName);
102 newTab->AddChild(colNew);
103 }
104 }
105 node = node->GetNext();
106 }
107
108 node = pDstTable->GetFirstChildNode();
109 while( node ) {
110 if( node->GetData()->IsKindOf( CLASSINFO(Column)) ){
111 Column* col = wxDynamicCast(node->GetData(), Column);
112 if (col->GetName() == m_cmbDstCol->GetStringSelection()) {
113 Column* colNew = (Column*) col->Clone();
114 dstColName = col->GetName();
115 dstLocColName = wxString::Format(wxT("%s_%s"), pDstTable->GetName().c_str(), col->GetName().c_str());
116 colNew->SetName(dstLocColName);
117 newTab->AddChild(colNew);
118 }
119 }
120 node = node->GetNext();
121 }
122 // create constraints ------------------------------------------------------------
123 Constraint* pCSrc = new Constraint();
124 pCSrc->SetName(wxString::Format(wxT("FK_%s_%s"),newTab->GetName().c_str(), pSrcTable->GetName().c_str()));
125 pCSrc->SetLocalColumn(srcLocColName);
126 pCSrc->SetRefTable(pSrcTable->GetName());
127 pCSrc->SetRefCol(srcColName);
128 pCSrc->SetType(Constraint::foreignKey);
129 pCSrc->SetOnDelete((Constraint::constraintAction) m_radioOnDelete->GetSelection());
130 pCSrc->SetOnUpdate((Constraint::constraintAction) m_radioOnUpdate->GetSelection());
131 newTab->AddChild(pCSrc);
132
133 Constraint* pCDest = new Constraint();
134 pCDest->SetName(wxString::Format(wxT("FK_%s_%s"),newTab->GetName().c_str(), pDstTable->GetName().c_str()));
135 pCDest->SetLocalColumn(dstLocColName);
136 pCDest->SetRefTable(pDstTable->GetName());
137 pCDest->SetRefCol(dstColName);
138 pCDest->SetType(Constraint::foreignKey);
139 pCDest->SetOnDelete((Constraint::constraintAction) m_radioOnDelete->GetSelection());
140 pCDest->SetOnUpdate((Constraint::constraintAction) m_radioOnUpdate->GetSelection());
141 newTab->AddChild(pCDest);
142
143
144 int x = (m_pSrcTable->GetAbsolutePosition().x + m_pDstTable->GetAbsolutePosition().x) / 2;
145 int y = (m_pSrcTable->GetAbsolutePosition().y + m_pDstTable->GetAbsolutePosition().y) / 2;
146
147 ErdTable* newErdTable = (ErdTable*) m_pSrcTable->GetShapeManager()->AddShape(new ErdTable(), NULL, wxPoint(x,y), true);
148 newErdTable->SetUserData(newTab);
149 newErdTable->UpdateColumns();
150 newErdTable->Refresh();
151 }
152
153 EndModal(wxID_OK);
154 }
155
156 void CreateForeignKey::OnOKUI(wxUpdateUIEvent& event)
157 {
158 event.Enable(false);
159 if ((!m_cmbSrcCol->GetStringSelection().empty())&&(!m_cmbDstCol->GetStringSelection().empty())) event.Enable(true);
160 }