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