app = (empty($app) === true) ? \Flight::app() : $app;
$this->db = $this->app->db();
$nama_db = $this->db->fetchField('SELECT DATABASE()');
$arow = $this->db->fetchAll("SELECT TABLE_NAME,ENGINE,TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='".$nama_db."' AND TABLE_TYPE='BASE TABLE'");
foreach ($arow as $row) {
$this->__atabel[] = [ $row['TABLE_NAME'], $row['ENGINE'], ' ('.$row['ENGINE'].')',' ('.$row['TABLE_ROWS'].' Records)' ];
}
}
public function ignore_table() {
$tmp_arr = func_get_args();
if (is_array($tmp_arr[0])) $tmp_arr = $tmp_arr[0];
$farray = array_filter($this->__atabel, fn($child) => !in_array($child[0], $tmp_arr));
$this->__atabel = array_values($farray);
}
private function _check_db(array $atabel=[]) {
$pesan = 'OK';
$row = $this->db->runQuery('CHECK TABLE '.$atabel[0].' QUICK')->fetch(SimplePdo::FETCH_ASSOC);
if (($row['Msg_text'] ?? '') !== 'OK') {
$pesan = 'ERROR';
try {
$row2 = $this->db->runQuery('REPAIR TABLE '.$atabel[0])->fetch(SimplePdo::FETCH_ASSOC);
$pesan .= ' -> Auto Repair '. (($row2['Msg_text'] ?? '') === 'OK' ? 'FIXED' : 'ERROR') .'';
} catch (Exception $e) {
$pesan .= ' -> Auto Repair ' . $e->getMessage() . '';
}
}
return $atabel[0] . $atabel[3] ." ... ". $pesan ."
";
}
private function _opti_db(array $atabel=[]) {
$pesan = 'SKIP';
if ($atabel[1] === 'MyISAM') {
$pesan = 'OK';
$row = $this->db->runQuery('OPTIMIZE TABLE '.$atabel[0])->fetch(SimplePdo::FETCH_ASSOC);
if (!in_array(strtoupper($row['Msg_text'] ?? ''), ['OK','TABLE IS ALREADY UP TO DATE'])) $pesan = 'ERROR';
}
return $atabel[0] . $atabel[2] ." ... ". $pesan ."
";
}
public function count_db() {
return count($this->__atabel);
}
public function check_db() {
$hasil = "=== CEK DATABASE ===
";
for ($n=0; $n__atabel); $n++) {
$hasil .= $this->_check_db($this->__atabel[$n]);
}
return $hasil;
}
public function check_db_step($n) {
return $this->_check_db($this->__atabel[$n]);
}
public function optimize_db() {
$hasil = "=== OPTIMALISASI DATABASE ===
";
for ($n=0;$n__atabel);$n++) {
$hasil .= $this->_opti_db($this->__atabel[$n]);
}
return $hasil;
}
public function optimize_db_step($n) {
return $this->_opti_db($this->__atabel[$n]);
}
}
?>