Some refactoring of sqleditor.js.

This commit is contained in:
Yosry Muhammad 2019-08-23 16:15:43 +01:00 committed by Dave Page
parent 234efc3be7
commit 5887fb3815
5 changed files with 66 additions and 93 deletions

View File

@ -224,22 +224,22 @@ class ExecuteQuery {
} }
if (this.userManagement.isPgaLoginRequired(httpMessage.response)) { if (this.userManagement.isPgaLoginRequired(httpMessage.response)) {
this.sqlServerObject.saveState('execute', [this.explainPlan]); this.sqlServerObject.saveState('check_data_changes_to_execute_query', [this.explainPlan]);
this.userManagement.pgaLogin(); this.userManagement.pgaLogin();
} }
if (httpErrorHandler.httpResponseRequiresNewTransaction(httpMessage.response)) { if (httpErrorHandler.httpResponseRequiresNewTransaction(httpMessage.response)) {
this.sqlServerObject.saveState('execute', [this.explainPlan]); this.sqlServerObject.saveState('check_data_changes_to_execute_query', [this.explainPlan]);
this.sqlServerObject.initTransaction(); this.sqlServerObject.initTransaction();
} }
if (this.wasDatabaseConnectionLost(httpMessage)) { if (this.wasDatabaseConnectionLost(httpMessage)) {
this.sqlServerObject.saveState('execute', [this.explainPlan]); this.sqlServerObject.saveState('check_data_changes_to_execute_query', [this.explainPlan]);
this.sqlServerObject.handle_connection_lost(false, httpMessage); this.sqlServerObject.handle_connection_lost(false, httpMessage);
} }
if(this.isCryptKeyMissing(httpMessage)) { if(this.isCryptKeyMissing(httpMessage)) {
this.sqlServerObject.saveState('execute', [this.explainPlan]); this.sqlServerObject.saveState('check_data_changes_to_execute_query', [this.explainPlan]);
this.sqlServerObject.handle_cryptkey_missing(); this.sqlServerObject.handle_cryptkey_missing();
return; return;
} }

View File

