Skip to content

Commit

Permalink
RFE#654: Range Search Capability.
Browse files Browse the repository at this point in the history
Signed-off-by: Ashutosh Dhundhara <ashutoshdhundhara@yahoo.com>
  • Loading branch information
ashutoshdhundhara committed Jun 24, 2014
1 parent 0c850e4 commit 2a7cdbd
Show file tree
Hide file tree
Showing 7 changed files with 226 additions and 0 deletions.
17 changes: 17 additions & 0 deletions doc/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1937,6 +1937,23 @@ Using :config:option:`$cfg['NumFavoriteTables']` in your :file:`config.inc.php`
file, you can define the maximum number of favorite tables shown in the
navigation panel. Its default value is `10`.

.. _faq6_35:

6.35 How can I use the Range search feature?
---------------------------------------------------------

With the help of range search feature, one can specify a range of values for
particular column(s) while performing search operation on a table from the `Search`
tab.

To use this feature simply click on the `BETWEEN` or `NOT BETWEEN` operators
from the operator select list in front of the column name. On choosing one of the
above options, a dialog box will show up asking for the `Minimum` and `Maximum`
value for that column. Only the specified range of values will be included
in case of `BETWEEN` and excluded in case of `NOT BETWEEN` from the final results.

Note: The Range search feature will work only `Numeric` and `Date` data type columns.

.. _faqproject:

phpMyAdmin project
Expand Down
5 changes: 5 additions & 0 deletions js/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@
/* For tbl_select.js */
$js_messages['strHideSearchCriteria'] = __('Hide search criteria');
$js_messages['strShowSearchCriteria'] = __('Show search criteria');
$js_messages['strRangeSearch'] = __('Range search');
$js_messages['strColumnMax'] = __('Column maximum:');
$js_messages['strColumnMin'] = __('Column minimum:');
$js_messages['strMinValue'] = __('Minimum value:');
$js_messages['strMaxValue'] = __('Maximum value:');

/* For tbl_find_replace.js */
$js_messages['strHideFindNReplaceCriteria'] = __('Hide find and replace criteria');
Expand Down
169 changes: 169 additions & 0 deletions js/tbl_select.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,39 @@
* Table Search
*/

/**
* Checks if given data-type is numeric or date.
*
* @param string data_type Column data-type
*
* @return bool|string
*/
function PMA_checkIfDataTypeNumericOrDate(data_type)
{
// To test for numeric data-types.
var numeric_re = new RegExp(
'TINYINT|SMALLINT|MEDIUMINT|INT|BIGINT|DECIMAL|FLOAT|DOUBLE|REAL',
'i'
);

// To test for date data-types.
var date_re = new RegExp(
'DATETIME|DATE|TIMESTAMP|TIME|YEAR',
'i'
);

// Return matched data-type
if (numeric_re.test(data_type)) {
return numeric_re.exec(data_type)[0];
}

if (date_re.test(data_type)) {
return date_re.exec(data_type)[0];
}

return false;
}

/**
* Unbind all event handlers before tearing down a page
*/
Expand All @@ -21,6 +54,7 @@ AJAX.registerTeardown('tbl_select.js', function () {
$("#tbl_search_form.ajax").die('submit');
$('select.geom_func').unbind('change');
$('span.open_search_gis_editor').die('click');
$('body').off('click', 'select[name*="criteriaColumnOperators"]');
});

