public inbox for [email protected]  
help / color / mirror / Atom feed
From: Aditya Toshniwal <[email protected]>
To: Joao De Almeida Pereira <[email protected]>
Cc: Dave Page <[email protected]>
Cc: pgadmin-hackers <[email protected]>
Subject: Re: [pgAdmin4][RM#3289] Can't query SQL_ASCII database.
Date: Tue, 5 Jun 2018 14:20:20 +0530
Message-ID: <CAM9w-_=c=82+5ESAEMXUQf5NOrfouZ5JxJMfr+56vJAfGz2DrA@mail.gmail.com> (raw)
In-Reply-To: <CAM9w-_km5Zv=Gw6CUZF8m3zzacOKL-ir-QNw3xPjP2MJfLRg7w@mail.gmail.com>
References: <CAM9w-_ntCUguZp2rVJXPsDvWJbWneAz4-AHJoYB+nUTSYMXc9g@mail.gmail.com>
	<CA+OCxozRjJY6RWrXpMLD6-dWnm2LdPs6bS+VmJa403vxVoLwhg@mail.gmail.com>
	<CAM9w-_m-cJax16zqBonx-d0-HSy5dNoOzB13HWAad5Xv0Wu=CA@mail.gmail.com>
	<CANxYE3+qrGeZYzdYEtzxS8i2P+325V5E+n1xVpyzTwn7vRqRDg@mail.gmail.com>
	<CAM9w-_mTe0gW+2cdhMutMWyBsRt7Om3G_Em126Ryntf0nOiHbA@mail.gmail.com>
	<CAM9w-_mXdJhuRFn87yAuqWtfrhnRgw1KvU2PpthL-hbnOt2QpA@mail.gmail.com>
	<CAE+jjampAPoDq7xJgeNf1L=i_1jf83PLr=dZYicfkEMp6_UBAg@mail.gmail.com>
	<CAM9w-_k2Fo-qDy+qxM5i=Aba__TAWxrryR+m3izTXB=koToKcQ@mail.gmail.com>
	<CA+OCxoz+Wo3pUuu8SMBzAsQvrOjYLPQHCfQMtO3X3HKezKcapw@mail.gmail.com>
	<CAM9w-_nEDycbeEEeUhKwXjxmkNC39YzqhggJR3Frc4pq6WcJ0Q@mail.gmail.com>
	<CAM9w-_kDQkAWRZrRRtmMDz8DZ5x2wGoUro6oQbQqTzLmZZGyBw@mail.gmail.com>
	<CAE+jjametYGjStNFZFW544Jcm_by1OABtb7yFQWKGzyfk08QiA@mail.gmail.com>
	<CAM9w-_=rsazJWCu5xRqLGkYX3FW22vVuyc0NcR5TPskO9PApUg@mail.gmail.com>
	<CAE+jjakwbfsbwkvwa6N1QGjGeVb36sWnde+C6amnHQLHdTE3kQ@mail.gmail.com>
	<CAM9w-_km5Zv=Gw6CUZF8m3zzacOKL-ir-QNw3xPjP2MJfLRg7w@mail.gmail.com>

Hi Hackers,

PFA updated patch. The sqleditor change is sent separately and removed from
current patch as suggested.
The test cases were running fine when the module was specified using --pkg
but were failing in complete run. Fixed that.

Kindly review.

Thanks and Regards,
Aditya Toshniwal
Software Engineer | EnterpriseDB Software Solutions | Pune
"Don't Complain about Heat, Plant a tree"

On Tue, Jun 5, 2018 at 10:15 AM, Aditya Toshniwal <
[email protected]> wrote:

> Hi
>
> On Tue, Jun 5, 2018 at 1:08 AM, Joao De Almeida Pereira <
> [email protected]> wrote:
>
>> Hello Aditya,
>>
>>>
>>>
>>> There is no change related to notifications in this patch.
>>> The below code is minor fix related to connection status of sql editor.
>>> Can you please share the code snippet if it is not the below.
>>>
>>> -        # Check for the asynchronous notifies statements.
>>> -        conn.check_notifies(True)
>>> -        notifies = conn.get_notifies()
>>> +        if status is not None:
>>> +            # Check for the asynchronous notifies statements.
>>> +            conn.check_notifies(True)
>>> +            notifies = conn.get_notifies()
>>>
>>>
>> This is a minor fix, but is it related to querying SQL_ASCII database?
>>
> No its not. It is something I found when I was working on SQL_ASCII
> related changes.
> Well then, will send a separate patch for it.
>
>>
>> Thanks
>> Victoria && Joao
>>
>
>


Attachments:

  [application/octet-stream] RM3289.patch (7.9K, 3-RM3289.patch)
  download | inline diff:
diff --git a/web/pgadmin/tools/sqleditor/tests/test_encoding_charset.py b/web/pgadmin/tools/sqleditor/tests/test_encoding_charset.py
new file mode 100644
index 00000000..833e471e
--- /dev/null
+++ b/web/pgadmin/tools/sqleditor/tests/test_encoding_charset.py
@@ -0,0 +1,121 @@
+##########################################################################
+#
+# pgAdmin 4 - PostgreSQL Tools
+#
+# Copyright (C) 2013 - 2018, The pgAdmin Development Team
+# This software is released under the PostgreSQL Licence
+#
+##########################################################################
+
+from pgadmin.utils.route import BaseTestGenerator
+from pgadmin.browser.server_groups.servers.databases.tests import utils as \
+    database_utils
+from regression import parent_node_dict
+from regression.python_test_utils import test_utils
+import json
+
+
+class TestEncodingCharset(BaseTestGenerator):
+    """
+    This class validates character support in pgAdmin4 for
+    different PostgresDB encodings
+    """
+    scenarios = [
+        (
+            'With Encoding UTF8',
+            dict(
+                db_encoding='UTF8',
+                lc_collate='C',
+                test_str='A',
+                set_client_encoding=False
+            )),
+        (
+            'With Encoding WIN1252',
+            dict(
+                db_encoding='WIN1252',
+                lc_collate='C',
+                test_str='A',
+                set_client_encoding=False
+            )),
+        (
+            'With Encoding EUC_CN',
+            dict(
+                db_encoding='EUC_CN',
+                lc_collate='C',
+                test_str='A',
+                set_client_encoding=False
+            )),
+        (
+            'With Encoding SQL_ASCII',
+            dict(
+                db_encoding='SQL_ASCII',
+                lc_collate='C',
+                test_str='\\255',
+                set_client_encoding=True
+            )),
+    ]
+
+    def setUp(self):
+        self.encode_db_name = 'encoding_' + self.db_encoding
+        self.encode_sid = self.server_information['server_id']
+        self.encode_did = test_utils.create_database(
+            self.server, self.encode_db_name,
+            (self.db_encoding, self.lc_collate))
+
+    def runTest(self):
+
+        db_con = database_utils.connect_database(self,
+                                                 test_utils.SERVER_GROUP,
+                                                 self.encode_sid,
+                                                 self.encode_did)
+        if not db_con["info"] == "Database connected.":
+            raise Exception("Could not connect to the database.")
+
+        # Initialize query tool
+        url = '/datagrid/initialize/query_tool/{0}/{1}/{2}'.format(
+            test_utils.SERVER_GROUP, self.encode_sid, self.encode_did)
+        response = self.tester.post(url)
+        self.assertEquals(response.status_code, 200)
+
+        response_data = json.loads(response.data.decode('utf-8'))
+        self.trans_id = response_data['data']['gridTransId']
+
+        if self.set_client_encoding:
+            # Change Client Encoding
+            url = "/sqleditor/query_tool/start/{0}".format(self.trans_id)
+            sql = "set client_encoding = '{0}';".format(self.db_encoding)
+            response = self.tester.post(url, data=json.dumps({"sql": sql}),
+                                        content_type='html/json')
+            self.assertEquals(response.status_code, 200)
+            url = '/sqleditor/poll/{0}'.format(self.trans_id)
+            response = self.tester.get(url)
+            self.assertEquals(response.status_code, 200)
+            response_data = json.loads(response.data.decode('utf-8'))
+
+        # Check character
+        url = "/sqleditor/query_tool/start/{0}".format(self.trans_id)
+        sql = "select E'{0}';".format(self.test_str)
+        response = self.tester.post(url, data=json.dumps({"sql": sql}),
+                                    content_type='html/json')
+        self.assertEquals(response.status_code, 200)
+        url = '/sqleditor/poll/{0}'.format(self.trans_id)
+        response = self.tester.get(url)
+        self.assertEquals(response.status_code, 200)
+        response_data = json.loads(response.data.decode('utf-8'))
+        self.assertEquals(response_data['data']['rows_fetched_to'], 1)
+
+        database_utils.disconnect_database(self, self.encode_sid,
+                                           self.encode_did)
+
+    def tearDown(self):
+        server = [server['server'] for server in parent_node_dict['server']
+                  if server['server_id'] == self.encode_sid][0]
+        main_conn = test_utils.get_db_connection(
+            server['db'],
+            server['username'],
+            server['db_password'],
+            server['host'],
+            server['port'],
+            server['sslmode']
+        )
+        test_utils.drop_database(main_conn, self.encode_db_name)
diff --git a/web/pgadmin/utils/driver/psycopg2/connection.py b/web/pgadmin/utils/driver/psycopg2/connection.py
index cfd161a0..e8ca886e 100644
--- a/web/pgadmin/utils/driver/psycopg2/connection.py
+++ b/web/pgadmin/utils/driver/psycopg2/connection.py
@@ -51,6 +51,12 @@ else:
 _ = gettext
 
 
+# Replace default ascii encoder with unicode-escape
+# which translates characters to unicode format.
+# Escape special characters to ASCII based on unicode
+encodings['SQL_ASCII'] = 'unicode-escape'
+encodings['SQLASCII'] = 'unicode-escape'
+
 # Register global type caster which will be applicable to all connections.
 register_global_typecasters()
 
diff --git a/web/pgadmin/utils/driver/psycopg2/typecast.py b/web/pgadmin/utils/driver/psycopg2/typecast.py
index f1366049..a3fa2bc5 100644
--- a/web/pgadmin/utils/driver/psycopg2/typecast.py
+++ b/web/pgadmin/utils/driver/psycopg2/typecast.py
@@ -164,7 +164,7 @@ def register_global_typecasters():
 
 
 def register_string_typecasters(connection):
-    if connection.encoding != 'UTF8':
+    if connection.encoding not in ('UTF8', 'SQLASCII', 'SQL_ASCII'):
         # In python3 when database encoding is other than utf-8 and client
         # encoding is set to UNICODE then we need to map data from database
         # encoding to utf-8.
@@ -202,8 +202,8 @@ def register_string_typecasters(connection):
             (1002, 1003, 1009, 1014, 1015, 0
              ), 'UNICODEARRAY', unicode_type)
 
