forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCascadingAction.cs
298 lines (280 loc) · 9.71 KB
/
CascadingAction.cs
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
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by AsyncGenerator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using NHibernate.Collection;
using NHibernate.Event;
using NHibernate.Persister.Entity;
using NHibernate.Proxy;
using NHibernate.Type;
namespace NHibernate.Engine
{
using System.Threading.Tasks;
using System.Threading;
public abstract partial class CascadingAction
{
#region The CascadingAction contract
/// <summary> Cascade the action to the child object. </summary>
/// <param name="session">The session within which the cascade is occurring. </param>
/// <param name="child">The child to which cascading should be performed. </param>
/// <param name="entityName">The child's entity name </param>
/// <param name="anything">Typically some form of cascade-local cache which is specific to each CascadingAction type </param>
/// <param name="isCascadeDeleteEnabled">Are cascading deletes enabled. </param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
public abstract Task CascadeAsync(IEventSource session, object child, string entityName, object anything, bool isCascadeDeleteEnabled, CancellationToken cancellationToken);
/// <summary>
/// Called (in the case of <see cref="RequiresNoCascadeChecking"/> returning true) to validate
/// that no cascade on the given property is considered a valid semantic.
/// </summary>
/// <param name="session">The session within which the cascade is occurring. </param>
/// <param name="child">The property value </param>
/// <param name="parent">The property value owner </param>
/// <param name="persister">The entity persister for the owner </param>
/// <param name="propertyIndex">The index of the property within the owner. </param>
/// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
public virtual Task NoCascadeAsync(IEventSource session, object child, object parent, IEntityPersister persister, int propertyIndex, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
try
{
NoCascade(session, child, parent, persister, propertyIndex);
return Task.CompletedTask;
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}
#endregion
private partial class DeleteCascadingAction : CascadingAction
{
public override Task CascadeAsync(IEventSource session, object child, string entityName, object anything, bool isCascadeDeleteEnabled, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
try
{
if (log.IsDebugEnabled())
{
log.Debug("cascading to delete: {0}", entityName);
}
return session.DeleteAsync(entityName, child, isCascadeDeleteEnabled, (ISet<object>)anything, cancellationToken);
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}
}
private partial class LockCascadingAction : CascadingAction
{
public override Task CascadeAsync(IEventSource session, object child, string entityName, object anything, bool isCascadeDeleteEnabled, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
try
{
if (log.IsDebugEnabled())
{
log.Debug("cascading to lock: {0}", entityName);
}
return session.LockAsync(entityName, child, LockMode.None, cancellationToken);
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}
}
private partial class RefreshCascadingAction : CascadingAction
{
public override Task CascadeAsync(IEventSource session, object child, string entityName, object anything, bool isCascadeDeleteEnabled, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
try
{
if (log.IsDebugEnabled())
{
log.Debug("cascading to refresh: {0}", entityName);
}
return session.RefreshAsync(child, (IDictionary)anything, cancellationToken);
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}
}
private partial class EvictCascadingAction : CascadingAction
{
public override Task CascadeAsync(IEventSource session, object child, string entityName, object anything, bool isCascadeDeleteEnabled, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
try
{
if (log.IsDebugEnabled())
{
log.Debug("cascading to evict: {0}", entityName);
}
return session.EvictAsync(child, cancellationToken);
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}
}
private partial class SaveUpdateCascadingAction : CascadingAction
{
public override Task CascadeAsync(IEventSource session, object child, string entityName, object anything, bool isCascadeDeleteEnabled, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
try
{
if (log.IsDebugEnabled())
{
log.Debug("cascading to saveOrUpdate: {0}", entityName);
}
return session.SaveOrUpdateAsync(entityName, child, cancellationToken);
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}
}
private partial class MergeCascadingAction : CascadingAction
{
public override Task CascadeAsync(IEventSource session, object child, string entityName, object anything, bool isCascadeDeleteEnabled, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
try
{
if (log.IsDebugEnabled())
{
log.Debug("cascading to merge: {0}", entityName);
}
return session.MergeAsync(entityName, child, (IDictionary)anything, cancellationToken);
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}
}
private partial class PersistCascadingAction : CascadingAction
{
public override Task CascadeAsync(IEventSource session, object child, string entityName, object anything, bool isCascadeDeleteEnabled, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
try
{
if (log.IsDebugEnabled())
{
log.Debug("cascading to persist: {0}", entityName);
}
return session.PersistAsync(entityName, child, (IDictionary)anything, cancellationToken);
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}
}
private partial class PersistOnFlushCascadingAction : CascadingAction
{
public override Task CascadeAsync(IEventSource session, object child, string entityName, object anything, bool isCascadeDeleteEnabled, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
try
{
if (log.IsDebugEnabled())
{
log.Debug("cascading to persistOnFlush: {0}", entityName);
}
return session.PersistOnFlushAsync(entityName, child, (IDictionary)anything, cancellationToken);
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}
public override async Task NoCascadeAsync(IEventSource session, object child, object parent, IEntityPersister persister, int propertyIndex, CancellationToken cancellationToken)
{
cancellationToken.ThrowIfCancellationRequested();
if (child == null)
{
return;
}
IType type = persister.PropertyTypes[propertyIndex];
if (type.IsEntityType)
{
string childEntityName = ((EntityType)type).GetAssociatedEntityName(session.Factory);
if (!IsInManagedState(child, session) && !child.IsProxy() && await (ForeignKeys.IsTransientSlowAsync(childEntityName, child, session, cancellationToken)).ConfigureAwait(false))
{
string parentEntiytName = persister.EntityName;
string propertyName = persister.PropertyNames[propertyIndex];
throw new TransientObjectException(
string.Format(
"object references an unsaved transient instance - save the transient instance before flushing or set cascade action for the property to something that would make it autosave: {0}.{1} -> {2}",
parentEntiytName, propertyName, childEntityName));
}
}
}
}
private partial class ReplicateCascadingAction : CascadingAction
{
public override Task CascadeAsync(IEventSource session, object child, string entityName, object anything, bool isCascadeDeleteEnabled, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
{
return Task.FromCanceled<object>(cancellationToken);
}
try
{
if (log.IsDebugEnabled())
{
log.Debug("cascading to replicate: {0}", entityName);
}
return session.ReplicateAsync(entityName, child, (ReplicationMode)anything, cancellationToken);
}
catch (Exception ex)
{
return Task.FromException<object>(ex);
}
}
}
}
}