2006-03-11 Marek Sieradzki <marek.sieradzki@gmail.com>
[mono.git] / mcs / class / Mono.Data / Provider.cs
1 //\r
2 // Mono.Data.Provider\r
3 //\r
4 // Authors:\r
5 //   Brian Ritchie (brianlritchie@hotmail.com) \r
6 //  \r
7 //\r
8 // Copyright (C) Brian Ritchie, 2002\r
9 // \r
10 //\r
11 \r
12 //\r
13 // Permission is hereby granted, free of charge, to any person obtaining\r
14 // a copy of this software and associated documentation files (the\r
15 // "Software"), to deal in the Software without restriction, including\r
16 // without limitation the rights to use, copy, modify, merge, publish,\r
17 // distribute, sublicense, and/or sell copies of the Software, and to\r
18 // permit persons to whom the Software is furnished to do so, subject to\r
19 // the following conditions:\r
20 // \r
21 // The above copyright notice and this permission notice shall be\r
22 // included in all copies or substantial portions of the Software.\r
23 // \r
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
31 //\r
32 using System;\r
33 using System.Data;\r
34 using System.Reflection;\r
35 using System.IO;\r
36 \r
37 namespace Mono.Data\r
38 {\r
39         public class Provider\r
40         {\r
41                 private string name = null;\r
42                 private string connectionTypeName;\r
43                 private string adapterTypeName;\r
44                 private string commandTypeName;\r
45                 private Type connectionType;\r
46                 private Type adapterType;\r
47                 private Type commandType;\r
48                 private Assembly providerAssembly;\r
49                 private string assemblyName;\r
50                 private string description;\r
51                 private string parameterprefix;\r
52                 private string commandBuilderTypeName = String.Empty;\r
53                 private Type commandBuilderType;\r
54 \r
55                 public Provider(string _name, string _connection, \r
56                         string _dataadapter, string _command, string _assembly,\r
57                         string _description) \r
58                 {\r
59                         name = _name;\r
60                         connectionTypeName = _connection;\r
61                         adapterTypeName = _dataadapter;\r
62                         assemblyName = _assembly;\r
63                         commandTypeName = _command;\r
64                         description = _description;\r
65                 }\r
66 \r
67                 public Provider(string _name, string _connection, \r
68                         string _dataadapter, string _command, string _assembly,\r
69                         string _description, string _parameterprefix, string _commandbuilder)\r
70                 {\r
71                         name = _name;\r
72                         connectionTypeName = _connection;\r
73                         adapterTypeName = _dataadapter;\r
74                         assemblyName = _assembly;\r
75                         commandTypeName = _command;\r
76                         description = _description;\r
77 \r
78                         switch(_parameterprefix) {\r
79                         case "colon":\r
80                                 parameterprefix = ":"; // named parameter prefixed by a semicolon\r
81                                 break;\r
82                         case "at":\r
83                                 parameterprefix = "@"; // named parameter prefixed by an at symbol\r
84                                 break;\r
85                         case "questionmark":\r
86                                 parameterprefix = "?"; // postional parameter noted by the question mark\r
87                                 break;\r
88                         }\r
89 \r
90                         commandBuilderTypeName = _commandbuilder;\r
91                 }\r
92 \r
93                 public Provider(string _name, Type _connection, Type _dataadapter, Type _command,\r
94                         string _description)\r
95                 {\r
96                         if (_connection == null) \r
97                                 throw new System.ArgumentNullException ("_connection");\r
98                         if (_dataadapter == null) \r
99                                 throw new System.ArgumentNullException ("_dataadapter");\r
100                         if (_command == null) \r
101                                 throw new System.ArgumentNullException ("_command");\r
102 \r
103                         name = _name;\r
104                         connectionTypeName = _connection.FullName;\r
105                         adapterTypeName = _dataadapter.FullName;\r
106                         commandTypeName = _command.FullName;\r
107                         connectionType = _connection;\r
108                         adapterType = _dataadapter;\r
109                         commandType = _command;\r
110                         description = _description;\r
111                 }\r
112 \r
113                 public string Name\r
114                 {\r
115                         get {return name;}\r
116                 }\r
117 \r
118                 public string Description\r
119                 {\r
120                         get {return description;}\r
121                 }\r
122 \r
123                 public string ParameterPrefix \r
124                 {\r
125                         get {return parameterprefix;}\r
126                 }\r
127 \r
128                 public Assembly ProviderAssembly {\r
129                         get {\r
130                                 if (providerAssembly == null) {\r
131                                         if (assemblyName.IndexOf(',') == -1) //try to load with a partial name if that's all we have\r
132                                                 providerAssembly = Assembly.LoadWithPartialName (assemblyName);\r
133                                         else \r
134                                                 providerAssembly = Assembly.Load (assemblyName);\r
135                                 }\r
136 \r
137                                 return providerAssembly;\r
138                         }\r
139                 }\r
140 \r
141                 public Type ConnectionType\r
142                 {\r
143                         get {\r
144                                 if (connectionType == null) {\r
145                                         connectionType = ProviderAssembly.GetType (connectionTypeName, false);\r
146                                         if (connectionType == null) {\r
147                                                 throw new Exception (String.Format ("Unable to load type of connection class: {0} from assembly: {1}",
148                                                         connectionTypeName, assemblyName));\r
149                                         }\r
150                                 }\r
151                                 return connectionType;\r
152                         }\r
153                 }\r
154 \r
155                 public Type DataAdapterType\r
156                 {\r
157                         get {\r
158                                 if (adapterType == null) {\r
159                                         adapterType = ProviderAssembly.GetType (adapterTypeName, false);\r
160                                         if (adapterType == null) {\r
161                                                 throw new Exception (String.Format ("Unable to load type of adapter class: {0} from assembly: {1}",
162                                                         adapterTypeName, assemblyName));\r
163                                         }\r
164                                 }\r
165                                 return adapterType;\r
166                         }\r
167                 }\r
168 \r
169                 public Type CommandType {\r
170                         get {\r
171                                 if (commandType == null) {\r
172                                         commandType = ProviderAssembly.GetType (commandTypeName, false);\r
173                                         if (commandType == null) {\r
174                                                 throw new Exception (String.Format ("Unable to load type of command class: {0} from assembly: {1}",
175                                                         commandTypeName, assemblyName));\r
176                                         }\r
177                                 }\r
178                                 return commandType;\r
179                         }\r
180                 }\r
181 \r
182                 public Type CommandBuilderType {\r
183                         get {\r
184                                 if (commandBuilderType == null) {\r
185                                         if (commandBuilderTypeName.Equals(String.Empty))\r
186                                                 throw new Exception("Provider does not have CommandBuilder type defined.");\r
187                                         commandBuilderType = ProviderAssembly.GetType (commandBuilderTypeName, false);\r
188                                         if (commandBuilderType == null) {\r
189                                                 throw new Exception (String.Format ("Unable to load type of command class: {0} from assembly: {1}",
190                                                         commandBuilderTypeName, assemblyName));\r
191                                         }\r
192                                 }\r
193                                 return commandBuilderType;\r
194                         }\r
195                 }\r
196 \r
197                 public IDbConnection CreateConnection()\r
198                 {\r
199                         object connObj = null;\r
200 \r
201                         switch (Name) {
202                         case "System.Data.SqlClient":
203                                 connObj = new System.Data.SqlClient.SqlConnection ();
204                                 break;
205                         case "System.Data.Odbc":
206                                 connObj = new System.Data.Odbc.OdbcConnection ();
207                                 break;
208                         case "System.Data.OleDb":
209                                 connObj = new System.Data.OleDb.OleDbConnection ();
210                                 break;
211                         default:
212                                 connObj = Activator.CreateInstance (ConnectionType);\r
213                                 break;
214                         }\r
215 \r
216                         if (connObj == null)\r
217                                 throw new Exception (String.Format ("Unable to create instance of connection class: {0} from assembly: {1}",
218                                         connectionTypeName, assemblyName));
219                         \r
220                         return (IDbConnection) connObj;\r
221                 }\r
222 \r
223                 public IDbDataAdapter CreateDataAdapter()\r
224                 {\r
225                         object adapterObj = Activator.CreateInstance (DataAdapterType);\r
226                         if (adapterObj == null)\r
227                                 throw new Exception (String.Format ("Unable to create instance of adapter class: {0} from assembly: {1}",
228                                         adapterTypeName, assemblyName));\r
229 \r
230                         return (IDbDataAdapter) adapterObj;\r
231                 }\r
232 \r
233                 public IDbCommand CreateCommand()\r
234                 {\r
235                         object commandObj = Activator.CreateInstance (CommandType);\r
236                         if (commandObj == null)\r
237                                 throw new Exception (String.Format ("Unable to create instance of command class: {0} from assembly: {1}",
238                                         commandTypeName, assemblyName));\r
239 \r
240                         return (IDbCommand) commandObj;\r
241                 }\r
242 \r
243                 public object CreateCommandBuilder(IDbDataAdapter adapter) \r
244                 {\r
245                         if (adapter == null) \r
246                                 throw new System.ArgumentNullException ("adapter");\r
247 \r
248                         object obj = (object) adapter;\r
249                         if (!DataAdapterType.ToString ().Equals (obj.ToString ()))\r
250                                 throw new System.ArgumentException ("adapter not part of this provider.");\r
251                                 \r
252                         if (commandBuilderTypeName.Equals (String.Empty))\r
253                                 throw new Exception ("Provider does not have CommandBuilder type defined.");\r
254                         \r
255                         object[] parms = new object [] { obj };\r
256                         object commandBuilderObj = Activator.CreateInstance (CommandBuilderType, parms);\r
257                         if (commandBuilderObj == null)\r
258                                 throw new Exception (String.Format ("Unable to create instance of command builder class: {0} from assembly: {1}",
259                                         commandBuilderTypeName, assemblyName));\r
260 \r
261                         return commandBuilderObj;\r
262                 }\r
263         }\r
264 }\r
265 \r