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 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
|
//////////////////////////////////////////////////////////////////////////
//
// pgAdmin III - PostgreSQL Tools
//
// Copyright (C) 2002 - 2014, The pgAdmin Development Team
// This software is released under the PostgreSQL Licence
//
// ddRelationshipFigure.cpp - Figure to draw foreign keys between tables.
//
//////////////////////////////////////////////////////////////////////////
#include "pgAdmin3.h"
// wxWindows headers
#include <wx/wx.h>
#include <wx/dcbuffer.h>
// App headers
#include "dd/dditems/figures/ddRelationshipFigure.h"
#include "dd/dditems/figures/ddRelationshipItem.h"
#include "dd/ddmodel/ddDrawingEditor.h"
#include "dd/ddmodel/ddDatabaseDesign.h"
#include "dd/dditems/utilities/ddDataType.h"
#include "dd/dditems/utilities/ddSelectKindFksDialog.h"
#include "hotdraw/utilities/hdRemoveDeleteDialog.h"
ddRelationshipFigure::ddRelationshipFigure():
hdLineConnection()
{
constraintName = wxEmptyString;
setKindId(DDRELATIONSHIPFIGURE);
fkFromPk = true;
fkMandatory = true;
fkOneToMany = true;
fkIdentifying = false;
matchSimple = false;
ukIndex = -1;
disconnectedEndTable = NULL;
paintingFkColumns = false;
//DEFERRABLE, NOT DEFERRABLE, INITIALLY IMMEDIATE, INITIALLY DEFERRED can be added in a future
onUpdateAction = FK_ACTION_NO;
onDeleteAction = FK_ACTION_NO;
enablePopUp();
}
ddRelationshipFigure::ddRelationshipFigure(int posIdx, hdIFigure *figure1, hdIFigure *figure2):
hdLineConnection(posIdx, figure1, figure2)
{
enablePopUp();
}
ddRelationshipFigure::~ddRelationshipFigure()
{
columnsHashMap::iterator it;
ddRelationshipItem *item;
for (it = chm.begin(); it != chm.end(); ++it)
{
wxString key = it->first;
item = it->second;
delete item;
}
chm.clear();
}
wxString ddRelationshipFigure::getConstraintName()
{
return constraintName;
}
//This function avoid a bug with recursive indentifying relationships when
//a column is removed from pk or delete.
void ddRelationshipFigure::prepareFkForDelete(ddColumnFigure *column)
{
ddTableFigure *endTable = (ddTableFigure *) getEndFigure();
ddRelationshipItem *fkColumnRelItem;
columnsHashMap::iterator it;
for (it = chm.begin(); it != chm.end(); ++it)
{
fkColumnRelItem = it->second;
if(fkColumnRelItem->original == column)
fkColumnRelItem->original = NULL;
}
//go recursive to next table
if(endTable)
endTable->prepareForDeleteFkColumn(column);
}
void ddRelationshipFigure::updateForeignKey()
{
if(getEndFigure() && getStartFigure() && getStartFigure()->ms_classInfo.IsKindOf(&ddTableFigure::ms_classInfo) && getEndFigure()->ms_classInfo.IsKindOf(&ddTableFigure::ms_classInfo))
{
ddTableFigure *startTable = (ddTableFigure *) getStartFigure();
ddTableFigure *endTable = (ddTableFigure *) getEndFigure();
ddColumnFigure *col;
ddRelationshipItem *fkColumnRelItem;
//STEP 0: Look for changes on source columns names and short table names.
//Before iterate over all columns of source table look if any column used at relationship
//and changed their name and updated it when needed
columnsHashMap::iterator it;
for (it = chm.begin(); it != chm.end(); ++it)
{
wxString key = it->first;
fkColumnRelItem = it->second;
if(fkColumnRelItem->original != NULL) //original column wasn't mark for delete.
{
//if autonaming is set on this item and column name at original source column of fk have been changed
if(!fkColumnRelItem->original->getColumnName(false).IsSameAs(fkColumnRelItem->originalStartColName, false))
{
chm[fkColumnRelItem->original->getColumnName(false)] = fkColumnRelItem;
chm[fkColumnRelItem->originalStartColName] = NULL;
chm.erase(it);
fkColumnRelItem->syncAutoFkName();
break;
}
}
}
//STEP 1: Look at all source table columns add and delete fk
hdIteratorBase *iterator = startTable->figuresEnumerator();
iterator->Next(); //first figure is main rect
iterator->Next(); //second figure is table title
while(iterator->HasNext())
{
col = (ddColumnFigure *) iterator->Next();
if(fkFromPk) //fk is generated from a pk column: add new fk/pk(s) column(s) from source fk table to destination
{
//STEP 1.1a: Add fk columns from table source pk if not exists using same name
it = chm.find(col->getColumnName());
bool NotFound = it == chm.end();
if( col->isPrimaryKey() && NotFound )
{
fkColumnRelItem = new ddRelationshipItem(this, col, endTable, (fkMandatory ? notnull : null), (fkIdentifying ? pk : none) );
chm[col->getColumnName()] = fkColumnRelItem; //hashmap key will be original table name always
endTable->addColumn(0, fkColumnRelItem->fkColumn);
updateConnection(0);
}
//STEP 1.2a: Delete old Fk columns not pk now or deleted from source fk table.
//This part of code (repeat) is a hack cause every time a column is delete hashmap is modified inside and becomes invalid iterator at that loop
bool repeat;
do
{
repeat = false;
for (it = chm.begin(); it != chm.end(); ++it)
{
wxString key = it->first;
fkColumnRelItem = it->second;
if( fkColumnRelItem->original == NULL || !fkColumnRelItem->original->isPrimaryKey() || !startTable->includes(fkColumnRelItem->original) ) //order matters fkColumnRelItem->original==NULL short circuit evaluation should be first
{
if(fkColumnRelItem->isAutomaticallyGenerated()) //don't remove from fk_dest table fk column created from existing column, just mark now as not foreign key
{
fkColumnRelItem->getDestinationTable()->removeColumn(0, fkColumnRelItem->fkColumn);
}
else
{
fkColumnRelItem->fkColumn->setAsUserCreatedFk(NULL);
}
chm.erase(it);
delete fkColumnRelItem;
repeat = true;
updateConnection(0);
}
if (repeat)
break;
}
}
while(repeat);
}
else //fk is generated from a uk column: add new fk/pk(s) column(s) from source fk table to destination
{
//STEP 1.1b: Add fk columns from table source uk if not exists
columnsHashMap::iterator it = chm.find(col->getColumnName());
bool NotFound = it == chm.end();
if( col->isUniqueKey(ukIndex) && NotFound )
{
fkColumnRelItem = new ddRelationshipItem(this, col, endTable, (fkMandatory ? notnull : null), (fkIdentifying ? pk : none) );
chm[col->getColumnName()] = fkColumnRelItem; //hashmap key will be original table name always
endTable->addColumn(0, fkColumnRelItem->fkColumn);
updateConnection(0);
}
//STEP 1.2b: Delete old Fk columns not pk now or deleted from source fk table.
//This part of code (repeat) is a hack cause every time a column is delete hashmap is modified inside and becomes invalid iterator at that loop
bool repeat;
do
{
repeat = false;
for (it = chm.begin(); it != chm.end(); ++it)
{
wxString key = it->first;
fkColumnRelItem = it->second;
if( fkColumnRelItem->original == NULL || !fkColumnRelItem->original->isUniqueKey(ukIndex) || !startTable->includes(fkColumnRelItem->original) ) //order matters fkColumnRelItem->original==NULL short circuit evaluation should be first
{
if(fkColumnRelItem->isAutomaticallyGenerated()) //don't remove from fk_dest table fk column created from existing column, just mark now as not foreign key
{
fkColumnRelItem->getDestinationTable()->removeColumn(0, fkColumnRelItem->fkColumn);
}
else
{
fkColumnRelItem->fkColumn->setAsUserCreatedFk(NULL);
}
chm.erase(it);
delete fkColumnRelItem;
repeat = true;
updateConnection(0);
}
if (repeat)
break;
}
}
while(repeat);
}
}
delete iterator;
//Now if relationship is an identifying one, I should alert all observers of my observer to update it.
if(fkIdentifying)
{
if(endTable)
endTable->updateFkObservers();
}
}
else
{
wxMessageBox(wxT("Error invalid kind of start figure at relationship"), wxT("Error invalid kind of start figure at relationship"), wxICON_ERROR | wxOK);
}
}
void ddRelationshipFigure::createMenu(wxMenu &mnu)
{
wxMenu *submenu;
wxMenuItem *item;
wxString tmp;
submenu = new wxMenu(_("Select one"));
tmp = _("Foreign Key From");
mnu.AppendSubMenu(submenu, tmp);
item = submenu->AppendCheckItem(MNU_FKEYFROMPKEY, _("Primary Key"));
item->Check(fkFromPk);
if(getStartFigure())
{
ddTableFigure *startTable = (ddTableFigure *) getStartFigure();
int i, last = startTable->getUkConstraintsNames().Count();
int eventID; //Hack to allow multiple submenu items for select uk as fk origin
for(i = 0; i < last; i++)
{
eventID = MNU_FKEYFROMUKEYBASE + i;
tmp = _("Unique Key: ");
tmp += startTable->getUkConstraintsNames()[i];
item = submenu->AppendCheckItem(eventID, tmp );
item->Check(!fkFromPk && ukIndex == i);
}
}
mnu.AppendSeparator();
//disable right now item = mnu.AppendCheckItem(MNU_FKEYCUSTOMMAPPING, _("Foreign Key Columns Custom Mapping"));
item = mnu.AppendCheckItem(MNU_MANDATORYRELATIONSHIP, _("Mandatory relationship kind"));
item->Check(fkMandatory);
item = mnu.AppendCheckItem(MNU_IDENTIFYINGRELATIONSHIP, _("Identifying relationship"));
item->Check(fkIdentifying);
mnu.AppendSeparator();
item = mnu.AppendCheckItem(MNU_1MRELATIONSHIP, _("1:M"));
item->Check(fkOneToMany);
mnu.AppendSeparator();
mnu.Append(MNU_FKCONSTRAINTNAME, _("Foreign Key Constraint Name"));
submenu = new wxMenu(_("Select one"));
item = submenu->AppendCheckItem(MNU_FKMATCHTYPESIMPLE, _("Type Simple"));
item->Check(matchSimple);
item = submenu->AppendCheckItem(MNU_FKMATCHTYPEFULL, _("Type Full"));
item->Check(!matchSimple);
tmp = _("Match Type ");
if(matchSimple)
tmp += _("[ Simple ]");
else
tmp += _("[ Full ]");
mnu.AppendSubMenu(submenu, tmp);
tmp = _("On Delete ");
submenu = new wxMenu(_("Select one"));
item = submenu->AppendCheckItem(MNU_FKONDELETENOACTION, _("No Action"));
item->Check(onDeleteAction == FK_ACTION_NO);
if(onDeleteAction == FK_ACTION_NO)
tmp += _("[ No Action ]");
item = submenu->AppendCheckItem(MNU_FKONDELETERESTRICT, _("Restrict"));
item->Check(onDeleteAction == FK_RESTRICT);
if(onDeleteAction == FK_RESTRICT)
tmp += _("[ Restrict ]");
item = submenu->AppendCheckItem(MNU_FKONDELETECASCADE, _("Cascade"));
item->Check(onDeleteAction == FK_CASCADE);
if(onDeleteAction == FK_CASCADE)
tmp += _("[ Cascade ]");
item = submenu->AppendCheckItem(MNU_FKONDELETESETNULL, _("Set Null"));
item->Check(onDeleteAction == FK_SETNULL);
if(onDeleteAction == FK_SETNULL)
tmp += _("[ Set Null ]");
item = submenu->AppendCheckItem(MNU_FKONDELETESETDEFAULT, _("Set Default"));
item->Check(onDeleteAction == FK_SETDEFAULT);
if(onDeleteAction == FK_SETDEFAULT)
tmp += _("[ Set Default ]");
mnu.AppendSubMenu(submenu, tmp);
tmp = _("On Update ");
submenu = new wxMenu(_("Select one"));
item = submenu->AppendCheckItem(MNU_FKONUPDATENOACTION, _("No Action"));
item->Check(onUpdateAction == FK_ACTION_NO);
if(onUpdateAction == FK_ACTION_NO)
tmp += _("[ No Action ]");
item = submenu->AppendCheckItem(MNU_FKONUPDATERESTRICT, _("Restrict"));
item->Check(onUpdateAction == FK_RESTRICT);
if(onUpdateAction == FK_RESTRICT)
tmp += _("[ Restrict ]");
item = submenu->AppendCheckItem(MNU_FKONUPDATECASCADE, _("Cascade"));
item->Check(onUpdateAction == FK_CASCADE);
if(onUpdateAction == FK_CASCADE)
tmp += _("[ Cascade ]");
item = submenu->AppendCheckItem(MNU_FKONUPDATESETNULL, _("Set Null"));
item->Check(onUpdateAction == FK_SETNULL);
if(onUpdateAction == FK_SETNULL)
tmp += _("[ Set Null ]");
item = submenu->AppendCheckItem(MNU_FKONUPDATESETDEFAULT, _("Set Default"));
item->Check(onUpdateAction == FK_SETDEFAULT);
if(onUpdateAction == FK_SETDEFAULT)
tmp += _("[ Set Default ]");
mnu.AppendSubMenu(submenu, tmp);
mnu.AppendSeparator();
mnu.Append(MNU_DELETERELATIONSHIP, _("Delete Relationship..."));
};
void ddRelationshipFigure::OnGenericPopupClick(wxCommandEvent &event, hdDrawingView *view)
{
int answer;
ddTableFigure *startTable = NULL;
ddTableFigure *endTable = NULL;
wxTextEntryDialog *nameDialog = NULL;
hdRemoveDeleteDialog *delremDialog = NULL;
ddSelectKindFksDialog *mappingDialog = NULL;
wxString tmpString;
switch(event.GetId())
{
case MNU_MANDATORYRELATIONSHIP:
fkMandatory = !fkMandatory;
if(fkMandatory)
{
setLinePen(wxPen(*wxBLACK_PEN));
setOptionAtForeignKeys(notnull);
}
else
{
fkIdentifying = false;
setLinePen(wxPen(*wxBLACK, 1, wxSHORT_DASH));
setOptionAtForeignKeys(null);
}
view->notifyChanged();
break;
case MNU_IDENTIFYINGRELATIONSHIP:
fkMandatory = true;
setLinePen(wxPen(*wxBLACK_PEN));
fkIdentifying = !fkIdentifying;
fkOneToMany = true;
updatePkAtFkCols();
view->notifyChanged();
break;
case MNU_FKCONSTRAINTNAME:
startTable = (ddTableFigure *) getStartFigure();
endTable = (ddTableFigure *) getEndFigure();
if(constraintName.IsEmpty() && startTable && endTable )
{
constraintName = endTable->getTableName();
constraintName += _("_");
constraintName += startTable->getTableName();
}
nameDialog = new wxTextEntryDialog(view, wxT("Change Constraint Name"), wxT("Constraint Name"), constraintName );
answer = nameDialog->ShowModal();
if (answer == wxID_OK)
{
tmpString = nameDialog->GetValue();
constraintName = tmpString;
view->notifyChanged();
}
delete nameDialog;
break;
case MNU_FKMATCHTYPEFULL:
matchSimple = false;
view->notifyChanged();
break;
case MNU_FKMATCHTYPESIMPLE:
matchSimple = true;
view->notifyChanged();
break;
case MNU_FKONDELETENOACTION:
onDeleteAction = FK_ACTION_NO;
view->notifyChanged();
break;
case MNU_FKONDELETERESTRICT:
onDeleteAction = FK_RESTRICT;
view->notifyChanged();
break;
case MNU_FKONDELETECASCADE:
onDeleteAction = FK_CASCADE;
view->notifyChanged();
break;
case MNU_FKONDELETESETNULL:
onDeleteAction = FK_SETNULL;
view->notifyChanged();
break;
case MNU_FKONDELETESETDEFAULT:
onDeleteAction = FK_SETDEFAULT;
view->notifyChanged();
break;
case MNU_FKONUPDATENOACTION:
onUpdateAction = FK_ACTION_NO;
view->notifyChanged();
break;
case MNU_FKONUPDATERESTRICT:
onUpdateAction = FK_RESTRICT;
view->notifyChanged();
break;
case MNU_FKONUPDATECASCADE:
onUpdateAction = FK_CASCADE;
view->notifyChanged();
break;
case MNU_FKONUPDATESETNULL:
onUpdateAction = FK_SETNULL;
view->notifyChanged();
break;
case MNU_FKONUPDATESETDEFAULT:
onUpdateAction = FK_SETDEFAULT;
view->notifyChanged();
break;
case MNU_1MRELATIONSHIP:
fkOneToMany = !fkOneToMany;
view->notifyChanged();
break;
case MNU_DELETERELATIONSHIP:
if(getStartFigure() && getEndFigure())
{
ddTableFigure *t1 = (ddTableFigure *)getStartFigure();
ddTableFigure *t2 = (ddTableFigure *)getEndFigure();
//Relationship can be delete only NOT REMOVED
delremDialog = new hdRemoveDeleteDialog(
wxString::Format(_("Are you sure you wish to delete relationship between tables %s and %s?"),
t1->getTableName().c_str(), t2->getTableName().c_str()),
_("Delete relationship?"),
(wxScrolledWindow *)view,
false);
answer = delremDialog->ShowModal();
ddDrawingEditor *editor = (ddDrawingEditor *) view->editor();
if (answer == DD_DELETE)
{
editor->removeFromAllSelections(this);
removeForeignKeys();
disconnectStart();
disconnectEnd();
//Hack to autodelete relationship
ddRelationshipFigure *r = this;
if(r)
editor->deleteModelFigure(r);
editor->getDesign()->refreshBrowser();
view->notifyChanged();
}
else if(answer == DD_REMOVE)
{
editor->getExistingDiagram(view->getIdx())->removeFromSelection(this);
editor->getExistingDiagram(view->getIdx())->remove(this);
view->notifyChanged();
}
delete delremDialog;
}
break;
case MNU_FKEYFROMPKEY:
fkFromPk = true;
updateForeignKey();
view->notifyChanged();
break;
case MNU_FKEYCUSTOMMAPPING:
//disable right now
/*
mappingDialog = new ddSelectKindFksDialog(view,this);
mappingDialog->ShowModal();
delete mappingDialog;
*/
break;
default:
//Hack to allow multiple selection of Uk in submenu
answer = event.GetId();
if( answer >= MNU_FKEYFROMUKEYBASE)
{
fkFromPk = false;
ukIndex = answer - MNU_FKEYFROMUKEYBASE;
updateForeignKey();
view->notifyChanged();
}
break;
}
}
bool ddRelationshipFigure::getIdentifying()
{
return fkIdentifying;
}
bool ddRelationshipFigure::getOneToMany()
{
return fkOneToMany;
}
bool ddRelationshipFigure::getMandatory()
{
return fkMandatory;
}
// relationship is observed by several tables at same time, one is the
// owner (start connector table) others are just observers of that
// relationship (end connectors table)
void ddRelationshipFigure::connectEnd(hdIConnector *end, hdDrawingView *view)
{
ddSelectKindFksDialog *mappingDialog = NULL;
hdLineConnection::connectEnd(end);
if(view)
view->Refresh();
//Hack to allow abort of mapping of Fk
int answer = -1; //By default
//If connecte destination and start figure
if(getEndFigure() && getStartFigure())
{
mappingDialog = new ddSelectKindFksDialog(view, this);
answer = mappingDialog->ShowModal();
delete mappingDialog;
if(answer == wxID_OK)
{
if(view)
{
view->AcceptsFocus();
view->SetFocus();
}
updateForeignKey();
}
}
//Is Start is connected Tell to check consistency
if(getStartFigure())
{
getStartTable()->setSelectFkDestMode(false);
ddDrawingEditor *editor = (ddDrawingEditor *) view->editor();
editor->checkAllDigramsRelConsistency();
}
//Hack to Abort Connection because user press cancel button at fk mapping dialog
if (answer == wxID_CANCEL)
{
//By disconnecting both sides I force to delete relationship at ConnectionCreationTool
getStartTable()->setSelectFkDestMode(false);
this->disconnectStart();
this->disconnectEnd();
}
}
bool ddRelationshipFigure::isForeignKeyFromPk()
{
return fkFromPk;
}
void ddRelationshipFigure::setFkFrom(bool primaryKey, int useUkIndex, bool issueUpdateFk)
{
if(useUkIndex >= 0)
{
fkFromPk = false;
ukIndex = useUkIndex;
}
else
{
fkFromPk = true;
ukIndex = -1;
}
if(issueUpdateFk)
updateForeignKey();
}
void ddRelationshipFigure::connectStart(hdIConnector *start, hdDrawingView *view)
{
hdLineConnection::connectStart(start);
if(getEndFigure() && getStartFigure())
updateForeignKey();
}
void ddRelationshipFigure::disconnectStart(hdDrawingView *view)
{
paintingFkColumns = false;
changeFkOSTextColor( *wxBLACK, *wxBLACK, true );
disconnectedEndTable = (ddTableFigure *) getEndFigure();
removeForeignKeys();
hdLineConnection::disconnectStart();
}
void ddRelationshipFigure::disconnectEnd(hdDrawingView *view)
{
paintingFkColumns = false;
changeFkOSTextColor( *wxBLACK, *wxBLACK, true );
disconnectedEndTable = (ddTableFigure *) getEndFigure();
hdLineConnection::disconnectEnd();
removeForeignKeys();
}
void ddRelationshipFigure::addExistingColumnFk(ddColumnFigure *startTablesourceCol, wxString destColumn)
{
ddTableFigure *endTable = (ddTableFigure *) getEndFigure();
ddRelationshipItem *fkColumnRelItem;
ddColumnFigure *endTablesourceCol = endTable->getColumnByName(destColumn);
//Create a new relationship item but with an existing column for fk at destination table
if(endTablesourceCol)
{
fkColumnRelItem = new ddRelationshipItem(this, startTablesourceCol, endTable, (fkMandatory ? notnull : null), (fkIdentifying ? pk : noaction), endTablesourceCol);
//Mark it as Custom Fk (fk from existing column not an automatic generated)
endTablesourceCol->setAsUserCreatedFk(fkColumnRelItem);
chm[startTablesourceCol->getColumnName()] = fkColumnRelItem; //hashmap key will be original table name always
updateConnection(0);
}
}
void ddRelationshipFigure::removeForeignKeys()
{
if(disconnectedEndTable)
{
columnsHashMap::iterator it;
ddRelationshipItem *fkColumnRelItem;
//This part of code (repeat) is a hack cause every time a column is delete hashmap is modified inside and becomes invalid iterator at that loop
bool repeat;
do
{
repeat = false;
for( it = chm.begin(); it != chm.end(); ++it )
{
wxString key = it->first;
fkColumnRelItem = it->second;
if(fkColumnRelItem->getDestinationTable()->includes(fkColumnRelItem->fkColumn))
{
//Remove fk column only if that column is automatically generated
if(fkColumnRelItem->isAutomaticallyGenerated())
{
fkColumnRelItem->getDestinationTable()->removeColumn(0, fkColumnRelItem->fkColumn);
} //is an existing column use as fk
else
{
//Mark as existing column not used as foreign key destination
fkColumnRelItem->fkColumn->setAsUserCreatedFk(NULL);
}
chm.erase(it);
delete fkColumnRelItem;
repeat = true;
break;
}
}
}
while(repeat);
chm.clear();
disconnectedEndTable = NULL;
}
}
void ddRelationshipFigure::setOptionAtForeignKeys(ddColumnOptionType type)
{
columnsHashMap::iterator it;
ddRelationshipItem *item;
for (it = chm.begin(); it != chm.end(); ++it)
{
wxString key = it->first;
item = it->second;
if(item->isAutomaticallyGenerated())
item->fkColumn->setColumnOption(type);
}
}
void ddRelationshipFigure::updatePkAtFkCols()
{
bool changed = false;
columnsHashMap::iterator it;
ddRelationshipItem *item;
for (it = chm.begin(); it != chm.end(); ++it)
{
wxString key = it->first;
item = it->second;
if(item->isAutomaticallyGenerated()) //only update fk status at fk NOT created from an existing column
{
if(fkIdentifying)
{
item->fkColumn->enablePrimaryKey();
}
else
{
item->fkColumn->disablePrimaryKey();
}
changed = true;
}
}
if(changed) //set as identifying relationship (hierarchy)
{
ddTableFigure *table = (ddTableFigure *) getEndFigure();
table->updateFkObservers();
}
}
wxString ddRelationshipFigure::generateSQL(wxString schemaName)
{
wxString tmp;
if(chm.size() > 0)
{
tmp = wxT("\nALTER TABLE ");
if(!schemaName.IsEmpty())
tmp += wxT("\"") + schemaName + wxT("\".");
tmp += wxT("\"") + ((ddTableFigure *)getEndFigure())->getTableName() + wxT("\"") ;
tmp += wxT(" ADD ");
if(!constraintName.IsEmpty())
{
tmp += wxT("CONSTRAINT \"") + constraintName + wxT("\" ");
}
tmp += wxT("FOREIGN KEY ( ");
columnsHashMap::iterator it, end;
ddRelationshipItem *item;
for( it = chm.begin(); it != chm.end(); ++it )
{
wxString key = it->first;
item = it->second;
tmp += wxT("\"") + item->fkColumn->getColumnName() + wxT("\"");
end = it;
end++;
if(end != chm.end())
{
tmp += wxT(" , ");
}
else
{
tmp += wxT(" )");
}
}
tmp += wxT(" REFERENCES ");
if(!schemaName.IsEmpty())
tmp += wxT("\"") + schemaName + _("\".");
tmp += wxT("\"") + ((ddTableFigure *)getStartFigure())->getTableName() + wxT("\"") ;
tmp += wxT(" ( ");
for( it = chm.begin(); it != chm.end(); ++it )
{
wxString key = it->first;
item = it->second;
tmp += wxT("\"") + item->original->getColumnName() + wxT("\"");
end = it;
end++;
if(end != chm.end())
{
tmp += wxT(" , ");
}
else
{
tmp += wxT(" )");
}
}
if(matchSimple)
tmp += wxT(" MATCH SIMPLE ");
else
tmp += wxT(" MATCH FULL ");
tmp += wxT(" ON DELETE ");
switch(onDeleteAction)
{
case FK_ACTION_NO:
tmp += wxT(" NO ACTION ");
break;
case FK_RESTRICT:
tmp += wxT(" RESTRICT ");
break;
case FK_CASCADE:
tmp += wxT(" CASCADE ");
break;
case FK_SETNULL:
tmp += wxT(" SET NULL ");
break;
case FK_SETDEFAULT:
tmp += wxT(" SET DEFAULT ");
break;
}
tmp += wxT(" ON UPDATE ");
switch(onUpdateAction)
{
case FK_ACTION_NO:
tmp += wxT(" NO ACTION ");
break;
case FK_RESTRICT:
tmp += wxT(" RESTRICT ");
break;
case FK_CASCADE:
tmp += wxT(" CASCADE ");
break;
case FK_SETNULL:
tmp += wxT(" SET NULL ");
break;
case FK_SETDEFAULT:
tmp += wxT(" SET DEFAULT ");
break;
}
tmp += _(";\n");
}
return tmp;
}
ddTableFigure *ddRelationshipFigure::getStartTable()
{
return (ddTableFigure *) getStartFigure();
}
ddTableFigure *ddRelationshipFigure::getEndTable()
{
return (ddTableFigure *) getEndFigure();
}
void ddRelationshipFigure::basicDrawSelected(wxBufferedDC &context, hdDrawingView *view)
{
hdLineConnection::basicDrawSelected(context, view);
if(getEndFigure() && getStartFigure())
{
paintingFkColumns = true;
changeFkOSTextColor(wxColour(255, 0, 0), wxColour(255, 0, 0));
}
}
void ddRelationshipFigure::basicDraw(wxBufferedDC &context, hdDrawingView *view)
{
hdLineConnection::basicDraw(context, view);
if(getEndFigure() && getStartFigure() && paintingFkColumns)
{
paintingFkColumns = false;
changeFkOSTextColor( *wxBLACK, *wxBLACK, true );
}
}
void ddRelationshipFigure::changeFkOSTextColor(wxColour originalColour, wxColour fkColour, bool reset)
{
columnsHashMap::iterator it;
ddRelationshipItem *item;
for (it = chm.begin(); it != chm.end(); ++it)
{
wxString key = it->first;
item = it->second;
if(item->original)
{
if(!reset)
item->original->setTextColour(originalColour);
else
item->original->setTextColour(item->original->getOwnerTable()->fontColorAttribute->fontColor);
}
if(item->fkColumn)
{
if(!reset)
item->fkColumn->setTextColour(fkColour);
else
item->fkColumn->setTextColour(item->fkColumn->getOwnerTable()->fontColorAttribute->fontColor);
}
}
}
//Used by persistence classes
int ddRelationshipFigure::getUkIndex()
{
return ukIndex;
}
//Used by persistence classes
actionKind ddRelationshipFigure::getOnUpdateAction()
{
return onUpdateAction;
}
//Used by persistence classes
actionKind ddRelationshipFigure::getOnDeleteAction()
{
return onDeleteAction;
}
//Used by persistence classes
bool ddRelationshipFigure::getMatchSimple()
{
return matchSimple;
}
//Used by persistence classes
void ddRelationshipFigure::initRelationValues( ddTableFigure *source, ddTableFigure *destination, int ukIdx, wxString constraint, actionKind onUpdate, actionKind onDelete, bool simpleMatch, bool identifying, bool oneToMany, bool mandatory, bool fromPk )
{
ukIndex = ukIdx;
fkFromPk = fromPk;
fkMandatory = mandatory;
fkOneToMany = oneToMany;
fkIdentifying = identifying;
matchSimple = simpleMatch;
onUpdateAction = onUpdate;
onDeleteAction = onDelete;
constraintName = constraint;
//I need to add two points to relationship if there aren't any points for it a first position
int i, start = pointCount(0);
for(i = start; i < 2 ; i++)
{
addPoint(0, -100, -100);
}
//reestablish connections between figures
hdLineConnection::connectStart(source->connectorAt(0, getStartPoint(0).x, getStartPoint(0).y));
hdLineConnection::connectEnd(destination->connectorAt(0, getEndPoint(0).x, getEndPoint(0).y));
}
|