2 using System.Collections;
\r
3 using System.ComponentModel;
\r
5 using ByteFX.Data.Common;
\r
7 namespace ByteFX.Data.MySqlClient
\r
9 internal enum ConnectionProtocol
\r
11 Sockets, NamedPipe, UnixSocket
\r
15 /// Summary description for MySqlConnectionString.
\r
17 internal sealed class MySqlConnectionString : DBConnectionString
\r
19 private Hashtable defaults;
\r
21 public MySqlConnectionString() : base()
\r
25 public MySqlConnectionString(string connectString) : this()
\r
27 SetConnectionString( connectString );
\r
30 #region Server Properties
\r
34 get { return connectionName; }
\r
35 set { connectionName = value; }
\r
39 [Category("Connection")]
\r
40 [Description("The name or IP address of the server to use")]
\r
41 public string Server
\r
43 get { return GetString("host"); }
\r
44 set { keyValues["host"] = value; }
\r
47 [Category("Connection")]
\r
48 [Description("Port to use when connecting with sockets")]
\r
49 [DefaultValue(3306)]
\r
52 get { return GetInt("port"); }
\r
53 set { keyValues["port"] = value; }
\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
61 get { return (ConnectionProtocol)keyValues["protocol"]; }
\r
62 set { keyValues["protocol"] = value; }
\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
69 get { return GetString("use pipe"); }
\r
70 set { keyValues["use pipe"] = value; }
\r
73 [Category("Connection")]
\r
74 [Description("Should the connection ues compression")]
\r
75 [DefaultValue(false)]
\r
76 public bool UseCompression
\r
78 get { return GetBool("compress"); }
\r
79 set { keyValues["compress"] = value; }
\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
87 get { return GetString("database"); }
\r
88 set { keyValues["database"] = value; }
\r
91 [Category("Connection")]
\r
92 [Description("Number of seconds to wait for the connection to succeed")]
\r
94 public int ConnectionTimeout
\r
96 get { return GetInt("connect timeout"); }
\r
97 set { keyValues["connect timeout"] = value; }
\r
101 #region Authentication Properties
\r
103 [Category("Authentication")]
\r
104 [Description("The username to connect as")]
\r
105 public string UserId
\r
107 get { return GetString("user id"); }
\r
108 set { keyValues["user id"] = value; }
\r
111 [Category("Authentication")]
\r
112 [Description("The password to use for authentication")]
\r
113 public string Password
\r
115 get { return GetString("password"); }
\r
116 set { keyValues["password"] = value; }
\r
119 [Category("Authentication")]
\r
120 [Description("Should the connection use SSL. This currently has no effect.")]
\r
121 [DefaultValue(false)]
\r
124 get { return GetBool("use ssl"); }
\r
125 set { keyValues["use ssl"] = value; }
\r
128 [Category("Authentication")]
\r
129 [Description("Show user password in connection string")]
\r
130 [DefaultValue(false)]
\r
131 public bool PersistSecurityInfo
\r
133 get { return GetBool("persist security info"); }
\r
134 set { keyValues["persist security info"] = value; }
\r
138 #region Pooling Properties
\r
140 [Category("Pooling")]
\r
141 [Description("Should the connection support pooling")]
\r
142 [DefaultValue(true)]
\r
143 public bool Pooling
\r
145 get { return GetBool("pooling"); }
\r
146 set { keyValues["pooling"] = value; }
\r
149 [Category("Pooling")]
\r
150 [Description("Minimum number of connections to have in this pool")]
\r
152 public int MinPoolSize
\r
154 get { return GetInt("min pool size"); }
\r
155 set { keyValues["min pool size"] = value; }
\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
163 get { return GetInt("max pool size"); }
\r
164 set { keyValues["max pool size"] = value; }
\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
170 public int ConnectionLifetime
\r
172 get { return GetInt("connection lifetime"); }
\r
173 set { keyValues["connection lifetime"] = value; }
\r
180 /// Takes a given connection string and returns it, possible
\r
181 /// stripping out the password info
\r
183 /// <returns></returns>
\r
184 public string GetConnectionString()
\r
186 if (connectString == null) return CreateConnectionString();
\r
188 StringBuilder str = new StringBuilder();
\r
189 Hashtable ht = ParseKeyValuePairs( connectString );
\r
191 if (! PersistSecurityInfo)
\r
192 ht.Remove("password");
\r
194 foreach( string key in ht.Keys)
\r
195 str.AppendFormat("{0}={1};", key, ht[key]);
\r
197 if (str.Length > 0)
\r
198 str.Remove( str.Length-1, 1 );
\r
200 return str.ToString();
\r
204 /// Uses the values in the keyValues hash to create a
\r
205 /// connection string
\r
207 /// <returns></returns>
\r
208 public string CreateConnectionString()
\r
210 string cStr = String.Empty;
\r
212 Hashtable values = (Hashtable)keyValues.Clone();
\r
213 Hashtable defaultValues = GetDefaultValues();
\r
215 if (!PersistSecurityInfo && values.Contains("password") )
\r
216 values.Remove( "password" );
\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
223 foreach (string key in values.Keys)
\r
225 if (!values[key].Equals( defaultValues[key]))
\r
226 cStr += key + "=" + values[key] + ";";
\r
232 protected override Hashtable GetDefaultValues()
\r
234 defaults = base.GetDefaultValues();
\r
235 if (defaults == null)
\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
252 return (Hashtable)defaults.Clone();
\r
255 protected override bool ConnectionParameterParsed(Hashtable hash, string key, string value)
\r
257 switch (key.ToLower())
\r
259 case "use compression":
\r
261 hash["compress"] =
\r
262 value.ToLower() == "yes" || value.ToLower() == "true";
\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
276 hash["use pipe"] = value;
\r
280 if (! base.ConnectionParameterParsed(hash, key, value))
\r
281 throw new ArgumentException("Keyword not supported: '" + key + "'");
\r