Skip to content

Commit 7b22841

Browse files
committed
added InsertAll and UpdateAll methods
1 parent 85cdf2f commit 7b22841

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

Source/Persistence/Core/Spring.Persistence.Core.Session.pas

+52-4
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,15 @@ TSession = class(TAbstractSession)
166166
function FindAll<T: class, constructor>: IList<T>;
167167

168168
/// <summary>
169-
/// Inserts model to the database .
169+
/// Inserts model to the database.
170170
/// </summary>
171171
procedure Insert(const entity: TObject);
172172

173+
/// <summary>
174+
/// Inserts model and its aggregations to the database.
175+
/// </summary>
176+
procedure InsertAll(const entity: TObject);
177+
173178
/// <summary>
174179
/// Inserts models to the database.
175180
/// </summary>
@@ -182,12 +187,17 @@ TSession = class(TAbstractSession)
182187
function IsNew(const entity: TObject): Boolean;
183188

184189
/// <summary>
185-
/// Updates model in a database.
190+
/// Updates model in the database.
186191
/// </summary>
187192
procedure Update(const entity: TObject);
188193

189194
/// <summary>
190-
/// Updates multiple models in a database.
195+
/// Updates model and its aggregations in the database.
196+
/// </summary>
197+
procedure UpdateAll(const entity: TObject);
198+
199+
/// <summary>
200+
/// Updates multiple models in the database.
191201
/// </summary>
192202
procedure UpdateList<T: class, constructor>(const entities: IEnumerable<T>);
193203

@@ -447,6 +457,25 @@ procedure TSession.Insert(const entity: TObject);
447457
DoInsert(entity, inserter);
448458
end;
449459

460+
procedure TSession.InsertAll(const entity: TObject);
461+
var
462+
relations: IList<TObject>;
463+
relation: TObject;
464+
entityWrapper, foreignEntityWrapper: IEntityWrapper;
465+
begin
466+
TRttiExplorer.GetRelationsOf(entity, ManyToOneAttribute).ForEach(Insert);
467+
Insert(entity);
468+
469+
entityWrapper := TEntityWrapper.Create(entity);
470+
relations := TRttiExplorer.GetRelationsOf(entity, OneToManyAttribute);
471+
for relation in relations do
472+
begin
473+
foreignEntityWrapper := TEntityWrapper.Create(relation);
474+
UpdateForeignKeysFor(foreignEntityWrapper, entityWrapper);
475+
InsertAll(relation);
476+
end;
477+
end;
478+
450479
procedure TSession.InsertList<T>(const entities: IEnumerable<T>);
451480
var
452481
inserter: IInsertCommand;
@@ -515,10 +544,10 @@ procedure TSession.SaveAll(const entity: TObject);
515544
TRttiExplorer.GetRelationsOf(entity, ManyToOneAttribute).ForEach(Save);
516545
Save(entity);
517546

547+
entityWrapper := TEntityWrapper.Create(entity);
518548
relations := TRttiExplorer.GetRelationsOf(entity, OneToManyAttribute);
519549
for relation in relations do
520550
begin
521-
entityWrapper := TEntityWrapper.Create(entity);
522551
foreignEntityWrapper := TEntityWrapper.Create(relation);
523552
UpdateForeignKeysFor(foreignEntityWrapper, entityWrapper);
524553
SaveAll(relation);
@@ -578,6 +607,25 @@ procedure TSession.Update(const entity: TObject);
578607
DoUpdate(entity, updater);
579608
end;
580609

610+
procedure TSession.UpdateAll(const entity: TObject);
611+
var
612+
relations: IList<TObject>;
613+
relation: TObject;
614+
entityWrapper, foreignEntityWrapper: IEntityWrapper;
615+
begin
616+
TRttiExplorer.GetRelationsOf(entity, ManyToOneAttribute).ForEach(Update);
617+
Update(entity);
618+
619+
entityWrapper := TEntityWrapper.Create(entity);
620+
relations := TRttiExplorer.GetRelationsOf(entity, OneToManyAttribute);
621+
for relation in relations do
622+
begin
623+
foreignEntityWrapper := TEntityWrapper.Create(relation);
624+
UpdateForeignKeysFor(foreignEntityWrapper, entityWrapper);
625+
UpdateAll(relation);
626+
end;
627+
end;
628+
581629
procedure TSession.UpdateList<T>(const entities: IEnumerable<T>);
582630
var
583631
updater: IUpdateCommand;

0 commit comments

Comments
 (0)