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