Merge pull request #487 from mayerwin/patch-1
[mono.git] / mcs / class / System.Data / System.Data.Common / DbProviderFactories.cs
index 308d048d83aac95f47cd3916dff72427acbb0614..63c9a560ce21bd1050ba00714ff1203becc7c816 100644 (file)
@@ -31,8 +31,6 @@
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
-#if NET_2_0
-
 using System.Threading;
 using System.Reflection;
 using System.Collections;
@@ -41,70 +39,82 @@ using System.Configuration;
 namespace System.Data.Common {
        public static class DbProviderFactories
        {
-               private static object configEntries = null; // DataSet
+               private static object configEntries; // DataSet
+
+               internal const string CONFIG_SECTION_NAME        = "system.data";
+               internal const string CONFIG_SEC_TABLE_NAME      = "DbProviderFactories";
 
-                private const string CONFIG_SECTION_NAME        = "system.data";
-                private const string CONFIG_SEC_TABLE_NAME      = "DbProviderFactories";
-                
                #region Methods
 
                public static DbProviderFactory GetFactory (DataRow providerRow)
                {
-                        string assemblyType = (string) providerRow ["AssemblyQualifiedName"];
-#if TARGET_JVM // case insensitive GetType is not supported\r
-                        Type type = Type.GetType (assemblyType, false);
+                       string assemblyType = (string) providerRow ["AssemblyQualifiedName"];
+#if TARGET_JVM // case insensitive GetType is not supported
+                       Type type = Type.GetType (assemblyType, false);
 #else
-                        Type type = Type.GetType (assemblyType, false, true);
-#endif\r
-                        if (type != null && type.IsSubclassOf (typeof (DbProviderFactory))) {
-                                // Provider factories are singletons with Instance field having
-                                // the sole instance
-                                FieldInfo field = type.GetField ("Instance", BindingFlags.Public |
-                                                                 BindingFlags.Static);
-                                if (field != null) {
-                                        return field.GetValue (null) as DbProviderFactory;
-                                }
-                                
-                        }
-                        
-                        throw new ConfigurationException("DataProvider is missing!");
+                       Type type = Type.GetType (assemblyType, false, true);
+#endif
+                       if (type != null && type.IsSubclassOf (typeof (DbProviderFactory))) {
+                               // Provider factories are singletons with Instance field having
+                               // the sole instance
+                               FieldInfo field = type.GetField ("Instance", BindingFlags.Public |
+                                       BindingFlags.Static);
+                               if (field != null) {
+                                       return field.GetValue (null) as DbProviderFactory;
+                               }
+                       }
+
+                       throw new ConfigurationErrorsException("Failed to find or load the registered .Net Framework Data Provider.");
                }
 
                public static DbProviderFactory GetFactory (string providerInvariantName)
                {
-                        DataTable table = GetFactoryClasses ();
-                        if (table != null) {
-                                DataRow row = table.Rows.Find (providerInvariantName);
-                                if (row != null)
-                                        return GetFactory (row);
-                        }
-                        throw new ConfigurationException ("DataProvider is not found!");
+                       if (providerInvariantName == null)
+                               throw new ArgumentNullException ("providerInvariantName");
+
+                       DataTable table = GetFactoryClasses ();
+                       if (table != null) {
+                               DataRow row = table.Rows.Find (providerInvariantName);
+                               if (row != null)
+                                       return GetFactory (row);
+                       }
+
+                       throw new ConfigurationErrorsException (String.Format("Failed to find or load the registered .Net Framework Data Provider '{0}'.", providerInvariantName));
+               }
+               
+#if NET_4_5
+               public static DbProviderFactory GetFactory (DbConnection connection)
+               {
+                       if (connection == null)
+                               throw new ArgumentNullException ("connection");
+
+                       return connection.DbProviderFactory;
                }
+#endif
 
                public static DataTable GetFactoryClasses ()
                {
-                        DataSet ds = GetConfigEntries ();
-                        DataTable table = ds != null ? ds.Tables [CONFIG_SEC_TABLE_NAME] : null;
-                        if (table != null)
-                                table = table.Copy (); // avoid modifications by user
-                        return table;
+                               DataSet ds = GetConfigEntries ();
+                               DataTable table = ds != null ? ds.Tables [CONFIG_SEC_TABLE_NAME] : null;
+                               if (table != null)
+                                       table = table.Copy (); // avoid modifications by user
+                               return table;
                }
 
                #endregion // Methods
 
                #region Internal Methods
+
                internal static DataSet GetConfigEntries ()
                {
-                        
-                        if (configEntries != null)
-                                return configEntries as DataSet;
-                        
-                        DataSet ds = (DataSet) ConfigurationSettings.GetConfig (CONFIG_SECTION_NAME);
-                        Interlocked.CompareExchange (ref configEntries, ds, null);
-                        return configEntries as DataSet;
+                       if (configEntries != null)
+                               return configEntries as DataSet;
+
+                       DataSet ds = (DataSet) ConfigurationManager.GetSection (CONFIG_SECTION_NAME);
+                       Interlocked.CompareExchange (ref configEntries, ds, null);
+                       return configEntries as DataSet;
                }
+
                #endregion Internal Methods
        }
 }
-
-#endif // NET_2_0