Esempio n. 1
0
    def get_charset_info(cls, charset=None, collation=None):
        """Get character set information using charset name and/or collation
        
        Retrieves character set and collation information given character
        set name and/or a collation name.
        If charset is an integer, it will look up the character set based
        on the MySQL's ID.
        For example:
            get_charset_info('utf8',None)
            get_charset_info(collation='utf8_general_ci')
            get_charset_info(47)
        
        Raises ProgrammingError when character set is not supported.

        Returns a tuple with (id, characterset name, collation)
        """
        idx = None

        if isinstance(charset, int):
            try:
                info = cls.desc[charset]
                return (charset, info[0], info[1])
            except IndexError:
                ProgrammingError(
                    "Character set ID {0} unknown.".format(charset))

        if charset is not None and collation is None:
            info = cls.get_default_collation(charset)
            return (info[2], info[1], info[0])
        elif charset is None and collation is not None:
            for cid, info in enumerate(cls.desc):
                if info is None:
                    continue
                if collation == info[1]:
                    return (cid, info[0], info[1])
            raise ProgrammingError(
                "Collation '{0}' unknown.".format(collation))
        else:
            for cid, info in enumerate(cls.desc):
                if info is None:
                    continue
                if info[0] == charset and info[1] == collation:
                    return (cid, info[0], info[1])
            raise ProgrammingError(
                "Character set '{0}' unknown.".format(charset))
Esempio n. 2
0
    def get_default_collation(cls, charset):
        """Retrieves the default collation for given character set

      Raises ProgrammingError when character set is not supported.

      Returns list (collation, charset, index)
      """
        if isinstance(charset, int):
            try:
                c = cls.desc[charset]
                return c[1], c[0], charset
            except:
                ProgrammingError("Character set ID '%s' unsupported." %
                                 (charset))

        for cid, c in enumerate(cls.desc):
            if c is None:
                continue
            if c[0] == charset and c[2] is True:
                return c[1], c[0], cid

        raise ProgrammingError("Character set '%s' unsupported." % (charset))
Esempio n. 3
0
    def get_info(cls, setid):
        """Retrieves character set information as tuple using an ID
        
        Retrieves character set and collation information based on the
        given MySQL ID.

        Returns a tuple.
        """
        try:
            r = cls.desc[setid]
            if r is None:
                raise
            return r[0:2]
        except:
            raise ProgrammingError("Character set '%d' unsupported" % (setid))
Esempio n. 4
0
    def get_info(cls, setid):
        """Retrieves character set information as tuple using an ID

        Retrieves character set and collation information based on the
        given MySQL ID.

        Raises ProgrammingError when character set is not supported.

        Returns a tuple.
        """
        try:
            return cls.desc[setid][0:2]
        except IndexError:
            raise ProgrammingError(
                "Character set '{0}' unsupported".format(setid))