implemented AuthenticationService, ProxyGenerator
[mono.git] / mcs / class / Mono.Data / Provider.cs
old mode 100755 (executable)
new mode 100644 (file)
index 978a019..3fd9936
@@ -8,9 +8,31 @@
 // Copyright (C) Brian Ritchie, 2002\r
 // \r
 //\r
+\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
+using System.IO;\r
 \r
 namespace Mono.Data\r
 {\r
@@ -23,12 +45,16 @@ namespace Mono.Data
                private Type connectionType;\r
                private Type adapterType;\r
                private Type commandType;\r
+               private Assembly providerAssembly;\r
                private string assemblyName;\r
                private string description;\r
-      \r
+               private string parameterprefix;\r
+               private string commandBuilderTypeName = String.Empty;\r
+               private Type commandBuilderType;\r
+\r
                public Provider(string _name, string _connection, \r
                        string _dataadapter, string _command, string _assembly,\r
-                       string _description)\r
+                       string _description) \r
                {\r
                        name = _name;\r
                        connectionTypeName = _connection;\r
@@ -38,9 +64,42 @@ namespace Mono.Data
                        description = _description;\r
                }\r
 \r
+               public Provider(string _name, string _connection, \r
+                       string _dataadapter, string _command, string _assembly,\r
+                       string _description, string _parameterprefix, string _commandbuilder)\r
+               {\r
+                       name = _name;\r
+                       connectionTypeName = _connection;\r
+                       adapterTypeName = _dataadapter;\r
+                       assemblyName = _assembly;\r
+                       commandTypeName = _command;\r
+                       description = _description;\r
+\r
+                       switch(_parameterprefix) {\r
+                       case "colon":\r
+                               parameterprefix = ":"; // named parameter prefixed by a semicolon\r
+                               break;\r
+                       case "at":\r
+                               parameterprefix = "@"; // named parameter prefixed by an at symbol\r
+                               break;\r
+                       case "questionmark":\r
+                               parameterprefix = "?"; // postional parameter noted by the question mark\r
+                               break;\r
+                       }\r
+\r
+                       commandBuilderTypeName = _commandbuilder;\r
+               }\r
+\r
                public Provider(string _name, Type _connection, Type _dataadapter, Type _command,\r
                        string _description)\r
                {\r
+                       if (_connection == null) \r
+                               throw new System.ArgumentNullException ("_connection");\r
+                       if (_dataadapter == null) \r
+                               throw new System.ArgumentNullException ("_dataadapter");\r
+                       if (_command == null) \r
+                               throw new System.ArgumentNullException ("_command");\r
+\r
                        name = _name;\r
                        connectionTypeName = _connection.FullName;\r
                        adapterTypeName = _dataadapter.FullName;\r
@@ -61,13 +120,33 @@ namespace Mono.Data
                        get {return description;}\r
                }\r
 \r
