Debian Codegraph @

DatabaseExplorer/TableSettings.cpp

raw
1 //////////////////////////////////////////////////////////////////////////////
2 //////////////////////////////////////////////////////////////////////////////
3 //
4 // copyright : (C) 2014 Eran Ifrah
5 // file name : TableSettings.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 "TableSettings.h"
27 #include <wx/dataview.h>
28 #include <wx/vector.h>
29 #include "constraint.h"
30 #include "column.h"
31
32 TableSettings::TableSettings(wxWindow* parent, IDbAdapter* pDbAdapter, Table *pTable, wxSFDiagramManager *pManager)
33 : _TableSettings(parent)
34 {
35 m_pDbAdapter = pDbAdapter;
36 m_pTable = pTable;
37 m_pDiagramManager = pManager;
38 m_pEditedConstraint = NULL;
39
40 for( SerializableList::iterator it = pTable->GetChildrenList().begin();
41 it != pTable->GetChildrenList().end(); ++it ) {
42
43 if( (*it)->IsKindOf(CLASSINFO(Column)) ) m_lstColumns.Append( (Column*)(*it)->Clone());
44 else if( (*it)->IsKindOf(CLASSINFO(Constraint)) ) m_lstKeys.Append((Constraint*)(*it)->Clone());
45 }
46 }
47
48 TableSettings::~TableSettings()
49 {
50 m_lstColumns.DeleteContents( true );
51 m_lstColumns.Clear();
52
53 m_lstKeys.DeleteContents( true );
54 m_lstKeys.Clear();
55 }
56
57 void TableSettings::OnInit(wxInitDialogEvent& event)
58 {
59 m_textName->SetValue( m_pTable->GetName() );
60
61 // fill database data types
62 wxArrayString* pDbTypes = m_pDbAdapter->GetDbTypes();
63 if( pDbTypes ) {
64 wxArrayString choices;
65 for (unsigned int i = 0; i < pDbTypes->GetCount(); ++i) {
66 choices.Add( pDbTypes->Item(i) );
67 }
68
69 m_dvColumns->DeleteColumn( m_dvColumns->GetColumn(1) );
70 m_dvColumns->InsertColumn( 1, new wxDataViewColumn( _("Type"), new wxDataViewChoiceRenderer( choices, wxDATAVIEW_CELL_EDITABLE, wxDVR_DEFAULT_ALIGNMENT), 1, -2, wxALIGN_LEFT));
71
72 pDbTypes->Clear();
73 delete pDbTypes;
74 }
75
76 // fill referenced tables
77 ShapeList tables;
78 m_choiceRefTable->Append( wxT("") );
79 m_pDiagramManager->GetShapes( CLASSINFO(ErdTable), tables );
80 for( ShapeList::iterator it = tables.begin(); it != tables.end(); ++it ) {
81 Table *t = (Table*) (*it)->GetUserData();
82 if( t && t->GetName() != m_pTable->GetName() ) m_choiceRefTable->Append( t->GetName() );
83 }
84
85 UpdateView();
86
87 event.Skip();
88 }
89
90 void TableSettings::OnAddColumnClick(wxCommandEvent& event)
91 {
92 Column* col = new Column( MakeUniqueColumnName(_("column") ),
93 m_pTable->GetName(),
94 m_pDbAdapter->GetDbTypeByName( m_pDbAdapter->GetDbTypes()->Last() ) );
95
96 if (col) {
97 m_lstColumns.Append( col );
98 UpdateView();
99 }
100 }
101
102 void TableSettings::OnRemoveColumnClick(wxCommandEvent& event)
103 {
104 Column *col = GetColumn( GetSelectedColumnName() );
105 if( col ) {
106 // delete associated keys
107 SerializableList keys;
108 GetConstraints( keys, col->GetName() );
109 for(SerializableList::iterator it = keys.begin(); it != keys.end(); ++it ) {
110 Constraint *key = (Constraint*) *it;
111 m_lstKeys.DeleteObject( key );
112 delete key;
113 }
114 // delete the column
115 m_lstColumns.DeleteObject( col );
116 delete col;
117 UpdateView();
118 }
119 }
120
121 void TableSettings::OnMoveDownClick(wxCommandEvent& event)
122 {
123 Column *col = GetColumn( GetSelectedColumnName() );
124 if( col ) {
125 int i = m_lstColumns.IndexOf( col );
126 if( i != wxNOT_FOUND && i < (int)m_lstColumns.GetCount() - 1 ) {
127 m_lstColumns.DeleteObject( col );
128 m_lstColumns.Insert( i + 1, col );
129 UpdateView();
130 m_dvColumns->SelectRow( i + 1 );
131 }
132 }
133 }
134
135 void TableSettings::OnMoveUpClick(wxCommandEvent& event)
136 {
137 Column *col = GetColumn( GetSelectedColumnName() );
138 if( col ) {
139 int i = m_lstColumns.IndexOf( col );
140 if( i != wxNOT_FOUND && i > 0 ) {
141 m_lstColumns.DeleteObject( col );
142 m_lstColumns.Insert( i - 1, col );
143 UpdateView();
144 m_dvColumns->SelectRow( i - 1 );
145 }
146 }
147 }
148
149 void TableSettings::OnColumnChanged(wxDataViewEvent& event)
150 {
151 Column *col = reinterpret_cast<Column*>(m_dvColumns->GetItemData( event.GetItem() ) );
152 if( col ) {
153 wxVariant val;
154 event.GetModel()->GetValue( val, event.GetItem(), event.GetColumn() );
155 if( ! val.IsNull() ) {
156 switch( event.GetColumn() ) {
157 case 0: {
158 // rename local columns in keys
159 SerializableList keys;
160 GetConstraints( keys, col->GetName() );
161 for(SerializableList::iterator it = keys.begin(); it != keys.end(); ++it ) {
162 Constraint *key = (Constraint*) *it;
163 if( key->GetType() == Constraint::primaryKey ) key->SetName( wxT("PK_") + val.GetString() );
164 key->SetLocalColumn( val.GetString() );
165 }
166 // rename table column
167 col->SetName( val.GetString() );
168 break;
169 }
170 case 1: {
171 col->SetType( m_pDbAdapter->GetDbTypeByName( val.GetString() ) );
172 break;
173 }
174 case 2: {
175 long s1, s2;
176 s1 = s2 = 0;
177 wxSscanf( val.GetString(), wxT("%ld,%ld"), &s1, &s2 );
178 IDbType *type = col->GetType();
179 if( type->HaveSize() ) type->SetSize( s1 );
180 else {
181 m_infobar->ShowMessage( wxT("This data type doesn't support size definition."), wxICON_WARNING );
182 Refresh();
183 }
184 if( type->HaveSize2() ) type->SetSize2( s1 );
185 else {
186 m_infobar->ShowMessage( wxT("This data type doesn't support size definition."), wxICON_WARNING );
187 Refresh();
188 }
189 break;
190 }
191 case 3: {
192 IDbType *type = col->GetType();
193 if( type->HaveNotNull() ) type->SetNotNull( val.GetBool() );
194 else {
195 m_infobar->ShowMessage( wxT("This data type doesn't support NOT NULL feature."), wxICON_WARNING );
196 Refresh();
197 }
198 break;
199 }
200 case 4: {
201 IDbType *type = col->GetType();
202 if( type->HaveAutoIncrement() ) type->SetAutoIncrement( val.GetBool() );
203 else {
204 m_infobar->ShowMessage( wxT("This data type doesn't support AUTOINCREMENT feature."), wxICON_WARNING );
205 Refresh();
206 }
207 break;
208 }
209 case 5: {
210 Constraint *key = GetConstraint( Constraint::primaryKey, col->GetName() );
211 if( key ) {
212 // remove primary key if exists
213 m_lstKeys.DeleteObject( key );
214 delete key;
215 } else {
216 // create new primary key
217 key = new Constraint( wxT("PK_") + col->GetName(),
218 col->GetName(),
219 Constraint::primaryKey,
220 Constraint::noAction,
221 Constraint::noAction );
222
223 m_lstKeys.Append( key );
224 }
225 break;
226 }
227 }
228 }
229 }
230
231 event.Skip();
232
233 UpdateView();
234 }
235
236 void TableSettings::OnKeyChanged(wxDataViewEvent& event)
237 {
238 Constraint *key = reinterpret_cast<Constraint*>( m_dvKeys->GetItemData( event.GetItem() ) );
239 if( key ) {
240 wxVariant val;
241 event.GetModel()->GetValue( val, event.GetItem(), event.GetColumn() );
242 if( ! val.IsNull() ) {
243 key->SetName( val.GetString() );
244 }
245 }
246
247 event.Skip();
248 }
249
250 void TableSettings::OnAddKeyClick(wxCommandEvent& event)
251 {
252 Constraint *key = new Constraint( wxT("FK_") +
253 m_pTable->GetName() +
254 wxString::Format( wxT("_%d"), rand() % 100 ),
255 wxT(""),
256 Constraint::foreignKey,
257 Constraint::noAction,
258 Constraint::noAction );
259
260 m_lstKeys.Append( key );
261
262 UpdateView();
263 }
264
265 void TableSettings::OnRemoveKeyClick(wxCommandEvent& event)
266 {
267 if( m_pEditedConstraint ) {
268 m_lstKeys.DeleteObject( m_pEditedConstraint );
269 delete m_pEditedConstraint;
270 m_pEditedConstraint = NULL;
271
272 UpdateView();
273 }
274 }
275
276 void TableSettings::OnLocalColSelected(wxCommandEvent& event)
277 {
278 if( m_pEditedConstraint ) {
279 m_pEditedConstraint->SetLocalColumn( m_choiceLocalCol->GetStringSelection() );
280 }
281 }
282
283 void TableSettings::OnRadioDeleteSelected(wxCommandEvent& event)
284 {
285 if( m_pEditedConstraint ) {
286 m_pEditedConstraint->SetOnDelete( (Constraint::constraintAction) m_radioOnDelete->GetSelection() );
287 }
288 }
289
290 void TableSettings::OnRadioUpdateSelected(wxCommandEvent& event)
291 {
292 if( m_pEditedConstraint ) {
293 m_pEditedConstraint->SetOnUpdate( (Constraint::constraintAction) m_radioOnUpdate->GetSelection() );
294 }
295 }
296
297 void TableSettings::OnRefColSelected(wxCommandEvent& event)
298 {
299 if( m_pEditedConstraint ) {
300 m_pEditedConstraint->SetRefCol( m_choiceRefCol->GetStringSelection() );
301 }
302 }
303
304 void TableSettings::OnRefTableSelected(wxCommandEvent& event)
305 {
306 FillRefTableColums( GetRefTable( m_choiceRefTable->GetStringSelection() ) );
307 m_choiceRefCol->SetStringSelection( wxT("") );
308
309 if( m_pEditedConstraint ) {
310 m_pEditedConstraint->SetRefTable( m_choiceRefTable->GetStringSelection() );
311 }
312 }
313
314 void TableSettings::OnKeySelected(wxDataViewEvent& event)
315 {
316 if( m_dvKeys->GetItemCount() > 0 ) {
317 m_pEditedConstraint = reinterpret_cast<Constraint*>( m_dvKeys->GetItemData( event.GetItem() ) );
318
319 if( m_pEditedConstraint ) {
320 m_choiceLocalCol->SetStringSelection( m_pEditedConstraint->GetLocalColumn() );
321 m_choiceRefTable->SetStringSelection( m_pEditedConstraint->GetRefTable() );
322 FillRefTableColums( GetRefTable( m_pEditedConstraint->GetRefTable() ) );
323 m_choiceRefCol->SetStringSelection( m_pEditedConstraint->GetRefCol() );
324 m_radioOnUpdate->SetSelection( (int) m_pEditedConstraint->GetOnUpdate() );
325 m_radioOnDelete->SetSelection( (int) m_pEditedConstraint->GetOnDelete() );
326 }
327 } else
328 m_pEditedConstraint = NULL;
329 }
330
331 void TableSettings::OnUpdateKeys(wxUpdateUIEvent& event)
332 {
333 event.Enable( m_dvKeys->GetSelectedRow() != wxNOT_FOUND );
334 }
335
336 void TableSettings::OnUpdateColumns(wxUpdateUIEvent& event)
337 {
338 event.Enable( m_dvColumns->GetSelectedRow() != wxNOT_FOUND );
339 }
340
341 void TableSettings::OnUpdateMoveDown(wxUpdateUIEvent& event)
342 {
343 event.Enable( m_dvColumns->GetSelectedRow() != wxNOT_FOUND &&
344 m_dvColumns->GetSelectedRow() < m_dvColumns->GetItemCount() - 1 );
345 }
346
347 void TableSettings::OnUpdateMoveUp(wxUpdateUIEvent& event)
348 {
349 event.Enable( m_dvColumns->GetSelectedRow() != wxNOT_FOUND &&
350 m_dvColumns->GetSelectedRow() > 0 );
351 }
352
353 void TableSettings::FillColumns()
354 {
355 wxVector<wxVariant> line;
356
357 m_dvColumns->DeleteAllItems();
358
359 for( SerializableList::iterator it = m_lstColumns.begin();
360 it != m_lstColumns.end(); ++it ) {
361
362 Column *c = wxDynamicCast( *it, Column );
363 if( c ) {
364 IDbType *type = c->GetType();
365
366 line.clear();
367 line.push_back( wxVariant( c->GetName() ) );
368 line.push_back( wxVariant( type->GetTypeName() ) );
369 line.push_back( wxVariant( wxString::Format( wxT("%ld,%ld"), type->GetSize(), type->GetSize2() ) ) );
370 line.push_back( wxVariant( type->GetNotNull() ) );
371 line.push_back( wxVariant( type->GetAutoIncrement() ) );
372 line.push_back( wxVariant( IsPrimaryKey( c->GetName() ) ) );
373
374 m_dvColumns->AppendItem( line, reinterpret_cast<wxUIntPtr>(c) );
375 }
376 }
377 }
378
379 void TableSettings::FillKeys()
380 {
381 wxVector<wxVariant> line;
382
383 m_dvKeys->DeleteAllItems();
384
385 for( SerializableList::iterator it = m_lstKeys.begin();
386 it != m_lstKeys.end(); ++it ) {
387
388 Constraint *c = wxDynamicCast( *it, Constraint );
389 if( c && c->GetType() == Constraint::foreignKey ) {
390
391 line.clear();
392 line.push_back( wxVariant( c->GetName() ) );
393
394 m_dvKeys->AppendItem( line, reinterpret_cast<wxUIntPtr>(c) );
395 }
396 }
397 }
398
399 void TableSettings::FillRefTableColums(Table* tab)
400 {
401 if( tab ) {
402 m_choiceRefCol->Clear();
403 m_choiceRefCol->Append( wxT("") );
404
405 for( SerializableList::iterator it = tab->GetChildrenList().begin();
406 it != tab->GetChildrenList().end(); ++it ) {
407
408 Column *c = wxDynamicCast( *it, Column );
409 if( c ) m_choiceRefCol->Append( c->GetName() );
410 }
411 }
412 }
413
414 Table* TableSettings::GetRefTable(const wxString& name)
415 {
416 ShapeList tables;
417 m_pDiagramManager->GetShapes( CLASSINFO(ErdTable), tables );
418
419 for( ShapeList::iterator it = tables.begin(); it != tables.end(); ++it ) {
420 Table *t = (Table*) (*it)->GetUserData();
421 if( t->GetName() == name ) return t;
422 }
423
424 return NULL;
425 }
426
427 void TableSettings::UpdateView()
428 {
429 int crow = m_dvColumns->GetSelectedRow();
430 //int krow = m_dvKeys->GetSelectedRow();
431
432 FillColumns();
433 FillKeys();
434
435 m_choiceLocalCol->Clear();
436 m_choiceLocalCol->Append( wxT("") );
437 for( SerializableList::iterator it = m_lstColumns.begin(); it != m_lstColumns.end(); ++it ) {
438 Column *c = (Column*) *it;
439 if( c ) m_choiceLocalCol->Append( c->GetName() );
440 }
441
442 m_choiceLocalCol->SetStringSelection( wxT("") );
443 m_choiceRefTable->SetStringSelection( wxT("") );
444 m_choiceRefCol->SetStringSelection( wxT("") );
445 m_radioOnDelete->SetSelection( 0 );
446 m_radioOnUpdate->SetSelection( 0 );
447
448 if( crow != wxNOT_FOUND && crow < m_dvColumns->GetItemCount() ) m_dvColumns->SelectRow( crow );
449 //if( krow != wxNOT_FOUND && krow < m_dvKeys->GetItemCount() ) m_dvKeys->SelectRow( krow );
450 }
451
452 bool TableSettings::IsPrimaryKey(const wxString& colname)
453 {
454 for( SerializableList::iterator it = m_lstKeys.begin();
455 it != m_lstKeys.end(); ++it ) {
456
457 Constraint *c = wxDynamicCast( *it, Constraint );
458 if( c && ( c->GetType() == Constraint::primaryKey )
459 && ( c->GetLocalColumn() == colname ) ) return true;
460 }
461
462 return false;
463 }
464
465 Constraint* TableSettings::GetConstraint(Constraint::constraintType type,const wxString& localcol)
466 {
467 for( SerializableList::iterator it = m_lstKeys.begin();
468 it != m_lstKeys.end(); ++it ) {
469
470 Constraint *c = wxDynamicCast( *it, Constraint );
471 if( c && c->GetType() == type && c->GetLocalColumn() == localcol ) return c;
472 }
473
474 return NULL;
475 }
476
477 void TableSettings::GetConstraints(SerializableList& keys, const wxString& localcol)
478 {
479 for( SerializableList::iterator it = m_lstKeys.begin();
480 it != m_lstKeys.end(); ++it ) {
481
482 Constraint *c = wxDynamicCast( *it, Constraint );
483 if( c && ( c->GetLocalColumn() == localcol ) ) keys.Append( *it );
484 }
485 }
486
487 Column* TableSettings::GetColumn(const wxString& name)
488 {
489 for( SerializableList::iterator it = m_lstColumns.begin();
490 it != m_lstColumns.end(); ++it ) {
491
492 Column *c = wxDynamicCast( *it, Column );
493 if( c && ( c->GetName() == name ) ) return c;
494 }
495
496 return NULL;
497 }
498
499 wxString TableSettings::MakeUniqueColumnName(const wxString& name)
500 {
501 wxString newname = name;
502
503 while( GetColumn( newname ) ) {
504 newname += wxString::Format( wxT("%d"), rand() % 10 );
505 }
506
507 return newname;
508 }
509
510 wxString TableSettings::GetSelectedColumnName()
511 {
512 int row = m_dvColumns->GetSelectedRow();
513
514 if( row != wxNOT_FOUND ) {
515 wxVariant val;
516 m_dvColumns->GetValue( val, row, 0 );
517 return val.GetString();
518 } else {
519 return wxEmptyString;
520 }
521 }
522
523 void TableSettings::OnCancelClick(wxCommandEvent& event)
524 {
525 EndModal( wxID_CANCEL );
526 }
527
528 void TableSettings::OnOKClick(wxCommandEvent& event)
529 {
530 m_pTable->SetName( m_textName->GetValue() );
531
532 m_pTable->RemoveChildren();
533
534 for( SerializableList::iterator it = m_lstColumns.begin();
535 it != m_lstColumns.end(); ++it ) {
536 m_pTable->AddColumn( (Column*) (*it) );
537 }
538
539 for( SerializableList::iterator it = m_lstKeys.begin();
540 it != m_lstKeys.end(); ++it ) {
541 m_pTable->AddConstraint ( (Constraint*) (*it) );
542 }
543
544 m_lstColumns.Clear();
545 m_lstKeys.Clear();
546
547 EndModal( wxID_OK );
548 }