Skip to content

Commit

Permalink
Merged PR 4029: [5.1.2] Fix | Adding non-string support to SqlConnect…
Browse files Browse the repository at this point in the history
…ionStringBuilder property indexer

Ports [#2018](#2018)
  • Loading branch information
DavoudEshtehari committed Oct 17, 2023
1 parent c5dc895 commit f85aa55
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -816,10 +816,18 @@ internal static SqlConnectionEncryptOption ConvertToSqlConnectionEncryptOption(s
{
return DbConnectionStringDefaults.Encrypt;
}
else if(value is SqlConnectionEncryptOption eValue)
{
return eValue;
}
else if (value is string sValue)
{
return SqlConnectionEncryptOption.Parse(sValue);
}
else if(value is bool bValue)
{
return SqlConnectionEncryptOption.Parse(bValue);
}

throw ADP.InvalidConnectionOptionValue(keyword);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public static SqlConnectionEncryptOption Parse(string value)
}
}

internal static SqlConnectionEncryptOption Parse(bool value)
{
return value ? Mandatory : Optional;
}

/// <include file='../../../../../../doc/snippets/Microsoft.Data.SqlClient/SqlConnectionEncryptOption.xml' path='docs/members[@name="SqlConnectionEncryptOption"]/TryParse/*' />
public static bool TryParse(string value, out SqlConnectionEncryptOption result)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ public void SetInvalidPacketSize_Throws(int invalid)
}

[Theory]
[InlineData("AttachDBFilename","somefile.db")]
[InlineData("AttachDBFilename", "somefile.db")]
public void SetKeyword(string keyword, string value)
{
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
Expand Down Expand Up @@ -387,7 +387,7 @@ public void ConnectionBuilderEncryptBackwardsCompatibility()
builder.Encrypt = false;
Assert.Equal("Encrypt=False", builder.ConnectionString);
Assert.False(builder.Encrypt);

builder.Encrypt = true;
Assert.Equal("Encrypt=True", builder.ConnectionString);
Assert.True(builder.Encrypt);
Expand All @@ -409,6 +409,18 @@ public void ConnectionBuilderEncryptBackwardsCompatibility()
Assert.True(builder.Encrypt);
}

[Fact]
public void EncryptParserValidValuesPropertyIndexerForEncryptionOption()
{
SqlConnectionStringBuilder builder = new();
builder["Encrypt"] = SqlConnectionEncryptOption.Strict;
CheckEncryptType(builder, SqlConnectionEncryptOption.Strict);
builder["Encrypt"] = SqlConnectionEncryptOption.Optional;
CheckEncryptType(builder, SqlConnectionEncryptOption.Optional);
builder["Encrypt"] = SqlConnectionEncryptOption.Mandatory;
CheckEncryptType(builder, SqlConnectionEncryptOption.Mandatory);
}

[Theory]
[InlineData("true", "True")]
[InlineData("mandatory", "True")]
Expand All @@ -420,6 +432,16 @@ public void ConnectionBuilderEncryptBackwardsCompatibility()
public void EncryptParserValidValuesParsesSuccessfully(string value, string expectedValue)
=> Assert.Equal(expectedValue, SqlConnectionEncryptOption.Parse(value).ToString());

[Theory]
[InlineData(true)]
[InlineData(false)]
public void EncryptParserValidValuesPropertyIndexerForBoolean(bool value)
{
SqlConnectionStringBuilder builder = new();
builder["Encrypt"] = value;
CheckEncryptType(builder, value ? SqlConnectionEncryptOption.Mandatory : SqlConnectionEncryptOption.Optional);
}

[Theory]
[InlineData("something")]
[InlineData("")]
Expand Down Expand Up @@ -627,5 +649,11 @@ internal void ExecuteConnectionStringTests(string connectionString)
Assert.NotNull(connection);
}
}

internal static void CheckEncryptType(SqlConnectionStringBuilder builder, SqlConnectionEncryptOption expectedValue)
{
Assert.IsType<SqlConnectionEncryptOption>(builder.Encrypt);
Assert.Equal(expectedValue, builder.Encrypt);
}
}
}

0 comments on commit f85aa55

Please sign in to comment.