2007-12-18 Carlos Alberto Cortez <calberto.cortez@gmail.com>
[mono.git] / mcs / class / Mono.Data / ProviderFactory.cs
1 //\r
2 // Mono.Data.ProviderFactory\r
3 //\r
4 // Authors:\r
5 //   Brian Ritchie (brianlritchie@hotmail.com) \r
6 //  \r
7 //\r
8 // Copyright (C) Brian Ritchie, 2002\r
9 // \r
10 //\r
11 \r
12 //\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
20 // \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
23 // \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
31 //\r
32 using System;\r
33 using System.Data;\r
34 using System.Reflection;\r
35 using System.Runtime.Remoting;\r
36 using System.Configuration;\r
37 using System.Xml;\r
38 using System.Collections.Specialized;\r
39 \r
40 namespace Mono.Data\r
41 {\r
42         public class ProviderFactory\r
43         {\r
44                 private static ProviderCollection providers;\r
45 \r
46                 static ProviderFactory ()\r
47                 {\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
54                         }\r
55 \r
56                 }\r
57 \r
58                 static public ProviderCollection Providers\r
59                 {\r
60                         get {\r
61                                 return providers;\r
62                         }\r
63                 }\r
64 \r
65                 static public IDbConnection CreateConnectionFromConfig (string Setting)\r
66                 {\r
67                         if (Setting == null) \r
68                                 throw new System.ArgumentNullException ("Setting");\r
69 \r
70                         return CreateConnection (ConfigurationSettings.AppSettings [Setting]);\r
71                 }\r
72 \r
73                 static public IDbConnection CreateConnection(string ConnectionString)\r
74                 {\r
75                         if (ConnectionString == null) \r
76                                 throw new System.ArgumentNullException ("ConnectionString");\r
77 \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
85                                 else \r
86                                         NewConnectionString += ";" + s;\r
87                         }\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
92                 }\r
93 \r
94                 static public IDbConnection CreateConnection(string ProviderName, string ConnectionString)\r
95                 {\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
100 \r
101                         Provider provider = providers [ProviderName];\r
102                         IDbConnection conn = provider.CreateConnection ();\r
103                         conn.ConnectionString = ConnectionString;\r
104                         return conn;\r
105                 }\r
106 \r
107                 static public IDbCommand CreateStoredProc (IDbConnection Conn, string CommandName)\r
108                 {\r
109                         if (Conn == null) \r
110                                 throw new System.ArgumentNullException ("Conn");\r
111                         if (CommandName == null) \r
112                                 throw new System.ArgumentNullException ("CommandName");\r
113 \r
114                         IDbCommand cmd = Conn.CreateCommand ();\r
115                         cmd.CommandText = CommandName;\r
116                         cmd.CommandType = CommandType.StoredProcedure;\r
117                         return cmd;\r
118                 }\r
119 \r
120                 static public IDbDataAdapter CreateDataAdapter (IDbCommand SelectCommand)\r
121                 {\r
122                         if (SelectCommand == null) \r
123                                 throw new System.ArgumentNullException("SelectCommand");\r
124 \r
125                         Provider provider = providers.FindByCommandType (SelectCommand.GetType ());\r
126                         IDbDataAdapter adapter = provider.CreateDataAdapter ();\r
127                         adapter.SelectCommand = SelectCommand;\r
128                         return adapter;\r
129                 }\r
130 \r
131                 static public IDbDataAdapter CreateDataAdapter (string ProviderName)\r
132                 {\r
133                         if (ProviderName == null) \r
134                                 throw new System.ArgumentNullException("ProviderName");\r
135 \r
136                         Provider provider = providers [ProviderName];\r
137                         IDbDataAdapter adapter = provider.CreateDataAdapter ();\r
138                         return adapter;\r
139                 }\r
140 \r
141                 static public IDbDataAdapter CreateDataAdapter (IDbConnection Conn, string SelectCommand)\r
142                 {\r
143                         if (Conn == null) \r
144                                 throw new System.ArgumentNullException ("Conn");\r
145                         if (SelectCommand == null) \r
146                                 throw new System.ArgumentNullException("SelectCommand");\r
147 \r
148                         IDbCommand cmd = Conn.CreateCommand ();\r
149                         cmd.CommandText = SelectCommand;\r
150                         return CreateDataAdapter (cmd);\r
151                 }\r
152 \r
153                 static public IDbCommand CreateCommand (string ProviderName)\r
154                 {\r
155                         if (ProviderName == null) \r
156                                 throw new System.ArgumentNullException("ProviderName");\r
157 \r
158                         Provider provider = providers [ProviderName];\r
159                         return provider.CreateCommand ();\r
160                 }\r
161 \r
162                 static public IDbCommand CreateCommand (IDbConnection Conn)\r
163                 {\r
164                         if (Conn == null) \r
165                                 throw new System.ArgumentNullException("Conn");\r
166 \r
167                         return Conn.CreateCommand ();\r
168                 }\r
169 \r
170         }\r
171 }\r