public inbox for [email protected]  
help / color / mirror / Atom feed
From: Pradip Parkale <[email protected]>
To: pgadmin-hackers <[email protected]>
Subject: [pgAdmin][RM6520]: pgAdmin4 v 5.3 text export error
Date: Fri, 11 Jun 2021 19:17:18 +0530
Message-ID: <CAJ9T6Su+ib=bZ1EdHbD=HPfA6xBi0-N3RZ5dwsU6dReT2njeaQ@mail.gmail.com> (raw)

Hi Hackers,

Please find the attached patch for #6520.Added check to typecast the
numeric data while downloading the data in CSV format.

-- 
Thanks & Regards,
Pradip Parkale
Software Engineer | EnterpriseDB Corporation


Attachments:

  [application/octet-stream] RM6520.patch (3.7K, 3-RM6520.patch)
  download | inline diff:
diff --git a/web/pgadmin/tools/sqleditor/__init__.py b/web/pgadmin/tools/sqleditor/__init__.py
index 780c0243f..c7ec52277 100644
--- a/web/pgadmin/tools/sqleditor/__init__.py
+++ b/web/pgadmin/tools/sqleditor/__init__.py
@@ -1357,7 +1357,8 @@ def start_query_download_tool(trans_id):
     try:
 
         # This returns generator of records.
-        status, gen = sync_conn.execute_on_server_as_csv(records=2000)
+        status, gen, conn_obj = \
+            sync_conn.execute_on_server_as_csv(records=2000)
 
         if not status:
             return make_json_response(
@@ -1367,13 +1368,13 @@ def start_query_download_tool(trans_id):
             )
 
         r = Response(
-            gen(
+            gen(conn_obj,
                 trans_obj,
                 quote=blueprint.csv_quoting.get(),
                 quote_char=blueprint.csv_quote_char.get(),
                 field_separator=blueprint.csv_field_separator.get(),
                 replace_nulls_with=blueprint.replace_nulls_with.get()
-            ),
+                ),
             mimetype='text/csv' if
             blueprint.csv_field_separator.get() == ','
             else 'text/plain'
diff --git a/web/pgadmin/utils/driver/psycopg2/connection.py b/web/pgadmin/utils/driver/psycopg2/connection.py
index 8ff0963f4..3bddb9fa5 100644
--- a/web/pgadmin/utils/driver/psycopg2/connection.py
+++ b/web/pgadmin/utils/driver/psycopg2/connection.py
@@ -879,7 +879,7 @@ WHERE db.datname = current_database()""")
 
             return results
 
-        def gen(trans_obj, quote='strings', quote_char="'",
+        def gen(conn_obj, trans_obj, quote='strings', quote_char="'",
                 field_separator=',', replace_nulls_with=None):
 
             cur.scroll(0, mode='absolute')
@@ -889,7 +889,7 @@ WHERE db.datname = current_database()""")
                 return
 
             # Type cast the numeric values
-            results = numeric_typecasters(results)
+            results = numeric_typecasters(results, conn_obj)
 
             header = []
             json_columns = []
@@ -958,7 +958,7 @@ WHERE db.datname = current_database()""")
         # Registering back type caster for large size data types to string
         # which was unregistered at starting
         register_string_typecasters(self.conn)
-        return True, gen
+        return True, gen, self
 
     def execute_scalar(self, query, params=None,
                        formatted_exception_msg=False):
diff --git a/web/pgadmin/utils/driver/psycopg2/typecast.py b/web/pgadmin/utils/driver/psycopg2/typecast.py
index c6d043e7b..0250e9316 100644
--- a/web/pgadmin/utils/driver/psycopg2/typecast.py
+++ b/web/pgadmin/utils/driver/psycopg2/typecast.py
@@ -198,25 +198,21 @@ def register_string_typecasters(connection):
         psycopg2.extensions.register_type(unicode_array_type, connection)
 
 
-def is_numeric(val):
-    """Check if value is numeric or not"""
-    try:
-        if '.' in val:
-            float(val)
-        else:
-            int(val)
-    except ValueError:
-        return False
-    return True
-
-
-def numeric_typecasters(results):
+def numeric_typecasters(results, conn_obj):
     # This function is to convert pg types to numeic type caster
 
+    data = []
+    for obj_type in conn_obj.column_info:
+        if obj_type['type_code'] in TO_STRING_NUMERIC_DATATYPES:
+            data.append(obj_type['name'])
+
     for result in results:
         for key, value in result.items():
-            if isinstance(result[key], str) and is_numeric(result[key]):
+            if isinstance(result[key],
+                          str) and key in data and not value.isdigit():
                 result[key] = float(result[key])
+            elif isinstance(result[key], str) and key in data:
+                result[key] = int(result[key])
     return results
 
 


view thread (3+ 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: [pgAdmin][RM6520]: pgAdmin4 v 5.3 text export error
  In-Reply-To: <CAJ9T6Su+ib=bZ1EdHbD=HPfA6xBi0-N3RZ5dwsU6dReT2njeaQ@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