Skip to content

Commit fd3e997

Browse files
committed
Move MsSql constants from driver to dialect.
1 parent 2de124a commit fd3e997

File tree

4 files changed

+67
-35
lines changed

4 files changed

+67
-35
lines changed

src/NHibernate.Test/DialectTest/MsSql2005DialectFixture.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ public void NH2809()
100100
var t = d.GetTypeName(new BinarySqlType());
101101
Assert.That(t, Is.EqualTo("VARBINARY(MAX)"));
102102

103-
t = d.GetTypeName(new BinarySqlType(), SqlClientDriver.MaxSizeForLengthLimitedBinary - 1, 0, 0);
104-
Assert.That(t, Is.EqualTo(String.Format("VARBINARY({0})", SqlClientDriver.MaxSizeForLengthLimitedBinary - 1)));
103+
t = d.GetTypeName(new BinarySqlType(), MsSql2000Dialect.MaxSizeForLengthLimitedBinary - 1, 0, 0);
104+
Assert.That(t, Is.EqualTo(String.Format("VARBINARY({0})", MsSql2000Dialect.MaxSizeForLengthLimitedBinary - 1)));
105105

106-
t = d.GetTypeName(new BinarySqlType(), SqlClientDriver.MaxSizeForLengthLimitedBinary, 0, 0);
107-
Assert.That(t, Is.EqualTo(String.Format("VARBINARY({0})", SqlClientDriver.MaxSizeForLengthLimitedBinary)));
106+
t = d.GetTypeName(new BinarySqlType(), MsSql2000Dialect.MaxSizeForLengthLimitedBinary, 0, 0);
107+
Assert.That(t, Is.EqualTo(String.Format("VARBINARY({0})", MsSql2000Dialect.MaxSizeForLengthLimitedBinary)));
108108

109-
t = d.GetTypeName(new BinarySqlType(), SqlClientDriver.MaxSizeForLengthLimitedBinary + 1, 0, 0);
109+
t = d.GetTypeName(new BinarySqlType(), MsSql2000Dialect.MaxSizeForLengthLimitedBinary + 1, 0, 0);
110110
Assert.That(t, Is.EqualTo("VARBINARY(MAX)"));
111111
}
112112

