|
3 | 3 | using System.Collections.Generic;
|
4 | 4 | using System.Data;
|
5 | 5 | using System.Data.Common;
|
| 6 | +using MySqlConnector.Core; |
6 | 7 |
|
7 | 8 | namespace MySql.Data.MySqlClient
|
8 | 9 | {
|
@@ -67,38 +68,44 @@ public MySqlDataAdapter(string selectCommandText, string connectionString)
|
67 | 68 |
|
68 | 69 | public override int UpdateBatchSize { get; set; }
|
69 | 70 |
|
70 |
| - protected override void InitializeBatching() => m_batchCommands = new List<MySqlCommand>(); |
| 71 | + protected override void InitializeBatching() => m_batch = new MySqlBatch(); |
71 | 72 |
|
72 | 73 | protected override void TerminateBatching()
|
73 | 74 | {
|
74 |
| - if (m_batchCommands is object) |
75 |
| - ClearBatch(); |
76 |
| - m_batchCommands = null; |
| 75 | + m_batch?.Dispose(); |
| 76 | + m_batch = null; |
77 | 77 | }
|
78 | 78 |
|
79 | 79 | protected override int AddToBatch(IDbCommand command)
|
80 | 80 | {
|
81 |
| - var count = m_batchCommands.Count; |
82 |
| - m_batchCommands.Add(((MySqlCommand) command).Clone()); |
| 81 | + var mySqlCommand = (MySqlCommand) command; |
| 82 | + if (m_batch.Connection is null) |
| 83 | + { |
| 84 | + m_batch.Connection = mySqlCommand.Connection; |
| 85 | + m_batch.Transaction = mySqlCommand.Transaction; |
| 86 | + } |
| 87 | + |
| 88 | + var count = m_batch.BatchCommands.Count; |
| 89 | + var batchCommand = new MySqlBatchCommand |
| 90 | + { |
| 91 | + CommandText = command.CommandText, |
| 92 | + CommandType = command.CommandType, |
| 93 | + }; |
| 94 | + if (mySqlCommand.CloneRawParameters() is MySqlParameterCollection clonedParameters) |
| 95 | + { |
| 96 | + foreach (var clonedParameter in clonedParameters) |
| 97 | + batchCommand.Parameters.Add(clonedParameter); |
| 98 | + } |
| 99 | + |
| 100 | + m_batch.BatchCommands.Add(batchCommand); |
83 | 101 | return count;
|
84 | 102 | }
|
85 | 103 |
|
86 |
| - protected override void ClearBatch() |
87 |
| - { |
88 |
| - foreach (var command in m_batchCommands) |
89 |
| - command.Dispose(); |
90 |
| - m_batchCommands.Clear(); |
91 |
| - } |
| 104 | + protected override void ClearBatch() => m_batch.BatchCommands.Clear(); |
92 | 105 |
|
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 |
| - } |
| 106 | + protected override int ExecuteBatch() => m_batch.ExecuteNonQuery(); |
100 | 107 |
|
101 |
| - List<MySqlCommand> m_batchCommands; |
| 108 | + MySqlBatch m_batch; |
102 | 109 | }
|
103 | 110 |
|
104 | 111 | public delegate void MySqlRowUpdatingEventHandler(object sender, MySqlRowUpdatingEventArgs e);
|
|
0 commit comments