+               public string ParameterPrefix \r
+               {\r
+                       get {return parameterprefix;}\r
+               }\r
+\r
+               public Assembly ProviderAssembly {\r
+                       get {\r
+                               if (providerAssembly == null) {\r
+                                       if (assemblyName.IndexOf(',') == -1) //try to load with a partial name if that's all we have\r
+                                               providerAssembly = Assembly.LoadWithPartialName (assemblyName);\r
+                                       else \r
+                                               providerAssembly = Assembly.Load (assemblyName);\r
+                               }\r
+\r
+                               return providerAssembly;\r
+                       }\r
+               }\r
+\r
                public Type ConnectionType\r
                {\r
-                       get \r
-                       {\r
-                               if (connectionType==null)\r
-                               {\r
-                                       connectionType=Type.GetType(connectionTypeName+","+assemblyName);\r
+                       get {\r
+                               if (connectionType == null) {\r
+                                       connectionType = ProviderAssembly.GetType (connectionTypeName, false);\r
+                                       if (connectionType == null) {\r
+                                               throw new Exception (String.Format ("Unable to load type of connection class: {0} from assembly: {1}",
+                                                       connectionTypeName, assemblyName));\r
+                                       }\r
                                }\r
                                return connectionType;\r
                        }\r
@@ -75,41 +154,112 @@ namespace Mono.Data
 \r
                public Type DataAdapterType\r
                {\r
-                       get \r
-                       {\r
-                               if (adapterType==null)\r
-                               {\r
-                                       adapterType=Type.GetType(adapterTypeName+","+assemblyName);\r
+                       get {\r
+                               if (adapterType == null) {\r
+                                       adapterType = ProviderAssembly.GetType (adapterTypeName, false);\r
+                                       if (adapterType == null) {\r
+                                               throw new Exception (String.Format ("Unable to load type of adapter class: {0} from assembly: {1}",
+                                                       adapterTypeName, assemblyName));\r
+                                       }\r
                                }\r
                                return adapterType;\r
                        }\r
                }\r
 \r
-               public Type CommandType\r
-               {\r
-                       get \r
-                       {\r
-                               if (commandType==null)\r
-                               {\r
-                                       commandType=Type.GetType(commandTypeName+","+assemblyName);\r
+               public Type CommandType {\r
+                       get {\r
+                               if (commandType == null) {\r
+                                       commandType = ProviderAssembly.GetType (commandTypeName, false);\r
+                                       if (commandType == null) {\r
+                                               throw new Exception (String.Format ("Unable to load type of command class: {0} from assembly: {1}",
+                                                       commandTypeName, assemblyName));\r
+                                       }\r
                                }\r
                                return commandType;\r
                        }\r
                }\r
 \r
+               public Type CommandBuilderType {\r
+                       get {\r
+                               if (commandBuilderType == null) {\r
+                                       if (commandBuilderTypeName.Equals(String.Empty))\r
+                                               throw new Exception("Provider does not have CommandBuilder type defined.");\r
+                                       commandBuilderType = ProviderAssembly.GetType (commandBuilderTypeName, false);\r
+                                       if (commandBuilderType == null) {\r
+                                               throw new Exception (String.Format ("Unable to load type of command class: {0} from assembly: {1}",
+                                                       commandBuilderTypeName, assemblyName));\r
+                                       }\r
+                               }\r
+                               return commandBuilderType;\r
+                       }\r
+               }\r
+\r
                public IDbConnection CreateConnection()\r
                {\r
-                       return (IDbConnection) Activator.CreateInstance(ConnectionType);\r
+                       object connObj = null;\r
+\r
+                       switch (Name) {
+                       case "System.Data.SqlClient":
+                               connObj = new System.Data.SqlClient.SqlConnection ();
+                               break;
+                       case "System.Data.Odbc":
+                               connObj = new System.Data.Odbc.OdbcConnection ();
+                               break;
+                       case "System.Data.OleDb":
+                               connObj = new System.Data.OleDb.OleDbConnection ();
+                               break;
+                       default:
+                               connObj = Activator.CreateInstance (ConnectionType);\r
+                               break;
+                       }\r
+\r
+                       if (connObj == null)\r
+                               throw new Exception (String.Format ("Unable to create instance of connection class: {0} from assembly: {1}",
+                                       connectionTypeName, assemblyName));
+                       \r
+                       return (IDbConnection) connObj;\r
                }\r
 \r
                public IDbDataAdapter CreateDataAdapter()\r
                {\r
-                       return (IDbDataAdapter) Activator.CreateInstance(DataAdapterType);\r
+                       object adapterObj = Activator.CreateInstance (DataAdapterType);\r
+                       if (adapterObj == null)\r
+                               throw new Exception (String.Format ("Unable to create instance of adapter class: {0} from assembly: {1}",
+                                       adapterTypeName, assemblyName));\r
+\r
+                       return (IDbDataAdapter) adapterObj;\r
                }\r
 \r
                public IDbCommand CreateCommand()\r
                {\r
-                       return (IDbCommand) Activator.CreateInstance(CommandType);\r
+                       object commandObj = Activator.CreateInstance (CommandType);\r
+                       if (commandObj == null)\r
+                               throw new Exception (String.Format ("Unable to create instance of command class: {0} from assembly: {1}",
+                                       commandTypeName, assemblyName));\r
+\r
+                       return (IDbCommand) commandObj;\r
+               }\r
+\r
+               public object CreateCommandBuilder(IDbDataAdapter adapter) \r
+               {\r
+                       if (adapter == null) \r
+                               throw new System.ArgumentNullException ("adapter");\r
+\r
+                       object obj = (object) adapter;\r
+                       if (!DataAdapterType.ToString ().Equals (obj.ToString ()))\r
+                               throw new System.ArgumentException ("adapter not part of this provider.");\r
+                               \r
+                       if (commandBuilderTypeName.Equals (String.Empty))\r
+                               throw new Exception ("Provider does not have CommandBuilder type defined.");\r
+                       \r
+                       object[] parms = new object [] { obj };\r
+                       object commandBuilderObj = Activator.CreateInstance (CommandBuilderType, parms);\r
+                       if (commandBuilderObj == null)\r
+                               throw new Exception (String.Format ("Unable to create instance of command builder class: {0} from assembly: {1}",
+                                       commandBuilderTypeName, assemblyName));\r
+\r
+                       return commandBuilderObj;\r
                }\r
        }\r
 }\r
+\r