Skip to content

Commit

Permalink
RFE#861: Simulate UPDATE queries.
Browse files Browse the repository at this point in the history
Signed-off-by: Ashutosh Dhundhara <[email protected]>

Show 'Simulate query' button only in case of UPDATE and DELETE.

Signed-off-by: Ashutosh Dhundhara <[email protected]>

Fix code style.

Signed-off-by: Ashutosh Dhundhara <[email protected]>
  • Loading branch information
ashutoshdhundhara committed Jun 19, 2014
1 parent 9fc8156 commit 8a46508
Show file tree
Hide file tree
Showing 5 changed files with 399 additions and 3 deletions.
12 changes: 9 additions & 3 deletions import.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
$GLOBALS['show_as_php'] = $_REQUEST['show_as_php'];
}

// Import functions.
require_once 'libraries/import.lib.php';

// If there is a request to 'Simulate DML'.
if (isset($_REQUEST['simulate_dml'])) {
PMA_handleSimulateDMLRequest();
exit;
}

/**
* Sets globals from $_POST
*/
Expand Down Expand Up @@ -167,9 +176,6 @@
// We don't want anything special in format
$format = PMA_securePath($format);

// Import functions
require_once 'libraries/import.lib.php';

// Create error and goto url
if ($import_type == 'table') {
$err_url = 'tbl_import.php?' . PMA_URL_getCommon($db, $table);
Expand Down
32 changes: 32 additions & 0 deletions js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,37 @@ function setQuery(query)
}
}

/**
* Handles 'Simulate query' button on SQL query box.
*
* @return void
*/
function PMA_handleSimulateQueryButton()
{
var update_re = new RegExp('^\\s*UPDATE\\s+((`[^`]+`)|([A-Za-z0-9_$]+))\\s+SET\\s', 'i');
var delete_re = new RegExp('^\\s*DELETE\\s+FROM\\s', 'i');
var query = '';

if (codemirror_editor) {
query = codemirror_editor.getValue();
} else {
query = $('#sqlquery').val();
}

if (update_re.test(query) || delete_re.test(query)) {
if (! $('#simulate_dml').length) {
$('#button_submit_query')
.before('<input type="button" id="simulate_dml"' +
'tabindex="199" value="' +
PMA_messages.strSimulateDML +
'" />');
}
} else {
if ($('#simulate_dml').length) {
$('#simulate_dml').remove();
}
}
}

/**
* Create quick sql statements.
Expand Down Expand Up @@ -1477,6 +1508,7 @@ AJAX.registerOnload('functions.js', function () {

$('input.sqlbutton').click(function (evt) {
insertQuery(evt.target.id);
PMA_handleSimulateQueryButton();
return false;
});

Expand Down
5 changes: 5 additions & 0 deletions js/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
/* For Preview SQL*/
$js_messages['strPreviewSQL'] = __('Preview SQL');

/* For Simulate DML*/
$js_messages['strSimulateDML'] = __('Simulate query');
$js_messages['strMatchedRows'] = __('Matched rows:');
$js_messages['strSQLQuery'] = __('SQL query:');

/* Charts */
/* l10n: Default label for the y-Axis of Charts */
$js_messages['strYValues'] = __('Y Values');
Expand Down
87 changes: 87 additions & 0 deletions js/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ AJAX.registerTeardown('sql.js', function () {
$(window).unbind('scroll');
$(".filter_rows").die("keyup");
$('body').off('click','a.browse_foreign');
$('body').off('click', '#simulate_dml');
$('body').off('keyup', '#sqlqueryform');
});

/**
Expand Down Expand Up @@ -433,6 +435,91 @@ AJAX.registerOnload('sql.js', function () {
$target_table.find("th.dummy_th").remove();
});
// Filter row handling. --ENDS--

$('body').on('keyup', '#sqlqueryform', function () {
PMA_handleSimulateQueryButton();
});

/**
* Ajax event handler for 'Simulate DML'.
*/
$('body').on('click', '#simulate_dml', function () {
var $form = $('#sqlqueryform');
var query = '';
var delimiter = $('#id_sql_delimiter').val();
var db_name = $form.find('input[name="db"]').val();

if (codemirror_editor) {
query = codemirror_editor.getValue();
} else {
query = $('#sqlquery').val();
}

if (query.length === 0) {
alert(PMA_messages.strFormEmpty);
$('#sqlquery').focus();
return false;
}

var $msgbox = PMA_ajaxShowMessage();
$.ajax({
type: 'POST',
url: $form.attr('action'),
data: {
token: $form.find('input[name="token"]').val(),
db: db_name,
ajax_request: '1',
simulate_dml: '1',
sql_query: query,
sql_delimiter: delimiter
},
success: function (response) {
PMA_ajaxRemoveMessage($msgbox);
if (response.success) {
var dialog_content = '<div class="preview_sql">';
if (response.sql_data) {
var len = response.sql_data.length;
for (var i=0; i<len; i++) {
dialog_content += '<strong>' + PMA_messages.strSQLQuery +
'</strong>' + response.sql_data[i].sql_query +
PMA_messages.strMatchedRows +
' <a href="' + response.sql_data[i].matched_rows_url +
'">' + response.sql_data[i].matched_rows + '</a><br>';
if (i<len-1) {
dialog_content += '<hr>';
}
}
} else {
dialog_content += response.message;
}
dialog_content += '</div>';
$dialog_content = $(dialog_content);
var button_options = {};
button_options[PMA_messages.strClose] = function () {
$(this).dialog('close');
};
var $response_dialog = $('<div />').append($dialog_content).dialog({
minWidth: 540,
maxHeight: 400,
modal: true,
buttons: button_options,
title: PMA_messages.strSimulateDML,
open: function () {
PMA_highlightSQL($(this));
},
close: function () {
$(this).remove();
}
});
} else {
PMA_ajaxShowMessage(response.error);
}
},
error: function (response) {
PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest);
}
});
});
}); // end $()

/**
Expand Down
Loading

0 comments on commit 8a46508

Please sign in to comment.