@ -39,13 +39,8 @@ let queryToolActions = {
}, },
executeQuery: function (sqlEditorController) { executeQuery: function (sqlEditorController) {
if(sqlEditorController.is_query_tool) { this._clearMessageTab();
this._clearMessageTab(); sqlEditorController.check_data_changes_to_execute_query();
sqlEditorController.execute();
} else {
this._clearMessageTab();
sqlEditorController.execute_data_query();
}
}, },
explainAnalyze: function (sqlEditorController) { explainAnalyze: function (sqlEditorController) {
@ -60,7 +55,7 @@ let queryToolActions = {
settings: this._settings(), settings: this._settings(),
}; };
this._clearMessageTab(); this._clearMessageTab();
sqlEditorController.execute(explainObject); sqlEditorController.check_data_changes_to_execute_query(explainObject);
}, },
explain: function (sqlEditorController) { explain: function (sqlEditorController) {
@ -76,7 +71,7 @@ let queryToolActions = {
settings: this._settings(), settings: this._settings(),
}; };
this._clearMessageTab(); this._clearMessageTab();
sqlEditorController.execute(explainObject); sqlEditorController.check_data_changes_to_execute_query(explainObject);
}, },
download: function (sqlEditorController) { download: function (sqlEditorController) {

View File

@ -2272,7 +2272,7 @@ define('tools.querytool', [
} }
self.disable_tool_buttons(true); self.disable_tool_buttons(true);
pgBrowser.Events.on('pgadmin:query_tool:connected:'+ transId,()=>{ pgBrowser.Events.on('pgadmin:query_tool:connected:'+ transId,()=>{
self.execute_data_query(); self.check_data_changes_to_execute_query();
}); });
} }
}, },
@ -2314,9 +2314,8 @@ define('tools.querytool', [
self.on('pgadmin-sqleditor:unindent_selected_code', self._unindent_selected_code, self); self.on('pgadmin-sqleditor:unindent_selected_code', self._unindent_selected_code, self);
}, },
// Checks if there is any dirty data in the grid before // Checks if there is any dirty data in the grid before executing a query
// it executes the sql query in View Data mode check_data_changes_to_execute_query: function(explain_prefix=null, shouldReconnect=false) {
execute_data_query: function() {
var self = this; var self = this;
// Check if the data grid has any changes before running query // Check if the data grid has any changes before running query
@ -2328,8 +2327,13 @@ define('tools.querytool', [
alertify.confirm(gettext('Unsaved changes'), alertify.confirm(gettext('Unsaved changes'),
gettext('The data has been modified, but not saved. Are you sure you wish to discard the changes?'), gettext('The data has been modified, but not saved. Are you sure you wish to discard the changes?'),
function() { function() {
// Do nothing as user do not want to save, just continue // The user does not want to save, just continue
self._run_query(); if(self.is_query_tool) {
self._execute_sql_query(explain_prefix, shouldReconnect);
}
else {
self._execute_view_data_query();
}
}, },
function() { function() {
// Stop, User wants to save // Stop, User wants to save
@ -2340,12 +2344,17 @@ define('tools.querytool', [
cancel: gettext('No'), cancel: gettext('No'),
}); });
} else { } else {
self._run_query(); if(self.is_query_tool) {
self._execute_sql_query(explain_prefix, shouldReconnect);
}
else {
self._execute_view_data_query();
}
} }
}, },
// This function makes the ajax call to execute the sql query in View Data mode // Makes the ajax call to execute the sql query in View Data mode
_run_query: function() { _execute_view_data_query: function() {
var self = this, var self = this,
url = url_for('sqleditor.view_data_start', { url = url_for('sqleditor.view_data_start', {
'trans_id': self.transId, 'trans_id': self.transId,
@ -2422,12 +2431,36 @@ define('tools.querytool', [
.fail(function(e) { .fail(function(e) {
self.trigger('pgadmin-sqleditor:loading-icon:hide'); self.trigger('pgadmin-sqleditor:loading-icon:hide');
let msg = httpErrorHandler.handleQueryToolAjaxError( let msg = httpErrorHandler.handleQueryToolAjaxError(
pgAdmin, self, e, '_run_query', [], true pgAdmin, self, e, '_execute_view_data_query', [], true
); );
self.update_msg_history(false, msg); self.update_msg_history(false, msg);
}); });
}, },
// Executes sql query in the editor in Query Tool mode
_execute_sql_query: function(explain_prefix, shouldReconnect) {
var self = this, sql = '';
self.has_more_rows = false;
self.fetching_rows = false;
if (!_.isUndefined(self.special_sql)) {
sql = self.special_sql;
} else {
/* If code is selected in the code mirror then execute
* the selected part else execute the complete code.
*/
var selected_code = self.gridView.query_tool_obj.getSelection();
if (selected_code.length > 0)
sql = selected_code;
else
sql = self.gridView.query_tool_obj.getValue();
}
const executeQuery = new ExecuteQuery.ExecuteQuery(this, pgAdmin.Browser.UserManagement);
executeQuery.execute(sql, explain_prefix, shouldReconnect);
},
// This is a wrapper to call_render function // This is a wrapper to call_render function
// We need this because we have separated columns route & result route // We need this because we have separated columns route & result route
// We need to combine both result here in wrapper before rendering grid // We need to combine both result here in wrapper before rendering grid
@ -3760,60 +3793,6 @@ define('tools.querytool', [
} }
}, },
// Checks if there is any dirty data in the grid before
// it executes the sql query in Query Tool mode
execute: function(explain_prefix, shouldReconnect=false) {
var self = this;
// Check if the data grid has any changes before running query
if (self.can_edit && _.has(self, 'data_store') &&
(_.size(self.data_store.added) ||
_.size(self.data_store.updated) ||
_.size(self.data_store.deleted))
) {
alertify.confirm(gettext('Unsaved changes'),
gettext('The data has been modified, but not saved. Are you sure you wish to discard the changes?'),
function() {
// Do nothing as user do not want to save, just continue
self._execute_sql_query(explain_prefix, shouldReconnect);
},
function() {
// Stop, User wants to save
return true;
}
).set('labels', {
ok: gettext('Yes'),
cancel: gettext('No'),
});
} else {
self._execute_sql_query(explain_prefix, shouldReconnect);
}
},
// Executes sql query in the editor in Query Tool mode
_execute_sql_query: function(explain_prefix, shouldReconnect) {
var self = this, sql = '';
self.has_more_rows = false;
self.fetching_rows = false;
if (!_.isUndefined(self.special_sql)) {
sql = self.special_sql;
} else {
/* If code is selected in the code mirror then execute
* the selected part else execute the complete code.
*/
var selected_code = self.gridView.query_tool_obj.getSelection();
if (selected_code.length > 0)
sql = selected_code;
else
sql = self.gridView.query_tool_obj.getValue();
}
const executeQuery = new ExecuteQuery.ExecuteQuery(this, pgAdmin.Browser.UserManagement);
executeQuery.execute(sql, explain_prefix, shouldReconnect);
},
/* This function is used to highlight the error line and /* This function is used to highlight the error line and
* underlining for the error word. * underlining for the error word.
*/ */

View File

@ -1449,7 +1449,7 @@ describe('ExecuteQuery', () => {
it('should save the state', () => { it('should save the state', () => {
setTimeout(() => { setTimeout(() => {
expect(sqlEditorMock.saveState).toHaveBeenCalledWith( expect(sqlEditorMock.saveState).toHaveBeenCalledWith(
'execute', 'check_data_changes_to_execute_query',
[''] ['']
); );
}, 0); }, 0);
@ -1574,7 +1574,7 @@ describe('ExecuteQuery', () => {
it('should save the state', () => { it('should save the state', () => {
setTimeout(() => { setTimeout(() => {
expect(sqlEditorMock.saveState).toHaveBeenCalledWith( expect(sqlEditorMock.saveState).toHaveBeenCalledWith(
'execute', 'check_data_changes_to_execute_query',
[''] ['']
); );
}, 0); }, 0);
@ -1604,7 +1604,7 @@ describe('ExecuteQuery', () => {
it('saves state', () => { it('saves state', () => {
setTimeout(() => { setTimeout(() => {
expect(sqlEditorMock.saveState).toHaveBeenCalledWith( expect(sqlEditorMock.saveState).toHaveBeenCalledWith(
'execute', 'check_data_changes_to_execute_query',
[''] ['']
); );
}, 0); }, 0);

View File

@ -30,7 +30,7 @@ describe('queryToolActions', () => {
it('calls the execute function on the sqlEditorController', () => { it('calls the execute function on the sqlEditorController', () => {
queryToolActions.executeQuery(sqlEditorController); queryToolActions.executeQuery(sqlEditorController);
expect(sqlEditorController.execute).toHaveBeenCalled(); expect(sqlEditorController.check_data_changes_to_execute_query).toHaveBeenCalled();
}); });
}); });
describe('when the command is being run from the view data view', () => { describe('when the command is being run from the view data view', () => {
@ -39,10 +39,10 @@ describe('queryToolActions', () => {
sqlEditorController.is_query_tool = false; sqlEditorController.is_query_tool = false;
}); });
it('it calls the execute_data_query function on the sqlEditorController', () => { it('it calls the check_data_changes_to_execute_query function on the sqlEditorController', () => {
queryToolActions.executeQuery(sqlEditorController); queryToolActions.executeQuery(sqlEditorController);
expect(sqlEditorController.execute_data_query).toHaveBeenCalled(); expect(sqlEditorController.check_data_changes_to_execute_query).toHaveBeenCalled();
}); });
}); });
}); });
@ -74,7 +74,7 @@ describe('queryToolActions', () => {
settings: false, settings: false,
}; };
expect(sqlEditorController.execute).toHaveBeenCalledWith(explainObject); expect(sqlEditorController.check_data_changes_to_execute_query).toHaveBeenCalledWith(explainObject);
}); });
}); });
@ -100,7 +100,7 @@ describe('queryToolActions', () => {
summary: true, summary: true,
settings: true, settings: true,
}; };
expect(sqlEditorController.execute).toHaveBeenCalledWith(explainObject); expect(sqlEditorController.check_data_changes_to_execute_query).toHaveBeenCalledWith(explainObject);
}); });
}); });
@ -128,7 +128,7 @@ describe('queryToolActions', () => {
settings: false, settings: false,
}; };
expect(sqlEditorController.execute).toHaveBeenCalledWith(explainObject); expect(sqlEditorController.check_data_changes_to_execute_query).toHaveBeenCalledWith(explainObject);
}); });
}); });
@ -156,7 +156,7 @@ describe('queryToolActions', () => {
settings: false, settings: false,
}; };
expect(sqlEditorController.execute).toHaveBeenCalledWith(explainObject); expect(sqlEditorController.check_data_changes_to_execute_query).toHaveBeenCalledWith(explainObject);
}); });
}); });
@ -184,7 +184,7 @@ describe('queryToolActions', () => {
settings: true, settings: true,
}; };
expect(sqlEditorController.execute).toHaveBeenCalledWith(explainObject); expect(sqlEditorController.check_data_changes_to_execute_query).toHaveBeenCalledWith(explainObject);
}); });
}); });
}); });
@ -213,7 +213,7 @@ describe('queryToolActions', () => {
summary: false, summary: false,
settings: false, settings: false,
}; };
expect(sqlEditorController.execute).toHaveBeenCalledWith(explainObject); expect(sqlEditorController.check_data_changes_to_execute_query).toHaveBeenCalledWith(explainObject);
}); });
}); });
@ -239,7 +239,7 @@ describe('queryToolActions', () => {
settings: false, settings: false,
}; };
expect(sqlEditorController.execute).toHaveBeenCalledWith(explainObject); expect(sqlEditorController.check_data_changes_to_execute_query).toHaveBeenCalledWith(explainObject);
}); });
}); });
@ -264,7 +264,7 @@ describe('queryToolActions', () => {
summary: false, summary: false,
settings: false, settings: false,
}; };
expect(sqlEditorController.execute).toHaveBeenCalledWith(explainObject); expect(sqlEditorController.check_data_changes_to_execute_query).toHaveBeenCalledWith(explainObject);
}); });
}); });
}); });
@ -621,8 +621,7 @@ describe('queryToolActions', () => {
trigger: jasmine.createSpy('trigger'), trigger: jasmine.createSpy('trigger'),
table_name: 'iAmATable', table_name: 'iAmATable',
is_query_tool: true, is_query_tool: true,
execute: jasmine.createSpy('execute'), check_data_changes_to_execute_query: jasmine.createSpy('check_data_changes_to_execute_query'),
execute_data_query: jasmine.createSpy('execute_data_query'),
}; };
} }
}); });