* * @package Webapp\DB */ final class CheckSQL { private Array $__atabel = []; protected Engine $app; protected SimplePdo $db; /** * @param Engine $app */ public function __construct(Engine $app) { $this->app = $app; $this->db = $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(): void { $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=[]): string { $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=[]): string { $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(): int { return count($this->__atabel); } public function check_db(): string { $hasil = "=== CEK DATABASE ===
"; for ($n=0; $n__atabel); $n++) { $hasil .= $this->_check_db($this->__atabel[$n]); } return $hasil; } public function check_db_step(int $n): string { return $this->_check_db($this->__atabel[$n]); } public function optimize_db(): string { $hasil = "=== OPTIMALISASI DATABASE ===
"; for ($n=0;$n__atabel);$n++) { $hasil .= $this->_opti_db($this->__atabel[$n]); } return $hasil; } public function optimize_db_step(int $n): string { return $this->_opti_db($this->__atabel[$n]); } } ?>