diff --git a/CHANGELOG.md b/CHANGELOG.md index 3377e3a..2ac6e46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1413,3 +1413,22 @@ Developers who rely on these math utilities should install the new package separ ```bash composer require planetbiru/math +``` + +## New Feature: PDO Connection Verification Method + +A new method isPdoConnected() has been introduced to allow developers to verify not only the TCP-level connection, but also the ability of PHP to execute SQL statements on the database. + +Here’s the corrected version with improved grammar and clarity: + + +## Bug Fix: Handle Exception in method getDatabaseCredentialsFromPdo($pdo, $driver, $dbType) + +If an error occurs when executing: + +```php +$dsn = $pdo->getAttribute(PDO::ATTR_CONNECTION_STATUS); +``` + +`getDatabaseCredentialsFromPdo($pdo, $driver, $dbType)` will return an empty instance of `SecretObject`. + diff --git a/src/Database/PicoDatabase.php b/src/Database/PicoDatabase.php index 2c45ff7..7e58cfb 100644 --- a/src/Database/PicoDatabase.php +++ b/src/Database/PicoDatabase.php @@ -1378,4 +1378,28 @@ public function setCallbackDebugQuery($callbackDebugQuery) return $this; } + + /** + * Checks whether the PDO connection is fully active and able to execute queries. + * + * This method not only verifies that a TCP connection to the database exists, + * but also ensures that PHP can successfully execute a simple SQL statement + * on the target database. By running `SELECT 1 + 1 AS two`, it validates + * both the connection and query execution capability. + * + * @return bool True if the connection and query execution are successful, false otherwise. + */ + public function isPdoConnected() + { + try { + $stmt = $this->databaseConnection->query("SELECT 1 + 1 AS two"); + $row = $stmt->fetch(PDO::FETCH_ASSOC); + return isset($row) && isset($row['two']) && $row['two'] == 2; + } catch (PDOException $e) { + // If an error occurs, assume the connection/query is not valid + } + return false; + } + + } \ No newline at end of file