Skip to content

Commit abe8bf9

Browse files
committed
Extract WriteQueryPayload method.
1 parent 77ea90b commit abe8bf9

File tree

3 files changed

+15
-11
lines changed

3 files changed

+15
-11
lines changed

src/MySqlConnector/Core/SingleCommandPayloadCreator.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,14 @@ public bool WriteQueryCommand(ref CommandListPosition commandListPosition, IDict
2929
if (Log.IsDebugEnabled())
3030
Log.Debug("Session{0} Preparing command payload; CommandText: {1}", command.Connection.Session.Id, command.CommandText);
3131

32-
if (command.CommandType == CommandType.StoredProcedure)
33-
WriteStoredProcedure(command, cachedProcedures, writer);
34-
else
35-
WriteCommand(command, writer);
32+
writer.Write((byte) CommandKind.Query);
33+
WriteQueryPayload(command, cachedProcedures, writer);
3634

3735
commandListPosition.CommandIndex++;
3836
}
3937
else
4038
{
39+
writer.Write((byte) CommandKind.StatementExecute);
4140
WritePreparedStatement(command, preparedStatements.Statements[commandListPosition.PreparedStatementIndex], writer);
4241

4342
// advance to next prepared statement or next command
@@ -47,18 +46,24 @@ public bool WriteQueryCommand(ref CommandListPosition commandListPosition, IDict
4746
commandListPosition.PreparedStatementIndex = 0;
4847
}
4948
}
50-
5149
return true;
5250
}
5351

54-
private void WritePreparedStatement(IMySqlCommand command, PreparedStatement preparedStatement, ByteBufferWriter writer)
52+
public static void WriteQueryPayload(IMySqlCommand command, IDictionary<string, CachedProcedure> cachedProcedures, ByteBufferWriter writer)
53+
{
54+
if (command.CommandType == CommandType.StoredProcedure)
55+
WriteStoredProcedure(command, cachedProcedures, writer);
56+
else
57+
WriteCommand(command, writer);
58+
}
59+
60+
private static void WritePreparedStatement(IMySqlCommand command, PreparedStatement preparedStatement, ByteBufferWriter writer)
5561
{
5662
var parameterCollection = command.RawParameters;
5763

5864
if (Log.IsDebugEnabled())
5965
Log.Debug("Session{0} Preparing command payload; CommandId: {1}; CommandText: {2}", command.Connection.Session.Id, preparedStatement.StatementId, command.CommandText);
6066

61-
writer.Write((byte) CommandKind.StatementExecute);
6267
writer.Write(preparedStatement.StatementId);
6368
writer.Write((byte) 0);
6469
writer.Write(1);
@@ -119,7 +124,7 @@ private void WritePreparedStatement(IMySqlCommand command, PreparedStatement pre
119124
}
120125
}
121126

122-
private void WriteStoredProcedure(IMySqlCommand command, IDictionary<string, CachedProcedure> cachedProcedures, ByteBufferWriter writer)
127+
private static void WriteStoredProcedure(IMySqlCommand command, IDictionary<string, CachedProcedure> cachedProcedures, ByteBufferWriter writer)
123128
{
124129
var parameterCollection = command.RawParameters;
125130
var cachedProcedure = cachedProcedures[command.CommandText];
@@ -182,7 +187,7 @@ private void WriteStoredProcedure(IMySqlCommand command, IDictionary<string, Cac
182187
preparer.ParseAndBindParameters(writer);
183188
}
184189

185-
private void WriteCommand(IMySqlCommand command, ByteBufferWriter writer)
190+
private static void WriteCommand(IMySqlCommand command, ByteBufferWriter writer)
186191
{
187192
var preparer = new StatementPreparer(command.CommandText, command.RawParameters, command.CreateStatementPreparerOptions());
188193
preparer.ParseAndBindParameters(writer);

src/MySqlConnector/Core/StatementPreparer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public void ParseAndBindParameters(ByteBufferWriter writer)
3333
{
3434
if (!string.IsNullOrWhiteSpace(m_commandText))
3535
{
36-
writer.Write((byte) CommandKind.Query);
3736
var parser = new ParameterSqlParser(this, writer);
3837
parser.Parse(m_commandText);
3938
}

tests/MySqlConnector.Tests/StatementPreparerTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ private static string GetParsedSql(string input, MySqlParameterCollection parame
248248
var writer = new ByteBufferWriter();
249249
preparer.ParseAndBindParameters(writer);
250250
using (var payload = writer.ToPayloadData())
251-
return Encoding.UTF8.GetString(payload.AsSpan().Slice(1));
251+
return Encoding.UTF8.GetString(payload.AsSpan());
252252
}
253253
}
254254
}

0 commit comments

Comments
 (0)