AJAX.registerOnload('tbl_select.js', function () {
Expand Down Expand Up @@ -224,4 +258,139 @@ AJAX.registerOnload('tbl_select.js', function () {
}
});

/**
* Ajax event handler for Range-Search.
*/
$('body').on('click', 'select[name*="criteriaColumnOperators"]', function () {
$source_select = $(this);
// Get the column name.
var column_name = $(this)
.closest('tr')
.find('th:first')
.text();

// Get the data-type of column excluding size.
var data_type = $(this)
.closest('tr')
.find('td[data-type]')
.attr('data-type');
data_type = PMA_checkIfDataTypeNumericOrDate(data_type);

// Get the operator.
var operator = $(this).val();

if ((operator == 'BETWEEN' || operator == 'NOT BETWEEN')
&& data_type
) {
var $msgbox = PMA_ajaxShowMessage();
$.ajax({
url: 'tbl_select.php',
type: 'POST',
data: {
token: $('input[name="token"]').val(),
ajax_request: 1,
db: $('input[name="db"]').val(),
table: $('input[name="table"]').val(),
column: column_name,
range_search: 1
},
success: function (response) {
PMA_ajaxRemoveMessage($msgbox);
if (response.success) {
// Get the column min value.
var min = response.column_data.min
? '(' + PMA_messages.strColumnMin +
' ' + response.column_data.min + ')'
: '';
// Get the column max value.
var max = response.column_data.max
? '(' + PMA_messages.strColumnMax +
' ' + response.column_data.max + ')'
: '';
var button_options = {};
button_options[PMA_messages.strGo] = function () {
var min_value = $('#min_value').val();
var max_value = $('#max_value').val();
if (min_value.length && max_value.length) {
var final_value = min_value + ', ' +
max_value;
} else {
final_value = '';
}
var $target_field = $source_select.closest('tr')
.find('[name*="criteriaValues"]');

// If target field is a select list.
if ($target_field.is('select')) {
$target_field.attr('value', final_value);
var $options = $target_field.find('option');
var $closest_min = null;
var $closest_max = null;
// Find closest min and max value.
$options.each(function () {
if (
$closest_min === null
|| Math.abs($(this).val() - min_value) < Math.abs($closest_min.val() - min_value)
) {
$closest_min = $(this);
}

if (
$closest_max === null
|| Math.abs($(this).val() - max_value) < Math.abs($closest_max.val() - max_value)
) {
$closest_max = $(this);
}
});

$closest_min.attr('selected', 'selected');
$closest_max.attr('selected', 'selected');
} else {
$target_field.val(final_value);
}
$(this).dialog("close");
};
button_options[PMA_messages.strCancel] = function () {
$(this).dialog("close");
};

// Display dialog box.
$('<div/>').append(
'<fieldset>' +
'<legend>' + operator + '</legend>' +
'<lablel for="min_value">' + PMA_messages.strMinValue +
'</label>' +
'<input type="text" id="min_value" />' + '<br>' +
'<span class="small_font">' + min + '</span>' + '<br>' +
'<lablel for="max_value">' + PMA_messages.strMaxValue +
'</label>' +
'<input type="text" id="max_value" />' + '<br>' +
'<span class="small_font">' + max + '</span>' +
'</fieldset>'
).dialog({
minWidth: 500,
maxHeight: 400,
modal: true,
buttons: button_options,
title: PMA_messages.strRangeSearch,
open: function () {
// Add datepicker wherever required.
PMA_addDatepicker($('#min_value'), data_type);
PMA_addDatepicker($('#max_value'), data_type);
},
close: function () {
$(this).remove();
}
});
} else {
PMA_ajaxShowMessage(response.error);
}
},
error: function (response) {
PMA_ajaxShowMessage(PMA_messages.strErrorProcessingRequest);
}
});
}
});

});
19 changes: 19 additions & 0 deletions libraries/TableSearch.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1469,5 +1469,24 @@ function replace($columnIndex, $find, $replaceWith, $charSet)
);
$GLOBALS['sql_query'] = $sql_query;
}

/**
* Finds minimum and maximum value of a given column.
*
* @param string $column Column name
*
* @return array
*/
public function getColumnMinMax($column)
{
$sql_query = 'SELECT MIN(' . PMA_Util::backquote($column) . ') AS `min`, '
. 'MAX(' . PMA_Util::backquote($column) . ') AS `max` '
. 'FROM ' . PMA_Util::backquote($this->_db) . '.'
. PMA_Util::backquote($this->_table);

$result = $GLOBALS['dbi']->fetchSingleRow($sql_query);

return $result;
}
}
?>
8 changes: 8 additions & 0 deletions tbl_select.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@

$table_search = new PMA_TableSearch($db, $table, "normal");

// Request to column min-max value.
if (isset($_REQUEST['range_search'])) {
$response = PMA_Response::getInstance();
$min_max = $table_search->getColumnMinMax($_REQUEST['column']);
$response->addJSON('column_data', $min_max);
exit;
}

/**
* No selection criteria received -> display the selection form
*/
Expand Down
4 changes: 4 additions & 0 deletions themes/original/css/common.css.php
Original file line number Diff line number Diff line change
Expand Up @@ -2693,3 +2693,7 @@
.ui-dialog {
position: fixed;
}

.small_font {
font-size: smaller;
}
4 changes: 4 additions & 0 deletions themes/pmahomme/css/common.css.php
Original file line number Diff line number Diff line change
Expand Up @@ -2989,3 +2989,7 @@
.ui-dialog {
position: fixed;
}

.small_font {
font-size: smaller;
}

0 comments on commit 2a7cdbd

Please sign in to comment.