113 lines
3.8 KiB
PHP
113 lines
3.8 KiB
PHP
<?php
|
|
|
|
/*******************************************************************
|
|
|
|
Module : /DB/CheckSQL.php
|
|
Created By : Siau Pheng ([email protected]).
|
|
Created Date : May 11th, 2011.
|
|
Last Modified : August 21st, 2026.
|
|
|
|
(c) 3FONIA Software; WWW.3FONIA.COM.
|
|
|
|
*******************************************************************/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Webapp\DB;
|
|
|
|
use \Exception;
|
|
use \flight\Engine;
|
|
use \flight\database\SimplePdo;
|
|
|
|
/**
|
|
* Class CheckSQL
|
|
*
|
|
* Check Database Mysql/MariaDB dan Repair.
|
|
*
|
|
* @license MIT, https://www.3fonia.com
|
|
* @copyright Copyright (c) 2008-2026, Siau Pheng <[email protected]>
|
|
*
|
|
* @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'], ' <i>('.$row['ENGINE'].')</i>',' <i>('.$row['TABLE_ROWS'].' Records)</i>' ];
|
|
}
|
|
}
|
|
|
|
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 = '<span style="color:blue;">OK</span>';
|
|
$row = $this->db->runQuery('CHECK TABLE '.$atabel[0].' QUICK')->fetch(SimplePdo::FETCH_ASSOC);
|
|
if (($row['Msg_text'] ?? '') !== 'OK') {
|
|
$pesan = '<span style="color:red;">ERROR</span>';
|
|
try {
|
|
$row2 = $this->db->runQuery('REPAIR TABLE '.$atabel[0])->fetch(SimplePdo::FETCH_ASSOC);
|
|
$pesan .= ' -> Auto Repair <span style="color:blue;">'. (($row2['Msg_text'] ?? '') === 'OK' ? 'FIXED' : 'ERROR') .'</span>';
|
|
} catch (Exception $e) {
|
|
$pesan .= ' -> Auto Repair <span style="color:red;">' . $e->getMessage() . '</span>';
|
|
}
|
|
}
|
|
return $atabel[0] . $atabel[3] ." ... ". $pesan;
|
|
}
|
|
|
|
private function _opti_db(array $atabel=[]): string {
|
|
$pesan = '<span style="color:orange;">SKIP</span>';
|
|
if ($atabel[1] === 'MyISAM') {
|
|
$pesan = '<span style="color:blue;">OK</span>';
|
|
$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 = '<span style="color:red;">ERROR</span>';
|
|
}
|
|
return $atabel[0] . $atabel[2] ." ... ". $pesan;
|
|
}
|
|
|
|
public function count_db(): int {
|
|
return count($this->__atabel);
|
|
}
|
|
|
|
public function check_db(): string {
|
|
$hasil = "=== CEK DATABASE ===<br>";
|
|
for ($n=0; $n<count($this->__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 ===<br>";
|
|
for ($n=0;$n<count($this->__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]);
|
|
}
|
|
}
|
|
|
|
?>
|