Skip to content

Commit 0a896aa

Browse files
Fixed review comments
1 parent 8f30d62 commit 0a896aa

File tree

3 files changed

+48
-38
lines changed

3 files changed

+48
-38
lines changed

web/pgadmin/tools/sqleditor/__init__.py

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2232,38 +2232,7 @@ def download_binary_data(trans_id):
22322232
)
22332233
)
22342234

2235-
try:
2236-
register_binary_data_typecasters(cur)
2237-
row_pos = int(data['rowpos'])
2238-
col_pos = int(data['colpos'])
2239-
if row_pos < 0 or col_pos < 0:
2240-
raise ValueError
2241-
2242-
# Save the current cursor position
2243-
saved_pos = cur.rownumber if cur.rownumber is not None else 0
2244-
2245-
try:
2246-
# Scroll to the requested row and fetch it
2247-
cur.scroll(row_pos, mode='absolute')
2248-
row = cur.fetchone()
2249-
finally:
2250-
# Always restore the cursor position
2251-
cur.scroll(saved_pos, mode='absolute')
2252-
2253-
if row is None or col_pos >= len(row):
2254-
return internal_server_error(
2255-
errormsg=gettext('Requested cell is out of range.')
2256-
)
2257-
binary_data = row[col_pos]
2258-
except (ValueError, IndexError, TypeError) as e:
2259-
current_app.logger.error(e)
2260-
return internal_server_error(
2261-
errormsg='Invalid row/column position.'
2262-
)
2263-
finally:
2264-
# Always restore the original typecasters
2265-
# (works on connection or cursor)
2266-
register_binary_typecasters(cur)
2235+
binary_data = conn.download_binary_data(cur, data)
22672236

22682237
if binary_data is None:
22692238
return bad_request(

web/pgadmin/utils/driver/psycopg3/connection.py

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@
3030
from pgadmin.model import User
3131
from pgadmin.utils.exception import ConnectionLost, CryptKeyMissing
3232
from pgadmin.utils import get_complete_file_path
33+
from pgadmin.utils.ajax import internal_server_error
3334
from ..abstract import BaseConnection
3435
from .cursor import DictCursor, AsyncDictCursor, AsyncDictServerCursor
35-
from .typecast import register_global_typecasters,\
36-
register_string_typecasters, register_binary_typecasters, \
37-
register_array_to_string_typecasters, ALL_JSON_TYPES, \
38-
register_numeric_typecasters
36+
from .typecast import register_binary_data_typecasters,\
37+
register_global_typecasters, register_string_typecasters,\
38+
register_binary_typecasters, register_array_to_string_typecasters,\
39+
ALL_JSON_TYPES
3940
from .encoding import get_encoding, configure_driver_encodings
4041
from pgadmin.utils import csv_lib as csv
4142
from pgadmin.utils.master_password import get_crypt_key
@@ -1913,3 +1914,43 @@ def mogrify(self, query, parameters):
19131914
return _cur.mogrify(query, parameters)
19141915
else:
19151916
return query
1917+
1918+
def download_binary_data(self, cur, params):
1919+
"""
1920+
This function will return the binary data for the given query.
1921+
:param cur: cursor object
1922+
:param params: row/col params
1923+
:return:
1924+
"""
1925+
try:
1926+
register_binary_data_typecasters(cur)
1927+
row_pos = int(params['rowpos'])
1928+
col_pos = int(params['colpos'])
1929+
if row_pos < 0 or col_pos < 0:
1930+
raise ValueError
1931+
1932+
# Save the current cursor position
1933+
saved_pos = cur.rownumber if cur.rownumber is not None else 0
1934+
1935+
try:
1936+
# Scroll to the requested row and fetch it
1937+
cur.scroll(row_pos, mode='absolute')
1938+
row = cur.fetchone()
1939+
finally:
1940+
# Always restore the cursor position
1941+
cur.scroll(saved_pos, mode='absolute')
1942+
1943+
if row is None or col_pos >= len(row):
1944+
return internal_server_error(
1945+
errormsg=gettext('Requested cell is out of range.')
1946+
)
1947+
return row[col_pos]
1948+
except (ValueError, IndexError, TypeError) as e:
1949+
current_app.logger.error(e)
1950+
return internal_server_error(
1951+
errormsg='Invalid row/column position.'
1952+
)
1953+
finally:
1954+
# Always restore the original typecasters
1955+
# (works on connection or cursor)
1956+
register_binary_typecasters(cur)

web/pgadmin/utils/driver/psycopg3/typecast.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ def load(self, data):
261261
if data is None:
262262
return None
263263
raw = bytes(data) if isinstance(data, memoryview) else data
264-
if isinstance(raw, str) and raw.startswith('\\x'):
265-
return bytes.fromhex(raw[2:])
264+
if isinstance(raw, bytes) and raw.startswith(b'\\x'):
265+
return bytes.fromhex(raw[2:].decode())
266266
return raw
267267

268268

0 commit comments

Comments
 (0)