From: Date: September 27 2006 12:59am Subject: Connector/NET commit: r365 - in trunk: . TestSuite mysqlclient mysqlclient/Types List-Archive: http://lists.mysql.com/commits/12579 X-Bug: 9619 Message-Id: <200609262259.k8QMxWfW013129@bk-internal.mysql.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Modified: trunk/CHANGES trunk/TestSuite/DateTimeTests.cs trunk/mysqlclient/Types/MySqlDateTime.cs trunk/mysqlclient/datareader.cs Log: Bug #9619 Cannot update row using DbDataAdapter when row contains an invalid date Modified: trunk/CHANGES =================================================================== --- trunk/CHANGES 2006-09-26 21:36:31 UTC (rev 364) +++ trunk/CHANGES 2006-09-26 22:59:31 UTC (rev 365) @@ -77,6 +77,7 @@ Bug #14592 Wrong column length returned for VARCHAR UTF8 columns Bug #18391 Better error handling for the .NET class "MySqlCommand" needed. Bug #8131 Data Adapter doesn't close connection + Bug #9619 Cannot update row using DbDataAdapter when row contains an invalid date Version 1.0.7 Modified: trunk/TestSuite/DateTimeTests.cs =================================================================== --- trunk/TestSuite/DateTimeTests.cs 2006-09-26 21:36:31 UTC (rev 364) +++ trunk/TestSuite/DateTimeTests.cs 2006-09-26 22:59:31 UTC (rev 365) @@ -157,13 +157,16 @@ } } + /// + /// Bug #9619 Cannot update row using DbDataAdapter when row contains an invalid date + /// [Test] public void TestAllowZeroDateTime() { execSQL("INSERT INTO Test (id, d, dt) VALUES (1, '0000-00-00', '0000-00-00 00:00:00')"); MySqlConnection c = new MySqlConnection( - conn.ConnectionString + ";pooling=false;AllowZeroDatetime=true" ); + conn.ConnectionString + ";pooling=false;AllowZeroDatetime=true"); c.Open(); MySqlDataReader reader = null; try @@ -172,11 +175,11 @@ reader = cmd.ExecuteReader(); reader.Read(); - Assert.IsTrue( reader.GetValue(1) is MySqlDateTime ); - Assert.IsTrue( reader.GetValue(2) is MySqlDateTime ); + Assert.IsTrue(reader.GetValue(1) is MySqlDateTime); + Assert.IsTrue(reader.GetValue(2) is MySqlDateTime); - Assert.IsFalse( reader.GetMySqlDateTime(1).IsValidDateTime ); - Assert.IsFalse( reader.GetMySqlDateTime(2).IsValidDateTime ); + Assert.IsFalse(reader.GetMySqlDateTime(1).IsValidDateTime); + Assert.IsFalse(reader.GetMySqlDateTime(2).IsValidDateTime); try { @@ -184,12 +187,19 @@ Assert.Fail("This should not succeed"); } catch (MySqlConversionException) {} + reader.Close(); + reader = null; - + DataTable dt = new DataTable(); + MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM test", c); + MySqlCommandBuilder cb = new MySqlCommandBuilder(da); + da.Fill(dt); + dt.Rows[0]["id"] = 2; + da.Update(dt); } - catch (MySqlException ex) + catch (Exception ex) { - Assert.Fail( ex.Message ); + Assert.Fail(ex.Message); } finally { Modified: trunk/mysqlclient/Types/MySqlDateTime.cs =================================================================== --- trunk/mysqlclient/Types/MySqlDateTime.cs 2006-09-26 21:36:31 UTC (rev 364) +++ trunk/mysqlclient/Types/MySqlDateTime.cs 2006-09-26 22:59:31 UTC (rev 365) @@ -34,6 +34,7 @@ private bool isNull; private MySqlDbType type; private int year, month, day, hour, minute, second; + private int millisecond; private static string fullPattern; private static string shortPattern; @@ -49,6 +50,7 @@ this.hour = hour; this.minute = minute; this.second = second; + this.millisecond = 0; if (fullPattern == null) ComposePatterns(); @@ -68,6 +70,7 @@ hour = val.Hour; minute = val.Minute; second = val.Second; + millisecond = val.Millisecond; } #region Properties @@ -125,6 +128,12 @@ set { second = value; } } + public int Millisecond + { + get { return millisecond; } + set { millisecond = value; } + } + #endregion #region IMySqlValue Members @@ -182,7 +191,7 @@ } - private void SerializeText(MySqlStream stream, DateTime value) + private void SerializeText(MySqlStream stream, MySqlDateTime value) { string val = String.Empty; @@ -197,20 +206,20 @@ stream.WriteStringNoNull( "'" + val + "'" ); } - void IMySqlValue.WriteValue(MySqlStream stream, bool binary, - object value, int length) + void IMySqlValue.WriteValue(MySqlStream stream, bool binary, object value, int length) { - if (value is MySqlDateTime) - value = ((MySqlDateTime)value).GetDateTime(); + MySqlDateTime dtValue; - if (value is string) - value = DateTime.Parse((string)value, - System.Globalization.CultureInfo.CurrentCulture); + if (value is DateTime) + dtValue = new MySqlDateTime(type, (DateTime)value); + else if (value is string) + dtValue = new MySqlDateTime(type, DateTime.Parse((string)value, + System.Globalization.CultureInfo.CurrentCulture)); + else if (value is MySqlDateTime) + dtValue = (MySqlDateTime)value; + else + throw new MySqlException("Unable to serialize date/time value."); - if (! (value is DateTime)) - throw new MySqlException( "Only DateTime objects can be serialized by MySqlDateTime" ); - - DateTime dtValue = (DateTime)value; if (! binary) { SerializeText(stream, dtValue); @@ -222,24 +231,24 @@ else stream.WriteByte(7); - stream.WriteInteger( dtValue.Year, 2 ); - stream.WriteByte( (byte)dtValue.Month ); - stream.WriteByte( (byte)dtValue.Day ); + stream.WriteInteger(dtValue.Year, 2); + stream.WriteByte((byte)dtValue.Month); + stream.WriteByte((byte)dtValue.Day); if (type == MySqlDbType.Date) { - stream.WriteByte( 0 ); - stream.WriteByte( 0 ); - stream.WriteByte( 0 ); + stream.WriteByte(0); + stream.WriteByte(0); + stream.WriteByte(0); } else { - stream.WriteByte( (byte)dtValue.Hour ); - stream.WriteByte( (byte)dtValue.Minute ); - stream.WriteByte( (byte)dtValue.Second ); + stream.WriteByte((byte)dtValue.Hour); + stream.WriteByte((byte)dtValue.Minute); + stream.WriteByte((byte)dtValue.Second); } if (type == MySqlDbType.Timestamp) - stream.WriteInteger( dtValue.Millisecond, 4 ); + stream.WriteInteger(dtValue.Millisecond, 4); } private MySqlDateTime Parse40Timestamp(string s) @@ -593,6 +602,9 @@ if (Second < otherDate.Second) return -1; else if (Second > otherDate.Second) return 1; + if (Millisecond < otherDate.Millisecond) return -1; + else if (Millisecond > otherDate.Millisecond) return 1; + return 0; } Modified: trunk/mysqlclient/datareader.cs =================================================================== --- trunk/mysqlclient/datareader.cs 2006-09-26 21:36:31 UTC (rev 364) +++ trunk/mysqlclient/datareader.cs 2006-09-26 22:59:31 UTC (rev 365) @@ -404,8 +404,12 @@ if (! isOpen) throw new Exception("No current query in data reader"); if (i >= fields.Length) throw new IndexOutOfRangeException(); - if (values[i] is MySqlDateTime && !connection.Settings.AllowZeroDateTime) - return typeof(DateTime); + if (values[i] is MySqlDateTime) + { + if (!connection.Settings.AllowZeroDateTime) + return typeof(DateTime); + return typeof(MySqlDateTime); + } return values[i].SystemType; }