2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / ByteFX.Data / mysqlclient / ConnectionString.cs
1 using System;\r
2 using System.Collections;\r
3 using System.ComponentModel;\r
4 using System.Text;\r
5 using ByteFX.Data.Common;\r
6 \r
7 namespace ByteFX.Data.MySqlClient\r
8 {\r
9         internal enum ConnectionProtocol \r
10         {\r
11                 Sockets, NamedPipe, UnixSocket\r
12         }\r
13 \r
14         /// <summary>\r
15         /// Summary description for MySqlConnectionString.\r
16         /// </summary>\r
17         internal sealed class MySqlConnectionString : DBConnectionString\r
18         {\r
19                 private Hashtable       defaults;\r
20 \r
21                 public MySqlConnectionString() : base()\r
22                 {\r
23                 }\r
24 \r
25                 public MySqlConnectionString(string connectString) : this()\r
26                 {\r
27                         SetConnectionString( connectString );\r
28                 }\r
29 \r
30                 #region Server Properties\r
31                 [Browsable(false)]\r
32                 public string Name \r
33                 {\r
34                         get { return connectionName; }\r
35                         set { connectionName = value; }\r
36                 }\r
37 \r
38 \r
39                 [Category("Connection")]\r
40                 [Description("The name or IP address of the server to use")]\r
41                 public string Server \r
42                 {\r
43                         get { return GetString("host"); }\r
44                         set { keyValues["host"] = value; }\r
45                 }\r
46 \r
47                 [Category("Connection")]\r
48                 [Description("Port to use when connecting with sockets")]\r
49                 [DefaultValue(3306)]\r
50                 public int Port \r
51                 {\r
52                         get { return GetInt("port"); }\r
53                         set { keyValues["port"] = value; }\r
54                 }\r
55 \r
56                 [Category("Connection")]\r
57                 [Description("Protocol to use for connection to MySQL")]\r
58                 [DefaultValue(ConnectionProtocol.Sockets)]\r
59                 public ConnectionProtocol Protocol\r
60                 {\r
61                         get { return (ConnectionProtocol)keyValues["protocol"]; }\r
62                         set { keyValues["protocol"] = value; }\r
63                 }\r
64 \r
65                 [Category("Connection")]\r
66                 [Description("Name of pipe to use when connecting with named pipes (Win32 only)")]\r
67                 public string PipeName \r
68                 {\r
69                         get { return GetString("use pipe"); }\r
70                         set { keyValues["use pipe"] = value; }\r
71                 }\r
72 \r
73                 [Category("Connection")]\r
74                 [Description("Should the connection ues compression")]\r
75                 [DefaultValue(false)]\r
76                 public bool UseCompression \r
77                 {\r
78                         get { return GetBool("compress"); }\r
79                         set { keyValues["compress"] = value; }\r
80                 }\r
81 \r
82                 [Category("Connection")]\r
83                 [Description("Database to use initially")]\r
84                 [Editor("ByteFX.Data.MySqlClient.Design.DatabaseTypeEditor,MySqlClient.Design", typeof(System.Drawing.Design.UITypeEditor))]\r
85                 public string Database\r
86                 {\r
87                         get { return GetString("database"); }\r
88                         set { keyValues["database"] = value; }\r
89                 }\r
90 \r
91                 [Category("Connection")]\r
92                 [Description("Number of seconds to wait for the connection to succeed")]\r
93                 [DefaultValue(15)]\r
94                 public int ConnectionTimeout\r
95                 {\r
96                         get { return GetInt("connect timeout"); }\r
97                         set { keyValues["connect timeout"] = value; }\r
98                 }\r
99                 #endregion\r
100 \r
101                 #region Authentication Properties\r
102 \r
103                 [Category("Authentication")]\r
104                 [Description("The username to connect as")]\r
105                 public string UserId \r
106                 {\r
107                         get { return GetString("user id"); }\r
108                         set { keyValues["user id"] = value; }\r
109                 }\r
110 \r
111                 [Category("Authentication")]\r
112                 [Description("The password to use for authentication")]\r
113                 public string Password \r
114                 {\r
115                         get { return GetString("password"); }\r
116                         set { keyValues["password"] = value; }\r
117                 }\r
118 \r
119                 [Category("Authentication")]\r
120                 [Description("Should the connection use SSL.  This currently has no effect.")]\r
121                 [DefaultValue(false)]\r
122                 public bool UseSSL\r
123                 {\r
124                         get { return GetBool("use ssl"); }\r
125                         set { keyValues["use ssl"] = value; }\r
126                 }\r
127 \r
128                 [Category("Authentication")]\r
129                 [Description("Show user password in connection string")]\r
130                 [DefaultValue(false)]\r
131                 public bool PersistSecurityInfo \r
132                 {\r
133                         get { return GetBool("persist security info"); }\r
134                         set { keyValues["persist security info"] = value; }\r
135                 }\r
136                 #endregion\r
137 \r
138                 #region Pooling Properties\r
139 \r
140                 [Category("Pooling")]\r
141                 [Description("Should the connection support pooling")]\r
142                 [DefaultValue(true)]\r
143                 public bool Pooling \r
144                 {\r
145                         get { return GetBool("pooling"); }\r
146                         set { keyValues["pooling"] = value; }\r
147                 }\r
148 \r
149                 [Category("Pooling")]\r
150                 [Description("Minimum number of connections to have in this pool")]\r
151                 [DefaultValue(0)]\r
152                 public int MinPoolSize \r
153                 {\r
154                         get { return GetInt("min pool size"); }\r
155                         set { keyValues["min pool size"] = value; }\r
156                 }\r
157 \r
158                 [Category("Pooling")]\r
159                 [Description("Maximum number of connections to have in this pool")]\r
160                 [DefaultValue(100)]\r
161                 public int MaxPoolSize \r
162                 {\r
163                         get { return GetInt("max pool size"); }\r
164                         set { keyValues["max pool size"] = value; }\r
165                 }\r
166 \r
167                 [Category("Pooling")]\r
168                 [Description("Maximum number of seconds a connection should live.  This is checked when a connection is returned to the pool.")]\r
169                 [DefaultValue(0)]\r
170                 public int ConnectionLifetime \r
171                 {\r
172                         get { return GetInt("connection lifetime"); }\r
173                         set { keyValues["connection lifetime"] = value; }\r
174                 }\r
175 \r
176                 #endregion\r
177 \r
178 \r
179                 /// <summary>\r
180                 /// Takes a given connection string and returns it, possible\r
181                 /// stripping out the password info\r
182                 /// </summary>\r
183                 /// <returns></returns>\r
184                 public string GetConnectionString()\r
185                 {\r
186                         if (connectString == null) return CreateConnectionString();\r
187 \r
188                         StringBuilder str = new StringBuilder();\r
189                         Hashtable ht = ParseKeyValuePairs( connectString );\r
190 \r
191                         if (! PersistSecurityInfo) \r
192                                 ht.Remove("password");\r
193 \r
194                         foreach( string key in ht.Keys)\r
195                                 str.AppendFormat("{0}={1};", key, ht[key]);\r
196 \r
197                         if (str.Length > 0)\r
198                                 str.Remove( str.Length-1, 1 );\r
199 \r
200                         return str.ToString();\r
201                 }\r
202 \r
203                 /// <summary>\r
204                 /// Uses the values in the keyValues hash to create a\r
205                 /// connection string\r
206                 /// </summary>\r
207                 /// <returns></returns>\r
208                 public string CreateConnectionString()\r
209                 {\r
210                         string cStr = String.Empty;\r
211 \r
212                         Hashtable values = (Hashtable)keyValues.Clone();\r
213                         Hashtable defaultValues = GetDefaultValues();\r
214 \r
215                         if (!PersistSecurityInfo && values.Contains("password") )\r
216                                 values.Remove( "password" );\r
217 \r
218                         // we always return the server key.  It's not needed but \r
219                         // seems weird for it not to be there.\r
220                         cStr = "server=" + values["host"] + ";";\r
221                         values.Remove("server");\r
222 \r
223                         foreach (string key in values.Keys)\r
224                         {\r
225                                 if (!values[key].Equals( defaultValues[key]))\r
226                                         cStr += key + "=" + values[key] + ";";\r
227                         }\r
228 \r
229                         return cStr;\r
230                 }\r
231 \r
232                 protected override Hashtable GetDefaultValues()\r
233                 {\r
234                         defaults = base.GetDefaultValues();\r
235                         if (defaults == null)\r
236                         {\r
237                                 defaults = new Hashtable();\r
238                                 defaults["host"] = "localhost";\r
239                                 defaults["connect lifetime"] = 0;\r
240                                 defaults["user id"] = String.Empty;\r
241                                 defaults["password"] = String.Empty;\r
242                                 defaults["pooling"] = true;\r
243                                 defaults["min pool size"] = 0;\r
244                                 defaults["protocol"] = ConnectionProtocol.Sockets;\r
245                                 defaults["max pool size"] = 100;\r
246                                 defaults["connect timeout"] = 15;\r
247                                 defaults["port"] = 3306;\r
248                                 defaults["useSSL"] = false;\r
249                                 defaults["compress"] = false;\r
250                                 defaults["persist Security Info"] = false;\r
251                         }\r
252                         return (Hashtable)defaults.Clone();\r
253                 }\r
254 \r
255                 protected override bool ConnectionParameterParsed(Hashtable hash, string key, string value)\r
256                 {\r
257                         switch (key.ToLower()) \r
258                         {\r
259                                 case "use compression":\r
260                                 case "compress":\r
261                                         hash["compress"] = \r
262                                                 value.ToLower() == "yes" || value.ToLower() == "true";\r
263                                         return true;\r
264 \r
265                                 case "protocol":\r
266                                         if (value == "socket" || value == "tcp")\r
267                                                 hash["protocol"] = ConnectionProtocol.Sockets;\r
268                                         else if (value == "pipe")\r
269                                                 hash["protocol"] = ConnectionProtocol.NamedPipe;\r
270                                         else if (value == "unix")\r
271                                                 hash["protocol"] = ConnectionProtocol.UnixSocket;\r
272                                         return true;\r
273 \r
274                                 case "use pipe":\r
275                                 case "pipe":\r
276                                         hash["use pipe"] = value;\r
277                                         return true;\r
278                         }\r
279 \r
280                         if (! base.ConnectionParameterParsed(hash, key, value))\r
281                                 throw new ArgumentException("Keyword not supported: '" + key + "'");\r
282                         return true;\r
283                 }\r
284 \r
285         }\r
286 }\r