2 // Mono.Data.ProviderFactory
\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
35 using System.Runtime.Remoting;
\r
36 using System.Configuration;
\r
38 using System.Collections.Specialized;
\r
42 public class ProviderFactory
\r
44 private static ProviderCollection providers;
\r
46 static ProviderFactory ()
\r
48 providers = (ProviderCollection) ConfigurationSettings.GetConfig ("mono.data/providers");
\r
49 if (providers == null) {
\r
50 providers = new ProviderCollection ();
\r
51 // warn the developer or administrator that the provider list is empty
\r
52 System.Diagnostics.Debug.Listeners.Add (new System.Diagnostics.TextWriterTraceListener (Console.Out));
\r
53 System.Diagnostics.Debug.WriteLine ("No providers found. Did you set up a mono.data/providers area in your app.config or in machine.config?");
\r
58 static public ProviderCollection Providers
\r
65 static public IDbConnection CreateConnectionFromConfig (string Setting)
\r
67 if (Setting == null)
\r
68 throw new System.ArgumentNullException ("Setting");
\r
70 return CreateConnection (ConfigurationSettings.AppSettings [Setting]);
\r
73 static public IDbConnection CreateConnection(string ConnectionString)
\r
75 if (ConnectionString == null)
\r
76 throw new System.ArgumentNullException ("ConnectionString");
\r
78 string [] ConnectionAttributes = ConnectionString.Split (new Char [1] { ';' });
\r
79 string ProviderName = null;
\r
80 string NewConnectionString = "";
\r
81 foreach (string s in ConnectionAttributes) {
\r
82 string [] AttributeParts = s.Split (new Char [1] { '=' });
\r
83 if (AttributeParts [0].ToLower ().Trim () == "factory")
\r
84 ProviderName = AttributeParts [1].Trim ();
\r
86 NewConnectionString += ";" + s;
\r
88 NewConnectionString = NewConnectionString.Remove (0, 1); // remove the initial semicolon
\r
89 if (ProviderName == null)
\r
90 throw new System.ArgumentException ("The connection string must contain a 'factory=Provider.Class' token", "ConnectionString");
\r
91 return CreateConnection (ProviderName, NewConnectionString);
\r
94 static public IDbConnection CreateConnection(string ProviderName, string ConnectionString)
\r
96 if (ProviderName == null)
\r
97 throw new System.ArgumentNullException("ProviderName");
\r
98 if (ConnectionString == null)
\r
99 throw new System.ArgumentNullException ("ConnectionString");
\r
101 Provider provider = providers [ProviderName];
\r
102 IDbConnection conn = provider.CreateConnection ();
\r
103 conn.ConnectionString = ConnectionString;
\r
107 static public IDbCommand CreateStoredProc (IDbConnection Conn, string CommandName)
\r
110 throw new System.ArgumentNullException ("Conn");
\r
111 if (CommandName == null)
\r
112 throw new System.ArgumentNullException ("CommandName");
\r
114 IDbCommand cmd = Conn.CreateCommand ();
\r
115 cmd.CommandText = CommandName;
\r
116 cmd.CommandType = CommandType.StoredProcedure;
\r
120 static public IDbDataAdapter CreateDataAdapter (IDbCommand SelectCommand)
\r
122 if (SelectCommand == null)
\r
123 throw new System.ArgumentNullException("SelectCommand");
\r
125 Provider provider = providers.FindByCommandType (SelectCommand.GetType ());
\r
126 IDbDataAdapter adapter = provider.CreateDataAdapter ();
\r
127 adapter.SelectCommand = SelectCommand;
\r
131 static public IDbDataAdapter CreateDataAdapter (string ProviderName)
\r
133 if (ProviderName == null)
\r
134 throw new System.ArgumentNullException("ProviderName");
\r
136 Provider provider = providers [ProviderName];
\r
137 IDbDataAdapter adapter = provider.CreateDataAdapter ();
\r
141 static public IDbDataAdapter CreateDataAdapter (IDbConnection Conn, string SelectCommand)
\r
144 throw new System.ArgumentNullException ("Conn");
\r
145 if (SelectCommand == null)
\r
146 throw new System.ArgumentNullException("SelectCommand");
\r
148 IDbCommand cmd = Conn.CreateCommand ();
\r
149 cmd.CommandText = SelectCommand;
\r
150 return CreateDataAdapter (cmd);
\r
153 static public IDbCommand CreateCommand (string ProviderName)
\r
155 if (ProviderName == null)
\r
156 throw new System.ArgumentNullException("ProviderName");
\r
158 Provider provider = providers [ProviderName];
\r
159 return provider.CreateCommand ();
\r
162 static public IDbCommand CreateCommand (IDbConnection Conn)
\r
165 throw new System.ArgumentNullException("Conn");
\r
167 return Conn.CreateCommand ();
\r