Fixed an issue where a decimal number is appended for character varying fields while downloading the data in CSV format. Fixes #6520

This commit is contained in:
Pradip Parkale 2021-06-11 19:29:20 +05:30 committed by Akshay Joshi
parent b9b0a573e5
commit e3190b86ab
4 changed files with 18 additions and 20 deletions

View File

@ -35,3 +35,4 @@ Bug fixes
| `Issue #6478 <https://redmine.postgresql.org/issues/6478>`_ - Fixed duplicate SQL issue for tables with more than one partition.
| `Issue #6482 <https://redmine.postgresql.org/issues/6482>`_ - Fixed an issue where the Foreground Color property of server dialog does not work.
| `Issue #6513 <https://redmine.postgresql.org/issues/6513>`_ - Fixed an issue where pgAdmin does not open after password reset in server mode.
| `Issue #6520 <https://redmine.postgresql.org/issues/6520>`_ - Fixed an issue where a decimal number is appended for character varying fields while downloading the data in CSV format.

View File

@ -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'

View File

@ -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):

View File

@ -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