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