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
39 public class Provider
\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
55 public Provider(string _name, string _connection,
\r
56 string _dataadapter, string _command, string _assembly,
\r
57 string _description)
\r
60 connectionTypeName = _connection;
\r
61 adapterTypeName = _dataadapter;
\r
62 assemblyName = _assembly;
\r
63 commandTypeName = _command;
\r
64 description = _description;
\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
72 connectionTypeName = _connection;
\r
73 adapterTypeName = _dataadapter;
\r
74 assemblyName = _assembly;
\r
75 commandTypeName = _command;
\r
76 description = _description;
\r
78 switch(_parameterprefix) {
\r
80 parameterprefix = ":"; // named parameter prefixed by a semicolon
\r
83 parameterprefix = "@"; // named parameter prefixed by an at symbol
\r
85 case "questionmark":
\r
86 parameterprefix = "?"; // postional parameter noted by the question mark
\r
90 commandBuilderTypeName = _commandbuilder;
\r
93 public Provider(string _name, Type _connection, Type _dataadapter, Type _command,
\r
94 string _description)
\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
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
118 public string Description
\r
120 get {return description;}
\r
123 public string ParameterPrefix
\r
125 get {return parameterprefix;}
\r
128 public Assembly ProviderAssembly {
\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
134 providerAssembly = Assembly.Load (assemblyName);
\r
137 return providerAssembly;
\r
141 public Type ConnectionType
\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
151 return connectionType;
\r
155 public Type DataAdapterType
\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
165 return adapterType;
\r
169 public Type CommandType {
\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
178 return commandType;
\r
182 public Type CommandBuilderType {
\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
193 return commandBuilderType;
\r
197 public IDbConnection CreateConnection()
\r
199 object connObj = null;
\r
202 case "System.Data.SqlClient":
203 connObj = new System.Data.SqlClient.SqlConnection ();
205 case "System.Data.Odbc":
206 connObj = new System.Data.Odbc.OdbcConnection ();
208 case "System.Data.OleDb":
209 connObj = new System.Data.OleDb.OleDbConnection ();
212 connObj = Activator.CreateInstance (ConnectionType);
\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));
220 return (IDbConnection) connObj;
\r
223 public IDbDataAdapter CreateDataAdapter()
\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
230 return (IDbDataAdapter) adapterObj;
\r
233 public IDbCommand CreateCommand()
\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
240 return (IDbCommand) commandObj;
\r
243 public object CreateCommandBuilder(IDbDataAdapter adapter)
\r
245 if (adapter == null)
\r
246 throw new System.ArgumentNullException ("adapter");
\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
252 if (commandBuilderTypeName.Equals (String.Empty))
\r
253 throw new Exception ("Provider does not have CommandBuilder type defined.");
\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
261 return commandBuilderObj;
\r