Ticket #952: database_client_charset.diff

File database_client_charset.diff, 5.8 KB (added by me@…, 19 years ago)

Automagic added

  • django/conf/project_template/settings.py

     
    1515DATABASE_PASSWORD = ''         # Not used with sqlite3.
    1616DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
    1717DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
     18DATABASE_CLIENT_CHARSET = ''   # Set to empty string to guess from DEFAULT_CHARSET. Not used with sqlite3.
    1819
    1920# Local time zone for this installation. All choices can be found here:
    2021# http://www.postgresql.org/docs/current/static/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE
  • django/conf/global_settings.py

     
    7878DATABASE_PASSWORD = ''         # Not used with sqlite3.
    7979DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
    8080DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
     81DATABASE_CLIENT_CHARSET = ''   # Set to empty string to guess from DEFAULT_CHARSET. Not used with sqlite3.
    8182
    8283# Host for sending e-mail.
    8384EMAIL_HOST = 'localhost'
  • django/core/db/backends/postgresql.py

     
    1313    def __init__(self):
    1414        self.connection = None
    1515        self.queries = []
     16        self.charset_mappings = {
     17            'utf-8':'UNICODE',
     18            'windows-1251':'WINDOWS1251',
     19            'windows-1250':'WINDOWS1250',
     20            'shift-jis':'SJIS',
     21            'koi8-r':'KOI8',
     22            'koi8-u':'KOI8',
     23        }
    1624
    1725    def cursor(self):
    18         from django.conf.settings import DATABASE_USER, DATABASE_NAME, DATABASE_HOST, DATABASE_PORT, DATABASE_PASSWORD, DEBUG, TIME_ZONE
     26        from django.conf.settings import DEFAULT_CHARSET, DATABASE_USER, DATABASE_NAME, DATABASE_HOST, \
     27                                         DATABASE_PORT, DATABASE_PASSWORD, DATABASE_CLIENT_CHARSET, DEBUG, TIME_ZONE
    1928        if self.connection is None:
    2029            if DATABASE_NAME == '':
    2130                from django.core.exceptions import ImproperlyConfigured
     
    3342            self.connection.set_isolation_level(1) # make transactions transparent to all cursors
    3443        cursor = self.connection.cursor()
    3544        cursor.execute("SET TIME ZONE %s", [TIME_ZONE])
     45       
     46        try:
     47            charset = self.charset_mappings[DEFAULT_CHARSET.lower()]
     48        except KeyError:
     49            if (not DATABASE_CLIENT_CHARSET):
     50                from django.core.exceptions import ImproperlyConfigured
     51                raise ImproperlyConfigured, 'You need to specify DATABASE_CHARSET in your Django settings \
     52                    file because Django cannot determine it automatically. You should set it to the name \
     53                    that PostgreSQL uses for the character set "'+DEFAULT_CHARSET+'"'
     54            else:
     55                charset = DATABASE_CLIENT_CHARSET
     56
     57        self.connection.cursor().execute("SET ENCODING "+charset)
     58       
    3659        if DEBUG:
    3760            return base.CursorDebugWrapper(cursor, self)
    3861        return cursor
  • django/core/db/backends/mysql.py

     
    5151    def __init__(self):
    5252        self.connection = None
    5353        self.queries = []
     54        self.charset_mappings = {
     55            'utf-8':'utf8',
     56            'windows-1251':'cp1251',
     57            'windows-1250':'cp1250',
     58            'shift-jis':'sjis',
     59            'utf-16':'ucs2',
     60            'koi8-r':'koi8r',
     61            'koi8-u':'koi8u',
     62        }
    5463
    5564    def cursor(self):
    56         from django.conf.settings import DATABASE_USER, DATABASE_NAME, DATABASE_HOST, DATABASE_PORT, DATABASE_PASSWORD, DEBUG
     65        from django.conf.settings import DEFAULT_CHARSET, DATABASE_USER, DATABASE_NAME, DATABASE_HOST, \
     66                                         DATABASE_PORT, DATABASE_PASSWORD, DATABASE_CLIENT_CHARSET, DEBUG
     67       
     68        # additionally import charset-related settings that might not be present
     69        from django.conf.settings import DEFAULT_CHARSET
     70       
    5771        if self.connection is None:
    5872            kwargs = {
    5973                'user': DATABASE_USER,
     
    6579            if DATABASE_PORT:
    6680                kwargs['port'] = DATABASE_PORT
    6781            self.connection = Database.connect(**kwargs)
     82
     83            try:
     84                charset = self.charset_mappings[DEFAULT_CHARSET.lower()]
     85            except KeyError:
     86                if (not DATABASE_CLIENT_CHARSET):
     87                    from django.core.exceptions import ImproperlyConfigured
     88                    raise ImproperlyConfigured, 'You need to specify DATABASE_CHARSET in your Django settings \
     89                        file because Django cannot determine it automatically. You should set it to the name \
     90                        that MySQL uses for the character set "'+DEFAULT_CHARSET+'"'
     91                else:
     92                    charset = DATABASE_CLIENT_CHARSET
     93            try:
     94                self.connection.cursor().execute("SET NAMES "+charset)
     95            except Database.OperationalError: # this is MySQL older than 4.1
     96                pass
     97               
    6898        if DEBUG:
    6999            return base.CursorDebugWrapper(MysqlDebugWrapper(self.connection.cursor()), self)
    70100        return self.connection.cursor()
Back to Top