Skip to content

Commit fbec126

Browse files
committed
Respect CommandBehavior.SchemaOnly. Fixes #723
1 parent 52f8183 commit fbec126

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

src/MySqlConnector/Core/SingleCommandPayloadCreator.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private static bool WriteStoredProcedure(IMySqlCommand command, IDictionary<stri
173173
if (returnParameter is null)
174174
{
175175
commandText = inOutSetParameters + "CALL " + commandText;
176-
if (outParameters.Count > 0)
176+
if (outParameters.Count > 0 && (command.CommandBehavior & CommandBehavior.SchemaOnly) == 0)
177177
{
178178
commandText += "SELECT '" + OutParameterSentinelColumnName + "' AS '" + OutParameterSentinelColumnName + "', " + string.Join(", ", outParameterNames);
179179
}
@@ -191,17 +191,21 @@ private static bool WriteStoredProcedure(IMySqlCommand command, IDictionary<stri
191191

192192
private static bool WriteCommand(IMySqlCommand command, ByteBufferWriter writer)
193193
{
194+
var isSchemaOnly = (command.CommandBehavior & CommandBehavior.SchemaOnly) != 0;
194195
var isSingleRow = (command.CommandBehavior & CommandBehavior.SingleRow) != 0;
195-
if (isSingleRow)
196-
writer.Write(SetSqlSelectLimit);
196+
if (isSchemaOnly)
197+
writer.Write(SetSqlSelectLimit0);
198+
else if (isSingleRow)
199+
writer.Write(SetSqlSelectLimit1);
197200
var preparer = new StatementPreparer(command.CommandText!, command.RawParameters, command.CreateStatementPreparerOptions());
198201
var isComplete = preparer.ParseAndBindParameters(writer);
199-
if (isComplete && isSingleRow)
202+
if (isComplete && (isSchemaOnly || isSingleRow))
200203
writer.Write(ClearSqlSelectLimit);
201204
return isComplete;
202205
}
203206

204-
static ReadOnlySpan<byte> SetSqlSelectLimit => new byte[] { 83, 69, 84, 32, 115, 113, 108, 95, 115, 101, 108, 101, 99, 116, 95, 108, 105, 109, 105, 116, 61, 49, 59, 10 }; // SET sql_select_limit=1;\n
207+
static ReadOnlySpan<byte> SetSqlSelectLimit0 => new byte[] { 83, 69, 84, 32, 115, 113, 108, 95, 115, 101, 108, 101, 99, 116, 95, 108, 105, 109, 105, 116, 61, 48, 59, 10 }; // SET sql_select_limit=0;\n
208+
static ReadOnlySpan<byte> SetSqlSelectLimit1 => new byte[] { 83, 69, 84, 32, 115, 113, 108, 95, 115, 101, 108, 101, 99, 116, 95, 108, 105, 109, 105, 116, 61, 49, 59, 10 }; // SET sql_select_limit=1;\n
205209
static ReadOnlySpan<byte> ClearSqlSelectLimit => new byte[] { 10, 83, 69, 84, 32, 115, 113, 108, 95, 115, 101, 108, 101, 99, 116, 95, 108, 105, 109, 105, 116, 61, 100, 101, 102, 97, 117, 108, 116, 59 }; // \nSET sql_select_limit=default;
206210
static readonly IMySqlConnectorLogger Log = MySqlConnectorLogManager.CreateLogger(nameof(SingleCommandPayloadCreator));
207211
}

tests/SideBySide/DataTypes.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,8 @@ private void DoGetSchemaTable(string column, string table, MySqlDbType mySqlDbTy
10961096
command.CommandText = $"select `{column}` from `{table}`;";
10971097
command.Prepare();
10981098

1099-
using var reader = command.ExecuteReader();
1099+
using var reader = command.ExecuteReader(CommandBehavior.SchemaOnly);
1100+
Assert.False(reader.Read());
11001101
var schemaTable = reader.GetSchemaTable();
11011102
Assert.Single(schemaTable.Rows);
11021103
var schema = schemaTable.Rows[0];

0 commit comments

Comments
 (0)