/
SingleCommandPayloadCreator.cs
191 lines (167 loc) · 7.13 KB
/
SingleCommandPayloadCreator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.Data;
using MySql.Data.MySqlClient;
using MySqlConnector.Logging;
using MySqlConnector.Protocol;
using MySqlConnector.Protocol.Serialization;
using MySqlConnector.Utilities;
namespace MySqlConnector.Core
{
internal sealed class SingleCommandPayloadCreator : ICommandPayloadCreator
{
public static ICommandPayloadCreator Instance { get; } = new SingleCommandPayloadCreator();
public static string OutParameterSentinelColumnName => "\uE001\b";
public bool WriteQueryCommand(ref CommandListPosition commandListPosition, IDictionary<string, CachedProcedure> cachedProcedures, ByteBufferWriter writer)
{
if (commandListPosition.CommandIndex == commandListPosition.Commands.Count)
return false;
var command = commandListPosition.Commands[commandListPosition.CommandIndex];
var preparedStatements = command.TryGetPreparedStatements();
if (preparedStatements is null)
{
if (Log.IsDebugEnabled())
Log.Debug("Session{0} Preparing command payload; CommandText: {1}", command.Connection.Session.Id, command.CommandText);
if (command.CommandType == CommandType.StoredProcedure)
WriteStoredProcedure(command, cachedProcedures, writer);
else
WriteCommand(command, writer);
commandListPosition.CommandIndex++;
}
else
{
WritePreparedStatement(command, preparedStatements.Statements[commandListPosition.PreparedStatementIndex], writer);
// advance to next prepared statement or next command
if (++commandListPosition.PreparedStatementIndex == preparedStatements.Statements.Count)
{
commandListPosition.CommandIndex++;
commandListPosition.PreparedStatementIndex = 0;
}
}
return true;
}
private void WritePreparedStatement(IMySqlCommand command, PreparedStatement preparedStatement, ByteBufferWriter writer)
{
var parameterCollection = command.RawParameters;
if (Log.IsDebugEnabled())
Log.Debug("Session{0} Preparing command payload; CommandId: {1}; CommandText: {2}", command.Connection.Session.Id, preparedStatement.StatementId, command.CommandText);
writer.Write((byte) CommandKind.StatementExecute);
writer.Write(preparedStatement.StatementId);
writer.Write((byte) 0);
writer.Write(1);
if (preparedStatement.Parameters?.Length > 0)
{
// TODO: How to handle incorrect number of parameters?
// build subset of parameters for this statement
var parameters = new MySqlParameter[preparedStatement.Statement.ParameterNames.Count];
for (var i = 0; i < preparedStatement.Statement.ParameterNames.Count; i++)
{
var parameterName = preparedStatement.Statement.ParameterNames[i];
var parameterIndex = parameterName != null ? (parameterCollection?.NormalizedIndexOf(parameterName) ?? -1) : preparedStatement.Statement.ParameterIndexes[i];
if (parameterIndex == -1 && parameterName != null)
throw new MySqlException("Parameter '{0}' must be defined.".FormatInvariant(parameterName));
else if (parameterIndex < 0 || parameterIndex >= (parameterCollection?.Count ?? 0))
throw new MySqlException("Parameter index {0} is invalid when only {1} parameter{2} defined.".FormatInvariant(parameterIndex, parameterCollection?.Count ?? 0, parameterCollection?.Count == 1 ? " is" : "s are"));
parameters[i] = parameterCollection[parameterIndex];
}
// write null bitmap
byte nullBitmap = 0;
for (var i = 0; i < parameters.Length; i++)
{
var parameter = parameters[i];
if (parameter.Value is null || parameter.Value == DBNull.Value)
nullBitmap |= (byte) (1 << (i % 8));
if (i % 8 == 7)
{
writer.Write(nullBitmap);
nullBitmap = 0;
}
}
if (parameters.Length % 8 != 0)
writer.Write(nullBitmap);
// write "new parameters bound" flag
writer.Write((byte) 1);
foreach (var parameter in parameters)
{
// override explicit MySqlDbType with inferred type from the Value
var mySqlDbType = parameter.MySqlDbType;
var typeMapping = (parameter.Value is null || parameter.Value == DBNull.Value) ? null : TypeMapper.Instance.GetDbTypeMapping(parameter.Value.GetType());
if (typeMapping is object)
{
var dbType = typeMapping.DbTypes[0];
mySqlDbType = TypeMapper.Instance.GetMySqlDbTypeForDbType(dbType);
}
writer.Write(TypeMapper.ConvertToColumnTypeAndFlags(mySqlDbType, command.Connection.GuidFormat));
}
var options = command.CreateStatementPreparerOptions();
foreach (var parameter in parameters)
parameter.AppendBinary(writer, options);
}
}
private void WriteStoredProcedure(IMySqlCommand command, IDictionary<string, CachedProcedure> cachedProcedures, ByteBufferWriter writer)
{
var parameterCollection = command.RawParameters;
var cachedProcedure = cachedProcedures[command.CommandText];
if (cachedProcedure is object)
parameterCollection = cachedProcedure.AlignParamsWithDb(parameterCollection);
MySqlParameter returnParameter = null;
var outParameters = new MySqlParameterCollection();
var outParameterNames = new List<string>();
var inParameters = new MySqlParameterCollection();
var argParameterNames = new List<string>();
var inOutSetParameters = "";
for (var i = 0; i < (parameterCollection?.Count ?? 0); i++)
{
var param = parameterCollection[i];
var inName = "@inParam" + i;
var outName = "@outParam" + i;
switch (param.Direction)
{
case ParameterDirection.Input:
case ParameterDirection.InputOutput:
var inParam = param.WithParameterName(inName);
inParameters.Add(inParam);
if (param.Direction == ParameterDirection.InputOutput)
{
inOutSetParameters += $"SET {outName}={inName}; ";
goto case ParameterDirection.Output;
}
argParameterNames.Add(inName);
break;
case ParameterDirection.Output:
outParameters.Add(param);
outParameterNames.Add(outName);
argParameterNames.Add(outName);
break;
case ParameterDirection.ReturnValue:
returnParameter = param;
break;
}
}
// if a return param is set, assume it is a function; otherwise, assume stored procedure
var commandText = command.CommandText + "(" + string.Join(", ", argParameterNames) + ");";
if (returnParameter is null)
{
commandText = inOutSetParameters + "CALL " + commandText;
if (outParameters.Count > 0)
{
commandText += "SELECT '"+ OutParameterSentinelColumnName +"' AS '" + OutParameterSentinelColumnName + "', " + string.Join(", ", outParameterNames);
}
}
else
{
commandText = "SELECT " + commandText;
}
command.OutParameters = outParameters;
command.ReturnParameter = returnParameter;
var preparer = new StatementPreparer(commandText, inParameters, command.CreateStatementPreparerOptions());
preparer.ParseAndBindParameters(writer);
}
private void WriteCommand(IMySqlCommand command, ByteBufferWriter writer)
{
var preparer = new StatementPreparer(command.CommandText, command.RawParameters, command.CreateStatementPreparerOptions());
preparer.ParseAndBindParameters(writer);
}
static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager.CreateLogger(nameof(SingleCommandPayloadCreator));
}
}