Skip to content

Commit e132813

Browse files
committed
NH-4008 - Move MsSql constants from driver to dialect.
1 parent f0644cf commit e132813

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();
@@ -357,8 +366,8 @@ protected virtual void RegisterGuidTypeMapping()
357366
protected virtual void RegisterLargeObjectTypeMappings()
358367
{
359368
RegisterColumnType(DbType.Binary, "VARBINARY(8000)");
360-
RegisterColumnType(DbType.Binary, SqlClientDriver.MaxSizeForLengthLimitedBinary, "VARBINARY($l)");
361-
RegisterColumnType(DbType.Binary, SqlClientDriver.MaxSizeForBlob, "IMAGE");
369+
RegisterColumnType(DbType.Binary, MaxSizeForLengthLimitedBinary, "VARBINARY($l)");
370+
RegisterColumnType(DbType.Binary, MaxSizeForBlob, "IMAGE");
362371
}
363372

364373
protected virtual void RegisterDateTimeTypeMappings()
@@ -388,13 +397,13 @@ protected virtual void RegisterCharacterTypeMappings()
388397
RegisterColumnType(DbType.AnsiStringFixedLength, "CHAR(255)");
389398
RegisterColumnType(DbType.AnsiStringFixedLength, 8000, "CHAR($l)");
390399
RegisterColumnType(DbType.AnsiString, "VARCHAR(255)");
391-
RegisterColumnType(DbType.AnsiString, SqlClientDriver.MaxSizeForLengthLimitedAnsiString, "VARCHAR($l)");
392-
RegisterColumnType(DbType.AnsiString, SqlClientDriver.MaxSizeForAnsiClob, "TEXT");
400+
RegisterColumnType(DbType.AnsiString, MaxSizeForLengthLimitedAnsiString, "VARCHAR($l)");
401+
RegisterColumnType(DbType.AnsiString, MaxSizeForAnsiClob, "TEXT");
393402
RegisterColumnType(DbType.StringFixedLength, "NCHAR(255)");
394-
RegisterColumnType(DbType.StringFixedLength, SqlClientDriver.MaxSizeForLengthLimitedString, "NCHAR($l)");
403+
RegisterColumnType(DbType.StringFixedLength, MaxSizeForLengthLimitedString, "NCHAR($l)");
395404
RegisterColumnType(DbType.String, "NVARCHAR(255)");
396-
RegisterColumnType(DbType.String, SqlClientDriver.MaxSizeForLengthLimitedString, "NVARCHAR($l)");
397-
RegisterColumnType(DbType.String, SqlClientDriver.MaxSizeForClob, "NTEXT");
405+
RegisterColumnType(DbType.String, MaxSizeForLengthLimitedString, "NVARCHAR($l)");
406+
RegisterColumnType(DbType.String, MaxSizeForClob, "NTEXT");
398407
}
399408

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

449-
return String.Format(dropTable, tableName);
458+
return string.Format(dropTable, tableName);
450459
}
451460

452461
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
@@ -4,6 +4,7 @@
44
using System.Data.Common;
55
using System.Data.SqlClient;
66
using NHibernate.AdoNet;
7+
using NHibernate.Dialect;
78
using NHibernate.Engine;
89
using NHibernate.SqlTypes;
910