-        psycopg2.extensions.register_type(unicode_type)
-        psycopg2.extensions.register_type(unicode_array_type)
+        psycopg2.extensions.register_type(unicode_type, connection)
+        psycopg2.extensions.register_type(unicode_array_type, connection)
 
 
 def register_binary_typecasters(connection):
diff --git a/web/regression/python_test_utils/test_utils.py b/web/regression/python_test_utils/test_utils.py
index 3e517b61..464a09e1 100644
--- a/web/regression/python_test_utils/test_utils.py
+++ b/web/regression/python_test_utils/test_utils.py
@@ -116,7 +116,7 @@ def clear_node_info_dict():
         del node_info_dict[node][:]
 
 
-def create_database(server, db_name):
+def create_database(server, db_name, encoding=None):
     """This function used to create database and returns the database id"""
     try:
         connection = get_db_connection(
@@ -130,8 +130,14 @@ def create_database(server, db_name):
         old_isolation_level = connection.isolation_level
         connection.set_isolation_level(0)
         pg_cursor = connection.cursor()
-        pg_cursor.execute(
-            '''CREATE DATABASE "%s" TEMPLATE template0''' % db_name)
+        if encoding is None:
+            pg_cursor.execute(
+                '''CREATE DATABASE "%s" TEMPLATE template0''' % db_name)
+        else:
+            pg_cursor.execute(
+                '''CREATE DATABASE "%s" TEMPLATE template0
+                ENCODING='%s' LC_COLLATE='%s' LC_CTYPE='%s' ''' %
+                (db_name, encoding[0], encoding[1], encoding[1]))
         connection.set_isolation_level(old_isolation_level)
         connection.commit()
 


view thread (38+ 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], [email protected], [email protected]
  Subject: Re: [pgAdmin4][RM#3289] Can't query SQL_ASCII database.
  In-Reply-To: <CAM9w-_=c=82+5ESAEMXUQf5NOrfouZ5JxJMfr+56vJAfGz2DrA@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