2 // Mono.Data.Provider
\r
5 // Brian Ritchie (brianlritchie@hotmail.com)
\r
8 // Copyright (C) Brian Ritchie, 2002
\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
21 // The above copyright notice and this permission notice shall be
\r
22 // included in all copies or substantial portions of the Software.
\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
34 using System.Reflection;
\r
40 [Obsolete("ProviderFactory in assembly Mono.Data has been made obsolete by DbProviderFactories in assembly System.Data.")]
\r
42 public class Provider
\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
58 public Provider(string _name, string _connection,
\r
59 string _dataadapter, string _command, string _assembly,
\r
60 string _description)
\r
63 connectionTypeName = _connection;
\r
64 adapterTypeName = _dataadapter;
\r
65 assemblyName = _assembly;
\r
66 commandTypeName = _command;
\r
67 description = _description;
\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
75 connectionTypeName = _connection;
\r
76 adapterTypeName = _dataadapter;
\r
77 assemblyName = _assembly;
\r
78 commandTypeName = _command;
\r
79 description = _description;
\r
81 switch(_parameterprefix) {
\r
83 parameterprefix = ":"; // named parameter prefixed by a semicolon
\r
86 parameterprefix = "@"; // named parameter prefixed by an at symbol
\r
88 case "questionmark":
\r
89 parameterprefix = "?"; // postional parameter noted by the question mark
\r
93 commandBuilderTypeName = _commandbuilder;
\r
96 public Provider(string _name, Type _connection, Type _dataadapter, Type _command,
\r
97 string _description)
\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
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
121 public string Description
\r
123 get {return description;}
\r
126 public string ParameterPrefix
\r
128 get {return parameterprefix;}
\r
131 public Assembly ProviderAssembly {
\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
137 providerAssembly = Assembly.Load (assemblyName);
\r
140 return providerAssembly;
\r
144 public Type ConnectionType
\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
154 return connectionType;
\r
158 public Type DataAdapterType
\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
168 return adapterType;
\r
172 public Type CommandType {
\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
181 return commandType;
\r
185 public Type CommandBuilderType {
\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
196 return commandBuilderType;
\r
200 public IDbConnection CreateConnection()
\r
202 object connObj = null;
\r
205 case "System.Data.SqlClient":
\r
206 connObj = new System.Data.SqlClient.SqlConnection ();
\r
208 case "System.Data.Odbc":
\r
209 connObj = new System.Data.Odbc.OdbcConnection ();
\r
211 case "System.Data.OleDb":
\r
212 connObj = new System.Data.OleDb.OleDbConnection ();
\r
215 connObj = Activator.CreateInstance (ConnectionType);
\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
223 return (IDbConnection) connObj;
\r
226 public IDbDataAdapter CreateDataAdapter()
\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
233 return (IDbDataAdapter) adapterObj;
\r
236 public IDbCommand CreateCommand()
\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
243 return (IDbCommand) commandObj;
\r
246 public object CreateCommandBuilder(IDbDataAdapter adapter)
\r
248 if (adapter == null)
\r
249 throw new System.ArgumentNullException ("adapter");
\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
255 if (commandBuilderTypeName.Equals (String.Empty))
\r
256 throw new Exception ("Provider does not have CommandBuilder type defined.");
\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
264 return commandBuilderObj;
\r