* Makefile: Build the make-map.exe in Mono.Unix.Native; add /nowarn:0618 to
[mono.git] / mcs / class / System.Data / System.Data.Common / DbProviderFactories.cs
1 //
2 // System.Data.Common.DbProviderFactories.cs
3 //
4 // Author:
5 //   Sureshkumar T (tsureshkumar@novell.com)
6 //   Tim Coleman (tim@timcoleman.com)
7 //
8 // Copyright (C) Tim Coleman, 2003
9 //
10
11 //
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
13 //
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
21 // 
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
24 // 
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 //
33
34 #if NET_2_0
35
36 using System.Threading;
37 using System.Reflection;
38 using System.Collections;
39 using System.Configuration;
40
41 namespace System.Data.Common {
42         public sealed class DbProviderFactories
43         {
44                 private static object configEntries = null; // DataSet
45
46                 private const string CONFIG_SECTION_NAME        = "system.data";
47                 private const string CONFIG_SEC_TABLE_NAME      = "DbProviderFactories";
48                 
49                 #region Constructors
50                 private DbProviderFactories ()
51                 {
52                 }
53                 #endregion Constructors
54
55                 #region Methods
56
57                 public static DbProviderFactory GetFactory (DataRow providerRow)
58                 {
59                         string assemblyType = (string) providerRow ["AssemblyQualifiedName"];
60                         Type type = Type.GetType (assemblyType, false, true);
61                         if (type != null && type.IsSubclassOf (typeof (DbProviderFactory))) {
62                                 // Provider factories are singletons with Instance field having
63                                 // the sole instance
64                                 FieldInfo field = type.GetField ("Instance", BindingFlags.Public |
65                                                                  BindingFlags.Static);
66                                 if (field != null) {
67                                         return field.GetValue (null) as DbProviderFactory;
68                                 }
69                                 
70                         }
71                         
72                         throw new ConfigurationException("DataProvider is missing!");
73                 }
74
75                 public static DbProviderFactory GetFactory (string providerInvariantName)
76                 {
77                         DataTable table = GetFactoryClasses ();
78                         if (table != null) {
79                                 DataRow row = table.Rows.Find (providerInvariantName);
80                                 if (row != null)
81                                         return GetFactory (row);
82                         }
83                         throw new ConfigurationException ("DataProvider is not found!");
84                 }
85
86                 public static DataTable GetFactoryClasses ()
87                 {
88                         DataSet ds = GetConfigEntries ();
89                         DataTable table = ds != null ? ds.Tables [CONFIG_SEC_TABLE_NAME] : null;
90                         if (table != null)
91                                 table = table.Copy (); // avoid modifications by user
92                         return table;
93                 }
94
95                 #endregion // Methods
96
97                 #region Internal Methods
98                 internal static DataSet GetConfigEntries ()
99                 {
100                         
101                         if (configEntries != null)
102                                 return configEntries as DataSet;
103                         
104                         DataSet ds = (DataSet) ConfigurationSettings.GetConfig (CONFIG_SECTION_NAME);
105                         Interlocked.CompareExchange (ref configEntries, ds, null);
106                         return configEntries as DataSet;
107                 }
108                 #endregion Internal Methods
109         }
110 }
111
112 #endif // NET_2_0