From 0cc8121846d3f424182ecceca3d1d7ac4875d900 Mon Sep 17 00:00:00 2001 From: Chanaka Indrajith Date: Sun, 23 Dec 2012 16:45:35 +0530 Subject: [PATCH 1/4] Extend support for inline edit, smallint fields --- js/makegrid.js | 34 +++++++++++++++++++++++++--- tbl_replace.php | 59 ++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 89 insertions(+), 4 deletions(-) diff --git a/js/makegrid.js b/js/makegrid.js index e13ec7eece..66922efff4 100644 --- a/js/makegrid.js +++ b/js/makegrid.js @@ -376,10 +376,10 @@ function PMA_makegrid(t, enableResize, enableReorder, enableVisib, enableGridEdi table_create_time: g.tableCreateTime }; if (g.colOrder.length > 0) { - $.extend(post_params, { col_order: g.colOrder.toString() }); + $.extend(post_params, {col_order: g.colOrder.toString()}); } if (g.colVisib.length > 0) { - $.extend(post_params, { col_visib: g.colVisib.toString() }); + $.extend(post_params, {col_visib: g.colVisib.toString()}); } $.post('sql.php', post_params, function(data) { if (data.success != true) { @@ -634,7 +634,9 @@ function PMA_makegrid(t, enableResize, enableReorder, enableVisib, enableGridEdi $this_field.addClass('null'); } else { $this_field.removeClass('null'); - var new_html = $this_field.data('value'); + var new_html = data.isTruncatableField + ? data.truncatableFieldValue + : $this_field.data('value'); if ($this_field.is('.truncated')) { if (new_html.length > g.maxTruncatedLen) { new_html = new_html.substring(0, g.maxTruncatedLen) + '...'; @@ -1190,6 +1192,32 @@ function PMA_makegrid(t, enableResize, enableReorder, enableVisib, enableGridEdi } if(data.success == true) { PMA_ajaxShowMessage(data.message); + + // Generate new where clause again if the field + // can be truncated by MySQL and table does not have + // primary/unique key + if (data.isTruncatableField && !data.hasUniqueIdentifier) { + + var $toBeSavedColumn = $('.to_be_saved'); + var condition_array = jQuery.parseJSON($toBeSavedColumn.parent().find('.condition_array').val()); + var field_name = getFieldName($toBeSavedColumn); + var field_str = '`' + g.table + '`.' + '`' + field_name + '`'; + condition_array[field_str] = ' =' + data.truncatableFieldValue; + + // save new_clause + var new_clause = ''; + for (var field in condition_array) { + new_clause += field + ' ' + condition_array[field] + ' AND '; + } + new_clause = new_clause.substring(0, new_clause.length - 5); // remove the last AND + new_clause = PMA_urlencode(new_clause); + $toBeSavedColumn.parent().data('new_clause', new_clause); + + // save condition_array + $toBeSavedColumn.parent().find('.condition_array').val(JSON.stringify(condition_array)); + + } + // update where_clause related data in each edited row $('.to_be_saved').parents('tr').each(function() { var new_clause = $(this).data('new_clause'); diff --git a/tbl_replace.php b/tbl_replace.php index 6ba156de56..4abaa1f281 100644 --- a/tbl_replace.php +++ b/tbl_replace.php @@ -566,7 +566,40 @@ if ($GLOBALS['is_ajax_request'] == true) { } // end of loop for each transformation cell } // end of loop for each $mime_map } - + + // Need to check the inline edited value can be truncated by MySQL + // without informing while saving + $column_name = $_REQUEST['fields_name']['multi_edit'][0][0]; + $column_value = $_REQUEST['fields']['multi_edit'][0][0]; + $column_meta_data = PMA_DBI_get_columns($db, $table, $column_name); + + if (stripos($column_meta_data['Type'], 'smallint') !== false) { + + $extra_data['isTruncatableField'] = true; + $extra_data['hasUniqueIdentifier'] = hasPrimaryKeyOrUniqueKey($db, $table) + ? true + : false; + + // If table has unique identifier (primary/unique key), fetch the value + // of the field. (Yes, can be just round and return, but better need to + // retrieve the value really saved in the database when it is possible) + if ($extra_data['hasUniqueIdentifier']) { + + $sql_for_real_value = 'SELECT '. PMA_backquote($table) . '.' . PMA_backquote($column_name) + . ' FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) + . ' WHERE ' . $_REQUEST['where_clause'][0]; + + $extra_data['truncatableFieldValue'] = (PMA_DBI_fetch_value($sql_for_real_value) !== false) + ? PMA_DBI_fetch_value($sql_for_real_value) + : round($column_value); + + } else { + $extra_data['truncatableFieldValue'] = round($column_value); + } + + } + + /**Get the total row count of the table*/ $extra_data['row_count'] = PMA_Table::countRecords($_REQUEST['db'], $_REQUEST['table']); $extra_data['sql_query'] = PMA_showMessage($message, $GLOBALS['display_query']); @@ -604,4 +637,28 @@ require_once './libraries/header.inc.php'; */ require './' . PMA_securePath($goto_include); exit; + + +/** + * Check whether particular table has primary key or unique key + * + * @param string $db Database name + * @param string $table Table name + * + * @return boolean + */ +function hasPrimaryKeyOrUniqueKey($db, $table) { + + $table_indexes = PMA_DBI_get_table_indexes($db, $table); + + foreach ($table_indexes as $index) { + if (($index['Key_name'] == 'PRIMARY') || ($index['Non_unique'] == '0')) { + return true; + } + } + + return false; + +} + ?> From bd0b51bc172e3e6082dd1d4edee0bebb3d156961 Mon Sep 17 00:00:00 2001 From: Chanaka Indrajith Date: Sun, 23 Dec 2012 17:32:25 +0530 Subject: [PATCH 2/4] Change the way of calling backquote function --- tbl_replace.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tbl_replace.php b/tbl_replace.php index e296ec1548..547716afc6 100644 --- a/tbl_replace.php +++ b/tbl_replace.php @@ -364,8 +364,8 @@ if ($response->isAjax() && ! isset($_POST['ajax_page_request'])) { // retrieve the value really saved in the database when it is possible) if ($extra_data['hasUniqueIdentifier']) { - $sql_for_real_value = 'SELECT '. PMA_backquote($table) . '.' . PMA_backquote($column_name) - . ' FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) + $sql_for_real_value = 'SELECT '. PMA_Util::backquote($table) . '.' . PMA_Util::backquote($column_name) + . ' FROM ' . PMA_Util::backquote($db) . '.' . PMA_Util::backquote($table) . ' WHERE ' . $_REQUEST['where_clause'][0]; $extra_data['truncatableFieldValue'] = (PMA_DBI_fetch_value($sql_for_real_value) !== false) From 1ae30d2ba79d68b6f7a6029d901788c6b358f485 Mon Sep 17 00:00:00 2001 From: Chanaka Indrajith Date: Sun, 23 Dec 2012 18:24:29 +0530 Subject: [PATCH 3/4] Move hasPrimaryKeyOrUniqueKey function to insert_edit.lib.php file --- libraries/insert_edit.lib.php | 22 ++++++++++++++++++++++ tbl_replace.php | 23 ----------------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/libraries/insert_edit.lib.php b/libraries/insert_edit.lib.php index 8cda2cb230..30da942705 100644 --- a/libraries/insert_edit.lib.php +++ b/libraries/insert_edit.lib.php @@ -2329,4 +2329,26 @@ function PMA_getCurrentValueForDifferentTypes($possibly_uploaded_val, $key, return $current_value; } +/** + * Check whether particular table has primary key or unique key + * + * @param string $db Database name + * @param string $table Table name + * + * @return boolean + */ +function hasPrimaryKeyOrUniqueKey($db, $table) { + + $table_indexes = PMA_DBI_get_table_indexes($db, $table); + + foreach ($table_indexes as $index) { + if (($index['Key_name'] == 'PRIMARY') || ($index['Non_unique'] == '0')) { + return true; + } + } + + return false; + +} + ?> diff --git a/tbl_replace.php b/tbl_replace.php index 547716afc6..b3f2a89210 100644 --- a/tbl_replace.php +++ b/tbl_replace.php @@ -420,27 +420,4 @@ if (isset($_REQUEST['after_insert']) && 'new_insert' == $_REQUEST['after_insert' require '' . PMA_securePath($goto_include); exit; - -/** - * Check whether particular table has primary key or unique key - * - * @param string $db Database name - * @param string $table Table name - * - * @return boolean - */ -function hasPrimaryKeyOrUniqueKey($db, $table) { - - $table_indexes = PMA_DBI_get_table_indexes($db, $table); - - foreach ($table_indexes as $index) { - if (($index['Key_name'] == 'PRIMARY') || ($index['Non_unique'] == '0')) { - return true; - } - } - - return false; - -} - ?> From b16a6afeb6779fcf5013213b95ecfc9b778666c8 Mon Sep 17 00:00:00 2001 From: Chanaka Indrajith Date: Sun, 23 Dec 2012 19:48:15 +0530 Subject: [PATCH 4/4] Introduce new function for check truncatable values --- js/makegrid.js | 2 +- tbl_replace.php | 74 +++++++++++++++++++++++++++++++------------------ 2 files changed, 48 insertions(+), 28 deletions(-) diff --git a/js/makegrid.js b/js/makegrid.js index 66922efff4..3c4e27802f 100644 --- a/js/makegrid.js +++ b/js/makegrid.js @@ -1193,7 +1193,7 @@ function PMA_makegrid(t, enableResize, enableReorder, enableVisib, enableGridEdi if(data.success == true) { PMA_ajaxShowMessage(data.message); - // Generate new where clause again if the field + // Generate new where clause again if the column // can be truncated by MySQL and table does not have // primary/unique key if (data.isTruncatableField && !data.hasUniqueIdentifier) { diff --git a/tbl_replace.php b/tbl_replace.php index 4abaa1f281..5141a1310d 100644 --- a/tbl_replace.php +++ b/tbl_replace.php @@ -573,32 +573,9 @@ if ($GLOBALS['is_ajax_request'] == true) { $column_value = $_REQUEST['fields']['multi_edit'][0][0]; $column_meta_data = PMA_DBI_get_columns($db, $table, $column_name); - if (stripos($column_meta_data['Type'], 'smallint') !== false) { - - $extra_data['isTruncatableField'] = true; - $extra_data['hasUniqueIdentifier'] = hasPrimaryKeyOrUniqueKey($db, $table) - ? true - : false; - - // If table has unique identifier (primary/unique key), fetch the value - // of the field. (Yes, can be just round and return, but better need to - // retrieve the value really saved in the database when it is possible) - if ($extra_data['hasUniqueIdentifier']) { - - $sql_for_real_value = 'SELECT '. PMA_backquote($table) . '.' . PMA_backquote($column_name) - . ' FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) - . ' WHERE ' . $_REQUEST['where_clause'][0]; - - $extra_data['truncatableFieldValue'] = (PMA_DBI_fetch_value($sql_for_real_value) !== false) - ? PMA_DBI_fetch_value($sql_for_real_value) - : round($column_value); - - } else { - $extra_data['truncatableFieldValue'] = round($column_value); - } - - } - + PMA_verifyWhetherValueCanBeTruncatedAndAppendExtraData( + $db, $table, $column_name, $column_value, $column_meta_data, $extra_data + ); /**Get the total row count of the table*/ $extra_data['row_count'] = PMA_Table::countRecords($_REQUEST['db'], $_REQUEST['table']); @@ -647,7 +624,7 @@ exit; * * @return boolean */ -function hasPrimaryKeyOrUniqueKey($db, $table) { +function PMA_hasPrimaryKeyOrUniqueKey($db, $table) { $table_indexes = PMA_DBI_get_table_indexes($db, $table); @@ -661,4 +638,47 @@ function hasPrimaryKeyOrUniqueKey($db, $table) { } +/** + * Check whether inline edited value can be truncated or not, + * and add additional parameters for extra_data array if needed + * + * @param string $db Database name + * @param string $table Table name + * @param string $column_name Column name + * @param string $column_value Edited column value + * @param array $column_meta_data Column meta data + * @param array &$extra_data Extra data for ajax response + */ +function PMA_verifyWhetherValueCanBeTruncatedAndAppendExtraData( + $db, $table, $column_name, $column_value, $column_meta_data, &$extra_data +) { + + if (stripos($column_meta_data['Type'], 'smallint') !== false) { + + $extra_data['isTruncatableField'] = true; + $extra_data['hasUniqueIdentifier'] = PMA_hasPrimaryKeyOrUniqueKey($db, $table) + ? true + : false; + + // If table has unique identifier (primary/unique key), fetch the value + // of the field. (Yes, can be just round and return, but better need to + // retrieve the value really saved in the database when it is possible) + if ($extra_data['hasUniqueIdentifier']) { + + $sql_for_real_value = 'SELECT '. PMA_backquote($table) . '.' . PMA_backquote($column_name) + . ' FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) + . ' WHERE ' . $_REQUEST['where_clause'][0]; + + $extra_data['truncatableFieldValue'] = (PMA_DBI_fetch_value($sql_for_real_value) !== false) + ? PMA_DBI_fetch_value($sql_for_real_value) + : round($column_value); + + } else { + $extra_data['truncatableFieldValue'] = round($column_value); + } + + } + +} + ?>