* support-test-*.cs: Rename from test-*-p2.cs.
[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                 }\r
52 \r
53                 static public ProviderCollection Providers\r
54                 {\r
55                         get\r
56                         {\r
57                                 return providers;\r
58                         }\r
59                 }\r
60 \r
61                 static public IDbConnection CreateConnectionFromConfig(string Setting)\r
62                 {\r
63                         return CreateConnection(ConfigurationSettings.AppSettings[Setting]);\r
64                 }\r
65 \r
66                 static public IDbConnection CreateConnection(string ConnectionString)\r
67                 {\r
68                         string[] ConnectionAttributes=ConnectionString.Split(new Char[1] { ';' }); \r
69                         string ProviderName=null;\r
70                         string NewConnectionString="";\r
71                         foreach (string s in ConnectionAttributes)\r
72                         {\r
73                                 string[] AttributeParts=s.Split(new Char[1] { '=' });\r
74                                 if (AttributeParts[0].ToLower().Trim()=="factory")\r
75                                         ProviderName=AttributeParts[1].Trim();\r
76                                 else \r
77                                         NewConnectionString+=";"+s;\r
78                         }\r
79                         NewConnectionString=NewConnectionString.Remove(0,1);\r
80                         return CreateConnection(ProviderName, NewConnectionString);\r
81                 }\r
82 \r
83                 static public IDbConnection CreateConnection(string ProviderName, string ConnectionString)\r
84                 {\r
85                         Provider provider=providers[ProviderName];\r
86                         IDbConnection conn=provider.CreateConnection();\r
87                         conn.ConnectionString=ConnectionString;\r
88                         return conn;\r
89                 }\r
90 \r
91                 static public IDbCommand CreateStoredProc(IDbConnection Conn, string CommandName)\r
92                 {\r
93                         IDbCommand cmd=Conn.CreateCommand();\r
94                         cmd.CommandText=CommandName;\r
95                         cmd.CommandType=CommandType.StoredProcedure;\r
96                         return cmd;\r
97                 }\r
98 \r
99                 static public IDbDataAdapter CreateDataAdapter(IDbCommand SelectCommand)\r
100                 {\r
101                         Provider provider=providers.FindByCommandType(SelectCommand.GetType());\r
102                         IDbDataAdapter adapter=provider.CreateDataAdapter();\r
103                         adapter.SelectCommand=SelectCommand;\r
104                         return adapter;\r
105                 }\r
106 \r
107                 static public IDbDataAdapter CreateDataAdapter(string ProviderName)\r
108                 {\r
109                         Provider provider=providers[ProviderName];\r
110                         IDbDataAdapter adapter=provider.CreateDataAdapter();\r
111                         return adapter;\r
112                 }\r
113 \r
114                 static public IDbDataAdapter CreateDataAdapter(IDbConnection Conn, string SelectCommand)\r
115                 {\r
116                         IDbCommand cmd=Conn.CreateCommand();\r
117                         cmd.CommandText=SelectCommand;\r
118                         return CreateDataAdapter(cmd);\r
119                 }\r
120 \r
121                 static public IDbCommand CreateCommand(string ProviderName)\r
122                 {\r
123                         Provider provider=providers[ProviderName];\r
124                         return provider.CreateCommand();\r
125                 }\r
126 \r
127                 static public IDbCommand CreateCommand(IDbConnection Conn)\r
128                 {\r
129                         return Conn.CreateCommand();\r
130                 }\r
131 \r
132         }\r
133 }\r