Edit next row: do not offer if no unique key on table

This commit is contained in:
Marc Delisle
2007-03-26 13:23:24 +00:00
parent dadcb42f7e
commit d298724fd4
3 changed files with 23 additions and 5 deletions

View File

@@ -1848,16 +1848,18 @@ function PMA_checkParameters($params, $die = true, $request = true)
* @param integer $fields_cnt number of fields
* @param array $fields_meta meta information about fields
* @param array $row current row
* @param boolean $force_unique generate condition only on pk or unique
*
* @access public
* @author Michal Cihar (michal@cihar.com)
* @return string calculated condition
*/
function PMA_getUniqueCondition($handle, $fields_cnt, $fields_meta, $row)
function PMA_getUniqueCondition($handle, $fields_cnt, $fields_meta, $row, $force_unique=false)
{
$primary_key = '';
$unique_key = '';
$nonprimary_condition = '';
$preferred_condition = '';
for ($i = 0; $i < $fields_cnt; ++$i) {
$condition = '';
@@ -1946,7 +1948,7 @@ function PMA_getUniqueCondition($handle, $fields_cnt, $fields_meta, $row)
$preferred_condition = $primary_key;
} elseif ($unique_key) {
$preferred_condition = $unique_key;
} else {
} elseif (! $force_unique) {
$preferred_condition = $nonprimary_condition;
}

View File

@@ -30,6 +30,10 @@ if (isset($_REQUEST['repeat_cells'])) {
if (isset($_REQUEST['dontlimitchars'])) {
$dontlimitchars = $_REQUEST['dontlimitchars'];
}
/**
* @todo this one is badly named, it's really a WHERE condition
* and exists even for tables not having a primary key or unique key
*/
if (isset($_REQUEST['primary_key'])) {
$primary_key = $_REQUEST['primary_key'];
}
@@ -140,6 +144,7 @@ if (isset($primary_key)) {
$row = array();
$result = array();
$found_unique_key = false;
foreach ($primary_key_array as $rowcount => $primary_key) {
$local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ' WHERE ' . $primary_key . ';';
$result[$rowcount] = PMA_DBI_query($local_query, null, PMA_DBI_QUERY_STORE);
@@ -152,7 +157,13 @@ if (isset($primary_key)) {
PMA_showMessage($strEmptyResultSet, $local_query);
echo "\n";
require_once './libraries/footer.inc.php';
} // end if (no record returned)
} else { // end if (no record returned)
$meta = PMA_DBI_get_fields_meta($result[$rowcount]);
if ($tmp = PMA_getUniqueCondition($result[$rowcount], count($meta), $meta, $row[$rowcount], true)) {
$found_unique_key = true;
}
unset($tmp);
}
}
} else {
$result = PMA_DBI_query('SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ' LIMIT 1;', null, PMA_DBI_QUERY_STORE);
@@ -1008,7 +1019,7 @@ if (isset($primary_key)) {
// in 2.8.2, we were looking for `field_name` = numeric_value
//if (preg_match('@^[\s]*`[^`]*` = [0-9]+@', $primary_key)) {
// in 2.9.0, we are looking for `table_name`.`field_name` = numeric_value
if (preg_match('@^[\s]*`[^`]*`[\.]`[^`]*` = [0-9]+@', $primary_key)) {
if ($found_unique_key && preg_match('@^[\s]*`[^`]*`[\.]`[^`]*` = [0-9]+@', $primary_key)) {
?>
<option value="edit_next" <?php echo ($after_insert == 'edit_next' ? 'selected="selected"' : ''); ?>><?php echo $strAfterInsertNext; ?></option>
<?php

View File

@@ -98,7 +98,12 @@ if (isset($_REQUEST['after_insert'])
$res = PMA_DBI_query($local_query);
$row = PMA_DBI_fetch_row($res);
$meta = PMA_DBI_get_fields_meta($res);
$_SESSION['edit_next'] = PMA_getUniqueCondition($res, count($row), $meta, $row);
// must find a unique condition based on unique key,
// not a combination of all fields
if ($tmp = PMA_getUniqueCondition($res, count($meta), $meta, $row, true)) {
$_SESSION['edit_next'] = $tmp;
}
unset($tmp);
}
}
}