GetEncoding() public static method

Returns the text encoding for a given MySQL character set name
public static GetEncoding ( MySql.Data.Common.DBVersion version, string CharSetName ) : Encoding
version MySql.Data.Common.DBVersion Version of the connection requesting the encoding
CharSetName string Name of the character set to get the encoding for
return System.Text.Encoding
Ejemplo n.º 1
0
        public virtual void Configure(MySqlConnection connection)
        {
            this.connection = connection;

            bool firstConfigure = false;

            // if we have not already configured our server variables
            // then do so now
            if (serverProps == null)
            {
                firstConfigure = true;
                // load server properties
                serverProps = new Hashtable();
                MySqlCommand cmd = new MySqlCommand("SHOW VARIABLES", connection);

                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    try
                    {
                        while (reader.Read())
                        {
                            string key   = reader.GetString(0);
                            string value = reader.GetString(1);
                            serverProps[key] = value;
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.LogException(ex);
                        throw;
                    }
                }

                if (serverProps.Contains("max_allowed_packet"))
                {
                    maxPacketSize = Convert.ToInt64(serverProps["max_allowed_packet"]);
                }

                LoadCharacterSets();
            }

#if AUTHENTICATED
            string licenseType = serverProps["license"];
            if (licenseType == null || licenseType.Length == 0 ||
                licenseType != "commercial")
            {
                throw new MySqlException("This client library licensed only for use with commercially-licensed MySQL servers.");
            }
#endif
            // if the user has indicated that we are not to reset
            // the connection and this is not our first time through,
            // then we are done.
            if (!Settings.ConnectionReset && !firstConfigure)
            {
                return;
            }

            string charSet = connectionString.CharacterSet;
            if (charSet == null || charSet.Length == 0)
            {
                if (!version.isAtLeast(4, 1, 0))
                {
                    if (serverProps.Contains("character_set"))
                    {
                        charSet = serverProps["character_set"].ToString();
                    }
                }
                else
                {
                    if (serverCharSetIndex >= 0)
                    {
                        charSet = (string)charSets[serverCharSetIndex];
                    }
                    else
                    {
                        charSet = serverCharSet;
                    }
                }
            }

            // now tell the server which character set we will send queries in and which charset we
            // want results in
            if (version.isAtLeast(4, 1, 0))
            {
                MySqlCommand cmd = new MySqlCommand("SET character_set_results=NULL",
                                                    connection);
                object clientCharSet = serverProps["character_set_client"];
                object connCharSet   = serverProps["character_set_connection"];
                if ((clientCharSet != null && clientCharSet.ToString() != charSet) ||
                    (connCharSet != null && connCharSet.ToString() != charSet))
                {
                    cmd.CommandText = "SET NAMES " + charSet + ";" + cmd.CommandText;
                }
                cmd.ExecuteNonQuery();
            }

            if (charSet != null)
            {
                Encoding = CharSetMap.GetEncoding(version, charSet);
            }
            else
            {
                Encoding = CharSetMap.GetEncoding(version, "latin1");
            }
        }
Ejemplo n.º 2
0
        public virtual void Configure(MySqlConnection connection)
        {
            bool firstConfigure = false;

            // if we have not already configured our server variables
            // then do so now
            if (serverProps == null)
            {
                firstConfigure = true;

                // if we are in a pool and the user has said it's ok to cache the
                // properties, then grab it from the pool
                if (Pool != null && Settings.CacheServerProperties)
                {
                    if (Pool.ServerProperties == null)
                    {
                        Pool.ServerProperties = LoadServerProperties(connection);
                    }
                    serverProps = Pool.ServerProperties;
                }
                else
                {
                    serverProps = LoadServerProperties(connection);
                }

                LoadCharacterSets(connection);
            }

#if AUTHENTICATED
            string licenseType = serverProps["license"];
            if (licenseType == null || licenseType.Length == 0 ||
                licenseType != "commercial")
            {
                throw new MySqlException("This client library licensed only for use with commercially-licensed MySQL servers.");
            }
#endif
            // if the user has indicated that we are not to reset
            // the connection and this is not our first time through,
            // then we are done.
            if (!Settings.ConnectionReset && !firstConfigure)
            {
                return;
            }

            string charSet = connectionString.CharacterSet;
            if (charSet == null || charSet.Length == 0)
            {
                if (serverCharSetIndex >= 0)
                {
                    charSet = (string)charSets[serverCharSetIndex];
                }
                else
                {
                    charSet = serverCharSet;
                }
            }

            // now tell the server which character set we will send queries in and which charset we
            // want results in
            MySqlCommand charSetCmd = new MySqlCommand("SET character_set_results=NULL",
                                                       connection);
            charSetCmd.InternallyCreated = true;
            object clientCharSet = serverProps["character_set_client"];
            object connCharSet   = serverProps["character_set_connection"];
            if ((clientCharSet != null && clientCharSet.ToString() != charSet) ||
                (connCharSet != null && connCharSet.ToString() != charSet))
            {
                MySqlCommand setNamesCmd = new MySqlCommand("SET NAMES " + charSet, connection);
                setNamesCmd.InternallyCreated = true;
                setNamesCmd.ExecuteNonQuery();
            }
            charSetCmd.ExecuteNonQuery();

            if (charSet != null)
            {
                Encoding = CharSetMap.GetEncoding(Version, charSet);
            }
            else
            {
                Encoding = CharSetMap.GetEncoding(Version, "latin1");
            }

            handler.Configure();
        }
