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
* libraries/common.lib.php PMA_getUvaCondition():
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>
### 2.8.0-rc1 released from QA_2_8 branch

View File

@@ -28,6 +28,16 @@ class PMA_Table {
*/
var $settings = array();
/**
* @var array errors occured
*/
var $errors = array();
/**
* @var array messages
*/
var $messages = array();
/**
* Constructor
*
@@ -48,6 +58,16 @@ class PMA_Table {
return $this->getName();
}
function getLastError()
{
return end($this->errors);
}
function getLastMessage()
{
return end($this->messages);
}
/**
* sets table anme
*
@@ -186,9 +206,9 @@ class PMA_Table {
*
* @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>
*/
function duplicateInfo($work, $pma_table, $get_fields, $where_fields,
$new_fields) {
$new_fields)
{
$last_id = -1;
if ($GLOBALS['cfgRelation'][$work]) {
@@ -478,7 +499,8 @@ class PMA_Table {
*
* @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;
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
*
* @param string old tbale name
* @param string new table name
* @param string new database name
* @return boolean success
*/
function rename($old_name, $new_name)
function rename($new_name, $new_db = null)
{
if (null !== $new_db && $new_db !== $this->getDbName()) {
// Ensure the target is valid
if (count($GLOBALS['dblist']) > 0
&& ! in_array($GLOBALS['db'], $GLOBALS['dblist'])) {
&& ! 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'] = '
ALTER TABLE ' . PMA_backquote($old_name) . '
RENAME ' . PMA_backquote($new_name) . ';';
RENAME TABLE ' . $this->getFullName(true) . '
TO ' . $new_table->getFullName(true) . ';';
if (! PMA_DBI_query($GLOBALS['sql_query'])) {
// TODO add $GLOBALS['strErrorRenamingTable'];
$this->errors[] = $GLOBALS['strError'] . ': ' . $new_table->getFullName();
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
require_once './libraries/relation.lib.php';
$GLOBALS['cfgRelation'] = PMA_getRelationsParam();
if ($GLOBALS['cfgRelation']['commwork']) {
$remove_query = '
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['column_info']) . '
SET table_name = \'' . PMA_sqlAddslashes($new_name) . '\'
WHERE db_name = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\'
AND table_name = \'' . PMA_sqlAddslashes($old_name) . '\'';
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
. PMA_backquote($GLOBALS['cfgRelation']['column_info']) . '
SET `db_name` = \'' . PMA_sqlAddslashes($new_db) . '\',
`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);
unset($remove_query);
}
if ($GLOBALS['cfgRelation']['displaywork']) {
$table_query = '
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['table_info']) . '
SET table_name = \'' . PMA_sqlAddslashes($new_name) . '\'
WHERE db_name = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\'
AND table_name = \'' . PMA_sqlAddslashes($old_name) . '\'';
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
. PMA_backquote($GLOBALS['cfgRelation']['table_info']) . '
SET `db_name` = \'' . PMA_sqlAddslashes($new_db) . '\',
`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);
unset($table_query);
}
if ($GLOBALS['cfgRelation']['relwork']) {
$table_query = '
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation']) . '
SET foreign_table = \'' . PMA_sqlAddslashes($new_name) . '\'
WHERE foreign_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\'
AND foreign_table = \'' . PMA_sqlAddslashes($old_name) . '\'';
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
. PMA_backquote($GLOBALS['cfgRelation']['relation']) . '
SET `foreign_db` = \'' . PMA_sqlAddslashes($new_db) . '\',
`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);
$table_query = '
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['relation']) . '
SET master_table = \'' . PMA_sqlAddslashes($new_name) . '\'
WHERE master_db = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\'
AND master_table = \'' . PMA_sqlAddslashes($old_name) . '\'';
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
. PMA_backquote($GLOBALS['cfgRelation']['relation']) . '
SET `master_db` = \'' . PMA_sqlAddslashes($new_db) . '\',
`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);
unset($table_query);
}
if ($GLOBALS['cfgRelation']['pdfwork']) {
$table_query = '
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($GLOBALS['cfgRelation']['table_coords']) . '
SET table_name = \'' . PMA_sqlAddslashes($new_name) . '\'
WHERE db_name = \'' . PMA_sqlAddslashes($GLOBALS['db']) . '\'
AND table_name = \'' . PMA_sqlAddslashes($old_name) . '\'';
UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.'
. PMA_backquote($GLOBALS['cfgRelation']['table_coords']) . '
SET `db_name` = \'' . PMA_sqlAddslashes($new_db) . '\',
`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);
unset($table_query);
}
$this->messages[] = sprintf($GLOBALS['strRenameTableOK'],
htmlspecialchars($old_name), htmlspecialchars($new_name));
return true;
}
}

View File

@@ -5,11 +5,14 @@
require_once './libraries/common.lib.php';
require_once './libraries/Table.class.php';
$pma_table = new PMA_Table($GLOBALS['table'], $GLOBALS['db']);
/**
* Runs common work
*/
require './libraries/tbl_properties_common.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
@@ -41,21 +44,16 @@ $table_alters = array();
* Updates table comment, type and options if required
*/
if (isset($_REQUEST['submitoptions'])) {
if (isset($_REQUEST['new_name']) && $_REQUEST['new_name'] !== $GLOBALS['table']) {
if (trim($_REQUEST['new_name']) === '') {
$errors[] = $strTableEmpty;
} elseif (strpos($_REQUEST['new_name'], '.') !== false) {
$errors[] = $strError . ': ' . $_REQUEST['new_name'];
} else {
if (PMA_Table::rename($GLOBALS['table'], $_REQUEST['new_name'])) {
$message = sprintf($GLOBALS['strRenameTableOK'],
htmlspecialchars($GLOBALS['table']), htmlspecialchars($_REQUEST['new_name']));
$GLOBALS['table'] = $_REQUEST['new_name'];
$message = '';
if (isset($_REQUEST['new_name'])) {
if ($pma_table->rename($_REQUEST['new_name'])) {
$message .= $pma_table->getLastMessage();
$GLOBALS['table'] = $pma_table->getName();;
$reread_info = true;
$reload = true;
} else {
$errors[] = $strError . ': ' . $_REQUEST['new_name'];
}
$errors[] = $pma_table->getLastError();
$message .= $pma_table->getLastError();
}
}
if (isset($_REQUEST['comment'])
@@ -105,7 +103,7 @@ if (isset($_REQUEST['submitoptions'])) {
if (count($table_alters) > 0) {
$sql_query = 'ALTER TABLE ' . PMA_backquote($GLOBALS['table']);
$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;
unset($table_alters);
}