diff --git a/lib/Core.php b/lib/Core.php index e0085e21..aa34cf6e 100644 --- a/lib/Core.php +++ b/lib/Core.php @@ -27,6 +27,8 @@ if(SITE_STATUS == SITE_STATUS_LIVE){ }); } +$GLOBALS['DbConnection'] = new DbConnection(DATABASE_DEFAULT_DATABASE, DATABASE_DEFAULT_HOST); + $GLOBALS['User'] = Session::GetLoggedInUser(); if($GLOBALS['User'] === null){ diff --git a/lib/Db.php b/lib/Db.php index ecde9a93..db1ca7d2 100644 --- a/lib/Db.php +++ b/lib/Db.php @@ -15,32 +15,16 @@ class Db{ * @return Array */ public static function Query(string $query, array $args = [], string $class = 'stdClass'): array{ - if(!isset($GLOBALS['DbConnection'])){ - $GLOBALS['DbConnection'] = new DbConnection(DATABASE_DEFAULT_DATABASE, DATABASE_DEFAULT_HOST); - } - - if(!is_array($args)){ - $args = [$args]; - } - return $GLOBALS['DbConnection']->Query($query, $args, $class); } /** + * Returns a single integer value for the first column database query result. + * This is useful for queries that return a single integer as a result, like count(*) or sum(*). * @param string $query * @param array $args */ public static function QueryInt(string $query, array $args = []): int{ - // Useful for queries that return a single integer as a result, like count(*) or sum(*). - - if(!isset($GLOBALS['DbConnection'])){ - $GLOBALS['DbConnection'] = new DbConnection(DATABASE_DEFAULT_DATABASE, DATABASE_DEFAULT_HOST); - } - - if(!is_array($args)){ - $args = [$args]; - } - $result = $GLOBALS['DbConnection']->Query($query, $args); if(sizeof($result) > 0){ diff --git a/lib/DbConnection.php b/lib/DbConnection.php index 1e89db76..f115334f 100644 --- a/lib/DbConnection.php +++ b/lib/DbConnection.php @@ -8,7 +8,7 @@ class DbConnection{ public int $QueryCount = 0; public int $LastQueryAffectedRowCount = 0; - public function __construct(?string $defaultDatabase = null, string $host = 'localhost', ?string $user = null, string $password = '', bool $forceUtf8 = true, bool $require = true){ + public function __construct(?string $defaultDatabase = null, string $host = 'localhost', ?string $user = null, string $password = '', bool $forceUtf8 = true){ if($user === null){ // Get the user running the script for local socket login $user = posix_getpwuid(posix_geteuid()); @@ -51,13 +51,10 @@ class DbConnection{ $this->_link = new \PDO($connectionString, $user, $password, $params); } - // Inputs: string $sql = the SQL query to execute - // array $params = an array of parameters to bind to the SQL statement - // Returns: a resource record or null on error /** - * @param string $sql - * @param array $params - * @param string $class + * @param string $sql The SQL query to execute. + * @param array $params An array of parameters to bind to the SQL statement. + * @param string $class The type of object to return in the return array. * @return Array */ public function Query(string $sql, array $params = [], string $class = 'stdClass'): array{ @@ -69,9 +66,11 @@ class DbConnection{ $result = []; $preparedSql = $sql; - $handle = $this->_link->prepare($preparedSql); - if(!is_array($params)){ - $params = [$params]; + try{ + $handle = $this->_link->prepare($preparedSql); + } + catch(\PDOException $ex){ + throw $this->CreateDetailedException($ex, $preparedSql, $params); } $name = 0; @@ -122,15 +121,7 @@ class DbConnection{ } else{ $done = true; - if(SITE_STATUS == SITE_STATUS_DEV){ - throw($ex); - } - else{ - Log::WriteErrorLogEntry($ex->getMessage()); - Log::WriteErrorLogEntry($preparedSql); - Log::WriteErrorLogEntry(vds($params)); - throw($ex); - } + throw $this->CreateDetailedException($ex, $preparedSql, $params); } } } @@ -138,6 +129,16 @@ class DbConnection{ return $result; } + /** + * @param \PdoException $ex The exception to create details from. + * @param string $preparedSql The prepared SQL that caused the exception. + * @param array $params The parameters passed to the prepared SQL. + */ + private function CreateDetailedException(\PDOException $ex, string $preparedSql, array $params): Exceptions\DatabaseQueryException{ + // Throw a custom exception that includes more information on the query and paramaters + return new Exceptions\DatabaseQueryException('Error when executing query: ' . $ex->getMessage() . '. Query: ' . $preparedSql . '. Parameters: ' . vds($params)); + } + /** * @return Array */ @@ -260,7 +261,6 @@ class DbConnection{ return $result; } - // Gets the last auto-increment id public function GetLastInsertedId(): ?int{ if($this->_link === null){ return null; diff --git a/lib/Exceptions/DatabaseQueryException.php b/lib/Exceptions/DatabaseQueryException.php new file mode 100644 index 00000000..f28676e2 --- /dev/null +++ b/lib/Exceptions/DatabaseQueryException.php @@ -0,0 +1,5 @@ +