Ejemplo n.º 3
0
        public virtual void Configure(MySqlConnection connection)
        {
            bool firstConfigure = false;

            // if we have not already configured our server variables
            // then do so now
            if (serverProps == null)
            {
                firstConfigure = true;

                // if we are in a pool and the user has said it's ok to cache the
                // properties, then grab it from the pool
                try
                {
                    if (Pool != null && Settings.CacheServerProperties)
                    {
                        if (Pool.ServerProperties == null)
                        {
                            Pool.ServerProperties = LoadServerProperties(connection);
                        }
                        serverProps = Pool.ServerProperties;
                    }
                    else
                    {
                        serverProps = LoadServerProperties(connection);
                    }

                    LoadCharacterSets(connection);
                }
                catch (MySqlException ex)
                {
                    // expired password capability
                    if (ex.Number == 1820)
                    {
                        IsPasswordExpired = true;
                        return;
                    }
                    throw;
                }
            }


#if AUTHENTICATED
            string licenseType = serverProps["license"];
            if (licenseType == null || licenseType.Length == 0 ||
                licenseType != "commercial")
            {
                throw new MySqlException("This client library licensed only for use with commercially-licensed MySQL servers.");
            }
#endif
            // if the user has indicated that we are not to reset
            // the connection and this is not our first time through,
            // then we are done.
            if (!Settings.ConnectionReset && !firstConfigure)
            {
                return;
            }

            string charSet = ConnectionString.CharacterSet;
            if (string.IsNullOrEmpty(charSet))
            {
                if (ConnectionCharSetIndex >= 0 && CharacterSets.ContainsKey(ConnectionCharSetIndex))
                {
                    charSet = CharacterSets[ConnectionCharSetIndex];
                }
                else
                {
                    charSet = serverCharSet;
                }
            }

            if (serverProps.ContainsKey("max_allowed_packet"))
            {
                MaxPacketSize = Convert.ToInt64(serverProps["max_allowed_packet"]);
            }

            // now tell the server which character set we will send queries in and which charset we
            // want results in
            MySqlCommand charSetCmd = new MySqlCommand("SET character_set_results=NULL",
                                                       connection)
            {
                InternallyCreated = true
            };

            string clientCharSet;
            serverProps.TryGetValue("character_set_client", out clientCharSet);
            string connCharSet;
            serverProps.TryGetValue("character_set_connection", out connCharSet);
            if ((clientCharSet != null && clientCharSet.ToString() != charSet) ||
                (connCharSet != null && connCharSet.ToString() != charSet))
            {
                MySqlCommand setNamesCmd = new MySqlCommand("SET NAMES " + charSet, connection);
                setNamesCmd.InternallyCreated = true;
                setNamesCmd.ExecuteNonQuery();
            }
            // sets character_set_results to null to return values in their original character set
            charSetCmd.ExecuteNonQuery();

            Encoding = CharSetMap.GetEncoding(Version, charSet ?? "utf-8");

            handler.Configure();
        }
Ejemplo n.º 4
0
        public virtual void Configure(MySqlConnection connection)
        {
            bool flag = false;

            if (this.serverProps == null)
            {
                flag = true;
                try
                {
                    if (this.Pool != null && this.Settings.CacheServerProperties)
                    {
                        if (this.Pool.ServerProperties == null)
                        {
                            this.Pool.ServerProperties = this.LoadServerProperties(connection);
                        }
                        this.serverProps = this.Pool.ServerProperties;
                    }
                    else
                    {
                        this.serverProps = this.LoadServerProperties(connection);
                    }
                    this.LoadCharacterSets(connection);
                }
                catch (MySqlException arg_69_0)
                {
                    if (arg_69_0.Number == 1820)
                    {
                        this.IsPasswordExpired = true;
                        return;
                    }
                    throw;
                }
            }
            if (!this.Settings.ConnectionReset && !flag)
            {
                return;
            }
            string text = this.connectionString.CharacterSet;

            if (text == null || text.Length == 0)
            {
                if (this.serverCharSetIndex >= 0 && this.charSets.ContainsKey(this.serverCharSetIndex))
                {
                    text = this.charSets[this.serverCharSetIndex];
                }
                else
                {
                    text = this.serverCharSet;
                }
            }
            if (this.serverProps.ContainsKey("max_allowed_packet"))
            {
                this.maxPacketSize = Convert.ToInt64(this.serverProps["max_allowed_packet"]);
            }
            MySqlCommand expr_11A = new MySqlCommand("SET character_set_results=gbk", connection);

            expr_11A.InternallyCreated = true;
            string text2;

            this.serverProps.TryGetValue("character_set_client", out text2);
            string text3;

            this.serverProps.TryGetValue("character_set_connection", out text3);
            if ((text2 != null && text2.ToString() != text) || (text3 != null && text3.ToString() != text))
            {
                new MySqlCommand("SET NAMES " + text, connection)
                {
                    InternallyCreated = true
                }.ExecuteNonQuery();
            }
            expr_11A.ExecuteNonQuery();
            if (text != null)
            {
                this.Encoding = CharSetMap.GetEncoding(this.Version, text);
            }
            else
            {
                this.Encoding = CharSetMap.GetEncoding(this.Version, "gbk");
            }
            this.handler.Configure();
        }