public void ContainsKey()
        {
            MySqlConnectionStringBuilder s = new MySqlConnectionStringBuilder();

            s["database"] = "test";
            Assert.IsTrue(s.ContainsKey("initial catalog"));
            s["server"] = "myserver";
            Assert.IsTrue(s.ContainsKey("server"));
            Assert.IsTrue(s.ContainsKey("host"));
            Assert.IsFalse(s.ContainsKey("badkey"));
        }
Пример #2
0
        /// <summary>
        /// Initializes a new instance of the BaseSession class based on the specified anonymous type object.
        /// </summary>
        /// <param name="connectionData">The connection data as an anonymous type used to create the session.</param>
        /// <exception cref="ArgumentNullException"><paramref name="connectionData"/> is null.</exception>
        /// <remarks>
        /// <para>Multiple hosts can be specified as part of the <paramref name="connectionData"/>, which will enable client-side failover when trying to
        /// establish a connection.</para>
        /// <para>&#160;</para>
        /// <para>To assign multiple hosts create a property similar to the connection string examples (in basic format) shown in
        /// <see cref="BaseSession(string)"/>. Note that the value of the property must be a string.
        /// </para>
        /// </remarks>
        public BaseSession(object connectionData)
        {
            if (connectionData == null)
            {
                throw new ArgumentNullException("connectionData");
            }
            var values = Tools.GetDictionaryFromAnonymous(connectionData);

            if (!values.Keys.Any(s => s.ToLowerInvariant() == "port"))
            {
                values.Add("port", newDefaultPort);
            }
            Settings = new MySqlConnectionStringBuilder();
            bool hostsParsed = false;

            foreach (var value in values)
            {
                if (!Settings.ContainsKey(value.Key))
                {
                    throw new KeyNotFoundException(string.Format(ResourcesX.InvalidConnectionStringAttribute, value.Key));
                }
                Settings.SetValue(value.Key, value.Value);
                if (!hostsParsed && !string.IsNullOrEmpty(Settings["server"].ToString()))
                {
                    var server = value.Value.ToString();
                    if (IsUnixSocket(server))
                    {
                        Settings.SetValue(value.Key, server = NormalizeUnixSocket(server));
                    }
                    ParseHostList(server, false);
                    if (FailoverManager.FailoverGroup != null)
                    {
                        Settings["server"] = FailoverManager.FailoverGroup.ActiveHost.Host;
                    }
                    hostsParsed = true;
                }
            }
            this.connectionString = Settings.ToString();

            if (FailoverManager.FailoverGroup != null)
            {
                // Multiple hosts were specified.
                _internalSession = FailoverManager.AttemptConnection(this.connectionString, out this.connectionString);
                Settings         = new MySqlConnectionStringBuilder(this.connectionString);
            }
            else
            {
                _internalSession = InternalSession.GetSession(Settings);
            }

            if (!string.IsNullOrWhiteSpace(Settings.Database))
            {
                GetSchema(Settings.Database);
            }
        }
Пример #3
0
        /// <summary>
        /// Generate connection string with the given properties
        /// </summary>
        /// <param name="connectionDto">Connection details</param>
        /// <returns>Connection string of the database</returns>
        private static string GenerateConnectionString(ConnectionDto connectionDto)
        {
            DbConnectionStringBuilder connectionBuilder = null;

            string connectionString = string.Empty;

            connectionBuilder = new MySqlConnectionStringBuilder();

            if (connectionBuilder != null)
            {
                // Populate connection builder with connection properties
                foreach (KeyValuePair <string, string> property in connectionDto.Properties)
                {
                    if (connectionBuilder.ContainsKey(property.Key) && !string.IsNullOrEmpty(property.Value))
                    {
                        connectionBuilder.Add(property.Key, property.Value);
                    }
                }
                connectionString = connectionBuilder.ConnectionString;
                connectionDto.ConnectionString = connectionString;
            }
            return(connectionString);
        }
Пример #4
0
        public MySqlConnectionSettings(string connectionString)
        {
            var csb = new MySqlConnectionStringBuilder(connectionString);

            if (csb.GuidFormat == MySqlGuidFormat.Default)
            {
                GuidFormat = csb.OldGuids
                    ? MySqlGuidFormat.LittleEndianBinary16
                    : MySqlGuidFormat.Char36;
            }
            else
            {
                GuidFormat = csb.GuidFormat;
            }

            // It would be nice to have access to a public and currently non-existing
            // MySqlConnectionStringOption.TreatTinyAsBoolean.HasValue() method, so we can safely find out, whether
            // TreatTinyAsBoolean has been explicitly set or not.
            var treatTinyAsBooleanKeys = new[] { "Treat Tiny As Boolean", "TreatTinyAsBoolean" };

            TreatTinyAsBoolean = treatTinyAsBooleanKeys.Any(k => csb.ContainsKey(k))
                ? (bool?)csb.TreatTinyAsBoolean
                : null;
        }
 public void ContainsKey()
 {
     MySqlConnectionStringBuilder s = new MySqlConnectionStringBuilder();
     s["database"] = "test";
     Assert.IsTrue(s.ContainsKey("initial catalog"));
     s["server"] = "myserver";
     Assert.IsTrue(s.ContainsKey("server"));
     Assert.IsTrue(s.ContainsKey("host"));
     Assert.IsFalse(s.ContainsKey("badkey"));
 }