forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNativeGuidGenerator.cs
59 lines (54 loc) · 1.42 KB
/
NativeGuidGenerator.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
using System;
using System.Data;
using System.Data.Common;
using NHibernate.Engine;
using NHibernate.Exceptions;
using NHibernate.SqlCommand;
using NHibernate.SqlTypes;
using NHibernate.Type;
namespace NHibernate.Id
{
/// <summary>
/// Generates Guid values using the server side Guid function.
/// </summary>
public partial class NativeGuidGenerator : IIdentifierGenerator
{
private static readonly INHibernateLogger log = NHibernateLogger.For(typeof(NativeGuidGenerator));
private readonly IType identifierType = new GuidType();
#region Implementation of IIdentifierGenerator
public object Generate(ISessionImplementor session, object obj)
{
var sql = new SqlString(session.Factory.Dialect.SelectGUIDString);
try
{
var st = session.Batcher.PrepareCommand(CommandType.Text, sql, SqlTypeFactory.NoTypes);
DbDataReader reader = null;
try
{
reader = session.Batcher.ExecuteReader(st);
object result;
try
{
reader.Read();
result = IdentifierGeneratorFactory.Get(reader, identifierType, session);
}
finally
{
reader.Close();
}
log.Debug("GUID identifier generated: {0}", result);
return result;
}
finally
{
session.Batcher.CloseCommand(st, reader);
}
}
catch (Exception sqle)
{
throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle, "could not retrieve GUID", sql);
}
}
#endregion
}
}