diff --git a/web/pgadmin/utils/driver/psycopg2/connection.py b/web/pgadmin/utils/driver/psycopg2/connection.py index a9d55c637..be824da1e 100644 --- a/web/pgadmin/utils/driver/psycopg2/connection.py +++ b/web/pgadmin/utils/driver/psycopg2/connection.py @@ -30,7 +30,7 @@ from pgadmin.utils.exception import ConnectionLost, CryptKeyMissing from pgadmin.utils import get_complete_file_path from ..abstract import BaseConnection from .cursor import DictCursor -from .typecast import register_float_typecasters, register_global_typecasters,\ +from .typecast import numeric_typecasters, register_global_typecasters,\ register_string_typecasters, register_binary_typecasters, \ unregister_numeric_typecasters, \ register_array_to_string_typecasters, ALL_JSON_TYPES @@ -462,7 +462,6 @@ class Connection(BaseConnection): self._set_auto_commit(kwargs) register_string_typecasters(self.conn) - register_float_typecasters(self.conn) if self.array_to_string: register_array_to_string_typecasters(self.conn) @@ -846,6 +845,9 @@ WHERE db.datname = current_database()""") yield gettext('The query executed did not return any data.') return + # Type cast the numeric values + results = numeric_typecasters(results) + header = [] json_columns = [] @@ -912,8 +914,6 @@ WHERE db.datname = current_database()""") # Registering back type caster for large size data types to string # which was unregistered at starting - if any(type['type_code'] == 701 for type in self.column_info): - register_float_typecasters(self.conn) register_string_typecasters(self.conn) return True, gen diff --git a/web/pgadmin/utils/driver/psycopg2/typecast.py b/web/pgadmin/utils/driver/psycopg2/typecast.py index a93a53da7..c6d043e7b 100644 --- a/web/pgadmin/utils/driver/psycopg2/typecast.py +++ b/web/pgadmin/utils/driver/psycopg2/typecast.py @@ -22,7 +22,6 @@ from .encoding import configure_driver_encodings, get_encoding configure_driver_encodings(encodings) - # OIDs of data types which need to typecast as string to avoid JavaScript # compatibility issues. # e.g JavaScript does not support 64 bit integers. It has 64-bit double @@ -67,7 +66,6 @@ TO_ARRAY_OF_STRING_DATATYPES = ( # OID of record array data type RECORD_ARRAY = (2287,) - # OIDs of builtin array datatypes supported by psycopg2 # OID reference psycopg2/psycopg/typecast_builtins.c # @@ -103,22 +101,18 @@ PSYCOPG_SUPPORTED_JSON_ARRAY_TYPES = (199, 3807) ALL_JSON_TYPES = PSYCOPG_SUPPORTED_JSON_TYPES +\ PSYCOPG_SUPPORTED_JSON_ARRAY_TYPES - # INET[], CIDR[] # OID reference psycopg2/lib/_ipaddress.py PSYCOPG_SUPPORTED_IPADDRESS_ARRAY_TYPES = (1041, 651) - # uuid[] # OID reference psycopg2/lib/extras.py PSYCOPG_SUPPORTED_IPADDRESS_ARRAY_TYPES = (2951,) - # int4range, int8range, numrange, daterange tsrange, tstzrange[] # OID reference psycopg2/lib/_range.py PSYCOPG_SUPPORTED_RANGE_TYPES = (3904, 3926, 3906, 3912, 3908, 3910) - # int4range[], int8range[], numrange[], daterange[] tsrange[], tstzrange[] # OID reference psycopg2/lib/_range.py PSYCOPG_SUPPORTED_RANGE_ARRAY_TYPES = (3905, 3927, 3907, 3913, 3909, 3911) @@ -204,12 +198,26 @@ def register_string_typecasters(connection): psycopg2.extensions.register_type(unicode_array_type, connection) -def register_float_typecasters(connection): - # This function is to convert pg types into decimal type - string_type_to_float = \ - psycopg2.extensions.new_type(TO_STRING_NUMERIC_DATATYPES, - 'TYPECAST_TO_DECIMAL', _DECIMAL) - psycopg2.extensions.register_type(string_type_to_float, 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): + # This function is to convert pg types to numeic type caster + + for result in results: + for key, value in result.items(): + if isinstance(result[key], str) and is_numeric(result[key]): + result[key] = float(result[key]) + return results def register_binary_typecasters(connection):