From c044af95856500bcd01284db7f6c3afc1c9a3e4f Mon Sep 17 00:00:00 2001 From: Murtuza Zabuawala Date: Fri, 16 Sep 2016 16:46:58 +0100 Subject: [PATCH] Bulk delete rows in SlickGrid. Fixes #1696 --- web/pgadmin/tools/sqleditor/command.py | 27 ++++++++++++++----- .../templates/sqleditor/js/sqleditor.js | 20 +++++++++----- .../sqleditor/sql/9.1_plus/delete.sql | 17 +++++++++--- 3 files changed, 47 insertions(+), 17 deletions(-) diff --git a/web/pgadmin/tools/sqleditor/command.py b/web/pgadmin/tools/sqleditor/command.py index 4b8db6e2b..edd412f74 100644 --- a/web/pgadmin/tools/sqleditor/command.py +++ b/web/pgadmin/tools/sqleditor/command.py @@ -465,14 +465,27 @@ class TableCommand(GridCommand): # For deleted rows elif of_type == 'deleted': + is_first = True + rows_to_delete = [] + keys = None + no_of_keys = None for each_row in changed_data[of_type]: - data = changed_data[of_type][each_row] - sql = render_template("/".join([self.sql_path, 'delete.sql']), - data=data, - object_name=self.object_name, - nsp_name=self.nsp_name) - list_of_sql.append(sql) - list_of_rowid.append(data) + 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 + keys = list(changed_data[of_type][each_row].keys()) + no_of_keys = len(keys) + is_first = False + + sql = render_template("/".join([self.sql_path, 'delete.sql']), + data=rows_to_delete, + primary_key_labels=keys, + no_of_keys=no_of_keys, + object_name=self.object_name, + nsp_name=self.nsp_name) + list_of_sql.append(sql) for i, sql in enumerate(list_of_sql): if sql: diff --git a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js index f30652098..74502c70f 100644 --- a/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js +++ b/web/pgadmin/tools/sqleditor/templates/sqleditor/js/sqleditor.js @@ -2149,12 +2149,20 @@ define( // Remove deleted rows from client as well if(is_deleted) { var rows = grid.getSelectedRows(); - // Reverse the deletion from array - // so that when we remove it does not affect index - rows = rows.sort().reverse(); - rows.forEach(function(idx) { - data.splice(idx, 1); - }); + /* In JavaScript sorting by default is lexical, + * To make sorting numerical we need to pass function + * After that we will Reverse the order of sorted array + * so that when we remove it does not affect array index + */ + if(data.length == rows.length) { + // This means all the rows are selected, clear all data + data = []; + } else { + rows = rows.sort(function(a,b){return a - b}).reverse(); + rows.forEach(function(idx) { + data.splice(idx, 1); + }); + } grid.setData(data, true); grid.setSelectedRows([]); } diff --git a/web/pgadmin/tools/sqleditor/templates/sqleditor/sql/9.1_plus/delete.sql b/web/pgadmin/tools/sqleditor/templates/sqleditor/sql/9.1_plus/delete.sql index e5dbc9b4d..a3ca72bb6 100644 --- a/web/pgadmin/tools/sqleditor/templates/sqleditor/sql/9.1_plus/delete.sql +++ b/web/pgadmin/tools/sqleditor/templates/sqleditor/sql/9.1_plus/delete.sql @@ -1,4 +1,13 @@ -{# Delete the row with primary keys #} -DELETE FROM {{ conn|qtIdent(nsp_name, object_name) }} WHERE -{% for pk_key in data %} -{% if not loop.first %} AND {% endif %}{{ conn|qtIdent(pk_key) }} = {{ data[pk_key]|qtLiteral }}{% endfor %}; \ No newline at end of file +DELETE FROM {{ conn|qtIdent(nsp_name, object_name) }} +{### If there is only one primary key ###} +{% if no_of_keys == 1 %} + WHERE {{ conn|qtIdent(primary_key_labels[0]) }} IN +{### If there are multiple primary keys ###} +{% elif no_of_keys > 1 %} + WHERE ({% for each_label in primary_key_labels %}{{ conn|qtIdent(each_label) }}{% if not loop.last %}, {% endif %}{% endfor %}) IN +{% endif %} +{### Rows to delete ###} + ({% for obj in data %}{% if no_of_keys == 1 %}{{ obj[primary_key_labels[0]]|qtLiteral }}{% elif no_of_keys > 1 %} +{### Here we need to make tuple for each row ###} +({% for each_label in primary_key_labels %}{{ obj[each_label]|qtLiteral }}{% if not loop.last %}, {% endif %}{% endfor %}){% endif %}{% if not loop.last %}, {% endif %} +{% endfor %}); \ No newline at end of file