make use of PMA_Table object for renaming tables (added also some required functionality to PMA_Table class)

This commit is contained in:
Sebastian Mendel
2006-02-21 18:25:57 +00:00
parent c69bebb25b
commit ecc793bf83
3 changed files with 137 additions and 48 deletions

View File

@@ -39,6 +39,9 @@ $Source$
minor tweaks and formating minor tweaks and formating
* libraries/common.lib.php PMA_getUvaCondition(): * libraries/common.lib.php PMA_getUvaCondition():
fixed bug #1436058 Notices while browsing table fixed bug #1436058 Notices while browsing table
* tbl_properties_operations.php, libraries\Table.class.php:
make use of PMA_Table object for renaming tables
(added also some required functionality to PMA_Table class)
2006-02-20 Marc Delisle <lem9@users.sourceforge.net> 2006-02-20 Marc Delisle <lem9@users.sourceforge.net>
### 2.8.0-rc1 released from QA_2_8 branch ### 2.8.0-rc1 released from QA_2_8 branch

View File

@@ -28,6 +28,16 @@ class PMA_Table {
*/ */
var $settings = array(); var $settings = array();
/**
* @var array errors occured
*/
var $errors = array();
/**
* @var array messages
*/
var $messages = array();
/** /**
* Constructor * Constructor
* *
@@ -48,6 +58,16 @@ class PMA_Table {
return $this->getName(); return $this->getName();
} }
function getLastError()
{
return end($this->errors);
}
function getLastMessage()
{
return end($this->messages);
}
/** /**
* sets table anme * sets table anme
* *
@@ -186,9 +206,9 @@ class PMA_Table {
* *
* @see PMA_Table::__construct() * @see PMA_Table::__construct()
*/ */
function PMA_Table() function PMA_Table($table_name, $db_name)
{ {
$this->__construct(); $this->__construct($table_name, $db_name);
} }
/** /**
@@ -407,7 +427,8 @@ class PMA_Table {
* @author Garvin Hicking <me@supergarv.de> * @author Garvin Hicking <me@supergarv.de>
*/ */
function duplicateInfo($work, $pma_table, $get_fields, $where_fields, function duplicateInfo($work, $pma_table, $get_fields, $where_fields,
$new_fields) { $new_fields)
{
$last_id = -1; $last_id = -1;
if ($GLOBALS['cfgRelation'][$work]) { if ($GLOBALS['cfgRelation'][$work]) {
@@ -478,7 +499,8 @@ class PMA_Table {
* *
* @author Michal Cihar <michal@cihar.com> * @author Michal Cihar <michal@cihar.com>
*/ */
function moveCopy($source_db, $source_table, $target_db, $target_table, $what, $move) { function moveCopy($source_db, $source_table, $target_db, $target_table, $what, $move)
{
global $dblist, $err_url; global $dblist, $err_url;
if (! isset($GLOBALS['sql_query'])) { if (! isset($GLOBALS['sql_query'])) {
@@ -763,80 +785,146 @@ class PMA_Table {
} }
/**
* checks if given name is a valid table name,
* currently if not empty, trailing spaces, '.', '/' and '\'
*
* @todo add check for valid chars in filename on current system/os
* @see http://dev.mysql.com/doc/refman/5.0/en/legal-names.html
* @param string $table_name name to check
* @return boolean whether the string is valid or not
*/
function isValidName($table_name)
{
if ($table_name !== trim($table_name)) {
// trailing spaces
return false;
}
if (! strlen($table_name)) {
// zero length
return false;
}
if (preg_match('/[.\/\\\\]+/i', $table_name)) {
// illegal char . / \
return false;
}
return true;
}
/** /**
* renames table * renames table
* *
* @param string old tbale name
* @param string new table name * @param string new table name
* @param string new database name
* @return boolean success * @return boolean success
*/ */
function rename($old_name, $new_name) function rename($new_name, $new_db = null)
{ {
// Ensure the target is valid if (null !== $new_db && $new_db !== $this->getDbName()) {
if (count($GLOBALS['dblist']) > 0 // Ensure the target is valid
&& ! in_array($GLOBALS['db'], $GLOBALS['dblist'])) { if (count($GLOBALS['dblist']) > 0
return false; && ! in_array($new_db, $GLOBALS['dblist'])) {
// TODO add string $strInvalidDatabase
$this->errors[] = $GLOBALS['strError'] . ': ' . $new_db;
return false;
}
} else {
$new_db = $this->getDbName();
} }
PMA_DBI_select_db($GLOBALS['db']); $new_table = new PMA_Table($new_name, $new_db);
if ($this->getFullName() === $new_table->getFullName()) {
return true;
}
if (! PMA_Table::isValidName($new_name)) {
// TODO add string $strInvalidTableName
$this->errors[] = $GLOBALS['strError'] . ': ' . $new_table->getFullName();
return false;
}
$GLOBALS['sql_query'] = ' $GLOBALS['sql_query'] = '
ALTER TABLE ' . PMA_backquote($old_name) . ' RENAME TABLE ' . $this->getFullName(true) . '
RENAME ' . PMA_backquote($new_name) . ';'; TO ' . $new_table->getFullName(true) . ';';
if (! PMA_DBI_query($GLOBALS['sql_query'])) { if (! PMA_DBI_query($GLOBALS['sql_query'])) {
// TODO add $GLOBALS['strErrorRenamingTable'];
$this->errors[] = $GLOBALS['strError'] . ': ' . $new_table->getFullName();
return false; return false;
} }
$old_name = $this->getName();
$old_db = $this->getDbName();
$this->setName($new_name);
$this->setDbName($new_db);
// TODO move into extra function
// PMA_Relation::renameTable($new_name, $old_name, $new_db, $old_db)
// garvin: Move old entries from comments to new table // garvin: Move old entries from comments to new table
require_once './libraries/relation.lib.php'; require_once './libraries/relation.lib.php';
$GLOBALS['cfgRelation'] = PMA_getRelationsParam(); $GLOBALS['cfgRelation'] = PMA_getRelationsParam();
if ($GLOBALS['cfgRelation']['commwork']) { if ($GLOBALS['cfgRelation']['commwork']) {
$remove_query = ' $remove_query = '
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['column_info']) . ' UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
SET table_name = \'' . PMA_sqlAddslashes($new_name) . '\' . PMA_backquote($GLOBALS['cfgRelation']['column_info']) . '
WHERE db_name = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\' SET `db_name` = \'' . PMA_sqlAddslashes($new_db) . '\',
AND table_name = \'' . PMA_sqlAddslashes($old_name) . '\''; `table_name` = \'' . PMA_sqlAddslashes($new_name) . '\'
WHERE `db_name` = \'' . PMA_sqlAddslashes($old_db) . '\'
AND `table_name` = \'' . PMA_sqlAddslashes($old_name) . '\'';
PMA_query_as_cu($remove_query); PMA_query_as_cu($remove_query);
unset($remove_query); unset($remove_query);
} }
if ($GLOBALS['cfgRelation']['displaywork']) { if ($GLOBALS['cfgRelation']['displaywork']) {
$table_query = ' $table_query = '
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['table_info']) . ' UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
SET table_name = \'' . PMA_sqlAddslashes($new_name) . '\' . PMA_backquote($GLOBALS['cfgRelation']['table_info']) . '
WHERE db_name = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\' SET `db_name` = \'' . PMA_sqlAddslashes($new_db) . '\',
AND table_name = \'' . PMA_sqlAddslashes($old_name) . '\''; `table_name` = \'' . PMA_sqlAddslashes($new_name) . '\'
WHERE `db_name` = \'' . PMA_sqlAddslashes($old_db) . '\'
AND `table_name` = \'' . PMA_sqlAddslashes($old_name) . '\'';
PMA_query_as_cu($table_query); PMA_query_as_cu($table_query);
unset($table_query); unset($table_query);
} }
if ($GLOBALS['cfgRelation']['relwork']) { if ($GLOBALS['cfgRelation']['relwork']) {
$table_query = ' $table_query = '
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation']) . ' UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
SET foreign_table = \'' . PMA_sqlAddslashes($new_name) . '\' . PMA_backquote($GLOBALS['cfgRelation']['relation']) . '
WHERE foreign_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\' SET `foreign_db` = \'' . PMA_sqlAddslashes($new_db) . '\',
AND foreign_table = \'' . PMA_sqlAddslashes($old_name) . '\''; `foreign_table` = \'' . PMA_sqlAddslashes($new_name) . '\'
WHERE `foreign_db` = \'' . PMA_sqlAddslashes($old_db) . '\'
AND `foreign_table` = \'' . PMA_sqlAddslashes($old_name) . '\'';
PMA_query_as_cu($table_query); PMA_query_as_cu($table_query);
$table_query = ' $table_query = '
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation']) . ' UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
SET master_table = \'' . PMA_sqlAddslashes($new_name) . '\' . PMA_backquote($GLOBALS['cfgRelation']['relation']) . '
WHERE master_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\' SET `master_db` = \'' . PMA_sqlAddslashes($new_db) . '\',
AND master_table = \'' . PMA_sqlAddslashes($old_name) . '\''; `master_table` = \'' . PMA_sqlAddslashes($new_name) . '\'
WHERE `master_db` = \'' . PMA_sqlAddslashes($old_db) . '\'
AND `master_table` = \'' . PMA_sqlAddslashes($old_name) . '\'';
PMA_query_as_cu($table_query); PMA_query_as_cu($table_query);
unset($table_query); unset($table_query);
} }
if ($GLOBALS['cfgRelation']['pdfwork']) { if ($GLOBALS['cfgRelation']['pdfwork']) {
$table_query = ' $table_query = '
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['table_coords']) . ' UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
SET table_name = \'' . PMA_sqlAddslashes($new_name) . '\' . PMA_backquote($GLOBALS['cfgRelation']['table_coords']) . '
WHERE db_name = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\' SET `db_name` = \'' . PMA_sqlAddslashes($new_db) . '\',
AND table_name = \'' . PMA_sqlAddslashes($old_name) . '\''; `table_name` = \'' . PMA_sqlAddslashes($new_name) . '\'
WHERE `db_name` = \'' . PMA_sqlAddslashes($old_db) . '\'
AND `table_name` = \'' . PMA_sqlAddslashes($old_name) . '\'';
PMA_query_as_cu($table_query); PMA_query_as_cu($table_query);
unset($table_query); unset($table_query);
} }
$this->messages[] = sprintf($GLOBALS['strRenameTableOK'],
htmlspecialchars($old_name), htmlspecialchars($new_name));
return true; return true;
} }
} }

View File

@@ -5,11 +5,14 @@
require_once './libraries/common.lib.php'; require_once './libraries/common.lib.php';
require_once './libraries/Table.class.php'; require_once './libraries/Table.class.php';
$pma_table = new PMA_Table($GLOBALS['table'], $GLOBALS['db']);
/** /**
* Runs common work * Runs common work
*/ */
require './libraries/tbl_properties_common.php'; require './libraries/tbl_properties_common.php';
$url_query .= '&amp;goto=tbl_properties_operations.php&amp;back=tbl_properties_operations.php'; $url_query .= '&amp;goto=tbl_properties_operations.php&amp;back=tbl_properties_operations.php';
$url_params['goto'] = $url_params['back'] = 'tbl_properties_operations.php';
/** /**
* Gets relation settings * Gets relation settings
@@ -41,21 +44,16 @@ $table_alters = array();
* Updates table comment, type and options if required * Updates table comment, type and options if required
*/ */
if (isset($_REQUEST['submitoptions'])) { if (isset($_REQUEST['submitoptions'])) {
if (isset($_REQUEST['new_name']) && $_REQUEST['new_name'] !== $GLOBALS['table']) { $message = '';
if (trim($_REQUEST['new_name']) === '') { if (isset($_REQUEST['new_name'])) {
$errors[] = $strTableEmpty; if ($pma_table->rename($_REQUEST['new_name'])) {
} elseif (strpos($_REQUEST['new_name'], '.') !== false) { $message .= $pma_table->getLastMessage();
$errors[] = $strError . ': ' . $_REQUEST['new_name']; $GLOBALS['table'] = $pma_table->getName();;
$reread_info = true;
$reload = true;
} else { } else {
if (PMA_Table::rename($GLOBALS['table'], $_REQUEST['new_name'])) { $errors[] = $pma_table->getLastError();
$message = sprintf($GLOBALS['strRenameTableOK'], $message .= $pma_table->getLastError();
htmlspecialchars($GLOBALS['table']), htmlspecialchars($_REQUEST['new_name']));
$GLOBALS['table'] = $_REQUEST['new_name'];
$reread_info = true;
$reload = true;
} else {
$errors[] = $strError . ': ' . $_REQUEST['new_name'];
}
} }
} }
if (isset($_REQUEST['comment']) if (isset($_REQUEST['comment'])
@@ -105,7 +103,7 @@ if (isset($_REQUEST['submitoptions'])) {
if (count($table_alters) > 0) { if (count($table_alters) > 0) {
$sql_query = 'ALTER TABLE ' . PMA_backquote($GLOBALS['table']); $sql_query = 'ALTER TABLE ' . PMA_backquote($GLOBALS['table']);
$sql_query .= "\r\n" . implode("\r\n", $table_alters); $sql_query .= "\r\n" . implode("\r\n", $table_alters);
$message = PMA_DBI_query($sql_query) ? $strSuccess : $strError; $message .= PMA_DBI_query($sql_query) ? $strSuccess : $strError;
$reread_info = true; $reread_info = true;
unset($table_alters); unset($table_alters);
} }