2017-06-08 06:41:56 -05:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// pgAdmin 4 - PostgreSQL Tools
|
|
|
|
//
|
2019-01-02 04:24:12 -06:00
|
|
|
// Copyright (C) 2013 - 2019, The pgAdmin Development Team
|
2017-06-08 06:41:56 -05:00
|
|
|
// This software is released under the PostgreSQL Licence
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// This file contains common utilities functions used in sqleditor modules
|
|
|
|
|
2018-02-01 07:29:18 -06:00
|
|
|
define(['jquery', 'sources/gettext', 'sources/url_for'],
|
|
|
|
function ($, gettext, url_for) {
|
2017-06-08 06:41:56 -05:00
|
|
|
var sqlEditorUtils = {
|
|
|
|
/* Reference link http://stackoverflow.com/questions/105034/create-guid-uuid-in-javascript
|
|
|
|
* Modified as per requirement.
|
|
|
|
*/
|
|
|
|
epicRandomString: function(length) {
|
|
|
|
var s = [];
|
2018-01-12 01:29:51 -06:00
|
|
|
var hexDigits = '0123456789abcdef';
|
2017-06-08 06:41:56 -05:00
|
|
|
for (var i = 0; i < 36; i++) {
|
2018-01-12 01:29:51 -06:00
|
|
|
s[i] = hexDigits.substr(
|
2017-06-08 06:41:56 -05:00
|
|
|
Math.floor(Math.random() * 0x10), 1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
// bits 12-15 of the time_hi_and_version field to 0010
|
2018-01-12 01:29:51 -06:00
|
|
|
s[14] = '4';
|
2017-06-08 06:41:56 -05:00
|
|
|
// bits 6-7 of the clock_seq_hi_and_reserved to 01
|
|
|
|
s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
|
2018-01-12 01:29:51 -06:00
|
|
|
s[8] = s[13] = s[18] = s[23] = '-';
|
2017-06-08 06:41:56 -05:00
|
|
|
|
2018-01-12 01:29:51 -06:00
|
|
|
var uuid = s.join('');
|
2017-06-08 06:41:56 -05:00
|
|
|
return uuid.replace(/-/g, '').substr(0, length);
|
|
|
|
},
|
|
|
|
|
|
|
|
// Returns a unique hash for input string
|
|
|
|
getHash: function(input) {
|
|
|
|
var hash = 0, len = input.length;
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
hash = ((hash << 5) - hash) + input.charCodeAt(i);
|
|
|
|
hash |= 0; // to 32bit integer
|
|
|
|
}
|
|
|
|
return hash;
|
|
|
|
},
|
|
|
|
calculateColumnWidth: function (text) {
|
|
|
|
// Calculate column header width based on column name or type
|
|
|
|
// Create a temporary element with given label, append to body
|
|
|
|
// calculate its width and remove the element.
|
|
|
|
$('body').append(
|
|
|
|
'<span id="pg_text" style="visibility: hidden;">'+ text + '</span>'
|
|
|
|
);
|
2017-06-09 03:24:20 -05:00
|
|
|
var width = $('#pg_text').width() + 23;
|
2017-06-08 06:41:56 -05:00
|
|
|
$('#pg_text').remove(); // remove element
|
|
|
|
|
|
|
|
return width;
|
2017-06-15 06:19:47 -05:00
|
|
|
},
|
|
|
|
capitalizeFirstLetter: function (string) {
|
|
|
|
return string.charAt(0).toUpperCase() + string.slice(1);
|
2018-01-12 01:29:51 -06:00
|
|
|
},
|
2018-01-12 08:34:39 -06:00
|
|
|
|
|
|
|
// Status flag
|
|
|
|
previousStatus: null,
|
|
|
|
|
|
|
|
// This function will fetch the connection status via ajax
|
2018-02-01 07:29:18 -06:00
|
|
|
fetchConnectionStatus: function(target, $el, $status_el) {
|
2018-01-12 08:34:39 -06:00
|
|
|
// If user has switch the browser Tab or Minimized window or
|
|
|
|
// if wcDocker panel is not in focus then don't fire AJAX
|
2018-02-01 07:29:18 -06:00
|
|
|
var url = url_for('sqleditor.connection_status', {
|
|
|
|
'trans_id': target.transId,
|
|
|
|
});
|
|
|
|
|
2018-01-12 08:34:39 -06:00
|
|
|
if (document.visibilityState !== 'visible' ||
|
|
|
|
$el.data('panel-visible') !== 'visible' ) {
|
|
|
|
return;
|
|
|
|
}
|
2018-05-30 20:58:28 -05:00
|
|
|
|
|
|
|
let sqleditor_obj = target;
|
|
|
|
|
2018-01-12 08:34:39 -06:00
|
|
|
// Start polling..
|
|
|
|
$.ajax({
|
|
|
|
url: url,
|
|
|
|
method: 'GET',
|
2018-07-09 07:54:00 -05:00
|
|
|
})
|
|
|
|
.done(function (res) {
|
|
|
|
if(res && res.data) {
|
|
|
|
var status = res.data.status,
|
|
|
|
msg = res.data.message,
|
|
|
|
is_status_changed = false;
|
2018-01-12 08:34:39 -06:00
|
|
|
|
2018-07-09 07:54:00 -05:00
|
|
|
// Raise notify messages comes from database server.
|
|
|
|
sqleditor_obj.update_notifications(res.data.notifies);
|
2018-05-30 20:58:28 -05:00
|
|
|
|
2018-07-09 07:54:00 -05:00
|
|
|
// Inject CSS as required
|
|
|
|
switch(status) {
|
|
|
|
// Busy
|
|
|
|
case 1:
|
|
|
|
// if received busy status more than once then only
|
|
|
|
if(status == sqlEditorUtils.previousStatus &&
|
|
|
|
!$status_el.hasClass('fa-hourglass-half')) {
|
|
|
|
$status_el.removeClass()
|
|
|
|
.addClass('fa fa-hourglass-half');
|
|
|
|
is_status_changed = true;
|
2018-01-12 08:34:39 -06:00
|
|
|
}
|
2018-07-09 07:54:00 -05:00
|
|
|
break;
|
|
|
|
// Idle in transaction
|
|
|
|
case 2:
|
|
|
|
if(sqlEditorUtils.previousStatus != status &&
|
|
|
|
!$status_el.hasClass('fa-clock-o')) {
|
|
|
|
$status_el.removeClass()
|
|
|
|
.addClass('fa fa-clock-o');
|
|
|
|
is_status_changed = true;
|
2018-01-12 08:34:39 -06:00
|
|
|
}
|
2018-07-09 07:54:00 -05:00
|
|
|
break;
|
|
|
|
// Failed in transaction
|
|
|
|
case 3:
|
|
|
|
if(sqlEditorUtils.previousStatus != status &&
|
|
|
|
!$status_el.hasClass('fa-exclamation-circle')) {
|
2018-01-12 08:34:39 -06:00
|
|
|
$status_el.removeClass()
|
2018-07-09 07:54:00 -05:00
|
|
|
.addClass('fa fa-exclamation-circle');
|
|
|
|
is_status_changed = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
// Failed in transaction with unknown server side error
|
|
|
|
case 4:
|
|
|
|
if(sqlEditorUtils.previousStatus != status &&
|
|
|
|
!$status_el.hasClass('fa-exclamation-triangle')) {
|
|
|
|
$status_el.removeClass()
|
|
|
|
.addClass('fa fa-exclamation-triangle');
|
|
|
|
is_status_changed = true;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if(sqlEditorUtils.previousStatus != status &&
|
|
|
|
!$status_el.hasClass('fa-query_tool_connected')) {
|
|
|
|
$status_el.removeClass()
|
|
|
|
.addClass('fa-custom fa-query-tool-connected');
|
|
|
|
is_status_changed = true;
|
2018-01-12 08:34:39 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-09 07:54:00 -05:00
|
|
|
sqlEditorUtils.previousStatus = status;
|
|
|
|
// Set bootstrap popover message
|
|
|
|
if(is_status_changed) {
|
|
|
|
$el.popover('hide');
|
|
|
|
$el.attr('data-content', msg);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We come here means we did not receive expected response
|
|
|
|
// from server, we need to error out
|
|
|
|
sqlEditorUtils.previousStatus = -99;
|
|
|
|
msg = gettext('An unexpected error occurred - ' +
|
|
|
|
'ensure you are logged into the application.');
|
2018-01-12 08:34:39 -06:00
|
|
|
$el.attr('data-content', msg);
|
|
|
|
if(!$status_el.hasClass('fa-query-tool-disconnected')) {
|
|
|
|
$el.popover('hide');
|
|
|
|
$status_el.removeClass()
|
|
|
|
.addClass('fa-custom fa-query-tool-disconnected');
|
|
|
|
}
|
2018-07-09 07:54:00 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.fail(function (e) {
|
|
|
|
sqlEditorUtils.previousStatus = -1;
|
|
|
|
var msg = gettext('Transaction status check failed.');
|
|
|
|
if (e.readyState == 0) {
|
|
|
|
msg = gettext('Not connected to the server or the connection to ' +
|
|
|
|
'the server has been closed.');
|
|
|
|
} else if (e.responseJSON && e.responseJSON.errormsg) {
|
|
|
|
msg = e.responseJSON.errormsg;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set bootstrap popover
|
|
|
|
$el.attr('data-content', msg);
|
|
|
|
// Add error class
|
|
|
|
if(!$status_el.hasClass('fa-query-tool-disconnected')) {
|
|
|
|
$el.popover('hide');
|
|
|
|
$status_el.removeClass()
|
|
|
|
.addClass('fa-custom fa-query-tool-disconnected');
|
|
|
|
}
|
2018-01-12 08:34:39 -06:00
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// Updates the flag for connection status poll
|
|
|
|
updateConnectionStatusFlag: function(status) {
|
2018-01-17 01:54:25 -06:00
|
|
|
var $el = $('.connection_status');
|
2018-01-12 08:34:39 -06:00
|
|
|
if ($el.data('panel-visible') != status) {
|
|
|
|
$el.data('panel-visible', status);
|
|
|
|
}
|
|
|
|
},
|
2018-07-05 05:38:43 -05:00
|
|
|
|
|
|
|
calcFontSize: function(fontSize) {
|
|
|
|
if(fontSize) {
|
2018-07-10 07:58:44 -05:00
|
|
|
let rounded = Number((Math.round(fontSize + 'e+2') + 'e-2'));
|
|
|
|
if(rounded > 0) {
|
|
|
|
return rounded + 'em';
|
|
|
|
}
|
2018-07-05 05:38:43 -05:00
|
|
|
}
|
2018-07-10 07:58:44 -05:00
|
|
|
return '1em';
|
2018-07-05 05:38:43 -05:00
|
|
|
},
|
2017-06-08 06:41:56 -05:00
|
|
|
};
|
|
|
|
return sqlEditorUtils;
|
2018-01-12 01:29:51 -06:00
|
|
|
});
|