Updates referencesource to .NET 4.7
[mono.git] / mcs / class / referencesource / System.Web.Entity.Design / System / Data / WebControls / Design / EntityConnectionStringBuilderItem.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="EntityConnectionStringBuilderItem.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 //
6 // @owner       Microsoft
7 // @backupOwner Microsoft
8 //------------------------------------------------------------------------------
9
10 using System.Data.EntityClient;
11 using System.Diagnostics;
12
13 namespace System.Web.UI.Design.WebControls
14 {
15     internal class EntityConnectionStringBuilderItem : IComparable<EntityConnectionStringBuilderItem>
16     {
17         // Only one of the following should be set. This is enforced through the constructors and the fact that these fields are readonly.
18         private readonly EntityConnectionStringBuilder _connectionStringBuilder;
19         private readonly string _unknownConnectionString; // used when the string cannot be loaded into a connection string builder or is missing some required keywords
20
21         internal EntityConnectionStringBuilderItem(EntityConnectionStringBuilder connectionStringBuilder)
22         {
23             // empty connection string builder is allowed, but not null
24             Debug.Assert(connectionStringBuilder != null, "null connectionStringBuilder");
25
26             _connectionStringBuilder = connectionStringBuilder;
27         }
28
29         internal EntityConnectionStringBuilderItem(string unknownConnectionString)
30         {
31             // empty is not allowed -- use the constructor that takes a builder if the string is empty
32             Debug.Assert(!String.IsNullOrEmpty(unknownConnectionString), "null or empty unknownConnectionString");
33             _unknownConnectionString = unknownConnectionString;
34         }
35
36         internal string ConnectionString
37         {
38             get
39             {
40                 if (_connectionStringBuilder != null)
41                 {
42                     return _connectionStringBuilder.ConnectionString;
43                 }
44                 else
45                 {
46                     return _unknownConnectionString;
47                 }
48             }
49         }
50
51         internal EntityConnectionStringBuilder EntityConnectionStringBuilder
52         {
53             get
54             {
55                 return _connectionStringBuilder;
56             }
57         }
58
59         internal bool IsEmpty
60         {
61             get
62             {
63                 return String.IsNullOrEmpty(this.ConnectionString);
64             }
65         }
66
67         internal bool IsNamedConnection
68         {
69             get
70             {
71                 if (_connectionStringBuilder != null)
72                 {
73                     return !String.IsNullOrEmpty(_connectionStringBuilder.Name);
74                 }
75                 else
76                 {
77                     // if the connection string is not recognized by a EntityConnectionStringBuilder, it can't be a valid named connection
78                     return false;
79                 }
80             }
81         }
82         
83         public override string ToString()
84         {
85             // Display just the name for named connections, but the full connection string otherwise
86             if (_connectionStringBuilder != null)
87             {
88                 if (!String.IsNullOrEmpty(_connectionStringBuilder.Name))
89                 {
90                     return _connectionStringBuilder.Name;
91                 }
92                 else
93                 {
94                     return _connectionStringBuilder.ConnectionString;
95                 }
96             }
97             else
98             {
99                 return _unknownConnectionString;
100             }
101         }
102
103         int IComparable<EntityConnectionStringBuilderItem>.CompareTo(EntityConnectionStringBuilderItem other)
104         {
105             return (String.Compare(this.ToString(), other.ToString(), StringComparison.OrdinalIgnoreCase));
106         }
107     }
108 }