MySQL Lists are EOL. Please join:

List:Commits« Previous MessageNext Message »
From:Julio casal Date:March 28 2011 5:02pm
Subject:bzr commit into connector-net-6.0 branch (julio.casal:866) Bug#57339
Bug#1176450
View as plain text  
#At file:///C:/Users/jcasalt/Dev/connector-net/6.0/ based on revid:reggie.burnett@stripped

  866 Julio casal	2011-03-28
      Trims Double.MaxValue and Double.MinValue before sending to server to avoid overflows when reading values and server is version 5.4 and below (MySQL bug#57339, Oracle bug #1176450)

    modified:
      MySql.Data/Provider/Source/Types/MySqlDouble.cs
      MySql.Data/Tests/Source/DataTypeTests.cs
=== modified file 'MySql.Data/Provider/Source/Types/MySqlDouble.cs'
=== modified file 'MySql.Data/Provider/Source/Types/MySqlDouble.cs'
--- a/MySql.Data/Provider/Source/Types/MySqlDouble.cs	2010-08-10 10:32:49 +0000
+++ b/MySql.Data/Provider/Source/Types/MySqlDouble.cs	2011-03-28 17:02:30 +0000
@@ -22,14 +22,19 @@
 using System.Data;
 using System.Globalization;
 using MySql.Data.MySqlClient;
+using MySql.Data.Common;
 
 namespace MySql.Data.Types
 {
 
 	internal struct MySqlDouble : IMySqlValue
 	{
+        // These two constants refer to max and min doubles when interacting with MySQL 5.4 or below
+        private const double maxLegacyValue = 1.79769313486231E+308;
+        private const double minLegacyValue = -1.79769313486231E+308;
+
 		private double mValue;
-		private bool isNull;
+		private bool isNull;        
 
 		public MySqlDouble(bool isNull)
 		{
@@ -83,12 +88,34 @@
         void IMySqlValue.WriteValue(MySqlPacket packet, bool binary, object val, int length)
 		{
 			double v = (val is double) ? (double)val : Convert.ToDouble(val);
+
+            if (!packet.Version.isAtLeast(5, 5, 0))
+            {
+                // Double.MaxValue & Double.MinValue workaround for MySQL 5.4 and below. 
+                v = FixLegacyMaxMinValues(packet.Version, v);
+            }
+
 			if (binary)
                 packet.Write(BitConverter.GetBytes(v));
 			else
                 packet.WriteStringNoNull(v.ToString("R", CultureInfo.InvariantCulture));
 		}
 
+        private static double FixLegacyMaxMinValues(DBVersion version, double value)
+        {
+            // MySQL 5.4 and below would incorrectly round MaxValue and MinValue, so we trim them here.
+            if (value == double.MaxValue)
+            {
+                value = maxLegacyValue;
+            }
+            else if (value == double.MinValue)
+            {
+                value = minLegacyValue;
+            }
+
+            return value;
+        }
+
         IMySqlValue IMySqlValue.ReadValue(MySqlPacket packet, long length,
 				bool nullVal)
 		{

=== modified file 'MySql.Data/Tests/Source/DataTypeTests.cs'
--- a/MySql.Data/Tests/Source/DataTypeTests.cs	2010-11-30 18:28:31 +0000
+++ b/MySql.Data/Tests/Source/DataTypeTests.cs	2011-03-28 17:02:30 +0000
@@ -995,5 +995,49 @@
             cmd.CommandText = "INSERT INTO test (name) VALUES ('boo2')";
             cmd.ExecuteNonQuery();
         }
+
+        [Test]
+        public void MaxDoubleValueGetsTrimmedOnSave()
+        {
+            if (Version >= new Version(5, 5)) return; // Trimming is not necessary starting in version 5.5
+
+            execSQL("DROP TABLE IF EXISTS test");
+            execSQL("CREATE TABLE test(dbl double)");
+            MySqlCommand cmd = new MySqlCommand("insert into test values(?param1)");
+            cmd.Connection = conn;
+            cmd.Parameters.Add(new MySqlParameter("?param1", MySqlDbType.Double));
+            cmd.Parameters["?param1"].Value = Double.MaxValue;
+            cmd.ExecuteNonQuery();
+
+            cmd = new MySqlCommand("SELECT * FROM test", conn);
+            using (MySqlDataReader reader = cmd.ExecuteReader())
+            {
+                reader.Read();
+                double d = reader.GetDouble(0);
+                Assert.AreEqual(1.79769313486231E+308, d);
+            }
+        }
+
+        [Test]
+        public void MinDoubleValueGetsTrimmedOnSave()
+        {
+            if (Version >= new Version(5, 5)) return; // Trimming is not necessary starting in version 5.5
+
+            execSQL("DROP TABLE IF EXISTS test");
+            execSQL("CREATE TABLE test(dbl double)");
+            MySqlCommand cmd = new MySqlCommand("insert into test values(?param1)");
+            cmd.Connection = conn;
+            cmd.Parameters.Add(new MySqlParameter("?param1", MySqlDbType.Double));
+            cmd.Parameters["?param1"].Value = Double.MinValue;
+            cmd.ExecuteNonQuery();
+
+            cmd = new MySqlCommand("SELECT * FROM test", conn);
+            using (MySqlDataReader reader = cmd.ExecuteReader())
+            {
+                reader.Read();
+                double d = reader.GetDouble(0);
+                Assert.AreEqual(-1.79769313486231E+308, d);
+            }
+        }
     }
 }


Attachment: [text/bzr-bundle] bzr/julio.casal@oracle.com-20110328170230-o4yir2kyu69cvy9x.bundle
Thread
bzr commit into connector-net-6.0 branch (julio.casal:866) Bug#57339Bug#1176450Julio casal28 Mar