src/NHibernate/Dialect/MsSql2000Dialect.cs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using System.Text.RegularExpressions;
66
using NHibernate.Dialect.Function;
77
using NHibernate.Dialect.Schema;
8-
using NHibernate.Driver;
98
using NHibernate.Engine;
109
using NHibernate.Mapping;
1110
using NHibernate.SqlCommand;
@@ -42,6 +41,16 @@ namespace NHibernate.Dialect
4241
/// </remarks>
4342
public class MsSql2000Dialect : Dialect
4443
{
44+
public const int MaxSizeForAnsiClob = 2147483647; // int.MaxValue
45+
public const int MaxSizeForClob = 1073741823; // int.MaxValue / 2
46+
public const int MaxSizeForBlob = 2147483647; // int.MaxValue
47+
48+
public const int MaxSizeForLengthLimitedAnsiString = 8000;
49+
public const int MaxSizeForLengthLimitedString = 4000;
50+
public const int MaxSizeForLengthLimitedBinary = 8000;
51+
public const byte MaxDateTime2 = 8;
52+
public const byte MaxDateTimeOffset = 10;
53+
4554
public MsSql2000Dialect()
4655
{
4756
RegisterCharacterTypeMappings();
@@ -358,8 +367,8 @@ protected virtual void RegisterGuidTypeMapping()
358367
protected virtual void RegisterLargeObjectTypeMappings()
359368
{
360369
RegisterColumnType(DbType.Binary, "VARBINARY(8000)");
361-
RegisterColumnType(DbType.Binary, SqlClientDriver.MaxSizeForLengthLimitedBinary, "VARBINARY($l)");
362-
RegisterColumnType(DbType.Binary, SqlClientDriver.MaxSizeForBlob, "IMAGE");
370+
RegisterColumnType(DbType.Binary, MaxSizeForLengthLimitedBinary, "VARBINARY($l)");
371+
RegisterColumnType(DbType.Binary, MaxSizeForBlob, "IMAGE");
363372
}
364373

365374
protected virtual void RegisterDateTimeTypeMappings()
@@ -389,13 +398,13 @@ protected virtual void RegisterCharacterTypeMappings()
389398
RegisterColumnType(DbType.AnsiStringFixedLength, "CHAR(255)");
390399
RegisterColumnType(DbType.AnsiStringFixedLength, 8000, "CHAR($l)");
391400
RegisterColumnType(DbType.AnsiString, "VARCHAR(255)");
392-
RegisterColumnType(DbType.AnsiString, SqlClientDriver.MaxSizeForLengthLimitedAnsiString, "VARCHAR($l)");
393-
RegisterColumnType(DbType.AnsiString, SqlClientDriver.MaxSizeForAnsiClob, "TEXT");
401+
RegisterColumnType(DbType.AnsiString, MaxSizeForLengthLimitedAnsiString, "VARCHAR($l)");
402+
RegisterColumnType(DbType.AnsiString, MaxSizeForAnsiClob, "TEXT");
394403
RegisterColumnType(DbType.StringFixedLength, "NCHAR(255)");
395-
RegisterColumnType(DbType.StringFixedLength, SqlClientDriver.MaxSizeForLengthLimitedString, "NCHAR($l)");
404+
RegisterColumnType(DbType.StringFixedLength, MaxSizeForLengthLimitedString, "NCHAR($l)");
396405
RegisterColumnType(DbType.String, "NVARCHAR(255)");
397-
RegisterColumnType(DbType.String, SqlClientDriver.MaxSizeForLengthLimitedString, "NVARCHAR($l)");
398-
RegisterColumnType(DbType.String, SqlClientDriver.MaxSizeForClob, "NTEXT");
406+
RegisterColumnType(DbType.String, MaxSizeForLengthLimitedString, "NVARCHAR($l)");
407+
RegisterColumnType(DbType.String, MaxSizeForClob, "NTEXT");
399408
}
400409

401410
public override string AddColumnString
@@ -447,7 +456,7 @@ public override string GetDropTableString(string tableName)
447456
"if exists (select * from dbo.sysobjects where id = object_id(N'{0}') and OBJECTPROPERTY(id, N'IsUserTable') = 1)" +
448457
" drop table {0}";
449458

450-
return String.Format(dropTable, tableName);
459+
return string.Format(dropTable, tableName);
451460
}
452461

453462
public override string ForUpdateString

