/
operationlist.cpp
949 lines (817 loc) · 29 KB
/
operationlist.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
#include "operationlist.h"
template <class Classe>
void copyObject(BaseObject **psrc_obj, Classe *copy_obj)
{
Classe *orig_obj=NULL;
//Gets the objects stored in the pointer
orig_obj=dynamic_cast<Classe *>(*psrc_obj);
//Raises an error if the copy object is not allocated
if(!copy_obj)
throw Exception(ERR_ASG_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
//Allocates the source object if its not allocated
if(!orig_obj)
{
orig_obj=new Classe;
(*psrc_obj)=orig_obj;
}
//Makes the copy between the objects
(*orig_obj)=(*copy_obj);
}
void copyObject(BaseObject **psrc_obj, BaseObject *copy_obj, ObjectType obj_type)
{
switch(obj_type)
{
case OBJ_RELATIONSHIP:
Relationship *rel1;
rel1=new Relationship(dynamic_cast<Relationship *>(copy_obj));
if(!(*psrc_obj))
(*psrc_obj)=rel1;
else
(*(dynamic_cast<Relationship *>(*psrc_obj)))=(*rel1);
break;
case BASE_RELATIONSHIP:
BaseRelationship *rel;
rel=new BaseRelationship(dynamic_cast<BaseRelationship *>(copy_obj));
if(!(*psrc_obj))
(*psrc_obj)=rel;
else
(*(dynamic_cast<BaseRelationship *>(*psrc_obj)))=(*rel);
break;
case OBJ_COLUMN:
copyObject(psrc_obj, dynamic_cast<Column *>(copy_obj));
break;
case OBJ_CONSTRAINT:
copyObject(psrc_obj, dynamic_cast<Constraint *>(copy_obj));
break;
case OBJ_TRIGGER:
copyObject(psrc_obj, dynamic_cast<Trigger *>(copy_obj));
break;
case OBJ_RULE:
copyObject(psrc_obj, dynamic_cast<Rule *>(copy_obj));
break;
case OBJ_INDEX:
copyObject(psrc_obj, dynamic_cast<Index *>(copy_obj));
break;
case OBJ_TABLE:
copyObject(psrc_obj, dynamic_cast<Table *>(copy_obj));
break;
case OBJ_TEXTBOX:
copyObject(psrc_obj, dynamic_cast<Textbox *>(copy_obj));
break;
case OBJ_OPCLASS:
copyObject(psrc_obj, dynamic_cast<OperatorClass *>(copy_obj));
break;
case OBJ_CONVERSION:
copyObject(psrc_obj, dynamic_cast<Conversion *>(copy_obj));
break;
case OBJ_CAST:
copyObject(psrc_obj, dynamic_cast<Cast *>(copy_obj));
break;
case OBJ_DOMAIN:
copyObject(psrc_obj, dynamic_cast<Domain *>(copy_obj));
break;
case OBJ_TABLESPACE:
copyObject(psrc_obj, dynamic_cast<Tablespace *>(copy_obj));
break;
case OBJ_SCHEMA:
copyObject(psrc_obj, dynamic_cast<Schema *>(copy_obj));
break;
case OBJ_OPFAMILY:
copyObject(psrc_obj, dynamic_cast<OperatorFamily *>(copy_obj));
break;
case OBJ_FUNCTION:
copyObject(psrc_obj, dynamic_cast<Function *>(copy_obj));
break;
case OBJ_AGGREGATE:
copyObject(psrc_obj, dynamic_cast<Aggregate *>(copy_obj));
break;
case OBJ_LANGUAGE:
copyObject(psrc_obj, dynamic_cast<Language *>(copy_obj));
break;
case OBJ_OPERATOR:
copyObject(psrc_obj, dynamic_cast<Operator *>(copy_obj));
break;
case OBJ_ROLE:
copyObject(psrc_obj, dynamic_cast<Role *>(copy_obj));
break;
case OBJ_SEQUENCE:
copyObject(psrc_obj, dynamic_cast<Sequence *>(copy_obj));
break;
case OBJ_TYPE:
copyObject(psrc_obj, dynamic_cast<Type *>(copy_obj));
break;
case OBJ_VIEW:
copyObject(psrc_obj, dynamic_cast<View *>(copy_obj));
break;
default:
throw Exception(ERR_OPR_OBJ_INV_TYPE,__PRETTY_FUNCTION__,__FILE__,__LINE__);
break;
}
}
unsigned OperationList::max_size=500;
OperationList::OperationList(DatabaseModel *model)
{
/* Raises an error if the user tries to allocate an operation list linked to
to an unallocated model */
if(!model)
throw Exception(ERR_ASG_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
this->model=model;
current_index=0;
next_op_chain=Operation::NO_CHAIN;
ignore_chain=false;
}
OperationList::~OperationList(void)
{
removeOperations();
}
unsigned OperationList::getCurrentSize(void)
{
return(operations.size());
}
unsigned OperationList::getMaximumSize(void)
{
return(max_size);
}
int OperationList::getCurrentIndex(void)
{
return(current_index);
}
void OperationList::startOperationChain(void)
{
/* If the chaining is started and the user try it initializes
again, the earlier chaining is finished */
if(next_op_chain!=Operation::NO_CHAIN)
finishOperationChain();
/* The next operation inserted on the list will be the start
of the chaining */
next_op_chain=Operation::CHAIN_START;
}
void OperationList::finishOperationChain(void)
{
/* If the chain is not ignored indicates that the next
element of the list no longer will be part of chaining */
if(!ignore_chain)
next_op_chain=Operation::NO_CHAIN;
else if(ignore_chain)
/* If the chain is canceled indicates that the next element
of the list still part of the chaining */
next_op_chain=Operation::CHAIN_MIDDLE;
if(operations.size() > 0 && !ignore_chain)
{
unsigned idx=operations.size()-1;
/* Marks the last operatin as being the end of chaining in case it is
on the middle of chain */
if(operations[idx]->chain_type==Operation::CHAIN_MIDDLE)
operations[idx]->chain_type=Operation::CHAIN_END;
/* If the last operation is marked as CHAIN_START indicates that
the chaining was open but only one operation is recorded
and thus the operation is marked as NO_CHAIN because as it is
only one operation there is no need to treat it as chaining */
else if(operations[idx]->chain_type==Operation::CHAIN_START)
operations[idx]->chain_type=Operation::NO_CHAIN;
}
}
void OperationList::ignoreOperationChain(bool value)
{
ignore_chain=value;
}
bool OperationList::isOperationChainStarted(void)
{
return(next_op_chain==Operation::CHAIN_START ||
next_op_chain==Operation::CHAIN_MIDDLE);
}
bool OperationList::isRedoAvailable(void)
{
/* The redo operation only can be performed
if the current index from the list of operations is at most
the penultimate element and the list can not be empty */
return(!operations.empty() && current_index < static_cast<int>(operations.size()));
}
bool OperationList::isUndoAvailable(void)
{
/* For the undo operation be performed is
enough that the list of operations is not empty */
return(!operations.empty() && current_index > 0);
}
void OperationList::setMaximumSize(unsigned max)
{
//Raises an error if a zero max size is assigned to the list
if(max==0)
throw Exception(ERR_ASG_INV_MAX_SIZE_OP_LIST,__PRETTY_FUNCTION__,__FILE__,__LINE__);
max_size=max;
}
void OperationList::addToPool(BaseObject *object, unsigned op_type)
{
ObjectType obj_type;
//Raises an error if the object to be added is not allocated
if(!object)
throw Exception(ERR_ASG_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
obj_type=object->getObjectType();
//Stores a copy of the object if its about to be moved or modified
if(op_type==Operation::OBJECT_MODIFIED ||
op_type==Operation::OBJECT_MOVED)
{
BaseObject *copy_obj=NULL;
if(obj_type!=BASE_OBJECT && obj_type!=OBJ_DATABASE)
copyObject(©_obj, object, obj_type);
else
throw Exception(ERR_ASG_OBJECT_INV_TYPE,__PRETTY_FUNCTION__,__FILE__,__LINE__);
//Raises an error if the copy fails (returning a null object)
if(!copy_obj)
throw Exception(ERR_ASG_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
else
//Inserts the copy on the pool
object_pool.push_back(copy_obj);
//object=copy_obj;
}
else
//Inserts the original object on the pool (in case of adition or deletion operations)
object_pool.push_back(object);
}
void OperationList::removeOperations(void)
{
BaseObject *object=NULL;
TableObject *tab_obj=NULL;
Table *tab=NULL;
//Destroy the operations
while(!operations.empty())
{
delete(operations.back());
operations.pop_back();
}
//Destroy the object pool
while(!object_pool.empty())
removeFromPool(0);
//Case there is not removed object
while(!not_removed_objs.empty())
{
object=not_removed_objs.back();
tab_obj=dynamic_cast<TableObject *>(object);
//Deletes the object if its not referenced on the model
if(!tab_obj && model->obterIndiceObjeto(object) < 0)
delete(object);
else if(tab_obj)
{
//Deletes the object if its not referenced by some table
tab=dynamic_cast<Table *>(tab_obj->getParentTable());
if(!tab || (tab && tab->getObjectIndex(tab_obj)) < 0)
delete(tab_obj);
}
not_removed_objs.pop_back();
}
current_index=0;
}
void OperationList::validateOperations(void)
{
vector<Operation *>::iterator itr, itr_end;
Operation *oper=NULL;
itr=operations.begin();
itr_end=operations.end();
while(itr!=itr_end)
{
oper=(*itr);
//Case the object isn't on the pool
if(!isObjectOnPool((*itr)->pool_obj))
{
//Remove the operation
operations.erase(itr);
delete(oper);
itr=operations.begin();
itr_end=operations.end();
}
else itr++;
}
}
bool OperationList::isObjectOnPool(BaseObject *object)
{
bool found=false;
vector<BaseObject *>::iterator itr, itr_end;
if(!object)
throw Exception(ERR_OPR_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
itr=object_pool.begin();
itr_end=object_pool.end();
while(!found && itr!=itr_end)
{
found=*(itr)==object;
itr++;
}
return(found);
}
void OperationList::removeFromPool(unsigned obj_idx)
{
BaseObject *object=NULL;
vector<BaseObject *>::iterator itr;
//Raises an error if the object index is invalid (out of bound)
if(obj_idx >= object_pool.size())
throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);
//Gets the element to bo removed through its index
itr=object_pool.begin() + obj_idx;
object=(*itr);
//Removes the object from pool
object_pool.erase(itr);
/* Stores the object that was in the pool on the 'not_removed_objs' vector.
The object will be deleted in the destructor of the list. Note: The object is not
deleted immediately because the model / table / list of operations may still
referencing the object or it becomes referenced when an operation is performed on the list. */
not_removed_objs.push_back(object);
}
void OperationList::registerObject(BaseObject *object, unsigned op_type, int object_idx, BaseObject *parent_obj)
{
ObjectType obj_type;
Operation *operation=NULL;
Table *parent_tab=NULL;
Relationship *parent_rel=NULL;
int obj_idx=-1;
try
{
//Raises an error if the user tries to register an operation with null object
if(!object)
throw Exception(ERR_ASG_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
obj_type=object->getObjectType();
if((obj_type==OBJ_COLUMN || obj_type==OBJ_CONSTRAINT ||
obj_type==OBJ_INDEX || obj_type==OBJ_TRIGGER ||
obj_type==OBJ_RULE) && !parent_obj)
throw Exception(ERR_OPR_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
else if(parent_obj &&
(((obj_type==OBJ_COLUMN || obj_type==OBJ_CONSTRAINT) &&
(parent_obj->getObjectType()!=OBJ_RELATIONSHIP && parent_obj->getObjectType()!=OBJ_TABLE)) ||
((obj_type==OBJ_INDEX || obj_type==OBJ_TRIGGER || obj_type==OBJ_RULE) &&
parent_obj->getObjectType()!=OBJ_TABLE)))
throw Exception(ERR_OPR_OBJ_INV_TYPE,__PRETTY_FUNCTION__,__FILE__,__LINE__);
//If the operations list is full makes the automatic cleaning before inserting a new operation
if(current_index == static_cast<int>(max_size-1))
removeOperations();
/* If adding an operation and the current index is not pointing
to the end of the list (available redo / user ran undo operations)
all elements from the current index to the end of the list will be deleted
as well as the objects in the pool that were linked to the excluded operations */
if(current_index>=0 && static_cast<unsigned>(current_index)!=operations.size())
{
//Gets the last operation index
int i=operations.size()-1;
//Removes all the operation while the current index isn't reached
while(i >= current_index)
{
removeFromPool(i);
i--;
}
//Validates the remaining operatoins after the deletion
validateOperations();
}
//Creates the new operation
operation=new Operation;
operation->op_type=op_type;
operation->chain_type=next_op_chain;
operation->original_obj=object;
//Adds the object on te pool
addToPool(object, op_type);
//Assigns the pool object to the operation
operation->pool_obj=object_pool.back();
if(next_op_chain==Operation::CHAIN_START)
next_op_chain=Operation::CHAIN_MIDDLE;
/* Performing specific operations according to the type of object.
If the object has a parent object, it must be discovered
and moreover it is necessary to find and store the index of the object
in the list on the parent object */
if(obj_type==OBJ_COLUMN || obj_type==OBJ_CONSTRAINT ||
obj_type==OBJ_INDEX || obj_type==OBJ_TRIGGER ||
obj_type==OBJ_RULE)
{
TableObject *tab_obj=NULL;
tab_obj=dynamic_cast<TableObject *>(object);
if(parent_obj->getObjectType()==OBJ_TABLE)
parent_tab=dynamic_cast<Table *>(parent_obj);
else
parent_rel=dynamic_cast<Relationship *>(parent_obj);
/* Specific case to columns: on removal operations the permissions of the objects
must be removed too */
if(obj_type==OBJ_COLUMN && op_type==Operation::OBJECT_REMOVED)
model->removerPermissoes(tab_obj);
else if(((obj_type==OBJ_TRIGGER && dynamic_cast<Trigger *>(tab_obj)->isReferRelationshipAddedColumn()) ||
(obj_type==OBJ_INDEX && dynamic_cast<Index *>(tab_obj)->isReferRelationshipAddedColumn()) ||
(obj_type==OBJ_CONSTRAINT && dynamic_cast<Constraint *>(tab_obj)->isReferRelationshipAddedColumn())))
{
if(op_type==Operation::OBJECT_REMOVED)
tab_obj->setParentTable(parent_tab);
operation->xml_definition=model->validarDefinicaoObjeto(tab_obj, SchemaParser::XML_DEFINITION);
}
operation->parent_obj=parent_obj;
/* If there is a parent relationship will get the index of the object.
Only columns and constraints are handled case the parent is a relationship */
if(parent_rel && (obj_type==OBJ_COLUMN || obj_type==OBJ_CONSTRAINT))
{
//Case a specific index wasn't specified
if(object_idx < 0)
//Stores on the operation the index on its parent
obj_idx=parent_rel->getObjectIndex(tab_obj);
else
//Assigns the specific index to operation
obj_idx=object_idx;
}
//Case there is a parent table will get the object's index
else if(parent_tab)
{
if(object_idx < 0)
//Stores on the operation the index of object on its parent
obj_idx=parent_tab->getObjectIndex(object->getName(false), obj_type);
else
obj_idx=object_idx;
}
//Raises an error if both parent table / relationship isn't allocated
else
throw Exception(ERR_OPR_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
}
else
{
//Case a specific index wasn't specified
if(object_idx < 0)
//Stores on the operation the object index on the model
obj_idx=model->obterIndiceObjeto(object);
else
//Assigns the specific index to object
obj_idx=object_idx;
}
operation->object_idx=obj_idx;
operations.push_back(operation);
current_index=operations.size();
}
catch(Exception &e)
{
if(operation)
{
removeFromPool(object_pool.size()-1);
delete(operation);
}
throw Exception(e.getErrorMessage(),e.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__,&e);
}
}
void OperationList::getOperationData(unsigned oper_idx, unsigned &oper_type, QString &obj_name, ObjectType &obj_type)
{
Operation *operation=NULL;
if(oper_idx >= operations.size())
throw Exception(ERR_REF_OBJ_INV_INDEX,__PRETTY_FUNCTION__,__FILE__,__LINE__);
operation=operations[oper_idx];
oper_type=operation->op_type;
obj_type=operation->pool_obj->getObjectType();
if(obj_type==OBJ_CAST)
obj_name=operation->pool_obj->getName();
else
obj_name=operation->pool_obj->getName(true);
if(obj_type==OBJ_COLUMN || obj_type==OBJ_CONSTRAINT || obj_type==OBJ_RULE ||
obj_type==OBJ_TRIGGER || obj_type==OBJ_INDEX)
{
obj_name=operation->parent_obj->getName(true) + "." + obj_name;
}
}
unsigned OperationList::getChainSize(void)
{
int i=current_index-1;
unsigned size=0;
if(i < 0 && !operations.empty())
i=0;
//Checks if the current operations is chained
if(!operations.empty() &&
operations[i]->chain_type!=Operation::NO_CHAIN)
{
unsigned chain_type=0;
int inc=0;
//Case the operation is the end of a chain runs the list in reverse order (from end to start)
if(operations[i]->chain_type==Operation::CHAIN_END)
{
chain_type=Operation::CHAIN_START;
inc=-1;
}
//Case the operation is the start of a chain runs the list in normal order (from start to end)
else if(operations[i]->chain_type==Operation::CHAIN_START)
{
chain_type=Operation::CHAIN_END;
inc=1;
}
//Calculates the size of chain
while(i>=0 && i < static_cast<int>(operations.size()) && operations[i]->chain_type!=chain_type)
{
i+=inc;
size++;
}
}
return(size);
}
void OperationList::undoOperation(void)
{
if(isUndoAvailable())
{
Operation *operation=NULL;
bool chain_active=false;
Exception error;
unsigned chain_size=0, pos=0;
if(!this->signalsBlocked())
chain_size=getChainSize();
do
{
/* In the undo operation, it is necessary to obtain the object
at current index decremented by 1 since the current index always
points to the element after the last element */
operation=operations[current_index-1];
/* If it is detected that the operation is chained with other
and active chaining flag is cleared marks the flag to start
the execution several operations at once */
if(!ignore_chain && !chain_active &&
operation->chain_type!=Operation::NO_CHAIN)
chain_active=true;
/* If the chaining is active and the current operation is not part of
chain, aborts execution of the operation */
else if(chain_active &&
(operation->chain_type==Operation::CHAIN_END ||
operation->chain_type==Operation::NO_CHAIN))
break;
try
{
if(!this->signalsBlocked() && chain_size > 0)
{
//Emits a signal with the current progress of operation execution
pos++;
emit s_operationExecuted((pos/static_cast<float>(chain_size))*100,
trUtf8("Undoing operation on object: %1 (%2)")
.arg(operation->pool_obj->getName())
.arg(operation->pool_obj->getTypeName()),
operation->pool_obj->getObjectType());
}
//Executes the undo operation
executeOperation(operation, false);
}
catch(Exception &e)
{
error=e;
}
current_index--;
}
/* Performs the operations while the current operation is part of chain
or the undo option is available */
while(!ignore_chain && isUndoAvailable() && operation->chain_type!=Operation::NO_CHAIN);
if(error.getErrorType()!=ERR_CUSTOM)
throw Exception(error.getErrorMessage(), error.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__);
}
}
void OperationList::redoOperation(void)
{
if(isRedoAvailable())
{
Operation *operation=NULL;
bool chain_active=false;
Exception error;
unsigned chain_size=0, pos=0;
if(!this->signalsBlocked())
chain_size=getChainSize();
do
{
//Gets the current operation
operation=operations[current_index];
/* If it is detected that the operation is chained with other
and active chaining flag is cleared marks the flag to start
the execution several operations at once */
if(!ignore_chain && !chain_active &&
operation->chain_type!=Operation::NO_CHAIN)
chain_active=true;
/* If the chaining is active and the current operation is not part of
chain or it is at the start of chain, aborts execution of the operation */
else if(chain_active &&
(operation->chain_type==Operation::CHAIN_START ||
operation->chain_type==Operation::NO_CHAIN))
break;
try
{
if(chain_size > 0)
{
//Emits a signal with the current progress of operation execution
pos++;
emit s_operationExecuted((pos/static_cast<float>(chain_size))*100,
trUtf8("Redoing operation on object:: %1 (%2)")
.arg(operation->pool_obj->getName())
.arg(operation->pool_obj->getTypeName()),
operation->pool_obj->getObjectType());
}
//Executes the redo operation (second argument as 'true')
executeOperation(operation, true);
}
catch(Exception &e)
{
error=e;
}
current_index++;
}
/* Performs the operations while the current operation is part of chain
or the redo option is available */
while(!ignore_chain && isRedoAvailable() && operation->chain_type!=Operation::NO_CHAIN);
if(error.getErrorType()!=ERR_CUSTOM)
throw Exception(error.getErrorMessage(), error.getErrorType(),__PRETTY_FUNCTION__,__FILE__,__LINE__);
}
}
void OperationList::executeOperation(Operation *oper, bool redo)
{
if(oper)
{
BaseObject *orig_obj=NULL, *bkp_obj=NULL, *object=NULL, *aux_obj=NULL;
ObjectType obj_type;
Table *parent_tab=NULL;
Relationship *parent_rel=NULL;
object=oper->pool_obj;
obj_type=object->getObjectType();
/* Converting the parent object, if any, to the correct class according
to the type of the parent object. If OBJ_TABLE, the pointer
'parent_tab' get the reference to table and will be used as referential
in the operations below. If the parent object is a relationship, the pointer
'parent_rel' get the reference to the relationship */
if(oper->parent_obj)
{
if(oper->parent_obj->getObjectType()==OBJ_TABLE)
parent_tab=dynamic_cast<Table *>(oper->parent_obj);
else
parent_rel=dynamic_cast<Relationship *>(oper->parent_obj);
}
/* If the XML definition of object is set indicates that it is referencing a column
included by relationship (special object) */
if(!oper->xml_definition.isEmpty() &&
((oper->op_type==Operation::OBJECT_REMOVED && !redo) ||
(oper->op_type==Operation::OBJECT_CREATED && redo) ||
(oper->op_type==Operation::OBJECT_MODIFIED ||
oper->op_type==Operation::OBJECT_MOVED)))
{
//Resets the XML parser and loads the buffer xml from the operation
XMLParser::restartParser();
XMLParser::loadXMLBuffer(oper->xml_definition);
if(obj_type==OBJ_TRIGGER)
aux_obj=model->criarGatilho(parent_tab);
else if(obj_type==OBJ_INDEX)
aux_obj=model->criarIndice(parent_tab);
else if(obj_type==OBJ_CONSTRAINT)
aux_obj=model->criarRestricao(oper->parent_obj);
else if(obj_type==OBJ_SEQUENCE)
aux_obj=model->criarSequencia();
}
/* If the operation is a modified/moved object, the object copy
stored in the pool will be restored */
if(oper->op_type==Operation::OBJECT_MODIFIED ||
oper->op_type==Operation::OBJECT_MOVED)
{
if(obj_type==OBJ_RELATIONSHIP)
{
/* Due to the complexity of the class Relationship and the strong link between all
relationships of the model it is necessary to store XML for special objects and
disconnect ALL relationships, perform the modification at it and the revalidating
all relationships again. */
model->obterXMLObjetosEspeciais();
model->desconectarRelacionamentos();
}
//Gets the object in the current state from the parent object
if(parent_tab)
orig_obj=dynamic_cast<TableObject *>(parent_tab->getObject(oper->object_idx, obj_type));
else if(parent_rel)
orig_obj=dynamic_cast<TableObject *>(parent_rel->getObject(oper->object_idx, obj_type));
else
orig_obj=model->obterObjeto(oper->object_idx, obj_type);
if(aux_obj)
oper->xml_definition=model->validarDefinicaoObjeto(orig_obj, SchemaParser::SQL_DEFINITION);
/* The original object (obtained from the table, relationship or model) will have its
previous values restored with the existing copy on the pool. After restoring the object
on the pool will have the same attributes as the object before being restored
to enable redo operations */
copyObject(reinterpret_cast<BaseObject **>(&bkp_obj), orig_obj, obj_type);
copyObject(reinterpret_cast<BaseObject **>(&orig_obj), object, obj_type);
copyObject(reinterpret_cast<BaseObject **>(&object), bkp_obj, obj_type);
object=orig_obj;
if(aux_obj)
copyObject(reinterpret_cast<BaseObject **>(&object), aux_obj, obj_type);
}
/* If the operation is of object removed and is not a redo, or
if the object was previously created and wants to redo the operation
the existing pool object will be inserted into table or in your relationship
on its original index */
else if((oper->op_type==Operation::OBJECT_REMOVED && !redo) ||
(oper->op_type==Operation::OBJECT_CREATED && redo))
{
if(aux_obj)
copyObject(reinterpret_cast<BaseObject **>(&object), aux_obj, obj_type);
if(parent_tab)
{
parent_tab->addObject(dynamic_cast<TableObject *>(object), oper->object_idx);
if(object->getObjectType()==OBJ_CONSTRAINT &&
dynamic_cast<Constraint *>(object)->getConstraintType()==ConstraintType::foreign_key)
model->atualizarRelFkTabela(parent_tab);
}
else if(parent_rel)
parent_rel->addObject(dynamic_cast<TableObject *>(object), oper->object_idx);
else
if(dynamic_cast<Table *>(object))
dynamic_cast<Table *>(object)->getCodeDefinition(SchemaParser::SQL_DEFINITION);
model->adicionarObjeto(object, oper->object_idx);
}
/* If the operation is a previously created object or if the object
was removed and wants to redo the operation it'll be
excluded from the table or relationship */
else if((oper->op_type==Operation::OBJECT_CREATED && !redo) ||
(oper->op_type==Operation::OBJECT_REMOVED && redo))
{
if(parent_tab)
parent_tab->removeObject(oper->object_idx,obj_type);
else if(parent_rel)
parent_rel->removeObject(oper->object_idx,obj_type);
else
model->removerObjeto(object, oper->object_idx);
}
/* If the parent table or parent relationship is set
indicates that the operation was performed on a child object of these objects */
if(parent_tab || parent_rel)
{
//Marks the parent object as modified to for its redraw
if(parent_tab)
parent_tab->setModified(true);
else
parent_rel->setModified(true);
if(parent_tab &&
(object->getObjectType()==OBJ_COLUMN ||
object->getObjectType()==OBJ_CONSTRAINT))
{
model->validarRelacObjetoTabela(dynamic_cast<TableObject *>(object), parent_tab);
if(object->getObjectType()==OBJ_CONSTRAINT &&
dynamic_cast<Constraint *>(object)->getConstraintType()==ConstraintType::foreign_key)
model->atualizarRelFkTabela(parent_tab);
}
else if(parent_rel)
model->validarRelacionamentos();
}
/* If the object in question is graphical it has the modified flag
marked to force the redraw at the time of its restoration */
else if(obj_type==OBJ_TABLE || obj_type==OBJ_VIEW ||
obj_type==BASE_RELATIONSHIP || obj_type==OBJ_RELATIONSHIP ||
obj_type==OBJ_TEXTBOX)
{
BaseGraphicObject *obj_grafico=dynamic_cast<BaseGraphicObject *>(object);
if(oper->op_type==Operation::OBJECT_MODIFIED ||
oper->op_type==Operation::OBJECT_MOVED)
obj_grafico->setModified(true);
//Case the object is a view is necessary to update the table-view relationships on the model
if(obj_type==OBJ_VIEW && oper->op_type==Operation::OBJECT_MODIFIED)
model->atualizarRelTabelaVisao(dynamic_cast<View *>(obj_grafico));
else if((obj_type==OBJ_RELATIONSHIP ||
(obj_type==OBJ_TABLE && model->obterRelacionamento(dynamic_cast<BaseTable *>(object), NULL))) &&
oper->op_type==Operation::OBJECT_MODIFIED)
model->validarRelacionamentos();
}
}
}
void OperationList::removeLastOperation(void)
{
if(!operations.empty())
{
Operation *oper=NULL;
bool end=false;
vector<Operation *>::reverse_iterator itr;
unsigned obj_idx=operations.size()-1;
//Gets the last operation on the list using reverse iterator
itr=operations.rbegin();
while(!end)
{
oper=(*itr);
//Removes the object related to the operation from the pool
removeFromPool(obj_idx);
/* Stop condition for removing the operation:
1) The operation is not chained with others, or
2) If the last operation at the end of a chain
all chained operations are removed including
the first operation of the chain, when the latter
is removed the iteration is stopped.*/
end=(ignore_chain ||
(!ignore_chain &&
(oper->chain_type==Operation::NO_CHAIN ||
oper->chain_type==Operation::CHAIN_START)));
itr++; obj_idx--;
}
/* If the head of chaining is removed (CHAIN_START)
marks that the next element in the list is the new
start of chain */
if(oper && oper->chain_type==Operation::CHAIN_START)
next_op_chain=Operation::CHAIN_START;
//Validates the remaining operations
validateOperations();
/* Points the current index to the end of list if it is beyond
the last element */
if(static_cast<unsigned>(current_index) > operations.size())
current_index=operations.size();
}
}
void OperationList::updateObjectIndex(BaseObject *object, unsigned new_idx)
{
vector<Operation *>::iterator itr, itr_end;
Operation *oper=NULL;
if(!object)
throw Exception(ERR_OPR_NOT_ALOC_OBJECT,__PRETTY_FUNCTION__,__FILE__,__LINE__);
itr=operations.begin();
itr_end=operations.end();
while(itr!=itr_end)
{
oper=(*itr);
if(oper->original_obj==object)
oper->object_idx=new_idx;
itr++;
}
}