Skip to content

Commit 82e60e4

Browse files
committed
Remove unnecessary validation logic (and covering test)
1 parent af251dc commit 82e60e4

File tree

4 files changed

+6
-53
lines changed

4 files changed

+6
-53
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ protected override void AfterCleared(SqlCommand owner)
114114
}
115115

116116
private string _commandText;
117-
internal CommandType _commandType;
117+
private CommandType _commandType;
118118
private int? _commandTimeout;
119119
private UpdateRowSource _updatedRowSource = UpdateRowSource.Both;
120120
private bool _designTimeInvisible;

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ protected override void AfterCleared(SqlCommand owner)
119119
}
120120

121121
private string _commandText;
122-
internal CommandType _commandType;
122+
private CommandType _commandType;
123123
private int? _commandTimeout;
124124
private UpdateRowSource _updatedRowSource = UpdateRowSource.Both;
125125
private bool _designTimeInvisible;

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCommandSet.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -98,24 +98,16 @@ internal void Append(SqlCommand command)
9898
{
9999
ADP.CheckArgumentNull(command, nameof(command));
100100
SqlClientEventSource.Log.TryTraceEvent("SqlCommandSet.Append | API | Object Id {0}, Command '{1}', Parameter Count {2}", ObjectID, command.ObjectID, command.Parameters.Count);
101+
102+
// SqlCommandSet only supports commands of type Text or StoredProcedure. This aligns with SqlCommand (validated by its CommandType setter.)
103+
Debug.Assert(command.CommandType is CommandType.Text or CommandType.StoredProcedure);
104+
101105
string cmdText = command.CommandText;
102106
if (string.IsNullOrEmpty(cmdText))
103107
{
104108
throw ADP.CommandTextRequired(nameof(Append));
105109
}
106110

107-
CommandType commandType = command.CommandType;
108-
switch (commandType)
109-
{
110-
case CommandType.Text:
111-
case CommandType.StoredProcedure:
112-
break;
113-
case CommandType.TableDirect:
114-
throw SQL.NotSupportedCommandType(commandType);
115-
default:
116-
throw ADP.InvalidCommandType(commandType);
117-
}
118-
119111
SqlParameterCollection parameters = null;
120112

121113
SqlParameterCollection collection = command.Parameters;

src/Microsoft.Data.SqlClient/tests/UnitTests/Microsoft/Data/SqlClient/SqlCommandSetTest.cs

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,6 @@ public void AppendCommandWithEmptyString_Throws()
4242
Assert.Contains("CommandText property has not been initialized", ex.Message, StringComparison.OrdinalIgnoreCase);
4343
}
4444

45-
/// <summary>
46-
/// Returns a set of invalid CommandType values.
47-
/// </summary>
48-
/// <see cref="AppendBadCommandType_Throws(CommandType)"/>
49-
/// <remarks>
50-
/// .NET Framework puts system enums in the Global Assembly Cache (GAC), and xUnit refuses to serialize enums that live there.
51-
/// We make these system enum values a method, then disable enumeration of the test data to avoid warnings on the console when running tests.
52-
/// </remarks>
53-
public static TheoryData<CommandType> CommandTypeData()
54-
=> new(CommandType.TableDirect, (CommandType)5);
55-
56-
/// <summary>
57-
/// Verifies that adding a SqlCommand with an invalid CommandType to a SqlCommandSet throws an ArgumentOutOfRangeException.
58-
/// </summary>
59-
[Theory]
60-
[MemberData(
61-
nameof(CommandTypeData)
62-
#if NETFRAMEWORK
63-
, DisableDiscoveryEnumeration = true
64-
#endif
65-
)]
66-
public void AppendBadCommandType_Throws(CommandType commandType)
67-
{
68-
SqlCommandSet cmdSet = new();
69-
using SqlCommand cmd = GenerateBadCommand(commandType);
70-
71-
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(() => cmdSet.Append(cmd));
72-
Assert.Contains("CommandType", ex.Message, StringComparison.OrdinalIgnoreCase);
73-
}
74-
7545
/// <summary>
7646
/// Verifies that adding a SqlCommand containing a SqlParameter with an invalid name to a SqlCommandSet throws an ArgumentException.
7747
/// </summary>
@@ -164,13 +134,4 @@ public void NotSupportedCommandBehaviorValidateCommandBehavior_Throws()
164134
ArgumentOutOfRangeException ex = Assert.Throws<ArgumentOutOfRangeException>(() => cmdSet.ValidateCommandBehavior("ExecuteNonQuery", CommandBehavior.KeyInfo));
165135
Assert.Contains("not supported", ex.Message, StringComparison.OrdinalIgnoreCase);
166136
}
167-
168-
private static SqlCommand GenerateBadCommand(CommandType cType)
169-
{
170-
SqlCommand cmd = new("Test");
171-
// There's validation done on the CommandType property, but we need to create one that avoids the check for the test case.
172-
cmd._commandType = cType;
173-
174-
return cmd;
175-
}
176137
}

0 commit comments

Comments
 (0)