@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#if NETFRAMEWORK
using System ;
using System.Collections.Generic ;
using System.Data ;
@ -12,21 +14,15 @@ using Microsoft.Data.SqlClient;
namespace Microsoft.Data.Common
{
// @TODO: Theoretically this class could be replaced with SqlConnectionString.
[Serializable] // MDAC 83147
internal sealed class DBConnectionString
internal sealed class Db ConnectionString
{
// instances of this class are intended to be immutable, i.e readonly
// used by permission classes so it is much easier to verify correctness
// when not worried about the class being modified during execution
// @TODO: Remove in favor of DbConnectionStringKeywords
private static class KEY
{
internal const string Password = DbConnectionStringKeywords . Password ;
internal const string PersistSecurityInfo = DbConnectionStringKeywords . PersistSecurityInfo ;
internal const string Pwd = DbConnectionStringSynonyms . Pwd ;
} ;
// this class is serializable with Everett, so ugly field names can't be changed
readonly private string _encryptedUsersConnectionString ;
@ -51,21 +47,21 @@ namespace Microsoft.Data.Common
readonly private string _encryptedActualConnectionString ;
#pragma warning restore 169
internal DB ConnectionString ( string value , string restrictions , KeyRestrictionBehavior behavior , Dictionary < string , string > synonyms , bool useOdbcRules )
internal Db ConnectionString ( string value , string restrictions , KeyRestrictionBehavior behavior , Dictionary < string , string > synonyms , bool useOdbcRules )
: this ( new DbConnectionOptions ( value , synonyms ) , restrictions , behavior , synonyms , false )
{
// useOdbcRules is only used to parse the connection string, not to parse restrictions because values don't apply there
// the hashtable doesn't need clone since it isn't shared with anything else
}
internal DB ConnectionString ( DbConnectionOptions connectionOptions )
internal Db ConnectionString ( DbConnectionOptions connectionOptions )
: this ( connectionOptions , ( string ) null , KeyRestrictionBehavior . AllowOnly , null , true )
{
// used by DBDataPermission to convert from DbConnectionOptions to DB ConnectionString
// used by DBDataPermission to convert from DbConnectionOptions to Db ConnectionString
// since backward compatibility requires Everett level classes
}
private DB ConnectionString ( DbConnectionOptions connectionOptions , string restrictions , KeyRestrictionBehavior behavior , Dictionary < string , string > synonyms , bool mustCloneDictionary )
private Db ConnectionString ( DbConnectionOptions connectionOptions , string restrictions , KeyRestrictionBehavior behavior , Dictionary < string , string > synonyms , bool mustCloneDictionary )
{ // used by DBDataPermission
Debug . Assert ( connectionOptions ! = null , "null connectionOptions" ) ;
switch ( behavior )
@ -101,13 +97,13 @@ namespace Microsoft.Data.Common
// serialize out with '*' so already knows what we do. Better this way
// than to treat password specially later on which causes problems.
const string star = "*" ;
if ( _parsetable . ContainsKey ( KEY . Password ) )
if ( _parsetable . ContainsKey ( DbConnectionStringKeywords . Password ) )
{
_parsetable [ KEY . Password ] = star ;
_parsetable [ DbConnectionStringKeywords . Password ] = star ;
}
if ( _parsetable . ContainsKey ( KEY . Pwd ) )
if ( _parsetable . ContainsKey ( DbConnectionStringSynonyms . Pwd ) )
{
_parsetable [ KEY . Pwd ] = star ;
_parsetable [ DbConnectionStringSynonyms . Pwd ] = star ;
}
// replace user's password/pwd value with "*" in the linked list and build a new string
@ -121,7 +117,7 @@ namespace Microsoft.Data.Common
}
}
private DBConnectionString ( DB ConnectionString connectionString , string [ ] restrictionValues , KeyRestrictionBehavior behavior )
private DbConnectionString ( Db ConnectionString connectionString , string [ ] restrictionValues , KeyRestrictionBehavior behavior )
{
// used by intersect for two equal connection strings with different restrictions
_encryptedUsersConnectionString = connectionString . _encryptedUsersConnectionString ;
@ -198,7 +194,7 @@ namespace Microsoft.Data.Common
return _parsetable . ContainsKey ( keyword ) ;
}
internal DBConnectionString Intersect ( DB ConnectionString entry )
internal DbConnectionString Intersect ( Db ConnectionString entry )
{
KeyRestrictionBehavior behavior = _behavior ;
string [ ] restrictionValues = null ;
@ -287,10 +283,10 @@ namespace Microsoft.Data.Common
}
// verify _hasPassword & _parsetable are in sync between Everett/Whidbey
Debug . Assert ( ! _hasPassword | | ContainsKey ( KEY . Password ) | | ContainsKey ( KEY . Pwd ) , "OnDeserialized password mismatch this" ) ;
Debug . Assert ( entry = = null | | ! entry . _hasPassword | | entry . ContainsKey ( KEY . Password ) | | entry . ContainsKey ( KEY . Pwd ) , "OnDeserialized password mismatch entry" ) ;
Debug . Assert ( ! _hasPassword | | ContainsKey ( DbConnectionStringKeywords . Password ) | | ContainsKey ( DbConnectionStringSynonyms . Pwd ) , "OnDeserialized password mismatch this" ) ;
Debug . Assert ( entry = = null | | ! entry . _hasPassword | | entry . ContainsKey ( DbConnectionStringKeywords . Password ) | | entry . ContainsKey ( DbConnectionStringSynonyms . Pwd ) , "OnDeserialized password mismatch entry" ) ;
DB ConnectionString value = new DB ConnectionString ( this , restrictionValues , behavior ) ;
Db ConnectionString value = new Db ConnectionString ( this , restrictionValues , behavior ) ;
ValidateCombinedSet ( this , value ) ;
ValidateCombinedSet ( entry , value ) ;
@ -298,7 +294,7 @@ namespace Microsoft.Data.Common
}
[Conditional("DEBUG")]
private void ValidateCombinedSet ( DB ConnectionString componentSet , DB ConnectionString combinedSet )
private void ValidateCombinedSet ( Db ConnectionString componentSet , Db ConnectionString combinedSet )
{
Debug . Assert ( combinedSet ! = null , "The combined connection string should not be null" ) ;
if ( ( componentSet ! = null ) & & ( combinedSet . _restrictionValues ! = null ) & & ( componentSet . _restrictionValues ! = null ) )
@ -371,10 +367,10 @@ namespace Microsoft.Data.Common
return ( _restrictionValues = = null | | ( 0 > Array . BinarySearch ( _restrictionValues , key , StringComparer . Ordinal ) ) ) ;
}
internal bool IsSupersetOf ( DB ConnectionString entry )
internal bool IsSupersetOf ( Db ConnectionString entry )
{
Debug . Assert ( ! _hasPassword | | ContainsKey ( KEY . Password ) | | ContainsKey ( KEY . Pwd ) , "OnDeserialized password mismatch this" ) ;
Debug . Assert ( ! entry . _hasPassword | | entry . ContainsKey ( KEY . Password ) | | entry . ContainsKey ( KEY . Pwd ) , "OnDeserialized password mismatch entry" ) ;
Debug . Assert ( ! _hasPassword | | ContainsKey ( DbConnectionStringKeywords . Password ) | | ContainsKey ( DbConnectionStringSynonyms . Pwd ) , "OnDeserialized password mismatch this" ) ;
Debug . Assert ( ! entry . _hasPassword | | entry . ContainsKey ( DbConnectionStringKeywords . Password ) | | entry . ContainsKey ( DbConnectionStringSynonyms . Pwd ) , "OnDeserialized password mismatch entry" ) ;
switch ( _behavior )
{
@ -481,10 +477,10 @@ namespace Microsoft.Data.Common
return restrictionValues ;
}
private static string [ ] ParseRestrictions ( string restrictions , Dictionary < string , string > synonyms )
private static string [ ] ParseRestrictions ( string restrictions , IReadOnly Dictionary< string , string > synonyms )
{
#if DEBUG
SqlClientEventSource . Log . TryAdvancedTraceEvent ( "<comm.DB ConnectionString|INFO|ADV> Restrictions='{0}'" , restrictions ) ;
SqlClientEventSource . Log . TryAdvancedTraceEvent ( "<comm.Db ConnectionString|INFO|ADV> Restrictions='{0}'" , restrictions ) ;
#endif
List < string > restrictionValues = new List < string > ( ) ;
StringBuilder buffer = new StringBuilder ( restrictions . Length ) ;
@ -500,7 +496,7 @@ namespace Microsoft.Data.Common
if ( ! string . IsNullOrEmpty ( keyname ) )
{
#if DEBUG
SqlClientEventSource . Log . TryAdvancedTraceEvent ( "<comm.DB ConnectionString|INFO|ADV> KeyName='{0}'" , keyname ) ;
SqlClientEventSource . Log . TryAdvancedTraceEvent ( "<comm.Db ConnectionString|INFO|ADV> KeyName='{0}'" , keyname ) ;
#endif
string realkeyname = synonyms ! = null ? ( string ) synonyms [ keyname ] : keyname ; // MDAC 85144
if ( string . IsNullOrEmpty ( realkeyname ) )
@ -568,3 +564,5 @@ namespace Microsoft.Data.Common
}
}
}
#endif