mirror of
https://github.com/standardebooks/web.git
synced 2025-07-06 14:50:39 -04:00
Improve exceptions in DB classes
This commit is contained in:
parent
fdca261133
commit
e14988ff8c
4 changed files with 29 additions and 38 deletions
|
@ -27,6 +27,8 @@ if(SITE_STATUS == SITE_STATUS_LIVE){
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$GLOBALS['DbConnection'] = new DbConnection(DATABASE_DEFAULT_DATABASE, DATABASE_DEFAULT_HOST);
|
||||||
|
|
||||||
$GLOBALS['User'] = Session::GetLoggedInUser();
|
$GLOBALS['User'] = Session::GetLoggedInUser();
|
||||||
|
|
||||||
if($GLOBALS['User'] === null){
|
if($GLOBALS['User'] === null){
|
||||||
|
|
20
lib/Db.php
20
lib/Db.php
|
@ -15,32 +15,16 @@ class Db{
|
||||||
* @return Array<mixed>
|
* @return Array<mixed>
|
||||||
*/
|
*/
|
||||||
public static function Query(string $query, array $args = [], string $class = 'stdClass'): 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);
|
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 string $query
|
||||||
* @param array<mixed> $args
|
* @param array<mixed> $args
|
||||||
*/
|
*/
|
||||||
public static function QueryInt(string $query, array $args = []): int{
|
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);
|
$result = $GLOBALS['DbConnection']->Query($query, $args);
|
||||||
|
|
||||||
if(sizeof($result) > 0){
|
if(sizeof($result) > 0){
|
||||||
|
|
|
@ -8,7 +8,7 @@ class DbConnection{
|
||||||
public int $QueryCount = 0;
|
public int $QueryCount = 0;
|
||||||
public int $LastQueryAffectedRowCount = 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){
|
if($user === null){
|
||||||
// Get the user running the script for local socket login
|
// Get the user running the script for local socket login
|
||||||
$user = posix_getpwuid(posix_geteuid());
|
$user = posix_getpwuid(posix_geteuid());
|
||||||
|
@ -51,13 +51,10 @@ class DbConnection{
|
||||||
$this->_link = new \PDO($connectionString, $user, $password, $params);
|
$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 string $sql The SQL query to execute.
|
||||||
* @param array<mixed> $params
|
* @param array<mixed> $params An array of parameters to bind to the SQL statement.
|
||||||
* @param string $class
|
* @param string $class The type of object to return in the return array.
|
||||||
* @return Array<mixed>
|
* @return Array<mixed>
|
||||||
*/
|
*/
|
||||||
public function Query(string $sql, array $params = [], string $class = 'stdClass'): array{
|
public function Query(string $sql, array $params = [], string $class = 'stdClass'): array{
|
||||||
|
@ -69,9 +66,11 @@ class DbConnection{
|
||||||
$result = [];
|
$result = [];
|
||||||
$preparedSql = $sql;
|
$preparedSql = $sql;
|
||||||
|
|
||||||
$handle = $this->_link->prepare($preparedSql);
|
try{
|
||||||
if(!is_array($params)){
|
$handle = $this->_link->prepare($preparedSql);
|
||||||
$params = [$params];
|
}
|
||||||
|
catch(\PDOException $ex){
|
||||||
|
throw $this->CreateDetailedException($ex, $preparedSql, $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
$name = 0;
|
$name = 0;
|
||||||
|
@ -122,15 +121,7 @@ class DbConnection{
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
$done = true;
|
$done = true;
|
||||||
if(SITE_STATUS == SITE_STATUS_DEV){
|
throw $this->CreateDetailedException($ex, $preparedSql, $params);
|
||||||
throw($ex);
|
|
||||||
}
|
|
||||||
else{
|
|
||||||
Log::WriteErrorLogEntry($ex->getMessage());
|
|
||||||
Log::WriteErrorLogEntry($preparedSql);
|
|
||||||
Log::WriteErrorLogEntry(vds($params));
|
|
||||||
throw($ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -138,6 +129,16 @@ class DbConnection{
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param \PdoException $ex The exception to create details from.
|
||||||
|
* @param string $preparedSql The prepared SQL that caused the exception.
|
||||||
|
* @param array<mixed> $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<mixed>
|
* @return Array<mixed>
|
||||||
*/
|
*/
|
||||||
|
@ -260,7 +261,6 @@ class DbConnection{
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets the last auto-increment id
|
|
||||||
public function GetLastInsertedId(): ?int{
|
public function GetLastInsertedId(): ?int{
|
||||||
if($this->_link === null){
|
if($this->_link === null){
|
||||||
return null;
|
return null;
|
||||||
|
|
5
lib/Exceptions/DatabaseQueryException.php
Normal file
5
lib/Exceptions/DatabaseQueryException.php
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
<?
|
||||||
|
namespace Exceptions;
|
||||||
|
|
||||||
|
class DatabaseQueryException extends AppException{
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue