/
relationship.cpp
2167 lines (1823 loc) · 63.4 KB
/
relationship.cpp
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
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "relationship.h"
#include <QApplication>
const QString Relationship::SUFFIX_SEPARATOR("_");
Relationship::Relationship(Relationship *rel) : BaseRelationship(rel)
{
if(!rel)
throw Exception(ERR_ASG_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
(*(this))=(*rel);
}
Relationship::Relationship(unsigned rel_type, Table *src_tab,
Table *dst_tab, bool src_mdtry, bool dst_mdtry,
bool auto_suffix, const QString &src_suffix, const QString &dst_suffix,
bool identifier, bool deferrable, DeferralType deferral_type) :
BaseRelationship(rel_type, src_tab, dst_tab, src_mdtry, dst_mdtry)
{
try
{
obj_type=OBJ_RELATIONSHIP;
QString str_aux;
/* Raises an error if the user tries to create a relationship which some
table doesn't has a primary key */
if(((rel_type==RELATIONSHIP_11 || rel_type==RELATIONSHIP_1N) &&
!this->getReferenceTable()->getPrimaryKey()) ||
(rel_type==RELATIONSHIP_NN && (!src_tab->getPrimaryKey() || !dst_tab->getPrimaryKey())))
throw Exception(Exception::getErrorMessage(ERR_LINK_TABLES_NO_PK)
.arg(QString::fromUtf8(obj_name))
.arg(QString::fromUtf8(src_tab->getName(true)))
.arg(QString::fromUtf8(dst_tab->getName(true))),
ERR_LINK_TABLES_NO_PK,__PRETTY_FUNCTION__,__FILE__,__LINE__);
this->src_suffix=src_suffix;
this->dst_suffix=dst_suffix;
this->auto_suffix=auto_suffix;
table_relnn=NULL;
fk_rel1n=pk_relident=pk_special=uq_rel11=NULL;
this->deferrable=deferrable;
this->deferral_type=deferral_type;
this->invalidated=true;
if(rel_type==RELATIONSHIP_11)
str_aux=QApplication::translate("Relationship","%1_has_one_%2","",QApplication::UnicodeUTF8);
else if(rel_type==RELATIONSHIP_1N)
str_aux=QApplication::translate("Relationship","%1_has_many_%2","",QApplication::UnicodeUTF8);
else if(rel_type==RELATIONSHIP_NN)
str_aux=QApplication::translate("Relationship","many_%1_has_many_%2","",QApplication::UnicodeUTF8);
else if(rel_type==RELATIONSHIP_GEN)
str_aux=QApplication::translate("Relationship","%1_inherits_%2","",QApplication::UnicodeUTF8);
else
str_aux=QApplication::translate("Relationship","%1_copies_%2","",QApplication::UnicodeUTF8);
if(rel_type!=RELATIONSHIP_NN)
str_aux=str_aux.arg(this->getReferenceTable()->getName())
.arg(this->getReceiverTable()->getName());
else
str_aux=str_aux.arg(this->src_table->getName())
.arg(this->dst_table->getName());
setName(str_aux);
//Setting up the n-n relationship table name based on the suffixes, when they are defined
if(src_suffix!="" && dst_suffix!="")
tab_name_relnn=src_suffix + SUFFIX_SEPARATOR + dst_suffix;
else
tab_name_relnn=this->obj_name;
rejected_col_count=0;
setIdentifier(identifier);
attributes[ParsersAttributes::CONSTRAINTS]="";
attributes[ParsersAttributes::TABLE]="";
attributes[ParsersAttributes::RELATIONSHIP_NN]="";
attributes[ParsersAttributes::RELATIONSHIP_GEN]="";
attributes[ParsersAttributes::RELATIONSHIP_1N]="";
attributes[ParsersAttributes::ANCESTOR_TABLE]="";
}
catch(Exception &e)
{
throw Exception(e.getErrorMessage(),e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__, &e);
}
}
void Relationship::setMandatoryTable(unsigned table_id, bool value)
{
BaseRelationship::setMandatoryTable(table_id, value);
this->invalidated=true;
}
void Relationship::setTableSuffix(unsigned table_id, const QString &suffix)
{
if(table_id > DST_TABLE)
throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);
if(!suffix.isEmpty() && !BaseObject::isValidName(suffix))
throw Exception(Exception::getErrorMessage(ERR_ASG_INV_SUFFIX_REL)
.arg(QString::fromUtf8(this->getName())),
ERR_ASG_INV_SUFFIX_REL,__PRETTY_FUNCTION__,__FILE__,__LINE__);
if(table_id==SRC_TABLE)
src_suffix=suffix;
else
dst_suffix=suffix;
this->invalidated=true;
}
QString Relationship::getTableSuffix(unsigned table_id)
{
if(table_id > DST_TABLE)
throw Exception(ERR_REF_ARG_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);
if(table_id==SRC_TABLE)
return(src_suffix);
else
return(dst_suffix);
}
void Relationship::setDeferrable(bool value)
{
deferrable=value;
this->invalidated=true;
}
void Relationship::setIdentifier(bool value)
{
/* Raises an error if the user try to set an self relationship, n-n relationship,
generalization or copy as identifier. Only 1-1, 1-n relationships can be
set as identifier. */
if(value &&
(src_table==dst_table ||
(rel_type==RELATIONSHIP_NN ||
rel_type==RELATIONSHIP_GEN ||
rel_type==RELATIONSHIP_DEP)))
throw Exception(ERR_INV_IDENT_RELATIOSHIP,__PRETTY_FUNCTION__,__FILE__,__LINE__);
identifier=value;
this->invalidated=true;
}
void Relationship::setSpecialPrimaryKeyCols(vector<unsigned> &cols)
{
/* Raises an error if the user try to set columns for special primary key when the
relationship type is n-n or identifier or self relationship */
if(isSelfRelationship() || isIdentifier() || rel_type==RELATIONSHIP_NN)
throw Exception(Exception::getErrorMessage(ERR_INV_USE_ESPECIAL_PK)
.arg(QString::fromUtf8(this->getName())),
ERR_INV_USE_ESPECIAL_PK,__PRETTY_FUNCTION__,__FILE__,__LINE__);
this->column_ids_pk_rel=cols;
}
vector<unsigned> Relationship::getSpecialPrimaryKeyCols(void)
{
return(this->column_ids_pk_rel);
}
void Relationship::createSpecialPrimaryKey(void)
{
if(!column_ids_pk_rel.empty())
{
unsigned i, count;
/* Allocates the primary key with the following feature:
1) Protected and included by linking in order to be easily identified
on internal operations of the relationship
2) Use the same tablespace as the receiver table */
pk_special=new Constraint;
pk_special->setName(this->getName() + QString("_pk"));
pk_special->setConstraintType(ConstraintType::primary_key);
pk_special->setAddedByLinking(true);
pk_special->setProtected(true);
pk_special->setTablespace(dynamic_cast<Tablespace *>(getReceiverTable()->getTablespace()));
//Adds the columns to the primary key
count=column_ids_pk_rel.size();
for(i=0; i < count; i++)
{
if(column_ids_pk_rel[i] < gen_columns.size() &&
!pk_special->isColumnExists(gen_columns[column_ids_pk_rel[i]], Constraint::SOURCE_COLS))
pk_special->addColumn(gen_columns[column_ids_pk_rel[i]], Constraint::SOURCE_COLS);
}
try
{
this->addObject(pk_special);
}
catch(Exception &e)
{
//Case some error is raised deletes the special primary key
delete(pk_special);
pk_special=NULL;
}
}
}
void Relationship::setTableNameRelNN(const QString &name)
{
if(!BaseObject::isValidName(name))
throw Exception(ERR_ASG_INV_NAME_TABLE_RELNN, __PRETTY_FUNCTION__,__FILE__,__LINE__);
tab_name_relnn=name;
this->invalidated=true;
}
QString Relationship::getTableNameRelNN(void)
{
return(tab_name_relnn);
}
bool Relationship::isDeferrable(void)
{
return(deferrable);
}
void Relationship::setDeferralType(DeferralType defer_type)
{
deferral_type=defer_type;
this->invalidated=true;
}
DeferralType Relationship::getDeferralType(void)
{
return(deferral_type);
}
int Relationship::getObjectIndex(TableObject *object)
{
vector<TableObject *>::iterator itr, itr_end;
vector<TableObject *> *list=NULL;
TableObject *obj_aux=NULL;
ObjectType obj_type;
bool found=false;
//Raises an error if the object is not allocated
if(!object)
throw Exception(ERR_OPR_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
//Selecting the correct list using the object type
obj_type=object->getObjectType();
if(obj_type==OBJ_COLUMN)
list=&rel_attributes;
else if(obj_type==OBJ_CONSTRAINT)
list=&rel_constraints;
else
//Raises an error if the object type isn't valid (not a column or constraint)
throw Exception(ERR_REF_OBJ_INV_TYPE, __PRETTY_FUNCTION__,__FILE__,__LINE__);
itr=list->begin();
itr_end=list->end();
while(itr!=itr_end && !found)
{
obj_aux=(*itr);
found=(obj_aux==object || obj_aux->getName()==object->getName());
itr++;
}
if(found)
return((itr-list->begin())-1);
else
return(-1);
}
bool Relationship::isColumnExists(Column *column)
{
vector<Column *>::iterator itr, itr_end;
Column *col_aux=NULL;
bool found=false;
//Raises an error if the column is not allocated
if(!column)
throw Exception(ERR_OPR_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
itr=gen_columns.begin();
itr_end=gen_columns.end();
while(itr!=itr_end && !found)
{
col_aux=(*itr);
found=(col_aux==column || col_aux->getName()==column->getName());
itr++;
}
return(found);
}
void Relationship::addObject(TableObject *tab_obj, int obj_idx)
{
ObjectType obj_type;
vector<TableObject *> *obj_list=NULL;
/* Raises an error if the user try to add manually a special primary key on
the relationship and the relationship type is not generalization or copy */
if((rel_type==RELATIONSHIP_GEN ||
rel_type==RELATIONSHIP_DEP) &&
!(tab_obj->isAddedByRelationship() &&
tab_obj->isProtected() &&
tab_obj->getObjectType()==OBJ_CONSTRAINT))
throw Exception(ERR_ASG_OBJ_INV_REL_TYPE,__PRETTY_FUNCTION__,__FILE__,__LINE__);
try
{
//Checks if the object isn't exists on the relationshi and doesn't belongs to a table
if(!tab_obj->getParentTable() &&
getObjectIndex(tab_obj) < 0)
{
//Gets the object list according the object type
obj_type=tab_obj->getObjectType();
if(obj_type==OBJ_COLUMN)
obj_list=&rel_attributes;
else if(obj_type==OBJ_CONSTRAINT)
obj_list=&rel_constraints;
else
//Raises an error if the object type isn't valid (not a column or constraint)
throw Exception(ERR_ASG_OBJECT_INV_TYPE, __PRETTY_FUNCTION__,__FILE__,__LINE__);
//Defines the parent table for the object only for validation
tab_obj->setParentTable(src_table);
//Generates the code for the object only for validation
if(obj_type==OBJ_COLUMN)
dynamic_cast<Column *>(tab_obj)->getCodeDefinition(SchemaParser::SQL_DEFINITION);
else
{
Constraint *rest=NULL;
rest=dynamic_cast<Constraint *>(tab_obj);
//Raises an error if the user try to add as foreign key to relationship
if(rest->getConstraintType()==ConstraintType::foreign_key)
throw Exception(ERR_ASG_FOREIGN_KEY_REL,__PRETTY_FUNCTION__,__FILE__,__LINE__);
rest->getCodeDefinition(SchemaParser::SQL_DEFINITION);
}
//Switch back to null the object parent
tab_obj->setParentTable(NULL);
if(obj_idx < 0 || obj_idx >= static_cast<int>(obj_list->size()))
obj_list->push_back(tab_obj);
else
{
if(obj_list->size() > 0)
obj_list->insert((obj_list->begin() + obj_idx), tab_obj);
else
obj_list->push_back(tab_obj);
}
tab_obj->setAddedByLinking(true);
this->invalidated=true;
}
else
throw Exception(QString(Exception::getErrorMessage(ERR_ASG_DUPLIC_OBJECT))
.arg(tab_obj->getName(true))
.arg(tab_obj->getTypeName())
.arg(this->getName(true))
.arg(this->getTypeName()),
ERR_ASG_DUPLIC_OBJECT, __PRETTY_FUNCTION__,__FILE__,__LINE__);
}
catch(Exception &e)
{
if(e.getErrorType()==ERR_UNDEF_ATTRIB_VALUE)
throw Exception(Exception::getErrorMessage(ERR_ASG_OBJ_INV_DEFINITION)
.arg(QString::fromUtf8(tab_obj->getName()))
.arg(tab_obj->getTypeName()),
ERR_ASG_OBJ_INV_DEFINITION,__PRETTY_FUNCTION__,__FILE__,__LINE__, &e);
else
throw Exception(e.getErrorMessage(),e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__, &e);
}
}
void Relationship::removeObjects(void)
{
rel_attributes.clear();
rel_constraints.clear();
}
void Relationship::destroyObjects(void)
{
while(!rel_constraints.empty())
{
delete(rel_constraints.back());
rel_constraints.pop_back();
}
while(!rel_attributes.empty())
{
delete(rel_attributes.back());
rel_attributes.pop_back();
}
}
void Relationship::removeObject(unsigned obj_id, ObjectType obj_type)
{
vector<TableObject *> *obj_list=NULL;
if(obj_type==OBJ_COLUMN)
obj_list=&rel_attributes;
else if(obj_type==OBJ_CONSTRAINT)
obj_list=&rel_constraints;
else
throw Exception(ERR_REF_OBJ_INV_TYPE, __PRETTY_FUNCTION__,__FILE__,__LINE__);
//Raises an error if the object index is out of bound
if(obj_id >= obj_list->size())
throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);
if(obj_type==OBJ_COLUMN)
{
Column *col=NULL;
Constraint *constr=NULL;
vector<TableObject *>::iterator itr, itr_end;
bool refer=false;
itr=rel_constraints.begin();
itr_end=rel_constraints.end();
col=dynamic_cast<Column *>(obj_list->at(obj_id));
while(itr!=itr_end && !refer)
{
constr=dynamic_cast<Constraint *>(*itr);
//Check is the column is referenced by one relationship constraints
refer=(constr->getColumn(col->getName(), Constraint::SOURCE_COLS) ||
constr->getColumn(col->getName(), Constraint::REFERENCED_COLS));
itr++;
}
//Raises an error if the column to be removed is referenced by a relationship constraint
if(refer)
throw Exception(Exception::getErrorMessage(ERR_REM_INDIRECT_REFERENCE)
.arg(QString::fromUtf8(col->getName()))
.arg(col->getTypeName())
.arg(QString::fromUtf8(constr->getName()))
.arg(constr->getTypeName())
.arg(QString::fromUtf8(this->getName(true)))
.arg(this->getTypeName()),
ERR_REM_INDIRECT_REFERENCE,__PRETTY_FUNCTION__,__FILE__,__LINE__);
}
//Before the column removal is necessary disconnect the relationship
disconnectRelationship(false);
//Removes the column
obj_list->erase(obj_list->begin() + obj_id);
//Reconnects the relationship
connectRelationship();
}
void Relationship::removeObject(TableObject *object)
{
if(!object)
throw Exception(ERR_REM_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
removeObject(getObjectIndex(object),object->getObjectType());
}
void Relationship::removeAttribute(unsigned attrib_idx)
{
removeObject(attrib_idx, OBJ_COLUMN);
}
void Relationship::removeConstraint(unsigned constr_idx)
{
removeObject(constr_idx, OBJ_CONSTRAINT);
}
vector<Column *> Relationship::getGeneratedColumns(void)
{
return(gen_columns);
}
Table *Relationship::getGeneratedTable(void)
{
return(table_relnn);
}
vector<Constraint *> Relationship::getGeneratedConstraints(void)
{
vector<Constraint *> vect;
if(fk_rel1n)
vect.push_back(fk_rel1n);
if(uq_rel11)
vect.push_back(uq_rel11);
if(pk_relident)
vect.push_back(pk_relident);
return(vect);
}
TableObject *Relationship::getObject(unsigned obj_idx, ObjectType obj_type)
{
vector<TableObject *> *list=NULL;
if(obj_type==OBJ_COLUMN)
list=&rel_attributes;
else if(obj_type==OBJ_CONSTRAINT)
list=&rel_constraints;
else
throw Exception(ERR_REF_OBJ_INV_TYPE, __PRETTY_FUNCTION__,__FILE__,__LINE__);
if(obj_idx >= list->size())
throw Exception(ERR_REF_OBJ_INV_INDEX, __PRETTY_FUNCTION__,__FILE__,__LINE__);
return(list->at(obj_idx));
}
TableObject *Relationship::getObject(const QString &name, ObjectType obj_type)
{
vector<TableObject *>::iterator itr, itr_end;
vector<TableObject *> *list=NULL;
TableObject *obj_aux=NULL;
bool found=false;
if(obj_type==OBJ_COLUMN)
list=&rel_attributes;
else if(obj_type==OBJ_CONSTRAINT)
list=&rel_constraints;
else
throw Exception(ERR_REF_OBJ_INV_TYPE, __PRETTY_FUNCTION__,__FILE__,__LINE__);
itr=list->begin();
itr_end=list->end();
while(itr!=itr_end && !found)
{
obj_aux=(*itr);
found=(obj_aux->getName()==name);
itr++;
}
if(found)
return(obj_aux);
else
return(NULL);
}
Column *Relationship::getAttribute(unsigned attrib_idx)
{
//Raises an error if the attribute index is out of bound
if(attrib_idx >= rel_attributes.size())
throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);
return(dynamic_cast<Column *>(rel_attributes[attrib_idx]));
}
Column *Relationship::getAttribute(const QString &name)
{
return(dynamic_cast<Column *>(getObject(name,OBJ_COLUMN)));
}
Constraint *Relationship::getConstraint(unsigned constr_idx)
{
//Raises an error if the constraint index is out of bound
if(constr_idx >= rel_constraints.size())
throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);
return(dynamic_cast<Constraint *>(rel_constraints[constr_idx]));
}
Constraint *Relationship::getConstraint(const QString &name)
{
return(dynamic_cast<Constraint *>(getObject(name,OBJ_CONSTRAINT)));
}
unsigned Relationship::getAttributeCount(void)
{
return(rel_attributes.size());
}
unsigned Relationship::getConstraintCount(void)
{
return(rel_constraints.size());
}
unsigned Relationship::getObjectCount(ObjectType obj_type)
{
if(obj_type==OBJ_COLUMN)
return(rel_attributes.size());
else if(obj_type==OBJ_CONSTRAINT)
return(rel_constraints.size());
else
throw Exception(ERR_REF_OBJ_INV_TYPE,__PRETTY_FUNCTION__,__FILE__,__LINE__);
}
void Relationship::addConstraints(Table *dst_tab)
{
Constraint *constr=NULL, *pk=NULL;
unsigned constr_id, constr_cnt, i, count;
QString name, orig_name, aux;
try
{
constr_cnt=rel_constraints.size();
for(constr_id=0; constr_id < constr_cnt; constr_id++)
{
constr=dynamic_cast<Constraint *>(rel_constraints[constr_id]);
//Breaks the iteration if the constraist has a parent
if(constr->getParentTable())
break;
if(constr->getConstraintType()!=ConstraintType::primary_key)
{
i=1; aux[0]='\0';
name="";
//Configures the name of the constraint in order to avoid name duplication errors
orig_name=constr->getName();
while(dst_tab->getConstraint(orig_name + aux))
{
aux=QString("%1").arg(i);
name=orig_name + aux;
i++;
}
if(name!="") constr->setName(name);
//Adds the constraint to the table
dst_tab->addConstraint(constr);
}
else
{
/* Case the constraint is a primary key it will be merged with the
table's primary key */
//Gets the table primary key
pk=dst_tab->getPrimaryKey();
if(pk)
{
count=constr->getColumnCount(Constraint::SOURCE_COLS);
for(i=0; i < count; i++)
//Adds the colums from the constraint into the table's primary key
pk->addColumn(constr->getColumn(i, Constraint::SOURCE_COLS),
Constraint::SOURCE_COLS);
}
else
//Case the table doens't has a primary key the constraint will the be it
dst_tab->addConstraint(constr);
if(constr==pk_special)
{
rel_constraints.erase(rel_constraints.begin()+constr_id);
constr_cnt=rel_constraints.size();
}
}
}
}
catch(Exception &e)
{
throw Exception(e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__,&e);
}
}
void Relationship::addColumnsRelGen(void)
{
Table *src_tab=NULL, *dst_tab=NULL,
*parent_tab=NULL, *aux_tab=NULL;
Column *src_col=NULL, *dst_col=NULL,
*column=NULL, *aux_col=NULL;
unsigned src_count, dst_count,
i, i1, i2, id_tab,
idx, tab_count;
vector<Column *> columns;
ObjectType types[2]={OBJ_TABLE, BASE_TABLE};
ErrorType err_type=ERR_CUSTOM;
bool duplic=false, cond,
/* 0 -> Column created by inheritance relationship
1 -> Column created by copy relationship */
src_flags[2]={false,false},
dst_flags[2]={false,false};
QString str_aux, msg;
PgSQLType src_type, dst_type;
try
{
src_tab=dynamic_cast<Table *>(src_table);
dst_tab=dynamic_cast<Table *>(dst_table);
//Gets the column count from participant tables
src_count=src_tab->getColumnCount();
dst_count=dst_tab->getColumnCount();
rejected_col_count=0;
/* This for compares the columns of the receiver table
with the columns of the reference table in order to
resolve the conflicting names */
for(i=0; i < dst_count && err_type==ERR_CUSTOM; i++)
{
//Gets the column from the receiver (destination) table
dst_col=dst_tab->getColumn(i);
/* The copied column have the 'serial' like types converted to
integer like types in order to avoid error when configuring the
relationship foreign key */
dst_type=dst_col->getType();
if(dst_type=="serial") dst_type="integer";
else if(dst_type=="bigserial") dst_type="bigint";
/* This flag indicates that the column name is registered
in the other table column (duplication). This situation need
to be resolved in order to evict the creation of duplicated column
on the receiver table */
duplic=false;
for(i1=0; i1 < src_count && !duplic; i1++)
{
//Gets the reference (source) column converting its type
src_col=src_tab->getColumn(i1);
src_type=src_col->getType();
if(src_type=="serial") src_type="integer";
else if(src_type=="bigserial") src_type="bigint";
//Check the duplication on the column names
duplic=(src_col->getName()==dst_col->getName());
//In case of duplication
if(duplic)
{
/* It is necessary to check if the source column (reference) is of the table itself,
if it came from a parent table or a table copy. The same verification is the
destination column.
The duplicity of columns only generates error when the source column is
of the table itself and the target column was not from a parent table
of the receiver table in the case of a copy relationship.
If the source column is of the reference table or coming from a
copy relationship and the type of the current relationship is
inheritance, the only case in which the duplicity generates error is
the type incompatibility of the columns involved, otherwise they are merged. */
for(id_tab=0; id_tab < 2; id_tab++)
{
if(id_tab==0)
{
aux_col=src_col;
aux_tab=src_tab;
}
else
{
aux_col=dst_col;
aux_tab=dst_tab;
}
for(i2=0; i2 < 2; i2++)
{
tab_count=aux_tab->getObjectCount(types[i2]);
//Checking if the column came from a inheritance or copy relationship
for(idx=0; idx < tab_count; idx++)
{
parent_tab=dynamic_cast<Table *>(aux_tab->getObject(idx, types[i2]));
cond=(parent_tab->getColumn(aux_col->getName()));
if(id_tab==0)
src_flags[i2]=cond;
else
dst_flags[i2]=cond;
}
}
}
/* Error condition 1: The relationship type is dependency and the source
column is from the table itself or it came from a copy table and the
destination column is from the destination table or came from a copy table
of the destination table itself */
if(rel_type==RELATIONSHIP_DEP &&
((!src_flags[0] && !src_flags[1]) ||
(!src_flags[0] && src_flags[1])) &&
((!dst_flags[0] && !dst_flags[1]) ||
(!dst_flags[0] && dst_flags[1])))
{
err_type=ERR_DUPLIC_COLS_COPY_REL;
}
/* Error condition 2: The relationship type is generalization and the column
types is incompatible */
else if(rel_type==RELATIONSHIP_GEN &&
src_type!=dst_type)
err_type=ERR_INCOMP_COLS_INHERIT_REL;
}
}
//In case that no error was detected (ERR_CUSTOM)
if(err_type==ERR_CUSTOM)
{
//In case there is no column duplicity
if(!duplic)
{
//Creates a new column making the initial configurations
column=new Column;
(*column)=(*dst_col);
if(rel_type==RELATIONSHIP_GEN)
column->setAddedByGeneralization(true);
else
column->setAddedByCopy(true);
column->setParentTable(NULL);
//Converts the type
if(column->getType()=="serial")
column->setType(PgSQLType("integer"));
else if(column->getType()=="bigserial")
column->setType(PgSQLType("bigint"));
//Adds the new column to the temporary column list
columns.push_back(column);
}
else
/* If there is duplicity, the column is discarded and not included in the list,
instead, increases the attribute which counts the amount
duplicate columns of which were rejected by already exist
in the target (receiver) table */
rejected_col_count++;
}
}
//In case that no duplicity error is detected
if(err_type==ERR_CUSTOM)
{
vector<Column *>::iterator itr, itr_end;
/* The columns of the temporary list will be inserted
in the list of referencing columns, and additionally the
relationship columns will also be inserted directly in the
source table, which inherits or copy table columns from target table */
gen_columns=columns;
itr=gen_columns.begin();
itr_end=gen_columns.end();
while(itr!=itr_end)
{
src_tab->addColumn((*itr));
itr++;
}
}
else
{
//In case of duplicity error the temporary columns are destroyed
while(!columns.empty())
{
delete(columns.back());
columns.pop_back();
}
str_aux=Exception::getErrorMessage(err_type);
if(err_type==ERR_DUPLIC_COLS_COPY_REL)
{
msg=QString(str_aux)
.arg(dst_col->getName())
.arg(dst_tab->getName())
.arg(src_tab->getName());
}
else
{
msg=QString(str_aux)
.arg(dst_col->getName())
.arg(dst_tab->getName())
.arg(src_col->getName())
.arg(src_tab->getName());
}
throw Exception(msg, err_type,__PRETTY_FUNCTION__,__FILE__,__LINE__);
}
//Creates the special primary key if exists
this->createSpecialPrimaryKey();
//Adds the constraint on the receiver table
this->addConstraints(getReceiverTable());
}
catch(Exception &e)
{
throw Exception(e.getErrorMessage(),e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__,&e);
}
}
void Relationship::connectRelationship(void)
{
try
{
if(!connected)
{
if(rel_type==RELATIONSHIP_GEN)
{
//Creates the columns on the receiver table following the rules for generalization rules
addColumnsRelGen();
//The reference table is added as parent table on the receiver
getReceiverTable()->addAncestorTable(dynamic_cast<Table *>(getReferenceTable()));
}
else if(rel_type==RELATIONSHIP_DEP)
{
//Creates the columns on the receiver table following the rules for copy rules
addColumnsRelGen();
//The reference table is added as copy table on the receiver
getReceiverTable()->addCopyTable(dynamic_cast<Table *>(getReferenceTable()));
}
else if(rel_type==RELATIONSHIP_11 ||
rel_type==RELATIONSHIP_1N)
{
if(rel_type==RELATIONSHIP_11)
addColumnsRel11();
else
addColumnsRel1n();
}
else if(rel_type==RELATIONSHIP_NN)
{
if(!table_relnn)
//Allocates the table that represents the Many-to-Many relationship
table_relnn=new Table;
/* By default the schema and tablespace for the new table is the same as
the relationship source table */
table_relnn->setName(tab_name_relnn);
table_relnn->setSchema(src_table->getSchema());
table_relnn->setTablespace(src_table->getTablespace());
addColumnsRelNn();
}
BaseRelationship::connectRelationship();
this->invalidated=false;
}
}
catch(Exception &e)
{
if(table_relnn)
{
delete(table_relnn);
table_relnn=NULL;
}
throw Exception(e.getErrorMessage(),e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__,&e);
}
}
void Relationship::configureIndentifierRel(Table *dst_tab)
{
Constraint *pk=NULL;
unsigned i, count;
QString name, aux;
bool new_pk=false;
try
{
/* In the identifier relationship, the primary key of the receiver table (weak entity)
will be merged with the primary key of the reference table (strong entity) */
//Gets the primary key from the receiver table
pk=dst_tab->getPrimaryKey();
//Case the primary key doesn't exists it'll be created
if(!pk)
{
//Creates the primary key for the weak entity
if(!pk_relident)
{
pk=new Constraint;
pk->setConstraintType(ConstraintType::primary_key);
pk->setAddedByLinking(true);
this->pk_relident=pk;
}
else
pk=this->pk_relident;
new_pk=true;
i=1;
aux[0]='\0';
//Configures a basic name for the primary key
name=dst_tab->getName() + SUFFIX_SEPARATOR + "pk";
//Resolves any duplication of the new constraint name on the receiver table
while(dst_tab->getConstraint(name + aux))
{
aux=QString("%1").arg(i);
i++;
}
pk->setName(name + aux);
}
//Adds the columns from the strong entity primary key on the weak entity primary key
count=gen_columns.size();