2009-03-06 Ivan N. Zlatev <contact@i-nz.net>
[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 #if NET_2_0\r
40         [Obsolete("ProviderFactory in assembly Mono.Data has been made obsolete by DbProviderFactories in assembly System.Data.")]\r
41 #endif\r
42         public class Provider\r
43         {\r
44                 private string name = null;\r
45                 private string connectionTypeName;\r
46                 private string adapterTypeName;\r
47                 private string commandTypeName;\r
48                 private Type connectionType;\r
49                 private Type adapterType;\r
50                 private Type commandType;\r
51                 private Assembly providerAssembly;\r
52                 private string assemblyName;\r
53                 private string description;\r
54                 private string parameterprefix;\r
55                 private string commandBuilderTypeName = String.Empty;\r
56                 private Type commandBuilderType;\r
57 \r
58                 public Provider(string _name, string _connection, \r
59                         string _dataadapter, string _command, string _assembly,\r
60                         string _description) \r
61                 {\r
62                         name = _name;\r
63                         connectionTypeName = _connection;\r
64                         adapterTypeName = _dataadapter;\r
65                         assemblyName = _assembly;\r
66                         commandTypeName = _command;\r
67                         description = _description;\r
68                 }\r
69 \r
70                 public Provider(string _name, string _connection, \r
71                         string _dataadapter, string _command, string _assembly,\r
72                         string _description, string _parameterprefix, string _commandbuilder)\r
73                 {\r
74                         name = _name;\r
75                         connectionTypeName = _connection;\r
76                         adapterTypeName = _dataadapter;\r
77                         assemblyName = _assembly;\r
78                         commandTypeName = _command;\r
79                         description = _description;\r
80 \r
81                         switch(_parameterprefix) {\r
82                         case "colon":\r
83                                 parameterprefix = ":"; // named parameter prefixed by a semicolon\r
84                                 break;\r
85                         case "at":\r
86                                 parameterprefix = "@"; // named parameter prefixed by an at symbol\r
87                                 break;\r
88                         case "questionmark":\r
89                                 parameterprefix = "?"; // postional parameter noted by the question mark\r
90                                 break;\r
91                         }\r
92 \r
93                         commandBuilderTypeName = _commandbuilder;\r
94                 }\r
95 \r
96                 public Provider(string _name, Type _connection, Type _dataadapter, Type _command,\r
97                         string _description)\r
98                 {\r
99                         if (_connection == null) \r
100                                 throw new System.ArgumentNullException ("_connection");\r
101                         if (_dataadapter == null) \r
102                                 throw new System.ArgumentNullException ("_dataadapter");\r
103                         if (_command == null) \r
104                                 throw new System.ArgumentNullException ("_command");\r
105 \r
106                         name = _name;\r
107                         connectionTypeName = _connection.FullName;\r
108                         adapterTypeName = _dataadapter.FullName;\r
109                         commandTypeName = _command.FullName;\r
110                         connectionType = _connection;\r
111                         adapterType = _dataadapter;\r
112                         commandType = _command;\r
113                         description = _description;\r
114                 }\r
115 \r
116                 public string Name\r
117                 {\r
118                         get {return name;}\r
119                 }\r
120 \r
121                 public string Description\r
122                 {\r
123                         get {return description;}\r
124                 }\r
125 \r
126                 public string ParameterPrefix \r
127                 {\r
128                         get {return parameterprefix;}\r
129                 }\r
130 \r
131                 public Assembly ProviderAssembly {\r
132                         get {\r
133                                 if (providerAssembly == null) {\r
134                                         if (assemblyName.IndexOf(',') == -1) //try to load with a partial name if that's all we have\r
135                                                 providerAssembly = Assembly.LoadWithPartialName (assemblyName);\r
136                                         else \r
137                                                 providerAssembly = Assembly.Load (assemblyName);\r
138                                 }\r
139 \r
140                                 return providerAssembly;\r
141                         }\r
142                 }\r
143 \r
144                 public Type ConnectionType\r
145                 {\r
146                         get {\r
147                                 if (connectionType == null) {\r
148                                         connectionType = ProviderAssembly.GetType (connectionTypeName, false);\r
149                                         if (connectionType == null) {\r
150                                                 throw new Exception (String.Format ("Unable to load type of connection class: {0} from assembly: {1}",\r
151                                                         connectionTypeName, assemblyName));\r
152                                         }\r
153                                 }\r
154                                 return connectionType;\r
155                         }\r
156                 }\r
157 \r
158                 public Type DataAdapterType\r
159                 {\r
160                         get {\r
161                                 if (adapterType == null) {\r
162                                         adapterType = ProviderAssembly.GetType (adapterTypeName, false);\r
163                                         if (adapterType == null) {\r
164                                                 throw new Exception (String.Format ("Unable to load type of adapter class: {0} from assembly: {1}",\r
165                                                         adapterTypeName, assemblyName));\r
166                                         }\r
167                                 }\r
168                                 return adapterType;\r
169                         }\r
170                 }\r
171 \r
172                 public Type CommandType {\r
173                         get {\r
174                                 if (commandType == null) {\r
175                                         commandType = ProviderAssembly.GetType (commandTypeName, false);\r
176                                         if (commandType == null) {\r
177                                                 throw new Exception (String.Format ("Unable to load type of command class: {0} from assembly: {1}",\r
178                                                         commandTypeName, assemblyName));\r
179                                         }\r
180                                 }\r
181                                 return commandType;\r
182                         }\r
183                 }\r
184 \r
185                 public Type CommandBuilderType {\r
186                         get {\r
187                                 if (commandBuilderType == null) {\r
188                                         if (commandBuilderTypeName.Equals(String.Empty))\r
189                                                 throw new Exception("Provider does not have CommandBuilder type defined.");\r
190                                         commandBuilderType = ProviderAssembly.GetType (commandBuilderTypeName, false);\r
191                                         if (commandBuilderType == null) {\r
192                                                 throw new Exception (String.Format ("Unable to load type of command class: {0} from assembly: {1}",\r
193                                                         commandBuilderTypeName, assemblyName));\r
194                                         }\r
195                                 }\r
196                                 return commandBuilderType;\r
197                         }\r
198                 }\r
199 \r
200                 public IDbConnection CreateConnection()\r
201                 {\r
202                         object connObj = null;\r
203 \r
204                         switch (Name) {\r
205                         case "System.Data.SqlClient":\r
206                                 connObj = new System.Data.SqlClient.SqlConnection ();\r
207                                 break;\r
208                         case "System.Data.Odbc":\r
209                                 connObj = new System.Data.Odbc.OdbcConnection ();\r
210                                 break;\r
211                         case "System.Data.OleDb":\r
212                                 connObj = new System.Data.OleDb.OleDbConnection ();\r
213                                 break;\r
214                         default:\r
215                                 connObj = Activator.CreateInstance (ConnectionType);\r
216                                 break;\r
217                         }\r
218 \r
219                         if (connObj == null)\r
220                                 throw new Exception (String.Format ("Unable to create instance of connection class: {0} from assembly: {1}",\r
221                                         connectionTypeName, assemblyName));\r
222                         \r
223                         return (IDbConnection) connObj;\r
224                 }\r
225 \r
226                 public IDbDataAdapter CreateDataAdapter()\r
227                 {\r
228                         object adapterObj = Activator.CreateInstance (DataAdapterType);\r
229                         if (adapterObj == null)\r
230                                 throw new Exception (String.Format ("Unable to create instance of adapter class: {0} from assembly: {1}",\r
231                                         adapterTypeName, assemblyName));\r
232 \r
233                         return (IDbDataAdapter) adapterObj;\r
234                 }\r
235 \r
236                 public IDbCommand CreateCommand()\r
237                 {\r
238                         object commandObj = Activator.CreateInstance (CommandType);\r
239                         if (commandObj == null)\r
240                                 throw new Exception (String.Format ("Unable to create instance of command class: {0} from assembly: {1}",\r
241                                         commandTypeName, assemblyName));\r
242 \r
243                         return (IDbCommand) commandObj;\r
244                 }\r
245 \r
246                 public object CreateCommandBuilder(IDbDataAdapter adapter) \r
247                 {\r
248                         if (adapter == null) \r
249                                 throw new System.ArgumentNullException ("adapter");\r
250 \r
251                         object obj = (object) adapter;\r
252                         if (!DataAdapterType.ToString ().Equals (obj.ToString ()))\r
253                                 throw new System.ArgumentException ("adapter not part of this provider.");\r
254                                 \r
255                         if (commandBuilderTypeName.Equals (String.Empty))\r
256                                 throw new Exception ("Provider does not have CommandBuilder type defined.");\r
257                         \r
258                         object[] parms = new object [] { obj };\r
259                         object commandBuilderObj = Activator.CreateInstance (CommandBuilderType, parms);\r
260                         if (commandBuilderObj == null)\r
261                                 throw new Exception (String.Format ("Unable to create instance of command builder class: {0} from assembly: {1}",\r
262                                         commandBuilderTypeName, assemblyName));\r
263 \r
264                         return commandBuilderObj;\r
265                 }\r
266         }\r
267 }\r
268 \r