src/NHibernate/Dialect/MsSql2005Dialect.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Data;
2-
using NHibernate.Driver;
32
using NHibernate.Mapping;
43
using NHibernate.SqlCommand;
54
using NHibernate.Util;
@@ -8,6 +7,9 @@ namespace NHibernate.Dialect
87
{
98
public class MsSql2005Dialect : MsSql2000Dialect
109
{
10+
///<remarks>http://stackoverflow.com/a/7264795/259946</remarks>
11+
public const int MaxSizeForXml = 2147483647; // int.MaxValue
12+
1113
public MsSql2005Dialect()
1214
{
1315
RegisterColumnType(DbType.Xml, "XML");
@@ -16,16 +18,16 @@ public MsSql2005Dialect()
1618
protected override void RegisterCharacterTypeMappings()
1719
{
1820
base.RegisterCharacterTypeMappings();
19-
RegisterColumnType(DbType.String, SqlClientDriver.MaxSizeForClob, "NVARCHAR(MAX)");
20-
RegisterColumnType(DbType.AnsiString, SqlClientDriver.MaxSizeForAnsiClob, "VARCHAR(MAX)");
21+
RegisterColumnType(DbType.String, MaxSizeForClob, "NVARCHAR(MAX)");
22+
RegisterColumnType(DbType.AnsiString, MaxSizeForAnsiClob, "VARCHAR(MAX)");
2123
}
2224

2325
protected override void RegisterLargeObjectTypeMappings()
2426
{
2527
base.RegisterLargeObjectTypeMappings();
2628
RegisterColumnType(DbType.Binary, "VARBINARY(MAX)");
27-
RegisterColumnType(DbType.Binary, SqlClientDriver.MaxSizeForLengthLimitedBinary, "VARBINARY($l)");
28-
RegisterColumnType(DbType.Binary, SqlClientDriver.MaxSizeForBlob, "VARBINARY(MAX)");
29+
RegisterColumnType(DbType.Binary, MaxSizeForLengthLimitedBinary, "VARBINARY($l)");
30+
RegisterColumnType(DbType.Binary, MaxSizeForBlob, "VARBINARY(MAX)");
2931
}
3032

3133
protected override void RegisterKeywords()

src/NHibernate/Driver/SqlClientDriver.cs

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Data.SqlClient;
77
#endif
88
using NHibernate.AdoNet;
9+
using NHibernate.Dialect;
910
using NHibernate.Engine;
1011
using NHibernate.SqlTypes;
1112

@@ -21,21 +22,41 @@ public class SqlClientDriver
2122
: ReflectionBasedDriver, IEmbeddedBatcherFactoryProvider
2223
#endif
2324
{
25+
// Since v5.1
26+
[Obsolete("Use MsSql2000Dialect.MaxSizeForAnsiClob")]
2427
public const int MaxSizeForAnsiClob = 2147483647; // int.MaxValue
28+
// Since v5.1
29+
[Obsolete("Use MsSql2000Dialect.MaxSizeForClob")]
2530
public const int MaxSizeForClob = 1073741823; // int.MaxValue / 2
31+
// Since v5.1
32+
[Obsolete("Use MsSql2000Dialect.MaxSizeForBlob")]
2633
public const int MaxSizeForBlob = 2147483647; // int.MaxValue
27-
//http://stackoverflow.com/a/7264795/259946
34+
35+
///<remarks>http://stackoverflow.com/a/7264795/259946</remarks>
36+
// Since v5.1
37+
[Obsolete("Use MsSql2005Dialect.MaxSizeForXml")]
2838
public const int MaxSizeForXml = 2147483647; // int.MaxValue
39+
40+
// Since v5.1
41+
[Obsolete("Use MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString")]
2942
public const int MaxSizeForLengthLimitedAnsiString = 8000;
43+
// Since v5.1
44+
[Obsolete("Use MsSql2000Dialect.MaxSizeForLengthLimitedString")]
3045
public const int MaxSizeForLengthLimitedString = 4000;
46+
// Since v5.1
47+
[Obsolete("Use MsSql2000Dialect.MaxSizeForLengthLimitedBinary")]
3148
public const int MaxSizeForLengthLimitedBinary = 8000;
3249
// Since v5.1
3350
[Obsolete("This member has no more usages and will be removed in a future version")]
3451
public const byte MaxPrecision = 28;
3552
// Since v5.1
3653
[Obsolete("This member has no more usages and will be removed in a future version")]
3754
public const byte MaxScale = 5;
55+
// Since v5.1
56+
[Obsolete("Use MsSql2000Dialect.MaxDateTime2")]
3857
public const byte MaxDateTime2 = 8;
58+
// Since v5.1
59+
[Obsolete("Use MsSql2000Dialect.MaxDateTimeOffset")]
3960
public const byte MaxDateTimeOffset = 10;
4061

4162
private Dialect.Dialect _dialect;
@@ -139,10 +160,10 @@ protected override void InitializeParameter(DbParameter dbParam, string name, Sq
139160
{
140161
case DbType.AnsiString:
141162
case DbType.AnsiStringFixedLength:
142-
dbParam.Size = IsAnsiText(dbParam, sqlType) ? MaxSizeForAnsiClob : MaxSizeForLengthLimitedAnsiString;
163+
dbParam.Size = IsAnsiText(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForAnsiClob : MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString;
143164
break;
144165
case DbType.Binary:
145-
dbParam.Size = IsBlob(dbParam, sqlType) ? MaxSizeForBlob : MaxSizeForLengthLimitedBinary;
166+
dbParam.Size = IsBlob(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForBlob : MsSql2000Dialect.MaxSizeForLengthLimitedBinary;
146167
break;
147168
case DbType.Decimal:
148169
if (_dialect == null)
@@ -152,16 +173,16 @@ protected override void InitializeParameter(DbParameter dbParam, string name, Sq
152173
break;
153174
case DbType.String:
154175
case DbType.StringFixedLength:
155-
dbParam.Size = IsText(dbParam, sqlType) ? MaxSizeForClob : MaxSizeForLengthLimitedString;
176+
dbParam.Size = IsText(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForClob : MsSql2000Dialect.MaxSizeForLengthLimitedString;
156177
break;
157178
case DbType.DateTime2:
158-
dbParam.Size = MaxDateTime2;
179+
dbParam.Size = MsSql2000Dialect.MaxDateTime2;
159180
break;
160181
case DbType.DateTimeOffset:
161-
dbParam.Size = MaxDateTimeOffset;
182+
dbParam.Size = MsSql2000Dialect.MaxDateTimeOffset;
162183
break;
163184
case DbType.Xml:
164-
dbParam.Size = MaxSizeForXml;
185+
dbParam.Size = MsSql2005Dialect.MaxSizeForXml;
165186
break;
166187
}
167188

@@ -203,27 +224,27 @@ protected static void SetDefaultParameterSize(DbParameter dbParam, SqlType sqlTy
203224
{
204225
case DbType.AnsiString:
205226
case DbType.AnsiStringFixedLength:
206-
dbParam.Size = IsAnsiText(dbParam, sqlType) ? MaxSizeForAnsiClob : MaxSizeForLengthLimitedAnsiString;
227+
dbParam.Size = IsAnsiText(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForAnsiClob : MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString;
207228
break;
208229
case DbType.Binary:
209-
dbParam.Size = IsBlob(dbParam, sqlType) ? MaxSizeForBlob : MaxSizeForLengthLimitedBinary;
230+
dbParam.Size = IsBlob(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForBlob : MsSql2000Dialect.MaxSizeForLengthLimitedBinary;
210231
break;
211232
case DbType.Decimal:
212233
dbParam.Precision = MaxPrecision;
213234
dbParam.Scale = MaxScale;
214235
break;
215236
case DbType.String:
216237
case DbType.StringFixedLength:
217-
dbParam.Size = IsText(dbParam, sqlType) ? MaxSizeForClob : MaxSizeForLengthLimitedString;
238+
dbParam.Size = IsText(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForClob : MsSql2000Dialect.MaxSizeForLengthLimitedString;
218239
break;
219240
case DbType.DateTime2:
220-
dbParam.Size = MaxDateTime2;
241+
dbParam.Size = MsSql2000Dialect.MaxDateTime2;
221242
break;
222243
case DbType.DateTimeOffset:
223-
dbParam.Size = MaxDateTimeOffset;
244+
dbParam.Size = MsSql2000Dialect.MaxDateTimeOffset;
224245
break;
225246
case DbType.Xml:
226-
dbParam.Size = MaxSizeForXml;
247+
dbParam.Size = MsSql2005Dialect.MaxSizeForXml;
227248
break;
228249
}
229250
}
@@ -236,7 +257,7 @@ protected static void SetDefaultParameterSize(DbParameter dbParam, SqlType sqlTy
236257
/// <returns>True, if the parameter should be interpreted as a Clob, otherwise False</returns>
237258
protected static bool IsAnsiText(DbParameter dbParam, SqlType sqlType)
238259
{
239-
return ((DbType.AnsiString == dbParam.DbType || DbType.AnsiStringFixedLength == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MaxSizeForLengthLimitedAnsiString));
260+
return ((DbType.AnsiString == dbParam.DbType || DbType.AnsiStringFixedLength == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString));
240261
}
241262

242263
/// <summary>
@@ -247,7 +268,7 @@ protected static bool IsAnsiText(DbParameter dbParam, SqlType sqlType)
247268
/// <returns>True, if the parameter should be interpreted as a Clob, otherwise False</returns>
248269
protected static bool IsText(DbParameter dbParam, SqlType sqlType)
249270
{
250-
return (sqlType is StringClobSqlType) || ((DbType.String == dbParam.DbType || DbType.StringFixedLength == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MaxSizeForLengthLimitedString));
271+
return (sqlType is StringClobSqlType) || ((DbType.String == dbParam.DbType || DbType.StringFixedLength == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedString));
251272
}
252273

253274
/// <summary>
@@ -258,7 +279,7 @@ protected static bool IsText(DbParameter dbParam, SqlType sqlType)
258279
/// <returns>True, if the parameter should be interpreted as a Blob, otherwise False</returns>
259280
protected static bool IsBlob(DbParameter dbParam, SqlType sqlType)
260281
{
261-
return (sqlType is BinaryBlobSqlType) || ((DbType.Binary == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MaxSizeForLengthLimitedBinary));
282+
return (sqlType is BinaryBlobSqlType) || ((DbType.Binary == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedBinary));
262283
}
263284

264285

0 commit comments

Comments
 (0)