upgrade first session to flightphp
This commit is contained in:
+48
-54
@@ -6,56 +6,68 @@
|
||||
Desc. : Class Check SQL / Repair Database
|
||||
Created By : Siau Pheng ([email protected]).
|
||||
Created Date : May 11th, 2011.
|
||||
Last Modified : May 8th, 2021.
|
||||
Last Modified : August 13th, 2026.
|
||||
|
||||
(c) 2011 - 2021, 3FONIA Software; WWW.3FONIA.COM.
|
||||
(c) 2011 - 2026, 3FONIA Software; WWW.3FONIA.COM.
|
||||
|
||||
*******************************************************************/
|
||||
|
||||
//OK
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace siaupheng\webapp\DB;
|
||||
|
||||
use Exception;
|
||||
use flight\Engine;
|
||||
use flight\Session;
|
||||
use flight\database\SimplePdo;
|
||||
|
||||
final class CheckSQL {
|
||||
private $__atabel = [];
|
||||
private $__aignore = [];
|
||||
protected $__db = null;
|
||||
private Array $__atabel = [];
|
||||
protected Engine $app;
|
||||
protected SimplePdo $db;
|
||||
|
||||
public function __construct($db = null){
|
||||
if (is_resource($db)) { $this->__db = $db; }
|
||||
else { $this->__db = new \siaupheng\webapp\DB\MySQL(); }
|
||||
public function __construct(?Engine $app = null){
|
||||
$this->app = (empty($app) === true) ? \Flight::app() : $app;
|
||||
$this->db = $this->app->db();
|
||||
|
||||
$this->__db->Query("SELECT TABLE_NAME,ENGINE,TABLE_ROWS FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='".$this->__db->getDBName()."' AND TABLE_TYPE='BASE TABLE'");
|
||||
while ($this->__db->Next()) {
|
||||
$this->__atabel[] = array($this->__db->Row("TABLE_NAME"), ' <i>('.$this->__db->Row("ENGINE").')</i>',' <i>('.uang($this->__db->Row("TABLE_ROWS")).' Records)</i>');
|
||||
$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() {
|
||||
$tmp_arr = func_get_args();
|
||||
if (is_array($tmp_arr[0])) $tmp_arr = $tmp_arr[0];
|
||||
$this->__aignore = $tmp_arr;
|
||||
$farray = array_filter($this->__atabel, fn($child) => !in_array($child[0], $tmp_arr));
|
||||
$this->__atabel = array_values($farray);
|
||||
}
|
||||
|
||||
private function _check_db($atabel=array()) {
|
||||
$hasil = "";
|
||||
if (!in_array($atabel[0], $this->__aignore)) {
|
||||
$pesan = "<span style=\"color:blue;\">OK</span>";
|
||||
$this->__db->Query("CHECK TABLE ".$atabel[0]);
|
||||
if ($this->__db->Next()) {
|
||||
if (strtoupper($this->__db->Row("Msg_text"))<>"OK") {
|
||||
$pesan = "<span style=\"color:red;\">ERROR</span>";
|
||||
$this->__db->Query("REPAIR TABLE ".$atabel[0]);
|
||||
if ($this->__db->Next()) {
|
||||
if (strtoupper($this->__db->Row("Msg_text"))=="OK") {
|
||||
$pesan .= " -> <span style=\"color:blue;\">FIXED</span>";
|
||||
}
|
||||
}
|
||||
}
|
||||
private function _check_db(array $atabel=[]) {
|
||||
$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>';
|
||||
}
|
||||
$hasil = $atabel[0] . $atabel[2] ." ... ". $pesan ."<br>";
|
||||
}
|
||||
return $hasil;
|
||||
return $atabel[0] . $atabel[3] ." ... ". $pesan ."<br>";
|
||||
}
|
||||
|
||||
private function _opti_db(array $atabel=[]) {
|
||||
$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 ."<br>";
|
||||
}
|
||||
|
||||
public function count_db() {
|
||||
@@ -64,45 +76,27 @@ final class CheckSQL {
|
||||
|
||||
public function check_db() {
|
||||
$hasil = "=== CEK DATABASE ===<br>";
|
||||
for ($n=0;$n<count($this->__atabel);$n++) {
|
||||
for ($n=0; $n<count($this->__atabel); $n++) {
|
||||
$hasil .= $this->_check_db($this->__atabel[$n]);
|
||||
}
|
||||
return $hasil;
|
||||
}
|
||||
|
||||
public function check_db_step($n) {
|
||||
$hasil .= $this->_check_db($this->__atabel[$n]);
|
||||
}
|
||||
|
||||
public function repair_db() {
|
||||
$hasil = "=== REPAIR DATABASE ===<br>";
|
||||
for ($n=0;$n<count($this->__atabel);$n++) {
|
||||
if (!in_array($this->__atabel[$n][0], $this->__aignore)) {
|
||||
$pesan = "<span style=\"color:blue;\">OK</span>";
|
||||
$this->__db->Query("REPAIR TABLE ".$this->__atabel[$n][0]);
|
||||
while ($this->__db->Next()) {
|
||||
$pesan = (strtoupper($this->__db->Row("Msg_text"))=="OK") ? "<span style=\"color:blue;\">OK</span>" : "<span style=\"color:red;\">ERROR</span>";
|
||||
}
|
||||
$hasil .= $this->__atabel[$n][0] . $this->__atabel[$n][1] ." ... ". $pesan ."<br>";
|
||||
}
|
||||
}
|
||||
return $hasil;
|
||||
return $this->_check_db($this->__atabel[$n]);
|
||||
}
|
||||
|
||||
public function optimize_db() {
|
||||
$hasil = "=== OPTIMALISASI DATABASE ===<br>";
|
||||
for ($n=0;$n<count($this->__atabel);$n++) {
|
||||
if (!in_array($this->__atabel[$n][0], $this->__aignore)) {
|
||||
$pesan = "<span style=\"color:blue;\">OK</span>";
|
||||
$this->__db->Query("OPTIMIZE TABLE ".$this->__atabel[$n][0]);
|
||||
while ($this->__db->Next()) {
|
||||
$pesan = (strtoupper($this->__db->Row("Msg_text"))=="OK" || strtoupper($this->__db->Row("Msg_text"))=="TABLE IS ALREADY UP TO DATE") ? "<span style=\"color:blue;\">OK</span>" : "<span style=\"color:red;\">ERROR</span>";
|
||||
}
|
||||
$hasil .= $this->__atabel[$n][0] . $this->__atabel[$n][1] ." ... ". $pesan ."<br>";
|
||||
}
|
||||
$hasil .= $this->_opti_db($this->__atabel[$n]);
|
||||
}
|
||||
return $hasil;
|
||||
}
|
||||
|
||||
public function optimize_db_step($n) {
|
||||
return $this->_opti_db($this->__atabel[$n]);
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user