-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathIncrementGenerator.cs
147 lines (133 loc) · 3.27 KB
/
IncrementGenerator.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
using System;
using System.Collections;
using System.Data;
using System.Runtime.CompilerServices;
using log4net;
using NHibernate.Engine;
using NHibernate.Mapping;
using NHibernate.Type;
using NHibernate.Util;
namespace NHibernate.Id
{
/// <summary>
/// An <c>IIdentifierGenerator</c> that returns a <c>Int64</c>, constructed by
/// counting from the maximum primary key value at startup. Not safe for use in a
/// cluster!
/// </summary>
/// <remarks>
/// <para>
/// java author Gavin King, .NET port Mark Holden
/// </para>
/// <para>
/// Mapping parameters supported, but not usually needed: table, column.
/// </para>
/// </remarks>
public class IncrementGenerator : IPersistentIdentifierGenerator, IConfigurable
{
private static readonly ILog log = LogManager.GetLogger(typeof(IncrementGenerator));
/// <summary></summary>
public const string Column = "target_column";
/// <summary></summary>
public const string Table = "target_table";
/// <summary></summary>
public const string Schema = "schema";
private string tableName;
private long next;
private string sql;
private System.Type returnClass;
/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <param name="parms"></param>
/// <param name="d"></param>
public void Configure(IType type, IDictionary parms, Dialect.Dialect d)
{
tableName = parms[Table] as string;
string columnName = ((Column) parms[Column]).GetQuotedName(d);
string schemaName = parms[Schema] as string;
if (schemaName != null && tableName.IndexOf(StringHelper.Dot) < 0)
{
tableName = schemaName + "." + tableName;
}
returnClass = type.ReturnedClass;
sql = "select max(" + columnName + ") from " + tableName;
}
/// <summary>
///
/// </summary>
/// <param name="session"></param>
/// <param name="obj"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public object Generate(ISessionImplementor session, object obj)
{
if (sql != null)
{
getNext(session);
}
return IdentifierGeneratorFactory.CreateNumber(next++, returnClass);
}
private void getNext(ISessionImplementor session)
{
log.Debug("fetching initial value: " + sql);
IDbConnection conn = session.Factory.OpenConnection();
IDbCommand qps = conn.CreateCommand();
qps.CommandText = sql;
qps.CommandType = CommandType.Text;
try
{
IDataReader rs = qps.ExecuteReader();
if (rs.Read())
{
if (!rs.IsDBNull(0))
{
next = Convert.ToInt64(rs.GetValue(0)) + 1;
}
else
{
next = 1L;
}
}
else
{
next = 1L;
}
sql = null;
log.Debug("first free id: " + next);
}
catch (Exception e)
{
log.Error("could not get increment value", e);
throw;
}
finally
{
session.Factory.CloseConnection(conn);
}
}
/// <summary>
///
/// </summary>
/// <param name="dialect"></param>
/// <returns></returns>
public string[] SqlCreateStrings(Dialect.Dialect dialect)
{
return new string[0];
}
/// <summary>
///
/// </summary>
/// <param name="dialect"></param>
/// <returns></returns>
public string SqlDropString(Dialect.Dialect dialect)
{
return null;
}
/// <summary></summary>
public object GeneratorKey()
{
return tableName;
}
}
}