Skip to content

Commit 9adfac2

Browse files
AddParam(name, DbType, direction) works on the two SQL Server commands, so an output parameter no longer sends the enum as its value (#89)
Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 98e3713 commit 9adfac2

4 files changed

Lines changed: 163 additions & 1 deletion

File tree

‎Source/MSSqlClient/ExpressiveSqlCommand.cs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,34 @@ public ExpressiveSqlCommand(
109109
{
110110
}
111111

112+
/// <summary>
113+
/// Adds a parameter of a <see cref="DbType"/> with no value, for example an output parameter: <c>AddParam("@id", DbType.Int32, ParameterDirection.Output)</c>.
114+
/// </summary>
115+
/// <remarks>
116+
/// The database type of this command is <see cref="SqlDbType"/>, so without this overload a <see cref="DbType"/> argument would be taken as the VALUE of the parameter. It is converted to the <see cref="SqlDbType"/> that the provider maps it to.
117+
/// The direction has no default on purpose: this overload is declared on the command, so it hides the inherited ones wherever it applies, and a literal 0 converts to any enum, which would turn <c>AddParam("@count", 0)</c> into a parameter with no value. A constant <c>0</c> with a direction (<c>AddParam("@n", 0, ParameterDirection.InputOutput)</c>) binds to this overload as a <see cref="DbType"/>: write <c>(object)0</c> or use a variable.
118+
/// </remarks>
119+
/// <param name="name">The name of the parameter.</param>
120+
/// <param name="type">The database type of the parameter.</param>
121+
/// <param name="direction">The direction of the parameter. An input or input-output parameter with no value is sent as a NULL of that type.</param>
122+
/// <returns>This instance for use in method chaining.</returns>
123+
/// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
124+
/// <exception cref="ArgumentException"><paramref name="name"/> is blank, or SQL Server has no type for <paramref name="type"/>.</exception>
125+
public ExpressiveSqlCommand AddParam(string name, DbType type, ParameterDirection direction)
126+
=> AddParam(name, new SqlParameter { DbType = type }.SqlDbType, direction);
127+
128+
/// <summary>
129+
/// Conditionally adds a parameter of a <see cref="DbType"/> with no value. See <see cref="AddParam(string, DbType, ParameterDirection)"/>.
130+
/// </summary>
131+
/// <param name="condition">The condition to add the param by. Only adds if true.</param>
132+
/// <param name="name">The name of the parameter.</param>
133+
/// <param name="type">The database type of the parameter.</param>
134+
/// <param name="direction">The direction of the parameter.</param>
135+
/// <returns>This instance for use in method chaining.</returns>
136+
/// <exception cref="ArgumentException">The parameter is added and <paramref name="name"/> is blank, or SQL Server has no type for <paramref name="type"/>.</exception>
137+
public ExpressiveSqlCommand AddParamIf(bool condition, string name, DbType type, ParameterDirection direction)
138+
=> condition ? AddParam(name, type, direction) : this;
139+
112140
/// <summary>
113141
/// Handles adding the list of parameters to a new command.
114142
/// </summary>

‎Source/SqlClient/ExpressiveSqlCommand.cs‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,34 @@ public ExpressiveSqlCommand(
109109
{
110110
}
111111

112+
/// <summary>
113+
/// Adds a parameter of a <see cref="DbType"/> with no value, for example an output parameter: <c>AddParam("@id", DbType.Int32, ParameterDirection.Output)</c>.
114+
/// </summary>
115+
/// <remarks>
116+
/// The database type of this command is <see cref="SqlDbType"/>, so without this overload a <see cref="DbType"/> argument would be taken as the VALUE of the parameter. It is converted to the <see cref="SqlDbType"/> that the provider maps it to.
117+
/// The direction has no default on purpose: this overload is declared on the command, so it hides the inherited ones wherever it applies, and a literal 0 converts to any enum, which would turn <c>AddParam("@count", 0)</c> into a parameter with no value. A constant <c>0</c> with a direction (<c>AddParam("@n", 0, ParameterDirection.InputOutput)</c>) binds to this overload as a <see cref="DbType"/>: write <c>(object)0</c> or use a variable.
118+
/// </remarks>
119+
/// <param name="name">The name of the parameter.</param>
120+
/// <param name="type">The database type of the parameter.</param>
121+
/// <param name="direction">The direction of the parameter. An input or input-output parameter with no value is sent as a NULL of that type.</param>
122+
/// <returns>This instance for use in method chaining.</returns>
123+
/// <exception cref="ArgumentNullException"><paramref name="name"/> is null.</exception>
124+
/// <exception cref="ArgumentException"><paramref name="name"/> is blank, or SQL Server has no type for <paramref name="type"/>.</exception>
125+
public ExpressiveSqlCommand AddParam(string name, DbType type, ParameterDirection direction)
126+
=> AddParam(name, new SqlParameter { DbType = type }.SqlDbType, direction);
127+
128+
/// <summary>
129+
/// Conditionally adds a parameter of a <see cref="DbType"/> with no value. See <see cref="AddParam(string, DbType, ParameterDirection)"/>.
130+
/// </summary>
131+
/// <param name="condition">The condition to add the param by. Only adds if true.</param>
132+
/// <param name="name">The name of the parameter.</param>
133+
/// <param name="type">The database type of the parameter.</param>
134+
/// <param name="direction">The direction of the parameter.</param>
135+
/// <returns>This instance for use in method chaining.</returns>
136+
/// <exception cref="ArgumentException">The parameter is added and <paramref name="name"/> is blank, or SQL Server has no type for <paramref name="type"/>.</exception>
137+
public ExpressiveSqlCommand AddParamIf(bool condition, string name, DbType type, ParameterDirection direction)
138+
=> condition ? AddParam(name, type, direction) : this;
139+
112140
/// <summary>
113141
/// Handles adding the list of parameters to a new command.
114142
/// </summary>

‎Tests/Open.Database.Extensions.Core.Tests/Open.Database.Extensions.Core.Tests.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<ProjectReference Include="..\..\Source\Dataflow\Open.Database.Extensions.Dataflow.csproj" />
3030
<ProjectReference Include="..\..\Source\Extensions\Open.Database.Extensions.csproj" />
3131
<ProjectReference Include="..\..\Source\SqlClient\Open.Database.Extensions.SqlClient.csproj" />
32-
<ProjectReference Include="..\..\Source\MSSqlClient\Open.Database.Extensions.MSSqlClient.csproj" />
32+
<ProjectReference Include="..\..\Source\MSSqlClient\Open.Database.Extensions.MSSqlClient.csproj" Aliases="mssql" />
3333
</ItemGroup>
3434

3535
<ItemGroup>
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
extern alias mssql;
2+
3+
namespace Open.Database.Extensions.Tests;
4+
5+
#nullable enable
6+
7+
// AddParam("@o", DbType.Int32, ParameterDirection.Output) on the two SQL Server commands. Their database type is SqlDbType, so a DbType used to bind to AddParam(string, object?, ParameterDirection)
8+
// and the enum itself became the VALUE of an output parameter. The overloads below take the DbType, as the generic commands do, and build a parameter of that type with no value.
9+
// Each case is written for both packages (System.Data.SqlClient and Microsoft.Data.SqlClient): the second is reached through an extern alias, as both define ExpressiveSqlCommand.
10+
// A command that declares an AddParam of its own hides the inherited ones that apply to the same call, so the last tests pin the calls that must keep their meaning, a literal 0 above all.
11+
[ExcludeFromCodeCoverage]
12+
public static class SqlServerParameterTypeTests
13+
{
14+
#pragma warning disable CS0618 // System.Data.SqlClient is obsolete; it is one of the two packages under test
15+
static global::Open.Database.Extensions.ExpressiveSqlCommand Legacy()
16+
=> new(new System.Data.SqlClient.SqlConnection(), CommandType.StoredProcedure, "dbo.p");
17+
#pragma warning restore CS0618
18+
19+
static mssql::Open.Database.Extensions.ExpressiveSqlCommand Microsoft()
20+
=> new(new Microsoft.Data.SqlClient.SqlConnection(), CommandType.StoredProcedure, "dbo.p");
21+
22+
static string Show(object? value)
23+
=> value is null ? "null" : value is DBNull ? "DBNull" : Convert.ToString(value, System.Globalization.CultureInfo.InvariantCulture)!;
24+
25+
// What the command holds: name=value databaseType direction, "-" when no database type was given.
26+
static string Sent(global::Open.Database.Extensions.ExpressiveSqlCommand command)
27+
=> string.Join("; ", command.Params.Select(p => $"{p.Name}={Show(p.Value)} {(p.Type.HasValue ? p.Type.Value.ToString() : "-")} {p.Direction}"));
28+
29+
static string Sent(mssql::Open.Database.Extensions.ExpressiveSqlCommand command)
30+
=> string.Join("; ", command.Params.Select(p => $"{p.Name}={Show(p.Value)} {(p.Type.HasValue ? p.Type.Value.ToString() : "-")} {p.Direction}"));
31+
32+
static string Only(bool condition, string expected) => condition ? expected : "";
33+
34+
// ---- a DbType and a direction: a parameter of that type with no value ----
35+
36+
[Fact]
37+
public static void ADbTypeAndADirection_AreATypedParameterWithNoValue()
38+
{
39+
Assert.Equal("@o=null Int Output", Sent(Legacy().AddParam("@o", DbType.Int32, ParameterDirection.Output)));
40+
Assert.Equal("@o=null Int Output", Sent(Microsoft().AddParam("@o", DbType.Int32, ParameterDirection.Output)));
41+
Assert.Equal("@o=null BigInt Output", Sent(Legacy().AddParam("@o", DbType.Int64, ParameterDirection.Output)));
42+
Assert.Equal("@o=null NVarChar Output", Sent(Microsoft().AddParam("@o", DbType.String, ParameterDirection.Output)));
43+
Assert.Equal("@o=null VarChar Output", Sent(Legacy().AddParam("@o", DbType.AnsiString, ParameterDirection.Output)));
44+
Assert.Equal("@o=null DateTime2 Output", Sent(Microsoft().AddParam("@o", DbType.DateTime2, ParameterDirection.Output)));
45+
Assert.Equal("@o=null Bit Output", Sent(Legacy().AddParam("@o", DbType.Boolean, ParameterDirection.Output)));
46+
}
47+
48+
[Fact]
49+
public static void TheOtherDirections_KeepTheirMeaning()
50+
{
51+
Assert.Equal("@r=null Int ReturnValue", Sent(Legacy().AddParam("@r", DbType.Int32, ParameterDirection.ReturnValue)));
52+
Assert.Equal("@r=null Int ReturnValue", Sent(Microsoft().AddParam("@r", DbType.Int32, ParameterDirection.ReturnValue)));
53+
54+
// An input or input-output parameter with no value is a typed NULL, as for a SqlDbType.
55+
Assert.Equal("@i=DBNull Int Input", Sent(Legacy().AddParam("@i", DbType.Int32, ParameterDirection.Input)));
56+
Assert.Equal("@io=DBNull Int InputOutput", Sent(Microsoft().AddParam("@io", DbType.Int32, ParameterDirection.InputOutput)));
57+
}
58+
59+
[Theory]
60+
[InlineData(true)]
61+
[InlineData(false)]
62+
public static void AddParamIf_AddsOnlyWhenTrue(bool condition)
63+
{
64+
Assert.Equal(Only(condition, "@o=null Int Output"), Sent(Legacy().AddParamIf(condition, "@o", DbType.Int32, ParameterDirection.Output)));
65+
Assert.Equal(Only(condition, "@o=null Int Output"), Sent(Microsoft().AddParamIf(condition, "@o", DbType.Int32, ParameterDirection.Output)));
66+
}
67+
68+
[Fact]
69+
public static void ADbTypeSqlServerHasNoTypeFor_Throws_AsTheProviderDoes()
70+
{
71+
Assert.Throws<ArgumentException>(() => Legacy().AddParam("@o", DbType.UInt16, ParameterDirection.Output));
72+
Assert.Throws<ArgumentException>(() => Microsoft().AddParam("@o", DbType.UInt16, ParameterDirection.Output));
73+
}
74+
75+
// ---- the calls that must keep their meaning: the new overload is declared on the command, so it hides the inherited overloads of any call it applies to ----
76+
// A literal 0 converts implicitly to any enum, so a DbType parameter with a default direction would take AddParam("@count", 0) and add a typed NULL where an int 0 was meant.
77+
78+
[Fact]
79+
[SuppressMessage("Roslynator", "RCS1118:Mark local variable as const", Justification = "The local is a variable on purpose: a literal 0 converts implicitly to any enum, a variable does not, and the test pins both.")]
80+
public static void AValueIsStillAValue_AboveAllALiteralZero()
81+
{
82+
int zero = 0;
83+
84+
Assert.Equal("@x=0 - Input", Sent(Legacy().AddParam("@x", 0)));
85+
Assert.Equal("@x=0 - Input", Sent(Microsoft().AddParam("@x", 0)));
86+
Assert.Equal("@x=0 - Input", Sent(Legacy().AddParam("@x", zero)));
87+
Assert.Equal("@x=0 - Input", Sent(Microsoft().AddParam("@x", zero)));
88+
Assert.Equal("@x=0 - Input", Sent(Legacy().AddParam("@x", 0L)));
89+
Assert.Equal("@x=0 Int Input", Sent(Microsoft().AddParam("@x", 0, SqlDbType.Int)));
90+
Assert.Equal("@x=0 Int Input", Sent(Legacy().AddParamIf(true, "@x", 0, SqlDbType.Int)));
91+
Assert.Equal("@x=0 - Input", Sent(Microsoft().AddParamIf(true, "@x", 0)));
92+
Assert.Equal("@x=c - Input", Sent(Legacy().AddParam("@x", 'c')));
93+
Assert.Equal("@x=c - Input", Sent(Microsoft().AddParam("@x", 'c')));
94+
Assert.Equal("@x=5 - Output", Sent(Legacy().AddParam("@x", 5, ParameterDirection.Output)));
95+
Assert.Equal("@x=a - Input", Sent(Microsoft().AddParam("@x", "a")));
96+
}
97+
98+
[Fact]
99+
public static void ASqlDbTypeIsStillASqlDbType()
100+
{
101+
Assert.Equal("@x=DBNull Int Input", Sent(Legacy().AddParam("@x", SqlDbType.Int)));
102+
Assert.Equal("@x=null Int Output", Sent(Microsoft().AddParam("@x", SqlDbType.Int, ParameterDirection.Output)));
103+
Assert.Equal("@x=5 Int Input", Sent(Legacy().AddParam("@x", 5, SqlDbType.Int)));
104+
Assert.Equal("@x=null Int ReturnValue", Sent(Microsoft().AddReturnParam("@x", SqlDbType.Int)));
105+
}
106+
}

0 commit comments

Comments
 (0)