Skip to content

Commit e42117e

Browse files
committed
Add MySqlCommand.IsPrepared.
1 parent 6a2840d commit e42117e

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

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

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,12 @@ public override string CommandText
154154
if (m_connection?.HasActiveReader ?? false)
155155
throw new InvalidOperationException("Cannot set MySqlCommand.CommandText when there is an open DataReader for this command; it must be closed first.");
156156
m_commandText = value;
157-
m_statements = null;
157+
ClearPreparedStatements();
158158
}
159159
}
160160

161+
public bool IsPrepared => m_statements != null;
162+
161163
public new MySqlTransaction Transaction { get; set; }
162164

163165
public new MySqlConnection Connection
@@ -168,6 +170,7 @@ public override string CommandText
168170
if (m_connection?.HasActiveReader ?? false)
169171
throw new InvalidOperationException("Cannot set MySqlCommand.Connection when there is an open DataReader for this command; it must be closed first.");
170172
m_connection = value;
173+
ClearPreparedStatements();
171174
}
172175
}
173176

@@ -185,6 +188,7 @@ public override CommandType CommandType
185188
if (value != CommandType.Text && value != CommandType.StoredProcedure)
186189
throw new ArgumentException("CommandType must be Text or StoredProcedure.", nameof(value));
187190
m_commandType = value;
191+
ClearPreparedStatements();
188192
}
189193
}
190194

@@ -290,8 +294,7 @@ protected override void Dispose(bool disposing)
290294
if (disposing)
291295
{
292296
m_parameterCollection = null;
293-
m_parsedStatements?.Dispose();
294-
m_parsedStatements = null;
297+
ClearPreparedStatements();
295298
}
296299
}
297300
finally
@@ -397,6 +400,13 @@ private bool IsValid(out Exception exception)
397400
return exception == null;
398401
}
399402

403+
private void ClearPreparedStatements()
404+
{
405+
m_parsedStatements?.Dispose();
406+
m_parsedStatements = null;
407+
m_statements = null;
408+
}
409+
400410
internal void ReaderClosed() => (m_commandExecutor as StoredProcedureCommandExecutor)?.SetParams();
401411

402412
static int s_commandId = 1;

tests/SideBySide/QueryTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,13 +1012,15 @@ public void PreparedCommands(bool isPrepared, string dataType, object dataValue)
10121012
command.Parameters.AddWithValue("@data", dataValue);
10131013
if (isPrepared)
10141014
command.Prepare();
1015+
Assert.Equal(isPrepared, command.IsPrepared);
10151016
command.ExecuteNonQuery();
10161017
}
10171018

10181019
using (var command = new MySqlCommand("SELECT data FROM prepared_command_test ORDER BY rowid;", connection))
10191020
{
10201021
if (isPrepared)
10211022
command.Prepare();
1023+
Assert.Equal(isPrepared, command.IsPrepared);
10221024

10231025
using (var reader = command.ExecuteReader())
10241026
{

0 commit comments

Comments
 (0)