Skip to content

Commit eb383f7

Browse files
committed
NH-3109 - Fix rounding numeric types in cast expressions for MySQL5Dialect
1 parent 3cbd324 commit eb383f7

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/NHibernate/Dialect/MySQL5Dialect.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ public MySQL5Dialect()
1515
protected override void RegisterCastTypes() {
1616
base.RegisterCastTypes();
1717
// MySql 5 also supports DECIMAL as a cast type target
18-
// http://dev.mysql.com/doc/refman/5.5/en/cast-functions.html
19-
RegisterCastType(DbType.Decimal, "DECIMAL");
20-
RegisterCastType(DbType.Double, "DECIMAL");
21-
RegisterCastType(DbType.Single, "DECIMAL");
18+
// http://dev.mysql.com/doc/refman/5.0/en/cast-functions.html
19+
RegisterCastType(DbType.Decimal, "DECIMAL(19,5)");
20+
RegisterCastType(DbType.Decimal, 19, "DECIMAL($p, $s)");
21+
RegisterCastType(DbType.Double, "DECIMAL(19,5)");
22+
RegisterCastType(DbType.Single, "DECIMAL(19,5)");
23+
RegisterCastType(DbType.Guid, "BINARY(16)");
2224
}
2325

2426
//Reference 5.x

src/NHibernate/Dialect/MySQLDialect.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
using System.Data.Common;
44
using System.Text;
55
using NHibernate.Dialect.Function;
6-
using NHibernate.Dialect.Schema;
6+
using NHibernate.Dialect.Schema;
7+
using NHibernate.Mapping;
78
using NHibernate.SqlCommand;
89
using NHibernate.SqlTypes;
910
using NHibernate.Util;
@@ -272,15 +273,27 @@ protected virtual void RegisterCastTypes()
272273
}
273274

274275
/// <summary>
275-
/// Suclasses register a typename for the given type code, to be used in CAST()
276+
/// Subclasses register a typename for the given type code, to be used in CAST()
276277
/// statements.
277278
/// </summary>
278279
/// <param name="code">The typecode</param>
279280
/// <param name="name">The database type name</param>
280281
protected void RegisterCastType(DbType code, string name)
281282
{
282283
castTypeNames.Put(code, name);
283-
}
284+
}
285+
286+
/// <summary>
287+
/// Subclasses register a typename for the given type code, to be used in CAST()
288+
/// statements.
289+
/// </summary>
290+
/// <param name="code">The typecode</param>
291+
/// <param name="capacity"></param>
292+
/// <param name="name">The database type name</param>
293+
protected void RegisterCastType(DbType code, int capacity, string name)
294+
{
295+
castTypeNames.Put(code, capacity, name);
296+
}
284297

285298
/// <summary>
286299
/// Get the name of the database type appropriate for casting operations
@@ -289,8 +302,8 @@ protected void RegisterCastType(DbType code, string name)
289302
/// <param name="sqlType">The <see cref="SqlType"/> typecode </param>
290303
/// <returns> The database type name </returns>
291304
public override string GetCastTypeName(SqlType sqlType)
292-
{
293-
string result = castTypeNames.Get(sqlType.DbType);
305+
{
306+
string result = castTypeNames.Get(sqlType.DbType, Column.DefaultLength, Column.DefaultPrecision, Column.DefaultScale);
294307
if (result == null)
295308
{
296309
throw new HibernateException(string.Format("No CAST() type mapping for SqlType {0}", sqlType));

0 commit comments

Comments
 (0)