Bug #48460 Implement type safe cloning.
Submitted: 2 Nov 2009 10:39 Modified: 5 Nov 2009 17:01
Reporter: John Bayly (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S4 (Feature request)
Version:6.1.2 OS:Any
Assigned to: Vladislav Vaintroub CPU Architecture:Any
Tags: Clone, Type Safe

[2 Nov 2009 10:39] John Bayly
Description:
Since realising that my code broke commit #81553:
http://lists.mysql.com/commits/81553

I've decided to do as was suggested and clone the generate command. Doing this I noticed that the clone has to be called using:

MySqlCommand clone = (MySqlCommand)((ICloneable)comm).Clone();

The following diff implements cloning in a similar manner to that used in System.Data.SqlClient, and shouldn't break existing usage while providing type safe cloning.

How to repeat:
// Clone an existing MySqlCommand object
MySqlCommand clone = (MySqlCommand)((ICloneable)comm).Clone();

// It would be nice to be able to do it this way
MySqlCommand clone = comm.Clone();

Suggested fix:
--- command.cs   8 Oct 2009 16:30:04 -0000   1.1
+++ command.cs   1 Nov 2009 18:03:05 -0000   1.2
@@ -743,19 +743,25 @@
       /// are included as well as the entire parameter list.
       /// </summary>
       /// <returns>The cloned MySqlCommand object</returns>
-      object ICloneable.Clone()
+      public MySqlCommand Clone()
       {
          MySqlCommand clone = new MySqlCommand(cmdText, connection, curTransaction);
             clone.CommandType = CommandType;
             clone.CommandTimeout = CommandTimeout;
             clone.batchableCommandText = batchableCommandText;
+            clone.UpdatedRowSource = UpdatedRowSource;
 
          foreach (MySqlParameter p in parameters)
          {
-            clone.Parameters.Add((p as ICloneable).Clone());
+            clone.Parameters.Add(p.Clone());
          }
          return clone;
       }
+
+    object ICloneable.Clone()
+    {
+      return this.Clone();
+    }
       #endregion
 
         #region Batching support

--- Connection.cs   8 Oct 2009 16:30:04 -0000   1.1
+++ Connection.cs   1 Nov 2009 18:03:05 -0000   1.2
@@ -521,7 +521,7 @@
         /// Creates a new MySqlConnection object with the exact same ConnectionString value
         /// </summary>
         /// <returns>A cloned MySqlConnection object</returns>
-        object ICloneable.Clone()
+        public MySqlConnection Clone()
         {
             MySqlConnection clone = new MySqlConnection();
             string connectionString = settings.GetConnectionString(true);
@@ -530,6 +530,10 @@
             return clone;
         }
 
+        object ICloneable.Clone()
+        {
+          return this.Clone();
+        }
         #endregion
 
         #region IDisposeable

--- parameter.cs   8 Oct 2009 16:30:04 -0000   1.1
+++ parameter.cs   1 Nov 2009 18:03:05 -0000   1.2
@@ -608,7 +608,7 @@
 
         #region ICloneable
 
-        object ICloneable.Clone()
+        public MySqlParameter Clone()
         {
             MySqlParameter clone = new MySqlParameter(paramName, mySqlDbType, direction,
                 sourceColumn, sourceVersion, paramValue);
@@ -617,6 +617,10 @@
             return clone;
         }
 
+        object ICloneable.Clone()
+        {
+          return this.Clone();
+        }
         #endregion
 
         /// <summary>
[2 Nov 2009 12:22] Tonci Grgin
Hi John and thanks for your feature request.
[3 Nov 2009 17:58] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/89186

773 Reggie Burnett	2009-11-03
      - applied user-suggested patch to enable type-safe cloning (bug #48460)
[3 Nov 2009 17:59] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/89187

786 Reggie Burnett	2009-11-03 [merge]
      - applied user-suggested patch to enable type-safe cloning (bug #48460)
[3 Nov 2009 18:01] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/89189

794 Reggie Burnett	2009-11-03 [merge]
      - applied user-suggested patch to enable type-safe cloning (bug #48460)
[3 Nov 2009 18:02] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/89191

794 Reggie Burnett	2009-11-03 [merge]
      - applied user-suggested patch to enable type-safe cloning (bug #48460)
[3 Nov 2009 18:03] Reggie Burnett
Fixed in 6.0.5, 6.1.3, and 6.2.1+
[5 Nov 2009 17:01] Tony Bedford
A entry has been added to the 6.0.5, 6.1.3 and 6.2.1 changelogs:

Cloning of MySqlCommand was not typesafe. To clone a MySqlCommand it was necessary to do:

        MySqlCommand clone = (MySqlCommand)((ICloneable)comm).Clone();
      
MySQL Connector/NET was changed so that it was possible to do:

        MySqlCommand clone = comm.Clone();