forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollection.cs
632 lines (534 loc) · 13.7 KB
/
Collection.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
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
using System;
using System.Collections.Generic;
using NHibernate.Engine;
using NHibernate.SqlCommand;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Mapping
{
/// <summary>
/// Base class that stores the mapping information for <c><array></c>, <c><bag></c>,
/// <c><id-bag></c>, <c><list></c>, <c><map></c>, and <c><set></c>
/// collections.
/// </summary>
/// <remarks>
/// Subclasses are responsible for the specialization required for the particular
/// collection style.
/// </remarks>
[Serializable]
public abstract class Collection : IFetchable, IValue, IFilterable
{
private static readonly IEnumerable<ISelectable> EmptyColumns = System.Array.Empty<ISelectable>();
public const string DefaultElementColumnName = "elt";
public const string DefaultKeyColumnName = "id";
private IKeyValue key;
private IValue element;
private Table collectionTable;
private string role;
private bool lazy;
private bool extraLazy;
private bool inverse;
private bool mutable = true;
private string cacheConcurrencyStrategy;
private String cacheRegionName;
private string orderBy;
private string where;
private PersistentClass owner;
private bool sorted;
private object comparer;
private bool orphanDelete;
private int batchSize = -1;
private FetchMode fetchMode;
private System.Type collectionPersisterClass;
private string referencedPropertyName;
private string typeName;
private string loaderName;
private SqlString customSQLInsert;
private bool customInsertCallable;
private ExecuteUpdateResultCheckStyle insertCheckStyle;
private SqlString customSQLDelete;
private bool customDeleteCallable;
private ExecuteUpdateResultCheckStyle deleteCheckStyle;
private SqlString customSQLUpdate;
private bool customUpdateCallable;
private ExecuteUpdateResultCheckStyle updateCheckStyle;
private SqlString customSQLDeleteAll;
private bool customDeleteAllCallable;
private ExecuteUpdateResultCheckStyle deleteAllCheckStyle;
private bool isGeneric;
private System.Type[] genericArguments;
private readonly Dictionary<string, string> filters = new Dictionary<string, string>();
private readonly Dictionary<string, string> manyToManyFilters = new Dictionary<string, string>();
private bool subselectLoadable;
private string manyToManyWhere;
private string manyToManyOrderBy;
private bool optimisticLocked;
private readonly HashSet<string> synchronizedTables = new HashSet<string>();
private IDictionary<string, string> typeParameters;
protected Collection(PersistentClass owner)
{
this.owner = owner;
}
public int ColumnSpan
{
get { return 0; }
}
public virtual bool IsSet
{
get { return false; }
}
public IKeyValue Key
{
get { return key; }
set { key = value; }
}
public IValue Element
{
get { return element; }
set { element = value; }
}
public virtual bool IsIndexed
{
get { return false; }
}
public Table CollectionTable
{
get { return collectionTable; }
set { collectionTable = value; }
}
public Table Table
{
get { return Owner.Table; }
}
public bool IsSorted
{
get { return sorted; }
set { sorted = value; }
}
public bool HasOrder
{
get { return orderBy != null || manyToManyOrderBy != null; }
}
public PersistentClass Owner
{
get { return owner; }
set { owner = value; }
}
public System.Type CollectionPersisterClass
{
get { return collectionPersisterClass; }
set { collectionPersisterClass = value; }
}
// The type of this property is object, so as to accommodate
// both IComparer and IComparer<T>.
public object Comparer
{
get
{
if (comparer == null && !string.IsNullOrEmpty(ComparerClassName))
{
try
{
comparer = Cfg.Environment.ObjectsFactory.CreateInstance(ReflectHelper.ClassForName(ComparerClassName));
}
catch (Exception ex)
{
throw new MappingException("Could not instantiate comparator class [" + ComparerClassName + "] for collection " + Role, ex);
}
}
return comparer;
}
set { comparer = value; }
}
public string ComparerClassName { get; set; }
public bool IsLazy
{
get { return lazy; }
set { lazy = value; }
}
public string Role
{
get { return role; }
set { role = StringHelper.InternedIfPossible(value); }
}
public IEnumerable<ISelectable> ColumnIterator
{
get { return EmptyColumns; }
}
public Formula Formula
{
get { return null; }
}
public bool IsNullable
{
get { return true; }
}
public bool IsUnique
{
get { return false; }
}
public virtual CollectionType CollectionType
{
get
{
if (typeName == null)
{
return DefaultCollectionType;
}
else
{
return TypeFactory.CustomCollection(typeName, typeParameters, role, referencedPropertyName);
}
}
}
public abstract CollectionType DefaultCollectionType { get; }
public IType Type
{
get { return CollectionType; }
}
public virtual bool IsPrimitiveArray
{
get { return false; }
}
public virtual bool IsArray
{
get { return false; }
}
public virtual bool HasFormula
{
get { return false; }
}
public virtual bool IsIdentified
{
get { return false; }
}
public bool IsOneToMany
{
get { return element is OneToMany; }
}
public string CacheConcurrencyStrategy
{
get { return cacheConcurrencyStrategy; }
set { cacheConcurrencyStrategy = value; }
}
public string CacheRegionName
{
get { return cacheRegionName ?? Role; }
set { cacheRegionName = value; }
}
public bool IsInverse
{
get { return inverse; }
set { inverse = value; }
}
public string OwnerEntityName
{
get { return owner.EntityName; }
}
public string OrderBy
{
get { return orderBy; }
set { orderBy = value; }
}
public string Where
{
get { return where; }
set { where = value; }
}
public bool HasOrphanDelete
{
get { return orphanDelete; }
set { orphanDelete = value; }
}
public int BatchSize
{
get { return batchSize; }
set { batchSize = value; }
}
public FetchMode FetchMode
{
get { return fetchMode; }
set { fetchMode = value; }
}
/// <summary>
/// Gets or sets a <see cref="Boolean"/> indicating if this is a
/// mapping for a generic collection.
/// </summary>
/// <value>
/// <see langword="true" /> if a collection from the System.Collections.Generic namespace
/// should be used, <see langword="false" /> if a collection from the System.Collections
/// namespace should be used.
/// </value>
/// <remarks>
/// This has no affect on any versions of the .net framework before .net-2.0.
/// </remarks>
public bool IsGeneric
{
get { return isGeneric; }
set { isGeneric = value; }
}
/// <summary>
/// Gets or sets an array of <see cref="System.Type"/> that contains the arguments
/// needed to construct an instance of a closed type.
/// </summary>
public System.Type[] GenericArguments
{
get { return genericArguments; }
set { genericArguments = value; }
}
protected void CheckGenericArgumentsLength(int expectedLength)
{
if (genericArguments.Length != expectedLength)
{
throw new MappingException(
string.Format(
"Error mapping generic collection {0}: expected {1} generic parameters, but the property type has {2}",
Role, expectedLength, genericArguments.Length));
}
}
public void CreateForeignKey()
{
}
private void CreateForeignKeys()
{
// if ( !IsInverse() ) { // for inverse collections, let the "other end" handle it
if (string.IsNullOrEmpty(referencedPropertyName))
{
Element.CreateForeignKey();
key.CreateForeignKeyOfEntity(Owner.EntityName);
}
// }
}
public abstract void CreatePrimaryKey();
public virtual void CreateAllKeys()
{
CreateForeignKeys();
if (!IsInverse)
CreatePrimaryKey();
}
public bool IsSimpleValue
{
get { return false; }
}
public bool IsValid(IMapping mapping)
{
return true;
}
public string ReferencedPropertyName
{
get { return referencedPropertyName; }
set { referencedPropertyName = StringHelper.InternedIfPossible(value); }
}
public virtual void Validate(IMapping mapping)
{
if (Key.IsCascadeDeleteEnabled && (!IsInverse || !IsOneToMany))
{
throw new MappingException(string.Format("only inverse one-to-many associations may use on-delete=\"cascade\": {0}", Role));
}
if (!Key.IsValid(mapping))
{
throw new MappingException(string.Format("collection foreign key mapping has wrong number of columns: {0} type: {1}", Role, Key.Type.Name));
}
if (!Element.IsValid(mapping))
{
throw new MappingException(string.Format("collection element mapping has wrong number of columns: {0} type: {1}", Role, Element.Type.Name));
}
CheckColumnDuplication();
}
public bool[] ColumnInsertability
{
get { return System.Array.Empty<bool>(); }
}
public bool[] ColumnUpdateability
{
get { return System.Array.Empty<bool>(); }
}
public string TypeName
{
get { return typeName; }
set { typeName = value; }
}
public SqlString CustomSQLInsert
{
get { return customSQLInsert; }
}
public SqlString CustomSQLDelete
{
get { return customSQLDelete; }
}
public SqlString CustomSQLUpdate
{
get { return customSQLUpdate; }
}
public SqlString CustomSQLDeleteAll
{
get { return customSQLDeleteAll; }
}
public bool IsCustomInsertCallable
{
get { return customInsertCallable; }
}
public bool IsCustomDeleteCallable
{
get { return customDeleteCallable; }
}
public bool IsCustomUpdateCallable
{
get { return customUpdateCallable; }
}
public bool IsCustomDeleteAllCallable
{
get { return customDeleteAllCallable; }
}
public ExecuteUpdateResultCheckStyle CustomSQLInsertCheckStyle
{
get { return insertCheckStyle; }
}
public ExecuteUpdateResultCheckStyle CustomSQLDeleteCheckStyle
{
get { return deleteCheckStyle; }
}
public ExecuteUpdateResultCheckStyle CustomSQLUpdateCheckStyle
{
get { return updateCheckStyle; }
}
public ExecuteUpdateResultCheckStyle CustomSQLDeleteAllCheckStyle
{
get { return deleteAllCheckStyle; }
}
public void SetCustomSQLInsert(string sql, bool callable, ExecuteUpdateResultCheckStyle checkStyle)
{
customSQLInsert = SqlString.Parse(sql);
customInsertCallable = callable;
insertCheckStyle = checkStyle;
}
public void SetCustomSQLDelete(string sql, bool callable, ExecuteUpdateResultCheckStyle checkStyle)
{
customSQLDelete = SqlString.Parse(sql);
customDeleteCallable = callable;
deleteCheckStyle = checkStyle;
}
public void SetCustomSQLDeleteAll(string sql, bool callable, ExecuteUpdateResultCheckStyle checkStyle)
{
customSQLDeleteAll = SqlString.Parse(sql);
customDeleteAllCallable = callable;
deleteAllCheckStyle = checkStyle;
}
public void SetCustomSQLUpdate(string sql, bool callable, ExecuteUpdateResultCheckStyle checkStyle)
{
customSQLUpdate = SqlString.Parse(sql);
customUpdateCallable = callable;
updateCheckStyle = checkStyle;
}
public void AddFilter(string name, string condition)
{
filters[name] = condition;
}
public IDictionary<string,string> FilterMap
{
get { return filters; }
}
public void AddManyToManyFilter(string name, string condition)
{
manyToManyFilters[name] = condition;
}
public IDictionary<string, string> ManyToManyFilterMap
{
get { return manyToManyFilters; }
}
public string LoaderName
{
get { return loaderName; }
set { loaderName = StringHelper.InternedIfPossible(value); }
}
public bool IsSubselectLoadable
{
get { return subselectLoadable; }
set { subselectLoadable = value; }
}
public string ManyToManyWhere
{
get { return manyToManyWhere; }
set { manyToManyWhere = value; }
}
public string ManyToManyOrdering
{
get { return manyToManyOrderBy; }
set { manyToManyOrderBy = value; }
}
public bool IsOptimisticLocked
{
get { return optimisticLocked; }
set { optimisticLocked = value; }
}
public bool ExtraLazy
{
get { return extraLazy; }
set { extraLazy = value; }
}
private void CheckColumnDuplication()
{
HashSet<string> cols = new HashSet<string>();
CheckColumnDuplication(cols, Key.ColumnIterator);
if (IsIndexed)
{
CheckColumnDuplication(cols, ((IndexedCollection)this).Index.ColumnIterator);
}
if (IsIdentified)
{
CheckColumnDuplication(cols, ((IdentifierCollection)this).Identifier.ColumnIterator);
}
if (!IsOneToMany)
{
CheckColumnDuplication(cols, Element.ColumnIterator);
}
}
private void CheckColumnDuplication(ISet<string> distinctColumns, IEnumerable<ISelectable> columns)
{
foreach (ISelectable s in columns)
{
if(!s.IsFormula)
{
Column col = (Column)s;
if (!distinctColumns.Add(col.Name))
{
throw new MappingException(string.Format("Repeated column in mapping for collection: {0} column: {1}", Role, col.Name));
}
}
}
}
public virtual bool IsAlternateUniqueKey
{
get { return false; }
}
public void SetTypeUsingReflection(string className, string propertyName, string access)
{
}
public object Accept(IValueVisitor visitor)
{
return visitor.Accept(this);
}
public virtual bool IsMap
{
get { return false; }
}
public bool IsMutable
{
get { return mutable; }
set { mutable = value; }
}
public ISet<string> SynchronizedTables
{
get { return synchronizedTables; }
}
public IDictionary<string, string> TypeParameters
{
get { return typeParameters; }
set { typeParameters = value; }
}
public override string ToString()
{
return GetType().FullName + '(' + Role + ')';
}
}
}