diff --git a/web/pgadmin/tools/sqleditor/command.py b/web/pgadmin/tools/sqleditor/command.py index 3e4549cb1..3a3abf157 100644 --- a/web/pgadmin/tools/sqleditor/command.py +++ b/web/pgadmin/tools/sqleditor/command.py @@ -23,6 +23,7 @@ from pgadmin.tools.sqleditor.utils.is_query_resultset_updatable \ import is_query_resultset_updatable from pgadmin.tools.sqleditor.utils.save_changed_data import save_changed_data from pgadmin.tools.sqleditor.utils.get_column_types import get_columns_types +from pgadmin.utils.preferences import Preferences from config import PG_DEFAULT_DRIVER @@ -463,6 +464,11 @@ class TableCommand(GridCommand): # call base class init to fetch the table name super(TableCommand, self).__init__(**kwargs) + # Set the default sorting on table data by primary key if user + # preference value is set + self.data_sorting_by_pk = Preferences.module('sqleditor').preference( + 'table_view_data_by_pk').get() + def get_sql(self, default_conn=None): """ This method is used to create a proper SQL query @@ -486,7 +492,7 @@ class TableCommand(GridCommand): if data_sorting is None and \ not self.is_sorting_set_from_filter_dialog() \ and (self.cmd_type in (VIEW_FIRST_100_ROWS, VIEW_LAST_100_ROWS) or - (self.cmd_type == VIEW_ALL_ROWS and self.limit > 0)): + (self.cmd_type == VIEW_ALL_ROWS and self.data_sorting_by_pk)): sorting = {'data_sorting': []} for pk in primary_keys: sorting['data_sorting'].append( diff --git a/web/pgadmin/tools/sqleditor/tests/test_view_data.py b/web/pgadmin/tools/sqleditor/tests/test_view_data.py index 3466e5c7c..9e2119386 100644 --- a/web/pgadmin/tools/sqleditor/tests/test_view_data.py +++ b/web/pgadmin/tools/sqleditor/tests/test_view_data.py @@ -10,6 +10,7 @@ import uuid import json import random +import sys from pgadmin.utils.route import BaseTestGenerator from pgadmin.browser.server_groups.servers.databases.tests import utils as \ @@ -18,6 +19,11 @@ from regression import parent_node_dict from regression.python_test_utils import test_utils from pgadmin.utils import server_utils, IS_PY2 +if sys.version_info < (3, 3): + from mock import patch +else: + from unittest.mock import patch + class TestViewData(BaseTestGenerator): """ @@ -36,6 +42,34 @@ class TestViewData(BaseTestGenerator): result_data='SELECT 0', rows_fetched_to=0 ) + ), + ( + 'Sort table data without primary key in the table', + dict( + table_sql="""Create Table ( + id integer Not Null, + json_val json Not Null + );""", + result_data='SELECT 0', + rows_fetched_to=0 + ) + ), + ( + 'Sort table data by default order with primary key in table', + dict( + table_sql="""Create Table ( + id integer Not Null, + json_val json Not Null, + Constraint table_pk_sort Primary Key(id) + );""", + result_data='SELECT 0', + rows_fetched_to=0, + mock_data={ + 'function_to_be_mocked': "pgadmin.utils.preferences." + "_Preference.get", + 'return_value': False + } + ) ) ] @@ -71,10 +105,18 @@ class TestViewData(BaseTestGenerator): # Initialize query tool self.trans_id = str(random.randint(1, 9999999)) - url = '/datagrid/initialize/datagrid/{0}/3/table/{1}/{2}/{3}/{4}'\ + url = '/datagrid/initialize/datagrid/{0}/3/table/{1}/{2}/{3}/{4}' \ .format(self.trans_id, test_utils.SERVER_GROUP, self.server_id, self.db_id, table_id) - response = self.tester.post(url) + + if hasattr(self, 'mock_data'): + with patch( + self.mock_data['function_to_be_mocked'], + return_value=self.mock_data['return_value'] + ): + response = self.tester.post(url) + else: + response = self.tester.post(url) self.assertEquals(response.status_code, 200) diff --git a/web/pgadmin/tools/sqleditor/utils/query_tool_preferences.py b/web/pgadmin/tools/sqleditor/utils/query_tool_preferences.py index a4d74e860..07aad2b39 100644 --- a/web/pgadmin/tools/sqleditor/utils/query_tool_preferences.py +++ b/web/pgadmin/tools/sqleditor/utils/query_tool_preferences.py @@ -100,6 +100,17 @@ def RegisterQueryToolPreferences(self): ) ) + self.table_view_data_by_pk = self.preference.register( + 'Options', 'table_view_data_by_pk', + gettext("Sort View Data results by primary key columns?"), + 'boolean', True, + category_label=gettext('Options'), + help_str=gettext("If set to True, data returned when using the " + "View/Edit Data - All Rows option will be sorted by " + "the Primary Key columns by default. When using the " + "First/Last 100 Rows options, data is always sorted.") + ) + self.show_prompt_save_data_changes = self.preference.register( 'Options', 'prompt_save_data_changes', gettext("Prompt to save unsaved data changes?"), 'boolean', True,