public inbox for [email protected]
help / color / mirror / Atom feedFrom: Aditya Toshniwal <[email protected]>
To: pgadmin-hackers <[email protected]>
Subject: [pgAdmin4][patch] SQL_ASCII database related codec fix
Date: Thu, 10 Jan 2019 18:00:48 +0530
Message-ID: <CAM9w-_k+vS2zZq3_mtLYhjNxKUrydjr0mnxrQJ0sCVdVoRzcWw@mail.gmail.com> (raw)
Hi Hackers,
Attached is the patch to make SQL_ASCII database handling on pgAdmin more
robust.
Kindly review.
--
Thanks and Regards,
Aditya Toshniwal
Software Engineer | EnterpriseDB Software Solutions | Pune
"Don't Complain about Heat, Plant a tree"
Attachments:
[application/octet-stream] sqlascii.fix.patch (5.7K, 3-sqlascii.fix.patch)
download | inline diff:
diff --git a/web/pgadmin/utils/driver/psycopg2/connection.py b/web/pgadmin/utils/driver/psycopg2/connection.py
index dcf4b0e6..a3570f5a 100644
--- a/web/pgadmin/utils/driver/psycopg2/connection.py
+++ b/web/pgadmin/utils/driver/psycopg2/connection.py
@@ -36,7 +36,7 @@ from .cursor import DictCursor
from .typecast import register_global_typecasters, \
register_string_typecasters, register_binary_typecasters, \
register_array_to_string_typecasters, ALL_JSON_TYPES
-from .encoding import getEncoding
+from .encoding import getEncoding, configureDriverEncodings
from pgadmin.utils import csv
if sys.version_info < (3,):
@@ -50,7 +50,7 @@ _ = gettext
# Register global type caster which will be applicable to all connections.
register_global_typecasters()
-
+configureDriverEncodings(encodings)
class Connection(BaseConnection):
"""
@@ -408,15 +408,6 @@ class Connection(BaseConnection):
"SET client_encoding='{0}';"
.format(postgres_encoding))
- # Replace the python encoding for original name and renamed encodings
- # psycopg2 removes the underscore in conn.encoding
- # Setting the encodings dict value will only help for select statements
- # because for parameterized DML, param values are converted based on
- # python encoding of pyscopg2s internal encodings dict.
- for key, val in encodings.items():
- if key.replace('_', '') == self.conn.encoding:
- encodings[key] = self.python_encoding
-
if status is not None:
self.conn.close()
self.conn = None
diff --git a/web/pgadmin/utils/driver/psycopg2/cursor.py b/web/pgadmin/utils/driver/psycopg2/cursor.py
index c83f2854..d78ab4b0 100644
--- a/web/pgadmin/utils/driver/psycopg2/cursor.py
+++ b/web/pgadmin/utils/driver/psycopg2/cursor.py
@@ -18,7 +18,10 @@ try:
except ImportError:
from ordereddict import OrderedDict
-from psycopg2.extensions import cursor as _cursor
+from psycopg2.extensions import cursor as _cursor, encodings
+from .encoding import configureDriverEncodings
+
+configureDriverEncodings(encodings)
class _WrapperColumn(object):
diff --git a/web/pgadmin/utils/driver/psycopg2/encoding.py b/web/pgadmin/utils/driver/psycopg2/encoding.py
index f7d6e46f..33a2c90f 100644
--- a/web/pgadmin/utils/driver/psycopg2/encoding.py
+++ b/web/pgadmin/utils/driver/psycopg2/encoding.py
@@ -9,6 +9,23 @@
# Get Postgres and Python encoding
+encode_dict = {
+ 'SQL_ASCII': ['SQL_ASCII', 'raw_unicode_escape'],
+ 'SQLASCII': ['SQL_ASCII', 'raw_unicode_escape'],
+ 'MULE_INTERNAL': ['MULE_INTERNAL', 'raw_unicode_escape'],
+ 'MULEINTERNAL': ['MULEINTERNAL', 'raw_unicode_escape'],
+ 'LATIN1': ['LATIN1', 'latin1'],
+ 'LATIN2': ['LATIN2', 'latin2'],
+ 'LATIN3': ['LATIN3', 'latin3'],
+ 'LATIN4': ['LATIN4', 'latin4'],
+ 'LATIN5': ['LATIN5', 'latin5'],
+ 'LATIN6': ['LATIN6', 'latin6'],
+ 'LATIN7': ['LATIN7', 'latin7'],
+ 'LATIN8': ['LATIN8', 'latin8'],
+ 'LATIN9': ['LATIN9', 'latin9'],
+ 'LATIN10': ['LATIN10', 'latin10']
+}
+
def getEncoding(key):
"""
@@ -16,21 +33,14 @@ def getEncoding(key):
:return:
[Postgres_encoding, Python_encoding] - Postgres and Python encoding
"""
- encode_dict = {
- 'SQL_ASCII': ['SQL_ASCII', 'raw_unicode_escape'],
- 'SQLASCII': ['SQL_ASCII', 'raw_unicode_escape'],
- 'MULE_INTERNAL': ['MULE_INTERNAL', 'raw_unicode_escape'],
- 'MULEINTERNAL': ['MULEINTERNAL', 'raw_unicode_escape'],
- 'LATIN1': ['LATIN1', 'latin1'],
- 'LATIN2': ['LATIN2', 'latin2'],
- 'LATIN3': ['LATIN3', 'latin3'],
- 'LATIN4': ['LATIN4', 'latin4'],
- 'LATIN5': ['LATIN5', 'latin5'],
- 'LATIN6': ['LATIN6', 'latin6'],
- 'LATIN7': ['LATIN7', 'latin7'],
- 'LATIN8': ['LATIN8', 'latin8'],
- 'LATIN9': ['LATIN9', 'latin9'],
- 'LATIN10': ['LATIN10', 'latin10']
- }
-
return encode_dict.get(key, ['UNICODE', 'utf-8'])
+
+def configureDriverEncodings(encodings):
+ # Replace the python encoding for original name and renamed encodings
+ # psycopg2 removes the underscore in conn.encoding
+ # Setting the encodings dict value will only help for select statements
+ # because for parameterized DML, param values are converted based on
+ # python encoding of pyscopg2s internal encodings dict.
+ for key, val in encode_dict.items():
+ postgres_encoding, python_encoding = val
+ encodings[key] = python_encoding
diff --git a/web/pgadmin/utils/driver/psycopg2/typecast.py b/web/pgadmin/utils/driver/psycopg2/typecast.py
index 0e38e034..81cdcfec 100644
--- a/web/pgadmin/utils/driver/psycopg2/typecast.py
+++ b/web/pgadmin/utils/driver/psycopg2/typecast.py
@@ -18,6 +18,10 @@ from psycopg2 import STRING as _STRING
import psycopg2
from psycopg2.extensions import encodings
+from .encoding import configureDriverEncodings
+
+configureDriverEncodings(encodings)
+
# OIDs of data types which need to typecast as string to avoid JavaScript
# compatibility issues.
@@ -176,12 +180,12 @@ def register_string_typecasters(connection):
return None
return bytes(
value, encodings[cursor.connection.encoding]
- ).decode('raw_unicode_escape')
+ ).decode('raw_unicode_escape', errors='ignore')
else:
def non_ascii_escape(value, cursor):
if value is None:
return None
- return value.decode('raw_unicode_escape')
+ return value.decode('raw_unicode_escape', errors='ignore')
unicode_type = psycopg2.extensions.new_type(
# "char", name, text, character, character varying
view thread (2+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected]
Subject: Re: [pgAdmin4][patch] SQL_ASCII database related codec fix
In-Reply-To: <CAM9w-_k+vS2zZq3_mtLYhjNxKUrydjr0mnxrQJ0sCVdVoRzcWw@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox