Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run inserts in batch command as one block #50

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion MySQL.Data/src/Statement.cs
Expand Up @@ -178,7 +178,7 @@ protected virtual void BindParameters()
//long originalLength = packet.Length - 4;

// and attempt to stream the next command
string text = ResolvedCommandText;
string text = batchedCmd.BatchableCommandText;
if (text.StartsWith("(", StringComparison.Ordinal))
packet.WriteStringNoNull(", ");
else
Expand Down
Expand Up @@ -721,6 +721,43 @@ public void TestBatchingInsertsMoreThanMaxPacket()
Assert.AreEqual(i, dt.Rows[i]["id"]);
}

/// <summary>
/// Test similar to TestBatchingInserts, only that here a check is done that the
/// returned autoincrement id is the first one of the block to verify that all
/// rows are inserted as one batch.
/// </summary>
[Test]
public void TestBatchingInsertsAllRowsInOneGo()
{
ExecuteSQL("CREATE TABLE Test (id INT AUTO_INCREMENT, name VARCHAR(20), PRIMARY KEY(id))");
ExecuteSQL("INSERT INTO Test VALUES (1, 'Test 1')");

MySqlDataAdapter da = new MySqlDataAdapter("SELECT * FROM Test", Connection);
MySqlCommand ins = new MySqlCommand("INSERT INTO Test (id, name) VALUES (?p1, ?p2)", Connection);
da.InsertCommand = ins;
ins.UpdatedRowSource = UpdateRowSource.None;
ins.Parameters.Add("?p1", MySqlDbType.Int32).SourceColumn = "id";
ins.Parameters.Add("?p2", MySqlDbType.VarChar, 20).SourceColumn = "name";

DataTable dt = new DataTable();
da.Fill(dt);

for (int i = 1; i <= 3; i++)
{
DataRow row = dt.NewRow();
row["id"] = DBNull.Value;
row["name"] = "name " + i;
dt.Rows.Add(row);
}

da.UpdateBatchSize = 0;
da.Update(dt);

var lastInsertedId = (ulong)ExecuteScalar("SELECT LAST_INSERT_ID();");

Assert.AreEqual(2, lastInsertedId);
}

[Test]
public void FunctionsReturnString()
{
Expand Down