@@ -14,21 +15,41 @@ namespace NHibernate.Driver
1415
/// </summary>
1516
public class SqlClientDriver : DriverBase, IEmbeddedBatcherFactoryProvider
1617
{
18+
// Since v5.1
19+
[Obsolete("Use MsSql2000Dialect.MaxSizeForAnsiClob")]
1720
public const int MaxSizeForAnsiClob = 2147483647; // int.MaxValue
21+
// Since v5.1
22+
[Obsolete("Use MsSql2000Dialect.MaxSizeForClob")]
1823
public const int MaxSizeForClob = 1073741823; // int.MaxValue / 2
24+
// Since v5.1
25+
[Obsolete("Use MsSql2000Dialect.MaxSizeForBlob")]
1926
public const int MaxSizeForBlob = 2147483647; // int.MaxValue
20-
//http://stackoverflow.com/a/7264795/259946
27+
28+
///<remarks>http://stackoverflow.com/a/7264795/259946</remarks>
29+
// Since v5.1
30+
[Obsolete("Use MsSql2005Dialect.MaxSizeForXml")]
2131
public const int MaxSizeForXml = 2147483647; // int.MaxValue
32+
33+
// Since v5.1
34+
[Obsolete("Use MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString")]
2235
public const int MaxSizeForLengthLimitedAnsiString = 8000;
36+
// Since v5.1
37+
[Obsolete("Use MsSql2000Dialect.MaxSizeForLengthLimitedString")]
2338
public const int MaxSizeForLengthLimitedString = 4000;
39+
// Since v5.1
40+
[Obsolete("Use MsSql2000Dialect.MaxSizeForLengthLimitedBinary")]
2441
public const int MaxSizeForLengthLimitedBinary = 8000;
2542
// Since v5.1
2643
[Obsolete("This member has no more usages and will be removed in a future version")]
2744
public const byte MaxPrecision = 28;
2845
// Since v5.1
2946
[Obsolete("This member has no more usages and will be removed in a future version")]
3047
public const byte MaxScale = 5;
48+
// Since v5.1
49+
[Obsolete("Use MsSql2000Dialect.MaxDateTime2")]
3150
public const byte MaxDateTime2 = 8;
51+
// Since v5.1
52+
[Obsolete("Use MsSql2000Dialect.MaxDateTimeOffset")]
3253
public const byte MaxDateTimeOffset = 10;
3354

3455
private Dialect.Dialect _dialect;
@@ -117,10 +138,10 @@ protected override void InitializeParameter(DbParameter dbParam, string name, Sq
117138
{
118139
case DbType.AnsiString:
119140
case DbType.AnsiStringFixedLength:
120-
dbParam.Size = IsAnsiText(dbParam, sqlType) ? MaxSizeForAnsiClob : MaxSizeForLengthLimitedAnsiString;
141+
dbParam.Size = IsAnsiText(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForAnsiClob : MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString;
121142
break;
122143
case DbType.Binary:
123-
dbParam.Size = IsBlob(dbParam, sqlType) ? MaxSizeForBlob : MaxSizeForLengthLimitedBinary;
144+
dbParam.Size = IsBlob(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForBlob : MsSql2000Dialect.MaxSizeForLengthLimitedBinary;
124145
break;
125146
case DbType.Decimal:
126147
if (_dialect == null)
@@ -130,16 +151,16 @@ protected override void InitializeParameter(DbParameter dbParam, string name, Sq
130151
break;
131152
case DbType.String:
132153
case DbType.StringFixedLength:
133-
dbParam.Size = IsText(dbParam, sqlType) ? MaxSizeForClob : MaxSizeForLengthLimitedString;
154+
dbParam.Size = IsText(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForClob : MsSql2000Dialect.MaxSizeForLengthLimitedString;
134155
break;
135156
case DbType.DateTime2:
136-
dbParam.Size = MaxDateTime2;
157+
dbParam.Size = MsSql2000Dialect.MaxDateTime2;
137158
break;
138159
case DbType.DateTimeOffset:
139-
dbParam.Size = MaxDateTimeOffset;
160+
dbParam.Size = MsSql2000Dialect.MaxDateTimeOffset;
140161
break;
141162
case DbType.Xml:
142-
dbParam.Size = MaxSizeForXml;
163+
dbParam.Size = MsSql2005Dialect.MaxSizeForXml;
143164
break;
144165
}
145166

@@ -181,27 +202,27 @@ protected static void SetDefaultParameterSize(DbParameter dbParam, SqlType sqlTy
181202
{
182203
case DbType.AnsiString:
183204
case DbType.AnsiStringFixedLength:
184-
dbParam.Size = IsAnsiText(dbParam, sqlType) ? MaxSizeForAnsiClob : MaxSizeForLengthLimitedAnsiString;
205+
dbParam.Size = IsAnsiText(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForAnsiClob : MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString;
185206
break;
186207
case DbType.Binary:
187-
dbParam.Size = IsBlob(dbParam, sqlType) ? MaxSizeForBlob : MaxSizeForLengthLimitedBinary;
208+
dbParam.Size = IsBlob(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForBlob : MsSql2000Dialect.MaxSizeForLengthLimitedBinary;
188209
break;
189210
case DbType.Decimal:
190211
dbParam.Precision = MaxPrecision;
191212
dbParam.Scale = MaxScale;
192213
break;
193214
case DbType.String:
194215
case DbType.StringFixedLength:
195-
dbParam.Size = IsText(dbParam, sqlType) ? MaxSizeForClob : MaxSizeForLengthLimitedString;
216+
dbParam.Size = IsText(dbParam, sqlType) ? MsSql2000Dialect.MaxSizeForClob : MsSql2000Dialect.MaxSizeForLengthLimitedString;
196217
break;
197218
case DbType.DateTime2:
198-
dbParam.Size = MaxDateTime2;
219+
dbParam.Size = MsSql2000Dialect.MaxDateTime2;
199220
break;
200221
case DbType.DateTimeOffset:
201-
dbParam.Size = MaxDateTimeOffset;
222+
dbParam.Size = MsSql2000Dialect.MaxDateTimeOffset;
202223
break;
203224
case DbType.Xml:
204-
dbParam.Size = MaxSizeForXml;
225+
dbParam.Size = MsSql2005Dialect.MaxSizeForXml;
205226
break;
206227
}
207228
}
@@ -214,7 +235,7 @@ protected static void SetDefaultParameterSize(DbParameter dbParam, SqlType sqlTy
214235
/// <returns>True, if the parameter should be interpreted as a Clob, otherwise False</returns>
215236
protected static bool IsAnsiText(DbParameter dbParam, SqlType sqlType)
216237
{
217-
return ((DbType.AnsiString == dbParam.DbType || DbType.AnsiStringFixedLength == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MaxSizeForLengthLimitedAnsiString));
238+
return ((DbType.AnsiString == dbParam.DbType || DbType.AnsiStringFixedLength == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedAnsiString));
218239
}
219240

220241
/// <summary>
@@ -225,7 +246,7 @@ protected static bool IsAnsiText(DbParameter dbParam, SqlType sqlType)
225246
/// <returns>True, if the parameter should be interpreted as a Clob, otherwise False</returns>
226247
protected static bool IsText(DbParameter dbParam, SqlType sqlType)
227248
{
228-
return (sqlType is StringClobSqlType) || ((DbType.String == dbParam.DbType || DbType.StringFixedLength == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MaxSizeForLengthLimitedString));
249+
return (sqlType is StringClobSqlType) || ((DbType.String == dbParam.DbType || DbType.StringFixedLength == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedString));
229250
}
230251

231252
/// <summary>
@@ -236,7 +257,7 @@ protected static bool IsText(DbParameter dbParam, SqlType sqlType)
236257
/// <returns>True, if the parameter should be interpreted as a Blob, otherwise False</returns>
237258
protected static bool IsBlob(DbParameter dbParam, SqlType sqlType)
238259
{
239-
return (sqlType is BinaryBlobSqlType) || ((DbType.Binary == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MaxSizeForLengthLimitedBinary));
260+
return (sqlType is BinaryBlobSqlType) || ((DbType.Binary == dbParam.DbType) && sqlType.LengthDefined && (sqlType.Length > MsSql2000Dialect.MaxSizeForLengthLimitedBinary));
240261
}
241262

242263
#region IEmbeddedBatcherFactoryProvider Members

0 commit comments

Comments
 (0)