fixed database handler for DBmodel in Maintenance,
added test4 for Maintenance
This commit is contained in:
@ -26,17 +26,26 @@ class Maintenance
|
||||
|
||||
private DBmodel $dbh;
|
||||
|
||||
public function __construct(DBmodel $dbh)
|
||||
public function __construct(?DBmodel $dbh)
|
||||
{
|
||||
$this->dbh = $dbh;
|
||||
if (is_null($dbh)) {
|
||||
if (!is_null(DBmodel::$instance)) {
|
||||
$this->dbh = DBmodel::$instance;
|
||||
} else {
|
||||
throw new \Exception('DB handler is null');
|
||||
}
|
||||
} else {
|
||||
$this->dbh = $dbh;
|
||||
}
|
||||
}
|
||||
|
||||
public function testDB($test, $test_out)
|
||||
{
|
||||
global $core;
|
||||
if (is_array($test_out)) {
|
||||
$row = $core->dbh->getRow($test);
|
||||
$test_out = array_change_key_case($test_out, CASE_LOWER);
|
||||
$row = $this->dbh->getRow($test);
|
||||
if (!is_array($row)) return false;
|
||||
$row = array_change_key_case($row, CASE_LOWER);
|
||||
foreach ($test_out as $col => $val) {
|
||||
if (strlen($row[$col]) != strlen($val)) {
|
||||
$max_len = min(strlen($row[$col]), strlen($val));
|
||||
@ -51,17 +60,16 @@ class Maintenance
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return $core->dbh->getOne($test) == $test_out;
|
||||
return $this->dbh->getOne($test) == $test_out;
|
||||
}
|
||||
}
|
||||
|
||||
public function checkDB($test, $test_out, $fix)
|
||||
{
|
||||
global $core;
|
||||
if ($this->testDB($test, $test_out)) {
|
||||
return Maintenance::OK;
|
||||
} else {
|
||||
$core->dbh->query($fix);
|
||||
$this->dbh->query($fix);
|
||||
if ($this->testDB($test, $test_out)) {
|
||||
return Maintenance::FIXED;
|
||||
} else {
|
||||
@ -71,12 +79,10 @@ class Maintenance
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function checkDBTable($table_index, $definition)
|
||||
public function checkDBTable($table_name, $definition)
|
||||
{
|
||||
global $core;
|
||||
$table_name = $core->tables[$table_index];
|
||||
if (strlen($table_name) <= 0) {
|
||||
$this->lastMessage = 'Table ' . strtoupper($table_index) . ' - invalid index';
|
||||
$this->lastMessage = 'Table ' . strtoupper($table_name) . ' - invalid index';
|
||||
return Maintenance::ABNORMAL;
|
||||
}
|
||||
$status = $this->checkDB(
|
||||
@ -87,118 +93,127 @@ class Maintenance
|
||||
// FIX query
|
||||
'CREATE TABLE `' . $table_name . '` (' . $definition . ')'
|
||||
);
|
||||
$this->lastMessage = 'Table ' . strtoupper($table_index) . ' (' . $table_name . ')';
|
||||
$this->lastMessage = 'Table ' . strtoupper($table_name) . ' (' . $table_name . ')';
|
||||
return $status;
|
||||
}
|
||||
|
||||
public function checkDBAdd($table_index, $column, $definition)
|
||||
public function existsColumn($table_name, $column)
|
||||
{
|
||||
return $this->testDB(
|
||||
'DESCRIBE `' . $table_name . '` `' . $column . '`',
|
||||
$column
|
||||
);
|
||||
}
|
||||
|
||||
public function checkDBAdd($table_name, $column, $definition)
|
||||
{
|
||||
global $core;
|
||||
$status = $this->checkDB(
|
||||
// TEST query
|
||||
'DESCRIBE `' . $core->tables[$table_index] . '` `' . $column . '`',
|
||||
'DESCRIBE `' . $table_name . '` `' . $column . '`',
|
||||
// TEST output
|
||||
$column,
|
||||
// FIX query
|
||||
'ALTER TABLE `' . $core->tables[$table_index] . '` ADD `' . $column . '` ' . $definition
|
||||
'ALTER TABLE `' . $table_name . '` ADD `' . $column . '` ' . $definition
|
||||
);
|
||||
$this->lastMessage = "Added column $column to table $table_index";
|
||||
$this->lastMessage = "Added column $column to table $table_name";
|
||||
return $status;
|
||||
}
|
||||
|
||||
public function checkDBRename($table_index, $column_old, $column_new, $definition, $test = null)
|
||||
public function checkDBRename($table_name, $column_old, $column_new, $definition, $test = null)
|
||||
{
|
||||
global $core;
|
||||
if (!$this->existsColumn($table_name, $column_old)) {
|
||||
return Maintenance::ABNORMAL;
|
||||
}
|
||||
$status = $this->checkDB(
|
||||
// TEST query
|
||||
'DESCRIBE `' . $core->tables[$table_index] . '` `' . $column_old . '`',
|
||||
'DESCRIBE `' . $table_name . '` `' . $column_old . '`',
|
||||
// TEST output
|
||||
$test,
|
||||
// FIX query
|
||||
'ALTER TABLE `' . $core->tables[$table_index] . '` CHANGE `' . $column_old . '` `' . $column_new . '` ' . $definition
|
||||
'ALTER TABLE `' . $table_name . '` CHANGE `' . $column_old . '` `' . $column_new . '` ' . $definition
|
||||
);
|
||||
$this->lastMessage = "Renamed column $column_old to $column_new for $table_index";
|
||||
$this->lastMessage = "Renamed column $column_old to $column_new for $table_name";
|
||||
return $status;
|
||||
}
|
||||
|
||||
public function checkDBRetype($table_index, $column, $new_type, $definition)
|
||||
public function checkDBRetype($table_name, $column, $new_type, $definition)
|
||||
{
|
||||
global $core;
|
||||
if (!$this->existsColumn($table_name, $column_old)) {
|
||||
return Maintenance::ABNORMAL;
|
||||
}
|
||||
$status = $this->checkDB(
|
||||
// TEST query
|
||||
'DESCRIBE `' . $core->tables[$table_index] . '` `' . $column . '`',
|
||||
'DESCRIBE `' . $table_name . '` `' . $column . '`',
|
||||
// TEST output
|
||||
array('type' => strtolower($new_type)),
|
||||
// FIX query
|
||||
'ALTER TABLE `' . $core->tables[$table_index] . '` CHANGE `' . $column . '` `' . $column . '` ' . $definition
|
||||
'ALTER TABLE `' . $table_name . '` CHANGE `' . $column . '` `' . $column . '` ' . $definition
|
||||
);
|
||||
$this->lastMessage = "Changed type of column $column for tabke $table_index to tyoe " . strtoupper($new_type);
|
||||
$this->lastMessage = "Changed type of column $column for table " . strtoupper($table_name) . " to tyoe " . strtoupper($new_type);
|
||||
return $status;
|
||||
}
|
||||
|
||||
public function checkDBRemove($table_index, $column)
|
||||
public function checkDBRemove($table_name, $column)
|
||||
{
|
||||
global $core;
|
||||
$status = $this->checkDB(
|
||||
// TEST query
|
||||
'DESCRIBE `' . $core->tables[$table_index] . '` `' . $column . '`',
|
||||
'DESCRIBE `' . $table_name . '` `' . $column . '`',
|
||||
// TEST output
|
||||
NULL,
|
||||
// FIX query
|
||||
'ALTER TABLE `' . $core->tables[$table_index] . '` DROP COLUMN `' . $column . '`'
|
||||
'ALTER TABLE `' . $table_name . '` DROP COLUMN `' . $column . '`'
|
||||
);
|
||||
$this->lastMessage = "Removed column $column for $table_index";
|
||||
$this->lastMessage = "Removed column $column for " . strtoupper($table_name);
|
||||
return $status;
|
||||
}
|
||||
|
||||
public function dropKey($table_index, $key)
|
||||
public function addKey($table_name, $key)
|
||||
{
|
||||
global $core;
|
||||
$test = 'SHOW INDEX FROM `' . $core->tables[$table_index] . '` WHERE column_name = "' . $key . '"';
|
||||
$test = 'SHOW INDEX FROM `' . $table_name . '` WHERE column_name = "' . $key . '"';
|
||||
$test_out = array('table' => $table_name);
|
||||
if ($this->testDB($test, $test_out)) {
|
||||
$status = Maintenance::OK;
|
||||
} else {
|
||||
$fix = 'CREATE INDEX `' . $key . '` ON `' . $table_name . '` (`' . $key . '`)';
|
||||
$this->dbh->query($fix);
|
||||
if ($this->testDB($test, $test_out)) {
|
||||
$status = Maintenance::FIXED;
|
||||
} else {
|
||||
$status = Maintenance::FIX_FAILED;
|
||||
}
|
||||
}
|
||||
$this->lastMessage = 'Added KEY ' . $key . ' for ' . strtoupper($table_name);
|
||||
return $status;
|
||||
}
|
||||
|
||||
public function dropKey($table_name, $key)
|
||||
{
|
||||
$test = 'SHOW INDEX FROM `' . $table_name . '` WHERE column_name = "' . $key . '"';
|
||||
$test_out = null;
|
||||
if ($this->testDB($test, $test_out)) {
|
||||
$status = Maintenance::OK;
|
||||
} else {
|
||||
$index_name = $core->dbh->getRow('SHOW INDEX FROM `' . $core->tables[$table_index] . '` WHERE column_name = "' . $key . '"')['key_name'];
|
||||
$fix = 'ALTER TABLE `' . $core->tables[$table_index] . '` DROP INDEX ' . $index_name;
|
||||
$core->dbh->query($fix);
|
||||
$index_info = $this->dbh->getRow('SHOW INDEX FROM `' . $table_name . '` WHERE column_name = "' . $key . '"');
|
||||
$index_info = array_change_key_case($index_info, CASE_LOWER);
|
||||
$index_name = $index_info['key_name'];
|
||||
$fix = 'ALTER TABLE `' . $table_name . '` DROP INDEX `' . $index_name . '`';
|
||||
$this->dbh->query($fix);
|
||||
if ($this->testDB($test, $test_out)) {
|
||||
$status = Maintenance::FIXED;
|
||||
} else {
|
||||
$status = Maintenance::FIX_FAILED;
|
||||
}
|
||||
}
|
||||
$this->lastMessage = 'Deleted KEY ' . $key . ' for ' . $table_index;
|
||||
return $status;
|
||||
}
|
||||
|
||||
public function addKey($table_index, $key)
|
||||
{
|
||||
global $core;
|
||||
$test = 'SHOW INDEX FROM `' . $core->tables[$table_index] . '` WHERE column_name = "' . $key . '"';
|
||||
$test_out = array('table' => $core->tables[$table_index]);
|
||||
if ($this->testDB($test, $test_out)) {
|
||||
$status = Maintenance::OK;
|
||||
} else {
|
||||
$fix = 'CREATE INDEX `' . $key . '` ON `' . $core->tables[$table_index] . '` (`' . $key . '`)';
|
||||
$core->dbh->query($fix);
|
||||
if ($this->testDB($test, $test_out)) {
|
||||
$status = Maintenance::FIXED;
|
||||
} else {
|
||||
$status = Maintenance::FIX_FAILED;
|
||||
}
|
||||
}
|
||||
$this->lastMessage = 'Added KEY ' . $key . ' for ' . $table_index;
|
||||
$this->lastMessage = 'Deleted KEY ' . $key . ' for ' . strtoupper($table_name);
|
||||
return $status;
|
||||
}
|
||||
|
||||
public function updateDB($test, $test_out, $update)
|
||||
{
|
||||
global $core;
|
||||
if (!$this->testDB($test, $test_out)) {
|
||||
return Maintenance::OK;
|
||||
} else {
|
||||
$suc = $core->dbh->query($update);
|
||||
$suc = $this->dbh->query($update);
|
||||
if ($suc !== false) {
|
||||
return Maintenance::UPDATED;
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user