forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIncrementGenerator.cs
145 lines (132 loc) · 3.75 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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Runtime.CompilerServices;
using System.Text;
using NHibernate.Engine;
using NHibernate.Exceptions;
using NHibernate.SqlCommand;
using NHibernate.SqlTypes;
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: tables, column, schema, catalog.
/// </para>
/// </remarks>
public partial class IncrementGenerator : IIdentifierGenerator, IConfigurable
{
private static readonly INHibernateLogger Logger = NHibernateLogger.For(typeof(IncrementGenerator));
private long _next;
private SqlString _sql;
private System.Type _returnClass;
private readonly AsyncLock _asyncLock = new AsyncLock();
/// <summary>
///
/// </summary>
/// <param name="type"></param>
/// <param name="parms"></param>
/// <param name="dialect"></param>
public void Configure(IType type, IDictionary<string, string> parms, Dialect.Dialect dialect)
{
string tableList;
string column;
string schema;
string catalog;
if (!parms.TryGetValue("tables", out tableList))
parms.TryGetValue(PersistentIdGeneratorParmsNames.Tables, out tableList);
string[] tables = tableList.Split(", ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
if (!parms.TryGetValue("column", out column))
parms.TryGetValue(PersistentIdGeneratorParmsNames.PK, out column);
_returnClass = type.ReturnedClass;
parms.TryGetValue(PersistentIdGeneratorParmsNames.Schema, out schema);
parms.TryGetValue(PersistentIdGeneratorParmsNames.Catalog, out catalog);
StringBuilder buf = new StringBuilder();
for (int i = 0; i < tables.Length; i++)
{
if (tables.Length > 1)
{
buf.Append("select ").Append(column).Append(" from ");
}
buf.Append(dialect.Qualify(catalog, schema, tables[i]));
if (i < tables.Length - 1)
buf.Append(" union ");
}
if (tables.Length > 1)
{
buf.Insert(0, "( ").Append(" ) ids_");
column = "ids_." + column;
}
var sqlTxt = string.Format("select max({0}) from {1}", column, buf);
_sql = new SqlString(sqlTxt);
}
/// <summary>
///
/// </summary>
/// <param name="session"></param>
/// <param name="obj"></param>
/// <returns></returns>
public object Generate(ISessionImplementor session, object obj)
{
using (_asyncLock.Lock())
{
if (_sql != null)
{
GetNext(session);
}
return IdentifierGeneratorFactory.CreateNumber(_next++, _returnClass);
}
}
private void GetNext(ISessionImplementor session)
{
Logger.Debug("fetching initial value: {0}", _sql);
try
{
var cmd = session.Batcher.PrepareCommand(CommandType.Text, _sql, SqlTypeFactory.NoTypes);
DbDataReader reader = null;
try
{
reader = session.Batcher.ExecuteReader(cmd);
try
{
if (reader.Read())
{
_next = !reader.IsDBNull(0) ? Convert.ToInt64(reader.GetValue(0)) + 1 : 1L;
}
else
{
_next = 1L;
}
_sql = null;
Logger.Debug("first free id: {0}", _next);
}
finally
{
reader.Close();
}
}
finally
{
session.Batcher.CloseCommand(cmd, reader);
}
}
catch (DbException sqle)
{
Logger.Error(sqle, "could not get increment value");
throw ADOExceptionHelper.Convert(session.Factory.SQLExceptionConverter, sqle,
"could not fetch initial value for increment generator");
}
}
}
}