* roottypes.cs: Rename from tree.cs.
[mono.git] / mcs / class / Mono.Data / ProviderFactory.cs
old mode 100755 (executable)
new mode 100644 (file)
index e249a9b..f86fce1
@@ -8,27 +8,27 @@
 // Copyright (C) Brian Ritchie, 2002\r
 // \r
 //\r
-
-//
-// Permission is hereby granted, free of charge, to any person obtaining
-// a copy of this software and associated documentation files (the
-// "Software"), to deal in the Software without restriction, including
-// without limitation the rights to use, copy, modify, merge, publish,
-// distribute, sublicense, and/or sell copies of the Software, and to
-// permit persons to whom the Software is furnished to do so, subject to
-// the following conditions:
-// 
-// The above copyright notice and this permission notice shall be
-// included in all copies or substantial portions of the Software.
-// 
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
-// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
-// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
-// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-//
+\r
+//\r
+// Permission is hereby granted, free of charge, to any person obtaining\r
+// a copy of this software and associated documentation files (the\r
+// "Software"), to deal in the Software without restriction, including\r
+// without limitation the rights to use, copy, modify, merge, publish,\r
+// distribute, sublicense, and/or sell copies of the Software, and to\r
+// permit persons to whom the Software is furnished to do so, subject to\r
+// the following conditions:\r
+// \r
+// The above copyright notice and this permission notice shall be\r
+// included in all copies or substantial portions of the Software.\r
+// \r
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+//\r
 using System;\r
 using System.Data;\r
 using System.Reflection;\r
