* fix for zdb class.

This commit is contained in:
wangyidong
2015-12-02 17:32:51 +08:00
parent 33dff40092
commit 6ae74c1430
2 changed files with 15 additions and 12 deletions

View File

@@ -48,11 +48,12 @@ class zdb
/* Get all tables in database. */
$allTables = array();
$stmt = $this->dbh->query('show tables');
while($table = $stmt->fetch(PDO::FETCH_ASSOC))
$stmt = $this->dbh->query("show full tables");
while($table = $stmt->fetch(PDO::FETCH_ASSOC))
{
$table = current($table);
$allTables[$table] = $table;
$tableName = $table['Tables_in_zentao'];
$tableType = strtolower($table['Table_type']);
$allTables[$tableName] = $tableType == 'base table' ? 'table' : $tableType;
}
/* Dump all tables when tables is empty. */
@@ -75,15 +76,16 @@ class zdb
/* Open this file. */
$fp = fopen($fileName, 'w');
fwrite($fp, "SET NAMES utf8;\n");
foreach($tables as $table)
foreach($tables as $table => $tableType)
{
/* Check table exists. */
if(!isset($allTables[$table])) continue;
/* Create sql code. */
$backupSql = "DROP TABLE IF EXISTS `$table`;\n";
$backupSql .= $this->getSchemaSQL($table);
$backupSql = "DROP " . strtoupper($tableType) . " IF EXISTS `$table`;\n";
$backupSql .= $this->getSchemaSQL($table, $tableType);
fwrite($fp, $backupSql);
if($tableType != 'table') continue;
$rows = $this->dbh->query("select * from `$table`");
while($row = $rows->fetch(PDO::FETCH_ASSOC))
@@ -186,9 +188,10 @@ class zdb
* @access public
* @return string
*/
public function getSchemaSQL($table)
public function getSchemaSQL($table, $type = 'table')
{
$createSql = $this->dbh->query("show create table `$table`")->fetch(PDO::FETCH_ASSOC);
return $createSql['Create Table'] . ";\n";
$sql = "SHOW CREATE $type `$table`";
$createSql = $this->dbh->query($sql)->fetch(PDO::FETCH_ASSOC);
return $createSql['Create ' . ucfirst($type)] . ";\n";
}
}

View File

@@ -116,10 +116,10 @@ class admin extends control
*/
public function checkDB()
{
$tables = $this->dbh->query('SHOW TABLES')->fetchAll();
$tables = $this->dbh->query("show full tables where Table_Type != 'VIEW'")->fetchAll(PDO::FETCH_ASSOC);
foreach($tables as $table)
{
$tableName = current((array)$table);
$tableName = current($table);
$result = $this->dbh->query("REPAIR TABLE $tableName")->fetch();
echo "Repairing TABLE: " . $result->Table . "\t" . $result->Msg_type . ":" . $result->Msg_text . "\n";
}