Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Data / System / Data / SqlClient / SqlConnectionPoolKey.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="ConnectionPoolKey.cs" company="Microsoft">
3 //      Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">Microsoft</owner>
6 // <owner current="true" primary="false">Microsoft</owner>
7 //------------------------------------------------------------------------------
8
9 namespace System.Data.SqlClient
10 {
11
12     using System;
13     using System.Collections;
14     using System.Data.Common;
15     using System.Diagnostics;
16
17     // SqlConnectionPoolKey: Implementation of a key to connection pool groups for specifically to be used for SqlConnection
18     //  Connection string and SqlCredential are used as a key
19     internal class SqlConnectionPoolKey : DbConnectionPoolKey, ICloneable
20     {
21         private SqlCredential _credential;
22         private int _hashValue;
23         private readonly string _accessToken;
24
25         internal SqlConnectionPoolKey(string connectionString, SqlCredential credential, string accessToken) : base(connectionString)
26         {
27             Debug.Assert(_credential == null || _accessToken == null, "Credential and AccessToken can't have the value at the same time.");
28             _credential = credential;
29             _accessToken = accessToken;
30             CalculateHashCode();
31         }
32
33         private SqlConnectionPoolKey(SqlConnectionPoolKey key) : base (key)
34         {
35              _credential = key.Credential;
36              _accessToken = key.AccessToken;
37              CalculateHashCode();
38         }
39
40         object ICloneable.Clone()
41         {
42             return new SqlConnectionPoolKey(this);
43         }
44
45         internal override string ConnectionString
46         {
47             get
48             {
49                 return base.ConnectionString;
50             }
51
52             set
53             {
54                 base.ConnectionString = value;
55                 CalculateHashCode();
56             }
57         }
58
59         internal SqlCredential Credential
60         {
61             get
62             {
63                 return _credential;
64             }
65         }
66
67         internal string AccessToken
68         {
69             get
70             {
71                 return _accessToken;
72             }
73         }
74
75         public override bool Equals(object obj)
76         {
77             SqlConnectionPoolKey key = obj as SqlConnectionPoolKey;
78
79             return (key != null && _credential == key._credential && ConnectionString == key.ConnectionString && Object.ReferenceEquals(_accessToken, key._accessToken));
80         }
81
82         public override int GetHashCode()
83         {
84             return _hashValue;
85         }
86
87         private void CalculateHashCode()
88         {
89             _hashValue = base.GetHashCode();
90
91             if (_credential != null)
92             {
93                 unchecked
94                 {
95                     _hashValue = _hashValue * 17 + _credential.GetHashCode();
96                 }
97             }
98             else if (_accessToken != null)
99             {
100                 unchecked
101                 {
102                     _hashValue = _hashValue * 17 + _accessToken.GetHashCode();
103                 }
104             }
105         }
106     }
107 }