@@ -43,90 +43,128 @@ namespace Mono.Data
        {\r
                private static ProviderCollection providers;\r
 \r
-               static ProviderFactory()\r
+               static ProviderFactory ()\r
                {\r
-                       providers=(ProviderCollection) ConfigurationSettings.GetConfig("mono.data/providers");\r
-                       if (providers==null)\r
-                               providers=new ProviderCollection();\r
+                       providers = (ProviderCollection) ConfigurationSettings.GetConfig ("mono.data/providers");\r
+                       if (providers == null) {\r
+                               providers = new ProviderCollection ();\r
+                               // warn the developer or administrator that the provider list is empty\r
+                               System.Diagnostics.Debug.Listeners.Add (new System.Diagnostics.TextWriterTraceListener (Console.Out));\r
+                               System.Diagnostics.Debug.WriteLine ("No providers found. Did you set up a mono.data/providers area in your app.config or in machine.config?");\r
+                       }\r
+\r
                }\r
 \r
                static public ProviderCollection Providers\r
                {\r
-                       get\r
-                       {\r
+                       get {\r
                                return providers;\r
                        }\r
                }\r
 \r
-               static public IDbConnection CreateConnectionFromConfig(string Setting)\r
+               static public IDbConnection CreateConnectionFromConfig (string Setting)\r
                {\r
-                       return CreateConnection(ConfigurationSettings.AppSettings[Setting]);\r
+                       if (Setting == null) \r
+                               throw new System.ArgumentNullException ("Setting");\r
+\r
+                       return CreateConnection (ConfigurationSettings.AppSettings [Setting]);\r
                }\r
 \r
                static public IDbConnection CreateConnection(string ConnectionString)\r
                {\r
-                       string[] ConnectionAttributes=ConnectionString.Split(new Char[1] { ';' }); \r
-                       string ProviderName=null;\r
-                       string NewConnectionString="";\r
-                       foreach (string s in ConnectionAttributes)\r
-                       {\r
-                               string[] AttributeParts=s.Split(new Char[1] { '=' });\r
-                               if (AttributeParts[0].ToLower().Trim()=="factory")\r
-                                       ProviderName=AttributeParts[1].Trim();\r
+                       if (ConnectionString == null) \r
+                               throw new System.ArgumentNullException ("ConnectionString");\r
+\r
+                       string [] ConnectionAttributes = ConnectionString.Split (new Char [1] { ';' }); \r
+                       string ProviderName = null;\r
+                       string NewConnectionString = "";\r
+                       foreach (string s in ConnectionAttributes) {\r
+                               string [] AttributeParts = s.Split (new Char [1] { '=' });\r
+                               if (AttributeParts [0].ToLower ().Trim () == "factory")\r
+                                       ProviderName = AttributeParts [1].Trim ();\r
                                else \r
-                                       NewConnectionString+=";"+s;\r
+                                       NewConnectionString += ";" + s;\r
                        }\r
-                       NewConnectionString=NewConnectionString.Remove(0,1);\r
-                       return CreateConnection(ProviderName, NewConnectionString);\r
+                       NewConnectionString = NewConnectionString.Remove (0, 1); // remove the initial semicolon\r
+                       if (ProviderName == null) \r
+                               throw new System.ArgumentException ("The connection string must contain a 'factory=Provider.Class' token", "ConnectionString");\r
+                       return CreateConnection (ProviderName, NewConnectionString);\r
                }\r
 \r
                static public IDbConnection CreateConnection(string ProviderName, string ConnectionString)\r
                {\r
-                       Provider provider=providers[ProviderName];\r
-                       IDbConnection conn=provider.CreateConnection();\r
-                       conn.ConnectionString=ConnectionString;\r
+                       if (ProviderName == null) \r
+                               throw new System.ArgumentNullException("ProviderName");\r
+                       if (ConnectionString == null) \r
+                               throw new System.ArgumentNullException ("ConnectionString");\r
+\r
+                       Provider provider = providers [ProviderName];\r
+                       IDbConnection conn = provider.CreateConnection ();\r
+                       conn.ConnectionString = ConnectionString;\r
                        return conn;\r
                }\r
 \r
-               static public IDbCommand CreateStoredProc(IDbConnection Conn, string CommandName)\r
+               static public IDbCommand CreateStoredProc (IDbConnection Conn, string CommandName)\r
                {\r
-                       IDbCommand cmd=Conn.CreateCommand();\r
-                       cmd.CommandText=CommandName;\r
-                       cmd.CommandType=CommandType.StoredProcedure;\r
+                       if (Conn == null) \r
+                               throw new System.ArgumentNullException ("Conn");\r
+                       if (CommandName == null) \r
+                               throw new System.ArgumentNullException ("CommandName");\r
+\r
+                       IDbCommand cmd = Conn.CreateCommand ();\r
+                       cmd.CommandText = CommandName;\r
+                       cmd.CommandType = CommandType.StoredProcedure;\r
                        return cmd;\r
                }\r
 \r
-               static public IDbDataAdapter CreateDataAdapter(IDbCommand SelectCommand)\r
+               static public IDbDataAdapter CreateDataAdapter (IDbCommand SelectCommand)\r
                {\r
-                       Provider provider=providers.FindByCommandType(SelectCommand.GetType());\r
-                       IDbDataAdapter adapter=provider.CreateDataAdapter();\r
-                       adapter.SelectCommand=SelectCommand;\r
+                       if (SelectCommand == null) \r
+                               throw new System.ArgumentNullException("SelectCommand");\r
+\r
+                       Provider provider = providers.FindByCommandType (SelectCommand.GetType ());\r
+                       IDbDataAdapter adapter = provider.CreateDataAdapter ();\r
+                       adapter.SelectCommand = SelectCommand;\r
                        return adapter;\r
                }\r
 \r
-               static public IDbDataAdapter CreateDataAdapter(string ProviderName)\r
+               static public IDbDataAdapter CreateDataAdapter (string ProviderName)\r
                {\r
-                       Provider provider=providers[ProviderName];\r
-                       IDbDataAdapter adapter=provider.CreateDataAdapter();\r
+                       if (ProviderName == null) \r
+                               throw new System.ArgumentNullException("ProviderName");\r
+\r
+                       Provider provider = providers [ProviderName];\r
+                       IDbDataAdapter adapter = provider.CreateDataAdapter ();\r
                        return adapter;\r
                }\r
 \r
-               static public IDbDataAdapter CreateDataAdapter(IDbConnection Conn, string SelectCommand)\r
+               static public IDbDataAdapter CreateDataAdapter (IDbConnection Conn, string SelectCommand)\r
                {\r
-                       IDbCommand cmd=Conn.CreateCommand();\r
-                       cmd.CommandText=SelectCommand;\r
-                       return CreateDataAdapter(cmd);\r
+                       if (Conn == null) \r
+                               throw new System.ArgumentNullException ("Conn");\r
+                       if (SelectCommand == null) \r
+                               throw new System.ArgumentNullException("SelectCommand");\r
+\r
+                       IDbCommand cmd = Conn.CreateCommand ();\r
+                       cmd.CommandText = SelectCommand;\r
+                       return CreateDataAdapter (cmd);\r
                }\r
 \r
-               static public IDbCommand CreateCommand(string ProviderName)\r
+               static public IDbCommand CreateCommand (string ProviderName)\r
                {\r
-                       Provider provider=providers[ProviderName];\r
-                       return provider.CreateCommand();\r
+                       if (ProviderName == null) \r
+                               throw new System.ArgumentNullException("ProviderName");\r
+\r
+                       Provider provider = providers [ProviderName];\r
+                       return provider.CreateCommand ();\r
                }\r
 \r
-               static public IDbCommand CreateCommand(IDbConnection Conn)\r
+               static public IDbCommand CreateCommand (IDbConnection Conn)\r
                {\r
-                       return Conn.CreateCommand();\r
+                       if (Conn == null) \r
+                               throw new System.ArgumentNullException("Conn");\r
+\r
+                       return Conn.CreateCommand ();\r
                }\r
 \r
        }\r