-
Notifications
You must be signed in to change notification settings - Fork 936
/
Copy pathSqlClientDriver.cs
339 lines (308 loc) · 11.8 KB
/
SqlClientDriver.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
336
337
338
339
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
#if NETFX
using System.Data.SqlClient;
#endif
using NHibernate.AdoNet;
using NHibernate.Dialect;
using NHibernate.Engine;
using NHibernate.SqlTypes;
using NHibernate.Type;
namespace NHibernate.Driver
{
/// <summary>
/// A NHibernate Driver for using the SqlClient DataProvider
/// </summary>
public class SqlClientDriver
#if NETFX
: DriverBase,
#else
: ReflectionBasedDriver,
#endif
IEmbeddedBatcherFactoryProvider, IParameterAdjuster
{
// Since v5.1
[Obsolete("Use MsSql2000Dialect.MaxSizeForAnsiClob")]
public const int MaxSizeForAnsiClob = 2147483647; // int.MaxValue
// Since v5.1
[Obsolete("Use MsSql2000Dialect.MaxSizeForClob")]
public const int MaxSizeForClob = 1073741823; // int.MaxValue / 2
// Since v5.1
[Obsolete("Use MsSql2000Dialect.MaxSizeForBlob")]
public const int MaxSizeForBlob = 2147483647; // int.MaxValue
///<remarks>http://stackoverflow.com/a/7264795/259946</remarks>
// Since v5.1
[Obsolete("Use MsSql2005Dialect.MaxSizeForXml")]
public const int MaxSizeForXml = 2147483647; // int.MaxValue
// Since v5.1
[Obsolete("Use MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString")]
public const int MaxSizeForLengthLimitedAnsiString = 8000;
// Since v5.1
[Obsolete("Use MsSql2000Dialect.MaxSizeForLengthLimitedString")]
public const int MaxSizeForLengthLimitedString = 4000;
// Since v5.1
[Obsolete("Use MsSql2000Dialect.MaxSizeForLengthLimitedBinary")]
public const int MaxSizeForLengthLimitedBinary = 8000;
// Since v5.1
[Obsolete("This member has no more usages and will be removed in a future version")]
public const byte MaxPrecision = 28;
// Since v5.1
[Obsolete("This member has no more usages and will be removed in a future version")]
public const byte MaxScale = 5;
// Since v5.1
[Obsolete("Use MsSql2000Dialect.MaxDateTime2")]
public const byte MaxDateTime2 = 8;
// Since v5.1
[Obsolete("Use MsSql2000Dialect.MaxDateTimeOffset")]
public const byte MaxDateTimeOffset = 10;
private Dialect.Dialect _dialect;
public override void Configure(IDictionary<string, string> settings)
{
base.Configure(settings);
_dialect = Dialect.Dialect.GetDialect(settings);
}
#if !NETFX
public SqlClientDriver()
: base("System.Data.SqlClient", "System.Data.SqlClient.SqlConnection", "System.Data.SqlClient.SqlCommand")
{
}
System.Type IEmbeddedBatcherFactoryProvider.BatcherFactoryClass => typeof(GenericBatchingBatcherFactory);
#else
/// <summary>
/// Creates an uninitialized <see cref="DbConnection" /> object for
/// the SqlClientDriver.
/// </summary>
/// <value>An unitialized <see cref="System.Data.SqlClient.SqlConnection"/> object.</value>
public override DbConnection CreateConnection()
{
return new SqlConnection();
}
/// <summary>
/// Creates an uninitialized <see cref="DbCommand" /> object for
/// the SqlClientDriver.
/// </summary>
/// <value>An unitialized <see cref="System.Data.SqlClient.SqlCommand"/> object.</value>
public override DbCommand CreateCommand()
{
return new System.Data.SqlClient.SqlCommand();
}
System.Type IEmbeddedBatcherFactoryProvider.BatcherFactoryClass
{
get { return typeof(SqlClientBatchingBatcherFactory); }
}
#endif
/// <summary>
/// MsSql requires the use of a Named Prefix in the SQL statement.
/// </summary>
/// <remarks>
/// <see langword="true" /> because MsSql uses "<c>@</c>".
/// </remarks>
public override bool UseNamedPrefixInSql
{
get { return true; }
}
/// <summary>
/// MsSql requires the use of a Named Prefix in the Parameter.
/// </summary>
/// <remarks>
/// <see langword="true" /> because MsSql uses "<c>@</c>".
/// </remarks>
public override bool UseNamedPrefixInParameter
{
get { return true; }
}
/// <summary>
/// The Named Prefix for parameters.
/// </summary>
/// <value>
/// Sql Server uses <c>"@"</c>.
/// </value>
public override string NamedPrefix
{
get { return "@"; }
}
/// <summary>
/// The SqlClient driver does NOT support more than 1 open DbDataReader
/// with only 1 DbConnection.
/// </summary>
/// <value><see langword="false" /> - it is not supported.</value>
/// <remarks>
/// MS SQL Server 2000 (and 7) throws an exception when multiple DbDataReaders are
/// attempted to be opened. When SQL Server 2005 comes out a new driver will be
/// created for it because SQL Server 2005 is supposed to support it.
/// </remarks>
public override bool SupportsMultipleOpenReaders
{
get { return false; }
}
protected override void InitializeParameter(DbParameter dbParam, string name, SqlType sqlType)
{
base.InitializeParameter(dbParam, name, sqlType);
// Defaults size/precision/scale
switch (dbParam.DbType)
{
case DbType.AnsiString:
case DbType.AnsiStringFixedLength:
dbParam.Size = IsAnsiText(dbParam, sqlType)
? MsSql2000Dialect.MaxSizeForAnsiClob
: IsChar(dbParam, sqlType) ? sqlType.Length : MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString;
break;
case DbType.Binary:
dbParam.Size = IsBlob(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForBlob : MsSql2000Dialect.MaxSizeForLengthLimitedBinary;
break;
case DbType.Decimal:
if (_dialect == null)
throw new InvalidOperationException("Dialect not available, is this driver used without having been configured?");
dbParam.Precision = _dialect.DefaultCastPrecision;
dbParam.Scale = _dialect.DefaultCastScale;
break;
case DbType.String:
case DbType.StringFixedLength:
dbParam.Size = IsText(dbParam, sqlType)
? MsSql2000Dialect.MaxSizeForClob
: IsChar(dbParam, sqlType) ? sqlType.Length : MsSql2000Dialect.MaxSizeForLengthLimitedString;
break;
case DbType.DateTime2:
dbParam.Size = MsSql2000Dialect.MaxDateTime2;
break;
case DbType.DateTimeOffset:
dbParam.Size = MsSql2000Dialect.MaxDateTimeOffset;
break;
case DbType.Xml:
dbParam.Size = MsSql2005Dialect.MaxSizeForXml;
break;
}
// Do not override the default length for string using data from SqlType, since LIKE expressions needs
// larger columns. https://nhibernate.jira.com/browse/NH-3036
if (sqlType.PrecisionDefined)
{
dbParam.Precision = sqlType.Precision;
dbParam.Scale = sqlType.Scale;
}
}
// Since v5.1
[Obsolete("This method has no more usages and will be removed in a future version")]
public static void SetVariableLengthParameterSize(DbParameter dbParam, SqlType sqlType)
{
SetDefaultParameterSize(dbParam, sqlType);
// no longer override the defaults using data from SqlType, since LIKE expressions needs larger columns
// https://nhibernate.jira.com/browse/NH-3036
//if (sqlType.LengthDefined && !IsText(dbParam, sqlType) && !IsBlob(dbParam, sqlType))
//{
// dbParam.Size = sqlType.Length;
//}
if (sqlType.PrecisionDefined)
{
dbParam.Precision = sqlType.Precision;
dbParam.Scale = sqlType.Scale;
}
}
// Since v5.1
[Obsolete("This method has no more usages and will be removed in a future version")]
protected static void SetDefaultParameterSize(DbParameter dbParam, SqlType sqlType)
{
switch (dbParam.DbType)
{
case DbType.AnsiString:
case DbType.AnsiStringFixedLength:
dbParam.Size = IsAnsiText(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForAnsiClob : MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString;
break;
case DbType.Binary:
dbParam.Size = IsBlob(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForBlob : MsSql2000Dialect.MaxSizeForLengthLimitedBinary;
break;
case DbType.Decimal:
dbParam.Precision = MaxPrecision;
dbParam.Scale = MaxScale;
break;
case DbType.String:
case DbType.StringFixedLength:
dbParam.Size = IsText(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForClob : MsSql2000Dialect.MaxSizeForLengthLimitedString;
break;
case DbType.DateTime2:
dbParam.Size = MsSql2000Dialect.MaxDateTime2;
break;
case DbType.DateTimeOffset:
dbParam.Size = MsSql2000Dialect.MaxDateTimeOffset;
break;
case DbType.Xml:
dbParam.Size = MsSql2005Dialect.MaxSizeForXml;
break;
}
}
/// <summary>
/// Interprets if a parameter is a Clob (for the purposes of setting its default size)
/// </summary>
/// <param name="dbParam">The parameter</param>
/// <param name="sqlType">The <see cref="SqlType" /> of the parameter</param>
/// <returns>True, if the parameter should be interpreted as a Clob, otherwise False</returns>
protected static bool IsAnsiText(DbParameter dbParam, SqlType sqlType)
{
return ((DbType.AnsiString == dbParam.DbType || DbType.AnsiStringFixedLength == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString));
}
/// <summary>
/// Interprets if a parameter is a Clob (for the purposes of setting its default size)
/// </summary>
/// <param name="dbParam">The parameter</param>
/// <param name="sqlType">The <see cref="SqlType" /> of the parameter</param>
/// <returns>True, if the parameter should be interpreted as a Clob, otherwise False</returns>
protected static bool IsText(DbParameter dbParam, SqlType sqlType)
{
return (sqlType is StringClobSqlType) || ((DbType.String == dbParam.DbType || DbType.StringFixedLength == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedString));
}
/// <summary>
/// Interprets if a parameter is a Blob (for the purposes of setting its default size)
/// </summary>
/// <param name="dbParam">The parameter</param>
/// <param name="sqlType">The <see cref="SqlType" /> of the parameter</param>
/// <returns>True, if the parameter should be interpreted as a Blob, otherwise False</returns>
protected static bool IsBlob(DbParameter dbParam, SqlType sqlType)
{
return (sqlType is BinaryBlobSqlType) || ((DbType.Binary == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedBinary));
}
/// <summary>
/// Interprets if a parameter is a character (for the purposes of setting its default size)
/// </summary>
/// <param name="dbParam">The parameter</param>
/// <param name="sqlType">The <see cref="SqlType" /> of the parameter</param>
/// <returns>True, if the parameter should be interpreted as a character, otherwise False</returns>
protected static bool IsChar(DbParameter dbParam, SqlType sqlType)
{
return (DbType.StringFixedLength == dbParam.DbType || DbType.AnsiStringFixedLength == dbParam.DbType) &&
sqlType.LengthDefined && sqlType.Length == 1;
}
public override IResultSetsCommand GetResultSetsCommand(ISessionImplementor session)
{
return new BasicResultSetsCommand(session);
}
public override bool SupportsMultipleQueries
{
get { return true; }
}
/// <summary>
/// With read committed snapshot or lower, SQL Server may have not actually already committed the transaction
/// right after the scope disposal.
/// </summary>
public override bool HasDelayedDistributedTransactionCompletion => true;
/// <inheritdoc />
public override DateTime MinDate => new DateTime(1753, 1, 1);
public virtual void AdjustParameterForValue(DbParameter parameter, SqlType sqlType, object value)
{
if (value is string stringVal)
{
switch (parameter.DbType)
{
case DbType.AnsiString:
case DbType.AnsiStringFixedLength:
parameter.Size = IsAnsiText(parameter, sqlType) ? MsSql2000Dialect.MaxSizeForAnsiClob : Math.Max(stringVal.Length, sqlType.LengthDefined ? sqlType.Length : parameter.Size);
break;
case DbType.String:
case DbType.StringFixedLength:
parameter.Size = IsText(parameter, sqlType) ? MsSql2000Dialect.MaxSizeForClob : Math.Max(stringVal.Length, sqlType.LengthDefined ? sqlType.Length : parameter.Size);
break;
}
}
}
}
}