Skip to content

Commit

Permalink
Fixed an issue where deleting rows in the query tool would delete all…
Browse files Browse the repository at this point in the history
… rows in the table when 'Select All Remaining Rows' was used. #8460
  • Loading branch information
adityatoshniwal authored Feb 18, 2025
1 parent 39f92ff commit a0ddfad
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
56 changes: 27 additions & 29 deletions web/pgadmin/tools/sqleditor/utils/save_changed_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from collections import OrderedDict

from pgadmin.tools.sqleditor.utils.constant_definition import TX_STATUS_IDLE
from pgadmin.utils.exception import ExecuteError

ignore_type_cast_list = ['character', 'character[]', 'bit', 'bit[]']

Expand Down Expand Up @@ -190,35 +191,32 @@ def save_changed_data(changed_data, columns_info, conn, command_obj,
elif of_type == 'deleted':
delete_all = changed_data.get('delete_all', False)
list_of_sql[of_type] = []
is_first = True
rows_to_delete = []
keys = []
no_of_keys = 0
if not delete_all:
for each_row in changed_data[of_type]:
rows_to_delete.append(changed_data[of_type][each_row])
# Fetch the keys for SQL generation
if is_first:
# We need to covert dict_keys to normal list in
# Python3
# In Python2, it's already a list & We will also
# fetch column names using index
keys = list(
changed_data[of_type][each_row].keys()
)
no_of_keys = len(keys)
is_first = False
# Map index with column name for each row
for row in rows_to_delete:
for k, v in row.items():
# Set primary key with label & delete index based
# mapped key
try:
row[changed_data['columns']
[int(k)]['name']] = v
except ValueError:
continue
del row[k]
_, primary_keys = command_obj.get_primary_keys()
keys = list(primary_keys.keys())
no_of_keys = len(keys)
if delete_all:
# for delete all, we'll need all the row primary key
# values.
status, result = \
conn.async_fetchmany_2darray(records=-1)

if not status:
raise ExecuteError(result)

columns_info = conn.get_column_info()

rows_to_delete = [
dict([
(col['name'], r[col['pos']])
for col in filter(lambda c: c['name'] in keys,
changed_data['columns'])
])
for r in result
]
else:
rows_to_delete = [
r for _, r in changed_data[of_type].items()
]

sql = render_template(
"/".join([command_obj.sql_path, 'delete.sql']),
Expand Down
4 changes: 3 additions & 1 deletion web/pgadmin/utils/driver/psycopg3/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1350,7 +1350,9 @@ def async_fetchmany_2darray(self, records=2000,
result = []
try:
if records == -1:
result = cur.fetchall(_tupples=True)
result = cur.fetchwindow(
from_rownum=0, to_rownum=cur.get_rowcount() - 1,
_tupples=True)
elif records is None:
result = cur.fetchwindow(from_rownum=from_rownum,
to_rownum=to_rownum,
Expand Down

0 comments on commit a0ddfad

Please sign in to comment.