Remove unnecessary try/catch from DB connection

This commit is contained in:
Alex Cabal 2024-04-27 12:37:09 -05:00
parent 07110829e7
commit fdca261133

View file

@ -19,51 +19,36 @@ class DbConnection{
$connectionString = 'mysql:'; $connectionString = 'mysql:';
try{ if(stripos($host, ':') !== false){
if(stripos($host, ':') !== false){ $port = null;
$port = null; preg_match('/([^:]*):([0-9]+)/ius', $host, $matches);
preg_match('/([^:]*):([0-9]+)/ius', $host, $matches); $host = $matches[1];
$host = $matches[1]; if(sizeof($matches) > 2){
if(sizeof($matches) > 2){ $port = $matches[2];
$port = $matches[2];
}
$connectionString .= 'host=' . $host;
if($port !== null){
$connectionString .= ';port=' . $port;
}
}
else{
$connectionString .= 'host=' . $host;
} }
if($defaultDatabase !== null){ $connectionString .= 'host=' . $host;
$connectionString .= ';dbname=' . $defaultDatabase;
}
$params = [\PDO::ATTR_EMULATE_PREPARES => false, \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_PERSISTENT => false]; if($port !== null){
$connectionString .= ';port=' . $port;
if($forceUtf8){
$params[\PDO::MYSQL_ATTR_INIT_COMMAND] = 'set names utf8mb4 collate utf8mb4_unicode_ci;';
}
// We can't use persistent connections (connection pooling) because we would have race condition problems with last_insert_id()
$this->_link = new \PDO($connectionString, $user, $password, $params);
}
catch(Exception $ex){
if(SITE_STATUS == SITE_STATUS_DEV){
var_dump($ex);
}
else{
Log::WriteErrorLogEntry('Error connecting to ' . $connectionString . '. Exception: ' . vds($ex));
}
if($require){
print("Something crazy happened in our database.");
exit();
} }
} }
else{
$connectionString .= 'host=' . $host;
}
if($defaultDatabase !== null){
$connectionString .= ';dbname=' . $defaultDatabase;
}
$params = [\PDO::ATTR_EMULATE_PREPARES => false, \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_PERSISTENT => false];
if($forceUtf8){
$params[\PDO::MYSQL_ATTR_INIT_COMMAND] = 'set names utf8mb4 collate utf8mb4_unicode_ci;';
}
// We can't use persistent connections (connection pooling) because we would have race condition problems with last_insert_id()
$this->_link = new \PDO($connectionString, $user, $password, $params);
} }
// Inputs: string $sql = the SQL query to execute // Inputs: string $sql = the SQL query to execute