Skip to content

Commit

Permalink
🔧 修復(Schema.php):新增 isConnected() 方法以檢查資料庫連線狀態
Browse files Browse the repository at this point in the history
🔧 修復(Schema.php):新增 connect() 方法以重新建立資料庫連線
  • Loading branch information
mathsgod committed Apr 24, 2024
1 parent 1952b9d commit 7b00582
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,15 +123,31 @@ static function Create(): Schema
$password = $_ENV["DATABASE_PASSWORD"];
$charset = $_ENV["DATABASE_CHARSET"] ?? "utf8mb4";

if(!$host) throw new \Exception("DATABASE_HOSTNAME not found in .env");
if(!$name) throw new \Exception("DATABASE_DATABASE not found in .env");
if(!$username) throw new \Exception("DATABASE_USERNAME not found in .env");
if (!$host) throw new \Exception("DATABASE_HOSTNAME not found in .env");
if (!$name) throw new \Exception("DATABASE_DATABASE not found in .env");
if (!$username) throw new \Exception("DATABASE_USERNAME not found in .env");


self::$Instance = new Schema($name, $host, $username, $password, $charset, $port);
return self::$Instance;
}

function isConnected(): bool
{
try {
$this->adapter->getDriver()->getConnection()->execute("SELECT 1");
} catch (\Exception $e) {
return false;
}
return true;
}

public function connect()
{
self::$Instance = null;
self::Create();
}

function beginTransaction(): bool
{
$this->in_transaction = true;
Expand Down

0 comments on commit 7b00582

Please sign in to comment.