| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| import psycopg |
| from flask import current_app |
|
|
| encode_dict = { |
| 'SQL_ASCII': ['SQL_ASCII', 'raw-unicode-escape'], |
| 'SQLASCII': ['SQLASCII', 'raw-unicode-escape'], |
| |
| |
| 'EUC_TW': ['BIG5', 'big5'], |
| 'EUCTW': ['BIG5', 'big5'], |
| |
| 'UNICODE': ['utf-8', 'utf-8'] |
| } |
|
|
|
|
| def get_encoding(key): |
| """ |
| :param key: Database Encoding |
| :return: |
| [Postgres_encoding, Python_encoding] - |
| Postgres encoding, Python encoding, type cast encoding |
| """ |
| |
| |
| |
| if key == 'ascii': |
| key = 'raw_unicode_escape' |
| try: |
| postgres_encoding = psycopg._encodings.py2pgenc(key).decode() |
| except Exception as e: |
| |
| current_app.logger.error(e) |
| postgres_encoding = 'utf-8' |
|
|
| python_encoding = psycopg._encodings._py_codecs.get(postgres_encoding, |
| 'utf-8') |
|
|
| _dict = encode_dict.get(postgres_encoding.upper(), |
| [postgres_encoding, |
| python_encoding]) |
| return _dict |
|
|
|
|
| def configure_driver_encodings(encodings): |
| |
| |
| |
| |
| |
|
|
| for key, val in encode_dict.items(): |
| _, python_encoding = val |
| psycopg._encodings._py_codecs[key] = python_encoding |
|
|
| encodings.update((k.encode(), v |
| ) for k, v in psycopg._encodings._py_codecs.items()) |
| psycopg._encodings.pg_codecs = { |
| v: k.encode() for k, v in psycopg._encodings._py_codecs.items()} |
|
|