1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Data ;
4
+ using System . Data . Common ;
4
5
using System . Runtime . CompilerServices ;
5
6
using System . Text ;
6
-
7
7
using NHibernate . Engine ;
8
8
using NHibernate . Exceptions ;
9
+ using NHibernate . SqlCommand ;
10
+ using NHibernate . SqlTypes ;
9
11
using NHibernate . Type ;
10
- using NHibernate . Util ;
11
- using System . Data . Common ;
12
12
13
13
namespace NHibernate . Id
14
14
{
@@ -27,11 +27,11 @@ namespace NHibernate.Id
27
27
/// </remarks>
28
28
public class IncrementGenerator : IIdentifierGenerator , IConfigurable
29
29
{
30
- private static readonly IInternalLogger log = LoggerProvider . LoggerFor ( typeof ( IncrementGenerator ) ) ;
30
+ private static readonly IInternalLogger Logger = LoggerProvider . LoggerFor ( typeof ( IncrementGenerator ) ) ;
31
31
32
- private long next ;
33
- private string sql ;
34
- private System . Type returnClass ;
32
+ private long _next ;
33
+ private SqlString _sql ;
34
+ private System . Type _returnClass ;
35
35
36
36
/// <summary>
37
37
///
@@ -49,9 +49,12 @@ public void Configure(IType type, IDictionary<string, string> parms, Dialect.Dia
49
49
if ( ! parms . TryGetValue ( "tables" , out tableList ) )
50
50
parms . TryGetValue ( PersistentIdGeneratorParmsNames . Tables , out tableList ) ;
51
51
string [ ] tables = tableList . Split ( ", " . ToCharArray ( ) , StringSplitOptions . RemoveEmptyEntries ) ;
52
+
52
53
if ( ! parms . TryGetValue ( "column" , out column ) )
53
54
parms . TryGetValue ( PersistentIdGeneratorParmsNames . PK , out column ) ;
54
- returnClass = type . ReturnedClass ;
55
+
56
+ _returnClass = type . ReturnedClass ;
57
+
55
58
parms . TryGetValue ( PersistentIdGeneratorParmsNames . Schema , out schema ) ;
56
59
parms . TryGetValue ( PersistentIdGeneratorParmsNames . Catalog , out catalog ) ;
57
60
@@ -72,7 +75,8 @@ public void Configure(IType type, IDictionary<string, string> parms, Dialect.Dia
72
75
column = "ids_." + column ;
73
76
}
74
77
75
- sql = "select max(" + column + ") from " + buf ;
78
+ var sqlTxt = string . Format ( "select max({0}) from {1}" , column , buf ) ;
79
+ _sql = new SqlString ( sqlTxt ) ;
76
80
}
77
81
78
82
/// <summary>
@@ -84,52 +88,50 @@ public void Configure(IType type, IDictionary<string, string> parms, Dialect.Dia
84
88
[ MethodImpl ( MethodImplOptions . Synchronized ) ]
85
89
public object Generate ( ISessionImplementor session , object obj )
86
90
{
87
- if ( sql != null )
91
+ if ( _sql != null )
88
92
{
89
93
GetNext ( session ) ;
90
94
}
91
- return IdentifierGeneratorFactory . CreateNumber ( next ++ , returnClass ) ;
95
+ return IdentifierGeneratorFactory . CreateNumber ( _next ++ , _returnClass ) ;
92
96
}
93
97
94
98
private void GetNext ( ISessionImplementor session )
95
99
{
96
- log . Debug ( "fetching initial value: " + sql ) ;
100
+ Logger . Debug ( "fetching initial value: " + _sql ) ;
97
101
98
102
try
99
103
{
100
- IDbConnection conn = session . Factory . ConnectionProvider . GetConnection ( ) ;
101
- IDbCommand qps = conn . CreateCommand ( ) ;
102
- qps . CommandText = sql ;
103
- qps . CommandType = CommandType . Text ;
104
+ var cmd = session . Batcher . PrepareCommand ( CommandType . Text , _sql , SqlTypeFactory . NoTypes ) ;
105
+ IDataReader reader = null ;
104
106
try
105
107
{
106
- IDataReader rs = qps . ExecuteReader ( ) ;
108
+ reader = session . Batcher . ExecuteReader ( cmd ) ;
107
109
try
108
110
{
109
- if ( rs . Read ( ) )
111
+ if ( reader . Read ( ) )
110
112
{
111
- next = ! rs . IsDBNull ( 0 ) ? Convert . ToInt64 ( rs . GetValue ( 0 ) ) + 1 : 1L ;
113
+ _next = ! reader . IsDBNull ( 0 ) ? Convert . ToInt64 ( reader . GetValue ( 0 ) ) + 1 : 1L ;
112
114
}
113
115
else
114
116
{
115
- next = 1L ;
117
+ _next = 1L ;
116
118
}
117
- sql = null ;
118
- log . Debug ( "first free id: " + next ) ;
119
+ _sql = null ;
120
+ Logger . Debug ( "first free id: " + _next ) ;
119
121
}
120
122
finally
121
123
{
122
- rs . Close ( ) ;
124
+ reader . Close ( ) ;
123
125
}
124
126
}
125
127
finally
126
128
{
127
- session . Factory . ConnectionProvider . CloseConnection ( conn ) ;
129
+ session . Batcher . CloseCommand ( cmd , reader ) ;
128
130
}
129
131
}
130
132
catch ( DbException sqle )
131
133
{
132
- log . Error ( "could not get increment value" , sqle ) ;
134
+ Logger . Error ( "could not get increment value" , sqle ) ;
133
135
throw ADOExceptionHelper . Convert ( session . Factory . SQLExceptionConverter , sqle ,
134
136
"could not fetch initial value for increment generator" ) ;
135
137
}
0 commit comments