Skip to content

Commit c11fd66

Browse files
committed
Implement batching for MySqlDataAdapter. Fixes #635
1 parent ad0f83d commit c11fd66

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

src/MySqlConnector/MySql.Data.MySqlClient/MySqlDataAdapter.cs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#if !NETSTANDARD1_3
22
using System;
3+
using System.Collections.Generic;
34
using System.Data;
45
using System.Data.Common;
56

@@ -63,6 +64,41 @@ public MySqlDataAdapter(string selectCommandText, string connectionString)
6364
protected override RowUpdatingEventArgs CreateRowUpdatingEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) => new MySqlRowUpdatingEventArgs(dataRow, command, statementType, tableMapping);
6465

6566
protected override RowUpdatedEventArgs CreateRowUpdatedEvent(DataRow dataRow, IDbCommand command, StatementType statementType, DataTableMapping tableMapping) => new MySqlRowUpdatedEventArgs(dataRow, command, statementType, tableMapping);
67+
68+
public override int UpdateBatchSize { get; set; }
69+
70+
protected override void InitializeBatching() => m_batchCommands = new List<MySqlCommand>();
71+
72+
protected override void TerminateBatching()
73+
{
74+
if (m_batchCommands is object)
75+
ClearBatch();
76+
m_batchCommands = null;
77+
}
78+
79+
protected override int AddToBatch(IDbCommand command)
80+
{
81+
var count = m_batchCommands.Count;
82+
m_batchCommands.Add(((MySqlCommand) command).Clone());
83+
return count;
84+
}
85+
86+
protected override void ClearBatch()
87+
{
88+
foreach (var command in m_batchCommands)
89+
command.Dispose();
90+
m_batchCommands.Clear();
91+
}
92+
93+
protected override int ExecuteBatch()
94+
{
95+
var result = 0;
96+
foreach (var command in m_batchCommands)
97+
result += command.ExecuteNonQuery();
98+
return result;
99+
}
100+
101+
List<MySqlCommand> m_batchCommands;
66102
}
67103

68104
public delegate void MySqlRowUpdatingEventHandler(object sender, MySqlRowUpdatingEventArgs e);

tests/SideBySide/DataAdapterTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,72 @@ public void InsertWithDataSet()
191191
}
192192
}
193193

194+
[Fact]
195+
public void BatchUpdate()
196+
{
197+
using (var ds = new DataSet())
198+
using (var da = new MySqlDataAdapter("SELECT * FROM data_adapter", m_connection))
199+
{
200+
da.Fill(ds);
201+
202+
da.UpdateCommand = new MySqlCommand("UPDATE data_adapter SET int_value=@int, text_value=@text WHERE id=@id", m_connection)
203+
{
204+
Parameters =
205+
{
206+
new MySqlParameter("@int", MySqlDbType.Int32) { Direction = ParameterDirection.Input, SourceColumn = "int_value" },
207+
new MySqlParameter("@text", MySqlDbType.String) { Direction = ParameterDirection.Input, SourceColumn = "text_value" },
208+
new MySqlParameter("@id", MySqlDbType.Int64) { Direction = ParameterDirection.Input, SourceColumn = "id" },
209+
},
210+
UpdatedRowSource = UpdateRowSource.None,
211+
};
212+
213+
da.UpdateBatchSize = 10;
214+
215+
var dt = ds.Tables[0];
216+
dt.Rows[0][1] = 2;
217+
dt.Rows[0][2] = "two";
218+
dt.Rows[1][1] = 3;
219+
dt.Rows[1][2] = "three";
220+
dt.Rows[2][1] = 4;
221+
dt.Rows[2][2] = "four";
222+
223+
da.Update(ds);
224+
}
225+
226+
Assert.Equal(new[] { "two", "three", "four" }, m_connection.Query<string>("SELECT text_value FROM data_adapter ORDER BY id"));
227+
}
228+
229+
230+
[Fact]
231+
public void BatchInsert()
232+
{
233+
using (var ds = new DataSet())
234+
using (var da = new MySqlDataAdapter("SELECT * FROM data_adapter", m_connection))
235+
{
236+
da.Fill(ds);
237+
238+
da.InsertCommand = new MySqlCommand("INSERT INTO data_adapter(int_value, text_value) VALUES(@int, @text);", m_connection)
239+
{
240+
Parameters =
241+
{
242+
new MySqlParameter("@int", MySqlDbType.Int32) { Direction = ParameterDirection.Input, SourceColumn = "int_value" },
243+
new MySqlParameter("@text", MySqlDbType.String) { Direction = ParameterDirection.Input, SourceColumn = "text_value" },
244+
},
245+
UpdatedRowSource = UpdateRowSource.None,
246+
};
247+
248+
da.UpdateBatchSize = 10;
249+
250+
var dt = ds.Tables[0];
251+
dt.Rows.Add(0, 2, "two");
252+
dt.Rows.Add(0, 3, "three");
253+
dt.Rows.Add(0, 4, "four");
254+
255+
da.Update(ds);
256+
}
257+
258+
Assert.Equal(new[] { null, "", "one", "two", "three", "four" }, m_connection.Query<string>("SELECT text_value FROM data_adapter ORDER BY id"));
259+
}
194260
readonly MySqlConnection m_connection;
195261
}
196262
}

0 commit comments

Comments
 (0)