Skip to content

Commit 138fe2d

Browse files
authored
Merge pull request #164 from Planetbiru/feature/version-3.20.0
Handle exception
2 parents 9ebf283 + 59fe6fc commit 138fe2d

1 file changed

Lines changed: 45 additions & 39 deletions

File tree

src/Database/PicoDatabase.php

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -156,51 +156,57 @@ public static function fromPdo($pdo)
156156
private static function getDatabaseCredentialsFromPdo($pdo, $driver, $dbType)
157157
{
158158
// Get the connection status, which includes the DSN (Data Source Name)
159-
$dsn = $pdo->getAttribute(PDO::ATTR_CONNECTION_STATUS);
160-
$dsnParts = parse_url($dsn);
159+
try
160+
{
161+
$dsn = $pdo->getAttribute(PDO::ATTR_CONNECTION_STATUS);
162+
$dsnParts = parse_url($dsn);
161163

162-
// Extract the host from the DSN (if available)
163-
$host = isset($dsnParts['host']) ? $dsnParts['host'] : null;
164+
// Extract the host from the DSN (if available)
165+
$host = isset($dsnParts['host']) ? $dsnParts['host'] : null;
164166

165-
// Extract the port from the DSN (if available)
166-
$port = isset($dsnParts['port']) ? $dsnParts['port'] : null;
167+
// Extract the port from the DSN (if available)
168+
$port = isset($dsnParts['port']) ? $dsnParts['port'] : null;
167169

168-
// Get the database name from the DSN (usually found at the end of the DSN after host and port)
169-
$databaseName = isset($dsnParts['path']) ? ltrim($dsnParts['path'], '/') : null;
170+
// Get the database name from the DSN (usually found at the end of the DSN after host and port)
171+
$databaseName = isset($dsnParts['path']) ? ltrim($dsnParts['path'], '/') : null;
170172

171-
// Initialize the schema and time zone
172-
$schema = null;
173-
$timezone = null;
174-
175-
// Retrieve the schema and time zone based on the database type
176-
if ($dbType == PicoDatabaseType::DATABASE_TYPE_PGSQL) {
177-
// For PostgreSQL, fetch the current schema and time zone using queries
178-
$stmt = $pdo->query('SELECT current_schema()');
179-
$schema = $stmt->fetchColumn(); // Fetch the schema name
180-
$timezone = self::convertOffsetToTimeZone(self::getTimeZoneOffset($pdo));
181-
}
182-
else if ($dbType == PicoDatabaseType::DATABASE_TYPE_MARIADB || $dbType == PicoDatabaseType::DATABASE_TYPE_MYSQL) {
183-
// For MySQL, the schema is the same as the database name
184-
$schema = $databaseName; // MySQL schema is the database name
185-
$timezone = self::convertOffsetToTimeZone(self::getTimeZoneOffset($pdo));
186-
}
187-
else {
188-
// For other drivers, set schema and time zone to null (or handle it as needed)
173+
// Initialize the schema and time zone
189174
$schema = null;
190-
$timezone = date_default_timezone_get();
191-
}
175+
$timezone = null;
176+
177+
// Retrieve the schema and time zone based on the database type
178+
if ($dbType == PicoDatabaseType::DATABASE_TYPE_PGSQL) {
179+
// For PostgreSQL, fetch the current schema and time zone using queries
180+
$stmt = $pdo->query('SELECT current_schema()');
181+
$schema = $stmt->fetchColumn(); // Fetch the schema name
182+
$timezone = self::convertOffsetToTimeZone(self::getTimeZoneOffset($pdo));
183+
}
184+
else if ($dbType == PicoDatabaseType::DATABASE_TYPE_MARIADB || $dbType == PicoDatabaseType::DATABASE_TYPE_MYSQL) {
185+
// For MySQL, the schema is the same as the database name
186+
$schema = $databaseName; // MySQL schema is the database name
187+
$timezone = self::convertOffsetToTimeZone(self::getTimeZoneOffset($pdo));
188+
}
189+
else {
190+
// For other drivers, set schema and time zone to null (or handle it as needed)
191+
$schema = null;
192+
$timezone = date_default_timezone_get();
193+
}
192194

193-
// Create and populate the SecretObject with the connection details
194-
$databaseCredentials = new SecretObject();
195-
$databaseCredentials->setDriver($driver);
196-
$databaseCredentials->setHost($host);
197-
$databaseCredentials->setPort($port);
198-
$databaseCredentials->setDatabaseName($databaseName);
199-
$databaseCredentials->setDatabaseSchema($schema);
200-
$databaseCredentials->setTimeZone($timezone);
201-
202-
// Return the populated SecretObject containing the connection details
203-
return $databaseCredentials;
195+
// Create and populate the SecretObject with the connection details
196+
$databaseCredentials = new SecretObject();
197+
$databaseCredentials->setDriver($driver);
198+
$databaseCredentials->setHost($host);
199+
$databaseCredentials->setPort($port);
200+
$databaseCredentials->setDatabaseName($databaseName);
201+
$databaseCredentials->setDatabaseSchema($schema);
202+
$databaseCredentials->setTimeZone($timezone);
203+
204+
// Return the populated SecretObject containing the connection details
205+
return $databaseCredentials;
206+
}
207+
catch (Exception $e) {
208+
return new SecretObject();
209+
}
204210
}
205211

206212
/**

0 commit comments

Comments
 (0)