2010-03-12 Jb Evain <jbevain@novell.com>
[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.Linq;\r
28 using System.Collections.Generic;\r
29 using System.Configuration;\r
30 \r
31 namespace DbMetal.Configuration\r
32 {\r
33     /// <summary>\r
34     /// Handles the providers section.\r
35     /// Each provider is defined as follows:\r
36     ///  &lt;provider name="MySQL"      dbLinqSchemaLoader="DbLinq.MySql.MySqlSchemaLoader, DbLinq.MySql"\r
37     ///                             databaseConnection="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" />\r
38     /// </summary>\r
39     public class ProvidersSection : ConfigurationSection\r
40     {\r
41         public class ProviderElement : ConfigurationElement\r
42         {\r
43             [ConfigurationProperty("name", IsRequired = true)]\r
44             public string Name\r
45             {\r
46                 get { return (string)this["name"]; }\r
47             }\r
48 \r
49             [ConfigurationProperty("dbLinqSchemaLoader", IsRequired = true)]\r
50             public string DbLinqSchemaLoader\r
51             {\r
52                 get { return (string)this["dbLinqSchemaLoader"]; }\r
53             }\r
54 \r
55             [ConfigurationProperty("databaseConnection", IsRequired = true)]\r
56             public string DatabaseConnection\r
57             {\r
58                 get { return (string)this["databaseConnection"]; }\r
59             }\r
60 \r
61             [ConfigurationProperty("sqlDialectType", IsRequired = false)]\r
62             public string SqlDialectType\r
63             {\r
64                 get { return (string)this["sqlDialectType"]; }\r
65             }\r
66         }\r
67 \r
68         public class ProvidersCollection : ConfigurationElementCollection\r
69         {\r
70             protected override ConfigurationElement CreateNewElement()\r
71             {\r
72                 return new ProviderElement();\r
73             }\r
74 \r
75             protected override object GetElementKey(ConfigurationElement element)\r
76             {\r
77                 var provider = (ProviderElement)element;\r
78                 return provider.Name.ToLower();\r
79             }\r
80 \r
81             public ProviderElement GetProvider(string name)\r
82             {\r
83                 return (ProviderElement)BaseGet(name.ToLower());\r
84             }\r
85             public bool TryGetProvider(string name, out ProviderElement element, out string error)\r
86             {\r
87                 //use Configuration namespace to get our config\r
88                 object[] allKeys = base.BaseGetAllKeys();\r
89                 if (Array.IndexOf(allKeys, name.ToLower())<0)\r
90                 {\r
91                     string[] allKeyStrings = allKeys.OfType<string>().ToArray();\r
92                     \r
93                     element = null;\r
94                     error = allKeys.Length == 0 \r
95                         ? "There are no <provider> entries in your app.config"\r
96                         : "Key " + name + " not found among " + allKeys.Length + " config entries {" + string.Join(",",allKeyStrings) + "}";\r
97                     return false;\r
98                 }\r
99                 element = (ProviderElement)BaseGet(name.ToLower());\r
100                 error = null;\r
101                 return true;\r
102             }\r
103         }\r
104 \r
105         [ConfigurationProperty("providers", IsDefaultCollection = true)]\r
106         [ConfigurationCollection(typeof(ProviderElement), AddItemName = "provider")]\r
107         public ProvidersCollection Providers\r
108         {\r
109             get { return (ProvidersCollection)this["providers"]; }\r
110         }\r
111     }\r
112 }