public inbox for [email protected]  
help / color / mirror / Atom feed
[pgAdmin4][patch] SQL_ASCII database related codec fix
2+ messages / 2 participants
[nested] [flat]

* [pgAdmin4][patch] SQL_ASCII database related codec fix
@ 2019-01-10 12:30  Aditya Toshniwal <[email protected]>
  0 siblings, 1 reply; 2+ messages in thread

From: Aditya Toshniwal @ 2019-01-10 12:30 UTC (permalink / raw)
  To: pgadmin-hackers

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


^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: [pgAdmin4][patch] SQL_ASCII database related codec fix
@ 2019-01-11 16:26  Dave Page <[email protected]>
  parent: Aditya Toshniwal <[email protected]>
  0 siblings, 0 replies; 2+ messages in thread

From: Dave Page @ 2019-01-11 16:26 UTC (permalink / raw)
  To: Aditya Toshniwal <[email protected]>; +Cc: pgadmin-hackers

Thanks, applied.

On Thu, Jan 10, 2019 at 6:01 PM Aditya Toshniwal
<[email protected]> wrote:
>
> 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"



-- 
Dave Page
Blog: http://pgsnake.blogspot.com
Twitter: @pgsnake

EnterpriseDB UK: http://www.enterprisedb.com
The Enterprise PostgreSQL Company





^ permalink  raw  reply  [nested|flat] 2+ messages in thread


end of thread, other threads:[~2019-01-11 16:26 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-01-10 12:30 [pgAdmin4][patch] SQL_ASCII database related codec fix Aditya Toshniwal <[email protected]>
2019-01-11 16:26 ` Dave Page <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox