List:Commits« Previous MessageNext Message »
From:rburnett Date:July 10 2006 4:09pm
Subject:Connector/NET commit: r257 - in trunk: TestSuite mysqlclient mysqlclient/docs
View as plain text  
Modified:
   trunk/TestSuite/CommandTests.cs
   trunk/mysqlclient/command.cs
   trunk/mysqlclient/datareader.cs
   trunk/mysqlclient/docs/MySqlCommand.xml
Log:
Implemented the MySqlCommand.LastInsertedId property

Modified: trunk/TestSuite/CommandTests.cs
===================================================================
--- trunk/TestSuite/CommandTests.cs	2006-07-10 15:05:14 UTC (rev 256)
+++ trunk/TestSuite/CommandTests.cs	2006-07-10 16:09:33 UTC (rev 257)
@@ -347,6 +347,37 @@
             Assert.AreEqual(2, table.Rows.Count);
             Assert.AreEqual(2, table.Rows[1]["id"]);
             Assert.AreEqual("Test2", table.Rows[1]["name"]);
+        }
+
+        [Test]
+        public void LastInsertid()
+        {
+            execSQL("DROP TABLE test");
+            execSQL("CREATE TABLE test(id int auto_increment, name varchar(20))");
+            MySqlCommand cmd = new MySqlCommand("INSERT INTO test VALUES(NULL, 'test')", conn);
+            cmd.ExecuteNonQuery();
+            Assert.AreEqual(1, cmd.LastInsertedId);
+
+            MySqlDataReader reader = null;
+            try
+            {
+                reader = cmd.ExecuteReader();
+                reader.Read();
+            }
+            catch (Exception ex)
+            {
+                Assert.Fail(ex.Message);
+            }
+            finally
+            {
+                if (reader != null)
+                    reader.Close();
+            }
+            Assert.AreEqual(2, cmd.LastInsertedId);
+
+            cmd.CommandText = "SELECT id FROM test";
+            cmd.ExecuteScalar();
+            Assert.AreEqual(-1, cmd.LastInsertedId);
         }
 
 	}

Modified: trunk/mysqlclient/command.cs
===================================================================
--- trunk/mysqlclient/command.cs	2006-07-10 15:05:14 UTC (rev 256)
+++ trunk/mysqlclient/command.cs	2006-07-10 16:09:33 UTC (rev 257)
@@ -47,7 +47,7 @@
 		private int					cursorPageSize;
 		private IAsyncResult		asyncResult;
         private bool                designTimeVisible;
-        //private Int64 lastInsertedId;
+        internal Int64 lastInsertedId;
         private Statement statement;
         private MySqlCommandType mysqlCmdType;
 
@@ -86,6 +86,16 @@
 
 		#region Properties
 
+
+        /// <include file='docs/mysqlcommand.xml' path='docs/LastInseredId/*'/>
+#if DESIGN
+		[Browsable(false)]
+#endif
+        public Int64 LastInsertedId
+        {
+            get { return lastInsertedId; }
+        }
+
 		/// <include file='docs/mysqlcommand.xml' path='docs/CommandText/*'/>
 #if DESIGN
 		[Category("Data")]
@@ -410,8 +420,10 @@
 		/// <include file='docs/mysqlcommand.xml' path='docs/ExecuteNonQuery/*'/>
 		public override int ExecuteNonQuery()
 		{
+            lastInsertedId = -1;
             MySqlDataReader reader = ExecuteReader();
             reader.Close();
+            lastInsertedId = reader.InsertedId;
             return reader.RecordsAffected;
 /*			CheckState();
 
@@ -447,6 +459,7 @@
 		/// <include file='docs/mysqlcommand.xml' path='docs/ExecuteReader1/*'/>
 		public new MySqlDataReader ExecuteReader(CommandBehavior behavior)
 		{
+            lastInsertedId = -1;
 			CheckState();
 
 			string sql = TrimSemicolons(cmdText);
@@ -485,14 +498,13 @@
 		/// <include file='docs/mysqlcommand.xml' path='docs/ExecuteScalar/*'/>
 		public override object ExecuteScalar()
 		{
-			// ExecuteReader will check out state
-//			updateCount = -1;
-
+            lastInsertedId = -1;
 			object val = null;
 			MySqlDataReader reader = ExecuteReader();
 			if (reader.Read())
 				val = reader.GetValue(0);
 			reader.Close();
+            lastInsertedId = reader.InsertedId;
 
 			return val;
 		}

Modified: trunk/mysqlclient/datareader.cs
===================================================================
--- trunk/mysqlclient/datareader.cs	2006-07-10 15:05:14 UTC (rev 256)
+++ trunk/mysqlclient/datareader.cs	2006-07-10 16:09:33 UTC (rev 257)
@@ -74,6 +74,11 @@
 
         #region Properties
 
+        internal long InsertedId
+        {
+            get { return this.lastInsertId; }
+        }
+
         internal CommandBehavior Behavior 
 		{
 			get { return commandBehavior; }
@@ -651,7 +656,10 @@
                 if (fieldCount > 0)
                     return fieldCount;
                 else if (fieldCount == 0)
+                {
+                    command.lastInsertedId = lastInsertId;
                     affectedRows += (long)affectedRowsTemp;
+                }
                 else if (fieldCount == -1)
                     if (!statement.ExecuteNext())
                         break;

Modified: trunk/mysqlclient/docs/MySqlCommand.xml
===================================================================
--- trunk/mysqlclient/docs/MySqlCommand.xml	2006-07-10 15:05:14 UTC (rev 256)
+++ trunk/mysqlclient/docs/MySqlCommand.xml	2006-07-10 16:09:33 UTC (rev 257)
@@ -698,8 +698,22 @@
 <IsPrepared>
 </IsPrepared>
 
+  <LastInsertedId>
+    <summary>Provides the id of the last inserted row.</summary>
+    <value>
+      Id of the last inserted row.  -1 if none exists.
+    </value>
+    <remarks>
+      An important point to remember is that this property can be used
+      in batch SQL scenarios but it's important to remember that it will
+      only reflect the insert id from the last insert statement in the batch.
+      
+      This property can also be used when the batch includes select statements
+      and ExecuteReader is used.  This property can be consulted during result set
+      processing.
+    </remarks>
+  </LastInsertedId>
 
-
 <Parameters>
 	<summary>Get the <see cref="MySqlParameterCollection"/></summary>
 	<value>The parameters of the SQL statement or stored procedure. The default is 

Thread
Connector/NET commit: r257 - in trunk: TestSuite mysqlclient mysqlclient/docsrburnett10 Jul