forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIdentifierGeneratorFactory.cs
335 lines (322 loc) · 10.9 KB
/
IdentifierGeneratorFactory.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Common;
using NHibernate.Engine;
using NHibernate.Type;
using NHibernate.Util;
using NHibernate.Id.Enhanced;
namespace NHibernate.Id
{
/// <summary>
/// Factory methods for <c>IdentifierGenerator</c> framework.
/// </summary>
/// <remarks>
/// <p>The built in strategies for identifier generation in NHibernate are:</p>
/// <list type="table">
/// <listheader>
/// <term>strategy</term>
/// <description>Implementation of strategy</description>
/// </listheader>
/// <item>
/// <term>assigned</term>
/// <description><see cref="Assigned"/></description>
/// </item>
/// <item>
/// <term>counter (or vm)</term>
/// <description><see cref="CounterGenerator"/></description>
/// </item>
/// <item>
/// <term>foreign</term>
/// <description><see cref="ForeignGenerator"/></description>
/// </item>
/// <item>
/// <term>guid</term>
/// <description><see cref="GuidGenerator"/></description>
/// </item>
/// <item>
/// <term>guid.comb</term>
/// <description><see cref="GuidCombGenerator"/></description>
/// </item>
/// <item>
/// <term>guid.native</term>
/// <description><see cref="NativeGuidGenerator"/></description>
/// </item>
/// <item>
/// <term>hilo</term>
/// <description><see cref="TableHiLoGenerator"/></description>
/// </item>
/// <item>
/// <term>enhanced-table</term>
/// <description><see cref="Enhanced.TableGenerator"/></description>
/// </item>
/// <item>
/// <term>identity</term>
/// <description><see cref="IdentityGenerator"/></description>
/// </item>
/// <item>
/// <term>native</term>
/// <description>
/// Chooses between <see cref="IdentityGenerator"/>, <see cref="SequenceGenerator"/>,
/// and <see cref="TableHiLoGenerator"/> based on the
/// <see cref="Dialect.Dialect"/>'s capabilities.
/// </description>
/// </item>
/// <item>
/// <term>seqhilo</term>
/// <description><see cref="SequenceHiLoGenerator"/></description>
/// </item>
/// <item>
/// <term>sequence</term>
/// <description><see cref="SequenceGenerator"/></description>
/// </item>
/// <item>
/// <term>enhanced-sequence</term>
/// <description><see cref="SequenceStyleGenerator"/></description>
/// </item>
/// <item>
/// <term>sequence-identity</term>
/// <description><see cref="SequenceIdentityGenerator"/></description>
/// </item>
/// <item>
/// <term>trigger-identity</term>
/// <description><see cref="TriggerIdentityGenerator"/></description>
/// </item>
/// <item>
/// <term>uuid.hex</term>
/// <description><see cref="UUIDHexGenerator"/></description>
/// </item>
/// <item>
/// <term>uuid.string</term>
/// <description><see cref="UUIDStringGenerator"/></description>
/// </item>
/// <item>
/// <term>select</term>
/// <description><see cref="SelectGenerator"/></description>
/// </item>
/// </list>
/// </remarks>
public static partial class IdentifierGeneratorFactory
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof (IdentifierGeneratorFactory));
/// <summary> Get the generated identifier when using identity columns</summary>
/// <param name="rs">The <see cref="DbDataReader"/> to read the identifier value from.</param>
/// <param name="type">The <see cref="IIdentifierType"/> the value should be converted to.</param>
/// <param name="session">The <see cref="ISessionImplementor"/> the value is retrieved in.</param>
/// <returns> The value for the identifier. </returns>
public static object GetGeneratedIdentity(DbDataReader rs, IType type, ISessionImplementor session)
{
if (!rs.Read())
{
throw new HibernateException("The database returned no natively generated identity value");
}
object id = Get(rs, type, session);
if (log.IsDebugEnabled())
{
log.Debug("Natively generated identity: {0}", id);
}
return id;
}
/// <summary>
/// Gets the value of the identifier from the <see cref="DbDataReader"/> and
/// ensures it is the correct <see cref="System.Type"/>.
/// </summary>
/// <param name="rs">The <see cref="DbDataReader"/> to read the identifier value from.</param>
/// <param name="type">The <see cref="IIdentifierType"/> the value should be converted to.</param>
/// <param name="session">The <see cref="ISessionImplementor"/> the value is retrieved in.</param>
/// <returns>
/// The value for the identifier.
/// </returns>
/// <exception cref="IdentifierGenerationException">
/// Thrown if there is any problem getting the value from the <see cref="DbDataReader"/>
/// or with converting it to the <see cref="System.Type"/>.
/// </exception>
public static object Get(DbDataReader rs, IType type, ISessionImplementor session)
{
// here is an interesting one:
// - MsSql's @@identity returns a Decimal
// - MySql LAST_IDENTITY() returns an Int64
try
{
return type.NullSafeGet(rs, rs.GetName(0), session, null);
}
catch (Exception e)
{
throw new IdentifierGenerationException("could not retrieve identifier value", e);
}
}
/// <summary>
/// An <see cref="Hashtable"/> where the <c>key</c> is the strategy and
/// the <c>value</c> is the <see cref="System.Type"/> for the strategy.
/// </summary>
private static readonly Dictionary<string, System.Type> idgenerators = new Dictionary<string, System.Type>();
/// <summary>
/// When this is returned by <c>Generate()</c> it indicates that the object
/// has already been saved.
/// </summary>
/// <value>
/// <see cref="string.Empty">String.Empty</see>
/// </value>
public static readonly object ShortCircuitIndicator = new object();
/// <summary>
/// When this is return
/// </summary>
public static readonly object PostInsertIndicator = new object();
/// <summary>
/// Initializes the static fields in <see cref="IdentifierGeneratorFactory"/>.
/// </summary>
static IdentifierGeneratorFactory()
{
idgenerators.Add("uuid.hex", typeof (UUIDHexGenerator));
idgenerators.Add("uuid.string", typeof (UUIDStringGenerator));
idgenerators.Add("hilo", typeof (TableHiLoGenerator));
idgenerators.Add("assigned", typeof (Assigned));
idgenerators.Add("counter", typeof (CounterGenerator));
idgenerators.Add("increment", typeof (IncrementGenerator));
idgenerators.Add("sequence", typeof (SequenceGenerator));
idgenerators.Add("seqhilo", typeof (SequenceHiLoGenerator));
idgenerators.Add("vm", typeof (CounterGenerator));
idgenerators.Add("foreign", typeof (ForeignGenerator));
idgenerators.Add("guid", typeof (GuidGenerator));
idgenerators.Add("guid.comb", typeof (GuidCombGenerator));
idgenerators.Add("guid.native", typeof (NativeGuidGenerator));
idgenerators.Add("select", typeof (SelectGenerator));
idgenerators.Add("sequence-identity", typeof (SequenceIdentityGenerator));
idgenerators.Add("trigger-identity", typeof (TriggerIdentityGenerator));
idgenerators.Add("enhanced-sequence", typeof(SequenceStyleGenerator));
idgenerators.Add("enhanced-table", typeof(Enhanced.TableGenerator));
}
/// <summary>
/// Creates an <see cref="IIdentifierGenerator"/> from the named strategy.
/// </summary>
/// <param name="strategy">
/// The name of the generator to create. This can be one of the NHibernate abbreviations (ie - <c>native</c>,
/// <c>sequence</c>, <c>guid.comb</c>, etc...), a full class name if the Type is in the NHibernate assembly, or
/// a full type name if the strategy is in an external assembly.
/// </param>
/// <param name="type">The <see cref="IType"/> that the retured identifier should be.</param>
/// <param name="parms">An <see cref="IDictionary"/> of <c><param></c> values from the mapping.</param>
/// <param name="dialect">The <see cref="Dialect.Dialect"/> to help with Configuration.</param>
/// <returns>
/// An instantiated and configured <see cref="IIdentifierGenerator"/>.
/// </returns>
/// <exception cref="MappingException">
/// Thrown if there are any exceptions while creating the <see cref="IIdentifierGenerator"/>.
/// </exception>
public static IIdentifierGenerator Create(string strategy, IType type, IDictionary<string, string> parms,
Dialect.Dialect dialect)
{
try
{
System.Type clazz = GetIdentifierGeneratorClass(strategy, dialect);
var idgen = (IIdentifierGenerator) Cfg.Environment.ObjectsFactory.CreateInstance(clazz);
var conf = idgen as IConfigurable;
if (conf != null)
{
conf.Configure(type, parms, dialect);
}
return idgen;
}
catch (IdentifierGenerationException)
{
throw;
}
catch (Exception e)
{
throw new MappingException("could not instantiate id generator: " + strategy, e);
}
}
/// <summary>
/// Create the correct boxed <see cref="System.Type"/> for the identifier.
/// </summary>
/// <param name="value">The value of the new identifier.</param>
/// <param name="type">The <see cref="IType"/> the identifier should be.</param>
/// <returns>
/// The identifier value converted to the <see cref="System.Type"/>.
/// </returns>
/// <exception cref="IdentifierGenerationException">
/// The <c>type</c> parameter must be an <see cref="short"/>, <see cref="int"/>,
/// or <see cref="long"/>.
/// </exception>
public static object CreateNumber(long value, System.Type type)
{
// Convert.ChangeType would be better here, but it fails if the value does not fit
// in the destination type, while we need the value to be truncated in this case.
if (type == typeof (byte))
{
return (byte) value;
}
else if (type == typeof (sbyte))
{
return (sbyte) value;
}
else if (type == typeof (short))
{
return (short) value;
}
else if (type == typeof (ushort))
{
return (ushort) value;
}
else if (type == typeof (int))
{
return (int) value;
}
else if (type == typeof (uint))
{
return (uint) value;
}
else if (type == typeof (long))
{
return value;
}
else if (type == typeof (ulong))
{
return (ulong) value;
}
else if (type == typeof (decimal))
{
return (decimal) value;
}
else
{
try
{
return Convert.ChangeType(value, type);
}
catch (Exception e)
{
throw new IdentifierGenerationException("could not convert generated value to type " + type, e);
}
}
}
public static System.Type GetIdentifierGeneratorClass(string strategy, Dialect.Dialect dialect)
{
System.Type clazz;
if ("native".Equals(strategy))
{
clazz = dialect.NativeIdentifierGeneratorClass;
}
else if ("identity".Equals(strategy))
{
clazz = dialect.IdentityStyleIdentifierGeneratorClass;
}
else
{
idgenerators.TryGetValue(strategy, out clazz);
}
try
{
if (clazz == null)
{
clazz = ReflectHelper.ClassForName(strategy);
}
}
catch (Exception ex)
{
throw new IdentifierGenerationException("Could not interpret id generator strategy: " + strategy, ex);
}
return clazz;
}
}
}