5 // Copyright (c) 2007-2008 Jiri Moudry, Pascal Craponne
\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
14 // The above copyright notice and this permission notice shall be included in
\r
15 // all copies or substantial portions of the Software.
\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
29 using System.Collections.Generic;
\r
30 using System.Configuration;
\r
33 namespace DbMetal.Configuration
\r
36 /// Handles the providers section.
\r
37 /// Each provider is defined as follows:
\r
38 /// <provider name="MySQL" dbLinqSchemaLoader="DbLinq.MySql.MySqlSchemaLoader, DbLinq.MySql"
\r
39 /// databaseConnection="MySql.Data.MySqlClient.MySqlConnection, MySql.Data" />
\r
41 public class ProvidersSection : ConfigurationSection
\r
43 public class ProviderElement : ConfigurationElement
\r
45 [ConfigurationProperty("name", IsRequired = true)]
\r
48 get { return (string)this["name"]; }
\r
51 [ConfigurationProperty("dbLinqSchemaLoader", IsRequired = true)]
\r
52 public string DbLinqSchemaLoader
\r
54 get { return (string)this["dbLinqSchemaLoader"]; }
\r
57 [ConfigurationProperty("databaseConnection", IsRequired = true)]
\r
58 public string DatabaseConnection
\r
60 get { return (string)this["databaseConnection"]; }
\r
63 [ConfigurationProperty("sqlDialectType", IsRequired = false)]
\r
64 public string SqlDialectType
\r
66 get { return (string)this["sqlDialectType"]; }
\r
70 public class ProvidersCollection : ConfigurationElementCollection
\r
72 protected override ConfigurationElement CreateNewElement()
\r
74 return new ProviderElement();
\r
77 protected override object GetElementKey(ConfigurationElement element)
\r
79 var provider = (ProviderElement)element;
\r
80 return provider.Name.ToLower();
\r
83 public ProviderElement GetProvider(string name)
\r
85 return (ProviderElement)BaseGet(name.ToLower());
\r
87 public bool TryGetProvider(string name, out ProviderElement element, out string error)
\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
93 string[] allKeyStrings = allKeys.OfType<string>().ToArray();
\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
102 element = (ProviderElement)BaseGet(name.ToLower());
\r
107 private string GetProvidersDescription(string name, int numKeys, string configFile)
\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
115 message.AppendFormat("\t{0} [{1}]",
\r
116 p.Name, p.DatabaseConnection);
\r
117 message.AppendLine();
\r
119 return message.ToString();
\r
123 [ConfigurationProperty("providers", IsDefaultCollection = true)]
\r
124 [ConfigurationCollection(typeof(ProviderElement), AddItemName = "provider")]
\r
125 public ProvidersCollection Providers
\r
127 get { return (ProvidersCollection)this["providers"]; }
\r