forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNHibernateUtil.cs
426 lines (373 loc) · 11.9 KB
/
NHibernateUtil.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
using System;
using System.Collections;
using NHibernate.Collection;
using NHibernate.Impl;
using NHibernate.Proxy;
using NHibernate.Type;
using NHibernate.UserTypes;
namespace NHibernate
{
/// <summary>
/// Provides access to the full range of NHibernate built-in types.
/// IType instances may be used to bind values to query parameters.
/// Also a factory for new Blobs and Clobs.
/// </summary>
public sealed class NHibernateUtil
{
/// <summary>
/// NHibernate Ansi String type
/// </summary>
public static readonly NullableType AnsiString = new AnsiStringType();
/// <summary>
/// NHibernate binary type
/// </summary>
public static readonly NullableType Binary = new BinaryType();
/// <summary>
/// NHibernate binary blob type
/// </summary>
public static readonly NullableType BinaryBlob = new BinaryBlobType();
/// <summary>
/// NHibernate boolean type
/// </summary>
public static readonly NullableType Boolean = new BooleanType();
/// <summary>
/// NHibernate byte type
/// </summary>
public static readonly NullableType Byte = new ByteType();
/// <summary>
/// NHibernate character type
/// </summary>
public static readonly NullableType Character = new CharType();
/// <summary>
/// NHibernate Culture Info type
/// </summary>
public static readonly NullableType CultureInfo = new CultureInfoType();
/// <summary>
/// NHibernate date type
/// </summary>
public static readonly NullableType DateTime = new DateTimeType();
/// <summary>
/// NHibernate date type
/// </summary>
public static readonly NullableType Date = new DateType();
/// <summary>
/// NHibernate decimal type
/// </summary>
public static readonly NullableType Decimal = new DecimalType();
/// <summary>
/// NHibernate double type
/// </summary>
public static readonly NullableType Double = new DoubleType();
/// <summary>
/// NHibernate Guid type.
/// </summary>
public static readonly NullableType Guid = new GuidType();
/// <summary>
/// NHibernate System.Int16 (short in C#) type
/// </summary>
public static readonly NullableType Int16 = new Int16Type();
/// <summary>
/// NHibernate System.Int32 (int in C#) type
/// </summary>
public static readonly NullableType Int32 = new Int32Type();
/// <summary>
/// NHibernate System.Int64 (long in C#) type
/// </summary>
public static readonly NullableType Int64 = new Int64Type();
/// <summary>
/// NHibernate System.SByte type
/// </summary>
public static readonly NullableType SByte = new SByteType();
/// <summary>
/// NHibernate System.UInt16 (ushort in C#) type
/// </summary>
public static readonly NullableType UInt16 = new UInt16Type();
/// <summary>
/// NHibernate System.UInt32 (uint in C#) type
/// </summary>
public static readonly NullableType UInt32 = new UInt32Type();
/// <summary>
/// NHibernate System.UInt64 (ulong in C#) type
/// </summary>
public static readonly NullableType UInt64 = new UInt64Type();
/// <summary>
/// NHIbernate System.Single (float in C#) Type
/// </summary>
public static readonly NullableType Single = new SingleType();
/// <summary>
/// NHibernate String type
/// </summary>
public static readonly NullableType String = new StringType();
/// <summary>
/// NHibernate string clob type
/// </summary>
public static readonly NullableType StringClob = new StringClobType();
/// <summary>
/// NHibernate Time type
/// </summary>
public static readonly NullableType Time = new TimeType();
/// <summary>
/// NHibernate Ticks type
/// </summary>
public static readonly NullableType Ticks = new TicksType();
/// <summary>
/// NHibernate Ticks type
/// </summary>
public static readonly NullableType TimeSpan = new TimeSpanType();
/// <summary>
/// NHibernate Timestamp type
/// </summary>
public static readonly NullableType Timestamp = new TimestampType();
/// <summary>
/// NHibernate TrueFalse type
/// </summary>
public static readonly NullableType TrueFalse = new TrueFalseType();
/// <summary>
/// NHibernate YesNo type
/// </summary>
public static readonly NullableType YesNo = new YesNoType();
/// <summary>
/// NHibernate class type
/// </summary>
public static readonly NullableType Class = new TypeType();
/// <summary>
/// NHibernate serializable type
/// </summary>
public static readonly NullableType Serializable = new SerializableType();
/// <summary>
/// NHibernate System.Object type
/// </summary>
public static readonly IType Object = new AnyType();
// /// <summary>
// /// NHibernate blob type
// /// </summary>
// public static readonly NullableType Blob = new BlobType();
// /// <summary>
// /// NHibernate clob type
// /// </summary>
// public static readonly NullableType Clob = new ClobType();
/// <summary>
/// Cannot be instantiated.
/// </summary>
private NHibernateUtil()
{
throw new NotSupportedException();
}
public static readonly NullableType AnsiChar = new AnsiCharType();
/// <summary>
/// A NHibernate persistent enum type
/// </summary>
/// <param name="enumClass"></param>
/// <returns></returns>
public static IType Enum(System.Type enumClass)
{
return new PersistentEnumType(enumClass);
}
/// <summary>
/// A NHibernate serializable type
/// </summary>
/// <param name="serializableClass"></param>
/// <returns></returns>
public static IType GetSerializable(System.Type serializableClass)
{
return new SerializableType(serializableClass);
}
/// <summary>
/// A NHibernate serializable type
/// </summary>
/// <param name="metaType">a type mapping <see cref="IType"/> to a single column</param>
/// <param name="identifierType">the entity identifier type</param>
/// <returns></returns>
public static IType Any(IType metaType, IType identifierType)
{
return new AnyType(metaType, identifierType);
}
/// <summary>
/// A NHibernate persistent object (entity) type
/// </summary>
/// <param name="persistentClass">a mapped entity class</param>
/// <returns></returns>
[Obsolete("use NHibernate.Entity instead")]
public static IType Association(System.Type persistentClass)
{
// not really a many-to-one association *necessarily*
return new ManyToOneType(persistentClass);
}
/// <summary>
/// A NHibernate persistent object (entity) type
/// </summary>
/// <param name="persistentClass">a mapped entity class</param>
/// <returns></returns>
public static IType Entity(System.Type persistentClass)
{
// not really a many-to-one association *necessarily*
return new ManyToOneType(persistentClass);
}
/// <summary>
/// A NHibernate custom type
/// </summary>
/// <param name="userTypeClass">a class that implements UserType</param>
/// <returns></returns>
public static IType Custom(System.Type userTypeClass)
{
if (typeof(ICompositeUserType).IsAssignableFrom(userTypeClass))
{
return new CompositeCustomType(userTypeClass, null);
}
else
{
return new CustomType(userTypeClass, null);
}
}
/// <summary>
/// Force initialization of a proxy or persistent collection.
/// </summary>
/// <param name="proxy">a persistable object, proxy, persistent collection or null</param>
/// <exception cref="HibernateException">if we can't initialize the proxy at this time, eg. the Session was closed</exception>
public static void Initialize(object proxy)
{
if (proxy == null)
{
return;
}
else if (proxy is INHibernateProxy)
{
((INHibernateProxy) proxy).HibernateLazyInitializer.Initialize();
}
else if (proxy is IPersistentCollection)
{
((IPersistentCollection) proxy).ForceInitialization();
}
}
/// <summary>
/// Is the proxy or persistent collection initialized?
/// </summary>
/// <param name="proxy">a persistable object, proxy, persistent collection or null</param>
/// <returns>true if the argument is already initialized, or is not a proxy or collection</returns>
public static bool IsInitialized(object proxy)
{
if (proxy is INHibernateProxy)
{
return !((INHibernateProxy) proxy).HibernateLazyInitializer.IsUninitialized;
}
else if (proxy is IPersistentCollection)
{
return ((IPersistentCollection) proxy).WasInitialized;
}
else
{
return true;
}
}
/// <summary>
/// Get the true, underlying class of a proxied persistent class. This operation
/// will initialize a proxy by side-effect.
/// </summary>
/// <param name="proxy">a persistable object or proxy</param>
/// <returns>the true class of the instance</returns>
public static System.Type GetClass(object proxy)
{
if (proxy is INHibernateProxy)
{
return ((INHibernateProxy) proxy).HibernateLazyInitializer.GetImplementation().GetType();
}
else
{
return proxy.GetType();
}
}
/*
/// <summary>
/// Create a new Blob. The returned object will be initially immutable.
/// </summary>
/// <param name="bytes">a byte array</param>
/// <returns></returns>
public static Blob CreateBlob(byte[] bytes) {
return new BlobImpl(bytes);
}
/// <summary>
/// Create a new Blob. The returned object will be initially immutable.
/// </summary>
/// <param name="stream">a binary stream</param>
/// <param name="length">the number of bytes in the stream</param>
/// <returns></returns>
public static Blob CreateBlob(TextReader stream, int length) {
return new BlobImpl(stream, length);
}
/// <summary>
/// Create a new Blob. The returned object will be initially immutable.
/// </summary>
/// <param name="stream">a binary stream</param>
/// <param name="length">the number of bytes in the stream</param>
/// <returns></returns>
public static Blob CreateBlob(BinaryReader stream, int length)
{
return new BlobImpl(stream, length);
}
/// <summary>
/// Create a new Blob. The returned object will be initially immutable.
/// </summary>
/// <param name="stream">a binary stream</param>
/// <returns></returns>
public static Blob CreateBlob(StreamReader stream)
{
return new BlobImpl( stream, stream.available() );
}
/// <summary>
/// Create a new Blob. The returned object will be initially immutable.
/// </summary>
/// <param name="stream">a binary stream</param>
/// <returns></returns>
public static Blob CreateBlob(BinaryReader stream)
{
return new BlobImpl( stream, stream.available() );
}
/// <summary>
/// Create a new Clob. The returned object will be
/// initially immutable.
/// </summary>
/// <param name="str">a String</param>
/// <returns></returns>
public static Clob CreateClob(string str)
{
return new ClobImpl(str);
}
/// <summary>
/// Create a new Clob. The returned object will be initially immutable.
/// </summary>
/// <param name="reader">a character stream</param>
/// <param name="length">the number of characters in the stream</param>
/// <returns></returns>
public static Clob CreateClob(TextReader reader, int length) {
return new ClobImpl(reader, length);
}
*/
/// <summary>
/// Close an <see cref="IEnumerator" /> obtained from an <see cref="IEnumerable" />
/// returned by NHibernate immediately, instead of waiting until the session is
/// closed or disconnected.
/// </summary>
public static void Close(IEnumerator enumerator)
{
EnumerableImpl hibernateEnumerator = enumerator as EnumerableImpl;
if (hibernateEnumerator == null)
{
throw new ArgumentException("Not a NHibernate enumerator", "enumerator");
}
hibernateEnumerator.Dispose();
}
/// <summary>
/// Close an <see cref="IEnumerable" /> returned by NHibernate immediately,
/// instead of waiting until the session is closed or disconnected.
/// </summary>
public static void Close(IEnumerable enumerable)
{
EnumerableImpl hibernateEnumerable = enumerable as EnumerableImpl;
if (hibernateEnumerable == null)
{
throw new ArgumentException("Not a NHibernate enumerable", "enumerable");
}
hibernateEnumerable.Dispose();
}
}
}