Merge pull request #273 from joncham/bug-getpid
[mono.git] / mcs / class / System.Data.Linq / src / DbMetal / Configuration / ProvidersSection.cs
1 #region MIT license\r
2 // \r
3 // MIT license\r
4 //\r
5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne\r
6 // \r
7 // Permission is hereby granted, free of charge, to any person obtaining a copy\r
8 // of this software and associated documentation files (the "Software"), to deal\r
9 // in the Software without restriction, including without limitation the rights\r
10 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r
11 // copies of the Software, and to permit persons to whom the Software is\r
12 // furnished to do so, subject to the following conditions:\r
13 // \r
14 // The above copyright notice and this permission notice shall be included in\r
15 // all copies or substantial portions of the Software.\r
16 // \r
17 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
18 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r
19 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r
20 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r
21 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r
22 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r
23 // THE SOFTWARE.\r
24 // \r
25 #endregion\r
26 using System;\r
27 using System.IO;\r
28 using System.Linq;\r
29 using System.Collections.Generic;\r
30 using System.Configuration;\r
31 using System.Text;\r
32 \r
33 namespace DbMetal.Configuration\r
34 {\r
35     /// <summary>\r
36     /// Handles the providers section.\r
37     /// Each provider is defined as follows:\r
38     ///  &lt;provider name="MySQL"      dbLinqSchemaLoader="DbLinq.MySql.MySqlSchemaLoader, DbLinq.MySql"\r
39     ///                             databaseConnection="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" />\r
40     /// </summary>\r
41     public class ProvidersSection : ConfigurationSection\r
42     {\r
43         public class ProviderElement : ConfigurationElement\r
44         {\r
45             [ConfigurationProperty("name", IsRequired = true)]\r
46             public string Name\r
47             {\r
48                 get { return (string)this["name"]; }\r
49             }\r
50 \r
51             [ConfigurationProperty("dbLinqSchemaLoader", IsRequired = true)]\r
52             public string DbLinqSchemaLoader\r
53             {\r
54                 get { return (string)this["dbLinqSchemaLoader"]; }\r
55             }\r
56 \r
57             [ConfigurationProperty("databaseConnection", IsRequired = true)]\r
58             public string DatabaseConnection\r
59             {\r
60                 get { return (string)this["databaseConnection"]; }\r
61             }\r
62 \r
63             [ConfigurationProperty("sqlDialectType", IsRequired = false)]\r
64             public string SqlDialectType\r
65             {\r
66                 get { return (string)this["sqlDialectType"]; }\r
67             }\r
68         }\r
69 \r
70         public class ProvidersCollection : ConfigurationElementCollection\r
71         {\r
72             protected override ConfigurationElement CreateNewElement()\r
73             {\r
74                 return new ProviderElement();\r
75             }\r
76 \r
77             protected override object GetElementKey(ConfigurationElement element)\r
78             {\r
79                 var provider = (ProviderElement)element;\r
80                 return provider.Name.ToLower();\r
81             }\r
82 \r
83             public ProviderElement GetProvider(string name)\r
84             {\r
85                 return (ProviderElement)BaseGet(name.ToLower());\r
86             }\r
87             public bool TryGetProvider(string name, out ProviderElement element, out string error)\r
88             {\r
89                 //use Configuration namespace to get our config\r
90                 object[] allKeys = base.BaseGetAllKeys();\r
91                 if (Array.IndexOf(allKeys, name.ToLower())<0)\r
92                 {\r
93                     string[] allKeyStrings = allKeys.OfType<string>().ToArray();\r
94                     \r
95                     element = null;\r
96                     string configFile = Path.GetFileName(typeof(Program).Assembly.Location)+ ".config";\r
97                     error = allKeys.Length == 0\r
98                         ? string.Format("There are no <provider/> entries in your {0} file.", configFile)\r
99                         : GetProvidersDescription(name, allKeyStrings.Length, configFile);\r
100                     return false;\r
101                 }\r
102                 element = (ProviderElement)BaseGet(name.ToLower());\r
103                 error = null;\r
104                 return true;\r
105             }\r
106 \r
107             private string GetProvidersDescription(string name, int numKeys, string configFile)\r
108             {\r
109                 var message = new StringBuilder();\r
110                 message.AppendFormat("Provider '{0}' not found among the {1} config entries in your {2} file.  ",\r
111                     name, numKeys, configFile);\r
112                 message.AppendLine("Valid providers include:");\r
113                 foreach (ProviderElement p in this.Cast<ProviderElement>().OrderBy(e => e.Name))\r
114                 {\r
115                     message.AppendFormat("\t{0} [{1}]",\r
116                         p.Name, p.DatabaseConnection);\r
117                     message.AppendLine();\r
118                 }\r
119                 return message.ToString();\r
120             }\r
121         }\r
122 \r
123         [ConfigurationProperty("providers", IsDefaultCollection = true)]\r
124         [ConfigurationCollection(typeof(ProviderElement), AddItemName = "provider")]\r
125         public ProvidersCollection Providers\r
126         {\r
127             get { return (ProvidersCollection)this["providers"]; }\r
128         }\r
129     }\r
130 }