primary_key is really a WHERE clause (that works also on tables where no PK is defined)

This commit is contained in:
Marc Delisle
2009-12-30 15:27:27 +00:00
parent d63ed916fc
commit 87b994743b
6 changed files with 68 additions and 71 deletions

View File

@@ -1074,7 +1074,7 @@ function PMA_displayTableBody(&$dt_result, &$is_display, $map, $analyzed_sql) {
} }
// 1. Prepares the row (gets primary keys to use) // 1. Prepares the row
// 1.1 Results from a "SELECT" statement -> builds the // 1.1 Results from a "SELECT" statement -> builds the
// WHERE clause to use in links (a unique key if possible) // WHERE clause to use in links (a unique key if possible)
/** /**
@@ -1101,7 +1101,7 @@ function PMA_displayTableBody(&$dt_result, &$is_display, $map, $analyzed_sql) {
$_url_params = array( $_url_params = array(
'db' => $db, 'db' => $db,
'table' => $table, 'table' => $table,
'primary_key' => $where_clause, 'where_clause' => $where_clause,
'clause_is_unique' => $clause_is_unique, 'clause_is_unique' => $clause_is_unique,
'sql_query' => $url_sql_query, 'sql_query' => $url_sql_query,
'goto' => 'sql.php', 'goto' => 'sql.php',
@@ -1239,7 +1239,7 @@ function PMA_displayTableBody(&$dt_result, &$is_display, $map, $analyzed_sql) {
$_url_params = array( $_url_params = array(
'db' => $db, 'db' => $db,
'table' => $table, 'table' => $table,
'primary_key' => $where_clause, 'where_clause' => $where_clause,
'transform_key' => $meta->name, 'transform_key' => $meta->name,
); );

View File

@@ -24,18 +24,14 @@ require_once './libraries/db_table_exists.lib.php';
* Here it's better to use a if, instead of the '?' operator * Here it's better to use a if, instead of the '?' operator
* to avoid setting a variable to '' when it's not present in $_REQUEST * to avoid setting a variable to '' when it's not present in $_REQUEST
*/ */
/** if (isset($_REQUEST['where_clause'])) {
* @todo this one is badly named, it's really a WHERE condition $where_clause = $_REQUEST['where_clause'];
* and exists even for tables not having a primary key or unique key
*/
if (isset($_REQUEST['primary_key'])) {
$primary_key = $_REQUEST['primary_key'];
} }
if (isset($_REQUEST['clause_is_unique'])) { if (isset($_REQUEST['clause_is_unique'])) {
$clause_is_unique = $_REQUEST['clause_is_unique']; $clause_is_unique = $_REQUEST['clause_is_unique'];
} }
if (isset($_SESSION['edit_next'])) { if (isset($_SESSION['edit_next'])) {
$primary_key = $_SESSION['edit_next']; $where_clause = $_SESSION['edit_next'];
unset($_SESSION['edit_next']); unset($_SESSION['edit_next']);
$after_insert = 'edit_next'; $after_insert = 'edit_next';
} }
@@ -156,26 +152,28 @@ PMA_DBI_select_db($db);
$table_fields = PMA_DBI_fetch_result('SHOW FIELDS FROM ' . PMA_backquote($table) . ';', $table_fields = PMA_DBI_fetch_result('SHOW FIELDS FROM ' . PMA_backquote($table) . ';',
null, null, null, PMA_DBI_QUERY_STORE); null, null, null, PMA_DBI_QUERY_STORE);
$rows = array(); $rows = array();
if (isset($primary_key)) { if (isset($where_clause)) {
// when in edit mode load all selected rows from table // when in edit mode load all selected rows from table
$insert_mode = false; $insert_mode = false;
if (is_array($primary_key)) { if (is_array($where_clause)) {
$primary_key_array = $primary_key; $where_clause_array = $where_clause;
} else { } else {
$primary_key_array = array(0 => $primary_key); $where_clause_array = array(0 => $where_clause);
} }
$result = array(); $result = array();
$found_unique_key = false; $found_unique_key = false;
foreach ($primary_key_array as $key_id => $primary_key) { $where_clauses = array();
$local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ' WHERE ' . $primary_key . ';';
foreach ($where_clause_array as $key_id => $where_clause) {
$local_query = 'SELECT * FROM ' . PMA_backquote($db) . '.' . PMA_backquote($table) . ' WHERE ' . $where_clause . ';';
$result[$key_id] = PMA_DBI_query($local_query, null, PMA_DBI_QUERY_STORE); $result[$key_id] = PMA_DBI_query($local_query, null, PMA_DBI_QUERY_STORE);
$rows[$key_id] = PMA_DBI_fetch_assoc($result[$key_id]); $rows[$key_id] = PMA_DBI_fetch_assoc($result[$key_id]);
$primary_keys[$key_id] = str_replace('\\', '\\\\', $primary_key); $where_clauses[$key_id] = str_replace('\\', '\\\\', $where_clause);
// No row returned // No row returned
if (! $rows[$key_id]) { if (! $rows[$key_id]) {
unset($rows[$key_id], $primary_key_array[$key_id]); unset($rows[$key_id], $where_clause_array[$key_id]);
PMA_showMessage($strEmptyResultSet, $local_query); PMA_showMessage($strEmptyResultSet, $local_query);
echo "\n"; echo "\n";
require_once './libraries/footer.inc.php'; require_once './libraries/footer.inc.php';
@@ -231,9 +229,9 @@ $_form_params = array(
'err_url' => $err_url, 'err_url' => $err_url,
'sql_query' => $sql_query, 'sql_query' => $sql_query,
); );
if (isset($primary_keys)) { if (isset($where_clauses)) {
foreach ($primary_key_array as $key_id => $primary_key) { foreach ($where_clause_array as $key_id => $where_clause) {
$_form_params['primary_key[' . $key_id . ']'] = trim($primary_key); $_form_params['where_clause[' . $key_id . ']'] = trim($where_clause);
} }
} }
if (isset($clause_is_unique)) { if (isset($clause_is_unique)) {
@@ -263,8 +261,8 @@ $biggest_max_file_size = 0;
// (currently does not work for multi-edits) // (currently does not work for multi-edits)
$url_params['db'] = $db; $url_params['db'] = $db;
$url_params['table'] = $table; $url_params['table'] = $table;
if (isset($primary_key)) { if (isset($where_clause)) {
$url_params['primary_key'] = trim($primary_key); $url_params['where_clause'] = trim($where_clause);
} }
if (! empty($sql_query)) { if (! empty($sql_query)) {
$url_params['sql_query'] = $sql_query; $url_params['sql_query'] = $sql_query;
@@ -752,7 +750,7 @@ foreach ($rows as $row_id => $vrow) {
echo '<option value="' . $enum_value['html'] . '"'; echo '<option value="' . $enum_value['html'] . '"';
if ($data == $enum_value['plain'] if ($data == $enum_value['plain']
|| ($data == '' || ($data == ''
&& (! isset($primary_key) || $field['Null'] != 'YES') && (! isset($where_clause) || $field['Null'] != 'YES')
&& isset($field['Default']) && isset($field['Default'])
&& $enum_value['plain'] == $field['Default'])) { && $enum_value['plain'] == $field['Default'])) {
echo ' selected="selected"'; echo ' selected="selected"';
@@ -773,7 +771,7 @@ foreach ($rows as $row_id => $vrow) {
echo $unnullify_trigger; echo $unnullify_trigger;
if ($data == $enum_value['plain'] if ($data == $enum_value['plain']
|| ($data == '' || ($data == ''
&& (! isset($primary_key) || $field['Null'] != 'YES') && (! isset($where_clause) || $field['Null'] != 'YES')
&& isset($field['Default']) && isset($field['Default'])
&& $enum_value['plain'] == $field['Default'])) { && $enum_value['plain'] == $field['Default'])) {
echo ' checked="checked"'; echo ' checked="checked"';
@@ -1101,7 +1099,7 @@ foreach ($rows as $row_id => $vrow) {
<td valign="middle" nowrap="nowrap"> <td valign="middle" nowrap="nowrap">
<select name="submit_type" tabindex="<?php echo ($tabindex + $tabindex_for_value + 1); ?>"> <select name="submit_type" tabindex="<?php echo ($tabindex + $tabindex_for_value + 1); ?>">
<?php <?php
if (isset($primary_key)) { if (isset($where_clause)) {
?> ?>
<option value="<?php echo $strSave; ?>"><?php echo $strSave; ?></option> <option value="<?php echo $strSave; ?>"><?php echo $strSave; ?></option>
<?php <?php
@@ -1125,15 +1123,15 @@ if (!isset($after_insert)) {
<option value="back" <?php echo ($after_insert == 'back' ? 'selected="selected"' : ''); ?>><?php echo $strAfterInsertBack; ?></option> <option value="back" <?php echo ($after_insert == 'back' ? 'selected="selected"' : ''); ?>><?php echo $strAfterInsertBack; ?></option>
<option value="new_insert" <?php echo ($after_insert == 'new_insert' ? 'selected="selected"' : ''); ?>><?php echo $strAfterInsertNewInsert; ?></option> <option value="new_insert" <?php echo ($after_insert == 'new_insert' ? 'selected="selected"' : ''); ?>><?php echo $strAfterInsertNewInsert; ?></option>
<?php <?php
if (isset($primary_key)) { if (isset($where_clause)) {
?> ?>
<option value="same_insert" <?php echo ($after_insert == 'same_insert' ? 'selected="selected"' : ''); ?>><?php echo $strAfterInsertSame; ?></option> <option value="same_insert" <?php echo ($after_insert == 'same_insert' ? 'selected="selected"' : ''); ?>><?php echo $strAfterInsertSame; ?></option>
<?php <?php
// If we have just numeric primary key, we can also edit next // If we have just numeric primary key, we can also edit next
// in 2.8.2, we were looking for `field_name` = numeric_value // in 2.8.2, we were looking for `field_name` = numeric_value
//if (preg_match('@^[\s]*`[^`]*` = [0-9]+@', $primary_key)) { //if (preg_match('@^[\s]*`[^`]*` = [0-9]+@', $where_clause)) {
// in 2.9.0, we are looking for `table_name`.`field_name` = numeric_value // in 2.9.0, we are looking for `table_name`.`field_name` = numeric_value
if ($found_unique_key && preg_match('@^[\s]*`[^`]*`[\.]`[^`]*` = [0-9]+@', $primary_key)) { if ($found_unique_key && preg_match('@^[\s]*`[^`]*`[\.]`[^`]*` = [0-9]+@', $where_clause)) {
?> ?>
<option value="edit_next" <?php echo ($after_insert == 'edit_next' ? 'selected="selected"' : ''); ?>><?php echo $strAfterInsertNext; ?></option> <option value="edit_next" <?php echo ($after_insert == 'edit_next' ? 'selected="selected"' : ''); ?>><?php echo $strAfterInsertNext; ?></option>
<?php <?php
@@ -1169,9 +1167,9 @@ if ($insert_mode) {
<input type="hidden" name="err_url" value="<?php echo htmlspecialchars($err_url); ?>" /> <input type="hidden" name="err_url" value="<?php echo htmlspecialchars($err_url); ?>" />
<input type="hidden" name="sql_query" value="<?php echo htmlspecialchars($sql_query); ?>" /> <input type="hidden" name="sql_query" value="<?php echo htmlspecialchars($sql_query); ?>" />
<?php <?php
if (isset($primary_keys)) { if (isset($where_clauses)) {
foreach ($primary_key_array as $key_id => $primary_key) { foreach ($where_clause_array as $key_id => $where_clause) {
echo '<input type="hidden" name="primary_key[' . $key_id . ']" value="' . htmlspecialchars(trim($primary_key)) . '" />'. "\n"; echo '<input type="hidden" name="where_clause[' . $key_id . ']" value="' . htmlspecialchars(trim($where_clause)) . '" />'. "\n";
} }
} }
$tmp = '<select name="insert_rows" id="insert_rows" onchange="this.form.submit();" >' . "\n"; $tmp = '<select name="insert_rows" id="insert_rows" onchange="this.form.submit();" >' . "\n";

View File

@@ -31,8 +31,8 @@ if (! empty($sql_query)) {
$analyzed_sql = PMA_SQP_analyze($parsed_sql); $analyzed_sql = PMA_SQP_analyze($parsed_sql);
// Need to generate WHERE clause? // Need to generate WHERE clause?
if (isset($primary_key)) { if (isset($where_clause)) {
// Yes => rebuild query from scracts, this doesn't work with nested // Yes => rebuild query from scratch; this doesn't work with nested
// selects :-( // selects :-(
$sql_query = 'SELECT '; $sql_query = 'SELECT ';
@@ -48,9 +48,9 @@ if (! empty($sql_query)) {
$wheres = array(); $wheres = array();
if (isset($primary_key) && is_array($primary_key) if (isset($where_clause) && is_array($where_clause)
&& count($primary_key) > 0) { && count($where_clause) > 0) {
$wheres[] = '(' . implode(') OR (',$primary_key) . ')'; $wheres[] = '(' . implode(') OR (',$where_clause) . ')';
} }
if (!empty($analyzed_sql[0]['where_clause'])) { if (!empty($analyzed_sql[0]['where_clause'])) {

View File

@@ -80,15 +80,15 @@ if (isset($_REQUEST['after_insert'])
//$GLOBALS['goto'] = 'tbl_change.php'; //$GLOBALS['goto'] = 'tbl_change.php';
$goto_include = 'tbl_change.php'; $goto_include = 'tbl_change.php';
if (isset($_REQUEST['primary_key'])) { if (isset($_REQUEST['where_clause'])) {
if ($_REQUEST['after_insert'] == 'same_insert') { if ($_REQUEST['after_insert'] == 'same_insert') {
foreach ($_REQUEST['primary_key'] as $pk) { foreach ($_REQUEST['where_clause'] as $one_where_clause) {
$url_params['primary_key'][] = $pk; $url_params['where_clause'][] = $one_where_clause;
} }
} elseif ($_REQUEST['after_insert'] == 'edit_next') { } elseif ($_REQUEST['after_insert'] == 'edit_next') {
foreach ($_REQUEST['primary_key'] as $pk) { foreach ($_REQUEST['where_clause'] as $one_where_clause) {
$local_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table']) $local_query = 'SELECT * FROM ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
. ' WHERE ' . str_replace('` =', '` >', $pk) . ' WHERE ' . str_replace('` =', '` >', $one_where_clause)
. ' LIMIT 1;'; . ' LIMIT 1;';
$res = PMA_DBI_query($local_query); $res = PMA_DBI_query($local_query);
$row = PMA_DBI_fetch_row($res); $row = PMA_DBI_fetch_row($res);
@@ -134,9 +134,9 @@ if (isset($_REQUEST['err_url'])) {
/** /**
* Prepares the update/insert of a row * Prepares the update/insert of a row
*/ */
if (isset($_REQUEST['primary_key'])) { if (isset($_REQUEST['where_clause'])) {
// we were editing something => use primary key // we were editing something => use the WHERE clause
$loop_array = (is_array($_REQUEST['primary_key']) ? $_REQUEST['primary_key'] : array($_REQUEST['primary_key'])); $loop_array = (is_array($_REQUEST['where_clause']) ? $_REQUEST['where_clause'] : array($_REQUEST['where_clause']));
$using_key = true; $using_key = true;
$is_insert = ($_REQUEST['submit_type'] == $GLOBALS['strInsertAsNewRow']); $is_insert = ($_REQUEST['submit_type'] == $GLOBALS['strInsertAsNewRow']);
} else { } else {
@@ -166,9 +166,9 @@ $func_no_param = array(
'CURRENT_USER', 'CURRENT_USER',
); );
foreach ($loop_array as $rowcount => $primary_key) { foreach ($loop_array as $rowcount => $where_clause) {
// skip fields to be ignored // skip fields to be ignored
if (! $using_key && isset($_REQUEST['insert_ignore_' . $primary_key])) { if (! $using_key && isset($_REQUEST['insert_ignore_' . $where_clause])) {
continue; continue;
} }
@@ -215,8 +215,8 @@ foreach ($loop_array as $rowcount => $primary_key) {
// Fetch the current values of a row to use in case we have a protected field // Fetch the current values of a row to use in case we have a protected field
// @todo possibly move to ./libraries/tbl_replace_fields.inc.php // @todo possibly move to ./libraries/tbl_replace_fields.inc.php
if ($is_insert && $using_key && isset($me_fields_type) && is_array($me_fields_type) && isset($primary_key)) { if ($is_insert && $using_key && isset($me_fields_type) && is_array($me_fields_type) && isset($where_clause)) {
$prot_row = PMA_DBI_fetch_single_row('SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . $primary_key . ';'); $prot_row = PMA_DBI_fetch_single_row('SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . $where_clause . ';');
} }
foreach ($me_fields as $key => $val) { foreach ($me_fields as $key => $val) {
@@ -298,13 +298,13 @@ foreach ($loop_array as $rowcount => $primary_key) {
} else { } else {
// build update query // build update query
$query[] = 'UPDATE ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table']) $query[] = 'UPDATE ' . PMA_backquote($GLOBALS['db']) . '.' . PMA_backquote($GLOBALS['table'])
. ' SET ' . implode(', ', $query_values) . ' WHERE ' . $primary_key . ($_REQUEST['clause_is_unique'] ? '' : ' LIMIT 1'); . ' SET ' . implode(', ', $query_values) . ' WHERE ' . $where_clause . ($_REQUEST['clause_is_unique'] ? '' : ' LIMIT 1');
} }
} }
} // end foreach ($loop_array as $primary_key) } // end foreach ($loop_array as $where_clause)
unset($me_fields_name, $me_fields_prev, $me_funcs, $me_fields_type, $me_fields_null, $me_fields_null_prev, unset($me_fields_name, $me_fields_prev, $me_funcs, $me_fields_type, $me_fields_null, $me_fields_null_prev,
$me_auto_increment, $cur_value, $key, $val, $loop_array, $primary_key, $using_key, $me_auto_increment, $cur_value, $key, $val, $loop_array, $where_clause, $using_key,
$func_no_param); $func_no_param);
@@ -420,11 +420,11 @@ $active_page = $goto_include;
/** /**
* If user asked for "and then Insert another new row" we have to remove * If user asked for "and then Insert another new row" we have to remove
* primary key information so that tbl_change.php does not go back * WHERE clause information so that tbl_change.php does not go back
* to the current record * to the current record
*/ */
if (isset($_REQUEST['after_insert']) && 'new_insert' == $_REQUEST['after_insert']) { if (isset($_REQUEST['after_insert']) && 'new_insert' == $_REQUEST['after_insert']) {
unset($_REQUEST['primary_key']); unset($_REQUEST['where_clause']);
} }
/** /**

View File

@@ -79,13 +79,13 @@ require_once './libraries/header.inc.php';
if (!empty($submit_mult)) { if (!empty($submit_mult)) {
switch($submit_mult) { switch($submit_mult) {
case 'row_edit': case 'row_edit':
// garvin: As we got the fields to be edited from the 'rows_to_delete' // garvin: As we got the fields to be edited from the
// checkbox, we use the index of it as the // 'rows_to_delete' checkbox, we use the index of it as the
// indicating primary key. Then we built the array which is used for // indicating WHERE clause. Then we build the array which is used
// the tbl_change.php script. // for the tbl_change.php script.
$primary_key = array(); $where_clause = array();
foreach ($_REQUEST['rows_to_delete'] as $i_primary_key => $del_query) { foreach ($_REQUEST['rows_to_delete'] as $i_where_clause => $del_query) {
$primary_key[] = urldecode($i_primary_key); $where_clause[] = urldecode($i_where_clause);
} }
$active_page = 'tbl_change.php'; $active_page = 'tbl_change.php';
@@ -96,14 +96,13 @@ if (!empty($submit_mult)) {
// Needed to allow SQL export // Needed to allow SQL export
$single_table = TRUE; $single_table = TRUE;
//$sql_query = urldecode($sql_query); // garvin: As we got the fields to be edited from the
// garvin: As we got the fields to be edited from the 'rows_to_delete' // 'rows_to_delete' checkbox, we use the index of it as the
// checkbox, we use the index of it as the // indicating WHERE clause. Then we build the array which is used
// indicating primary key. Then we built the array which is used for // for the tbl_change.php script.
// the tbl_change.php script. $where_clause = array();
$primary_key = array(); foreach ($_REQUEST['rows_to_delete'] as $i_where_clause => $del_query) {
foreach ($_REQUEST['rows_to_delete'] as $i_primary_key => $del_query) { $where_clause[] = urldecode($i_where_clause);
$primary_key[] = urldecode($i_primary_key);
} }
$active_page = 'tbl_export.php'; $active_page = 'tbl_export.php';

View File

@@ -30,8 +30,8 @@ require_once './libraries/db_table_exists.lib.php';
*/ */
PMA_DBI_select_db($db); PMA_DBI_select_db($db);
$table_def = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table), null, PMA_DBI_QUERY_STORE); $table_def = PMA_DBI_query('SHOW FIELDS FROM ' . PMA_backquote($table), null, PMA_DBI_QUERY_STORE);
if (isset($primary_key)) { if (isset($where_clause)) {
$result = PMA_DBI_query('SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . $primary_key . ';', null, PMA_DBI_QUERY_STORE); $result = PMA_DBI_query('SELECT * FROM ' . PMA_backquote($table) . ' WHERE ' . $where_clause . ';', null, PMA_DBI_QUERY_STORE);
$row = PMA_DBI_fetch_assoc($result); $row = PMA_DBI_fetch_assoc($result);
} else { } else {
$result = PMA_DBI_query('SELECT * FROM ' . PMA_backquote($table) . ' LIMIT 1;', null, PMA_DBI_QUERY_STORE); $result = PMA_DBI_query('SELECT * FROM ' . PMA_backquote($table) . ' LIMIT 1;', null, PMA_DBI_QUERY_STORE);