2 // Mono.Data.ProviderFactory
5 // Brian Ritchie (brianlritchie@hotmail.com)
8 // Copyright (C) Brian Ritchie, 2002
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:
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
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.
34 using System
.Reflection
;
35 using System
.Runtime
.Remoting
;
36 using System
.Configuration
;
38 using System
.Collections
.Specialized
;
43 [Obsolete("ProviderFactory in assembly Mono.Data has been made obsolete by DbProviderFactories in assembly System.Data.")]
45 public class ProviderFactory
47 private static ProviderCollection providers
;
49 static ProviderFactory ()
51 providers
= (ProviderCollection
) ConfigurationSettings
.GetConfig ("mono.data/providers");
52 if (providers
== null) {
53 providers
= new ProviderCollection ();
54 // warn the developer or administrator that the provider list is empty
55 System
.Diagnostics
.Debug
.Listeners
.Add (new System
.Diagnostics
.TextWriterTraceListener (Console
.Out
));
56 System
.Diagnostics
.Debug
.WriteLine ("No providers found. Did you set up a mono.data/providers area in your app.config or in machine.config?");
61 static public ProviderCollection Providers
68 static public IDbConnection
CreateConnectionFromConfig (string Setting
)
71 throw new System
.ArgumentNullException ("Setting");
73 return CreateConnection (ConfigurationSettings
.AppSettings
[Setting
]);
76 static public IDbConnection
CreateConnection(string ConnectionString
)
78 if (ConnectionString
== null)
79 throw new System
.ArgumentNullException ("ConnectionString");
81 string [] ConnectionAttributes
= ConnectionString
.Split (new Char
[1] { ';' }
);
82 string ProviderName
= null;
83 string NewConnectionString
= "";
84 foreach (string s
in ConnectionAttributes
) {
85 string [] AttributeParts
= s
.Split (new Char
[1] { '=' }
);
86 if (AttributeParts
[0].ToLower ().Trim () == "factory")
87 ProviderName
= AttributeParts
[1].Trim ();
89 NewConnectionString
+= ";" + s
;
91 NewConnectionString
= NewConnectionString
.Remove (0, 1); // remove the initial semicolon
92 if (ProviderName
== null)
93 throw new System
.ArgumentException ("The connection string must contain a 'factory=Provider.Class' token", "ConnectionString");
94 return CreateConnection (ProviderName
, NewConnectionString
);
97 static public IDbConnection
CreateConnection(string ProviderName
, string ConnectionString
)
99 if (ProviderName
== null)
100 throw new System
.ArgumentNullException("ProviderName");
101 if (ConnectionString
== null)
102 throw new System
.ArgumentNullException ("ConnectionString");
104 Provider provider
= providers
[ProviderName
];
106 if (provider
== null)
107 throw new ArgumentException ("ProviderName", "The specified provider does not exist");
109 IDbConnection conn
= provider
.CreateConnection ();
110 conn
.ConnectionString
= ConnectionString
;
114 static public IDbCommand
CreateStoredProc (IDbConnection Conn
, string CommandName
)
117 throw new System
.ArgumentNullException ("Conn");
118 if (CommandName
== null)
119 throw new System
.ArgumentNullException ("CommandName");
121 IDbCommand cmd
= Conn
.CreateCommand ();
122 cmd
.CommandText
= CommandName
;
123 cmd
.CommandType
= CommandType
.StoredProcedure
;
127 static public IDbDataAdapter
CreateDataAdapter (IDbCommand SelectCommand
)
129 if (SelectCommand
== null)
130 throw new System
.ArgumentNullException("SelectCommand");
132 Provider provider
= providers
.FindByCommandType (SelectCommand
.GetType ());
133 IDbDataAdapter adapter
= provider
.CreateDataAdapter ();
134 adapter
.SelectCommand
= SelectCommand
;
138 static public IDbDataAdapter
CreateDataAdapter (string ProviderName
)
140 if (ProviderName
== null)
141 throw new System
.ArgumentNullException("ProviderName");
143 Provider provider
= providers
[ProviderName
];
144 IDbDataAdapter adapter
= provider
.CreateDataAdapter ();
148 static public IDbDataAdapter
CreateDataAdapter (IDbConnection Conn
, string SelectCommand
)
151 throw new System
.ArgumentNullException ("Conn");
152 if (SelectCommand
== null)
153 throw new System
.ArgumentNullException("SelectCommand");
155 IDbCommand cmd
= Conn
.CreateCommand ();
156 cmd
.CommandText
= SelectCommand
;
157 return CreateDataAdapter (cmd
);
160 static public IDbCommand
CreateCommand (string ProviderName
)
162 if (ProviderName
== null)
163 throw new System
.ArgumentNullException("ProviderName");
165 Provider provider
= providers
[ProviderName
];
166 return provider
.CreateCommand ();
169 static public IDbCommand
CreateCommand (IDbConnection Conn
)
172 throw new System
.ArgumentNullException("Conn");
174 return Conn
.CreateCommand ();