CustomerRewardsRESTAPI/Model/Database.php

112 lines
3.5 KiB
PHP
Raw Normal View History

2024-04-15 11:38:59 -05:00
<?php
/**
* Description of Database
*
2024-05-22 14:55:08 -05:00
* @author Mike Howard
2024-04-15 11:38:59 -05:00
*/
class Database {
protected $connection = null;
2024-05-14 11:38:57 -05:00
2024-04-15 11:38:59 -05:00
public function __construct()
{
try {
2024-05-02 02:19:13 -05:00
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
2024-04-15 11:38:59 -05:00
$this->connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE_NAME);
2024-05-13 15:12:31 -05:00
2024-04-15 11:38:59 -05:00
if ( mysqli_connect_errno()) {
throw new Exception("Could not connect to database.");
}
} catch (Exception $e) {
throw new Exception($e->getMessage());
}
}
public function processQuery($query = "", $params = [])
{
try {
$stmt = $this->connection->prepare( $query );
if($stmt === false) {
throw New Exception("Unable to do prepared statement: " . $query);
}
if( $params ) {
$stmt->bind_param($params[0], $params[1]);
}
$stmt->execute();
2024-04-15 11:38:59 -05:00
$result = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $result;
} catch(Exception $e) {
throw New Exception( $e->getMessage() );
}
return false;
}
2024-05-03 12:41:39 -05:00
public function processStatement($query = "")
2024-04-15 11:38:59 -05:00
{
try {
2024-05-02 16:00:15 -05:00
//Prepare the statement
$stmt = $this->connection->prepare($query);
if($stmt === false) {
throw New Exception("Unable to prepare the statement: " . $query);
}
2024-05-02 16:00:15 -05:00
$result = $stmt->execute();
if($result === false) {
2024-05-03 19:27:12 -05:00
throw New Exception("Unable to execute the statement: " . $query);
2024-05-02 16:00:15 -05:00
}
$rowCount = $this->connection->affected_rows;
if($rowCount < 1)
{
throw New Exception("Statement did not return any rows: " . $query);
}
2024-04-15 11:38:59 -05:00
$stmt->close();
2024-05-02 16:00:15 -05:00
return $rowCount;
2024-04-15 11:38:59 -05:00
} catch(Exception $e) {
throw New Exception( $e->getMessage() );
}
return false;
}
2024-05-09 20:25:56 -05:00
2024-05-29 14:47:51 -05:00
public function processImage($query, $imageModel) {
2024-05-29 01:59:54 -05:00
try
{
2024-05-29 12:27:49 -05:00
$data = $this->connection->real_escape_string($imageModel->imageBlob);
$result = $this->connection->query($query."(".$imageModel->memberId.", '".$imageModel->imageType."', '".$data."')");
2024-05-09 20:25:56 -05:00
if($result === false) {
2024-05-29 01:59:54 -05:00
$msg = "Unable to execute the statement: " . $query;
return $this->unprocessableQueryResponse($msg);
2024-05-09 20:25:56 -05:00
}
$rowCount = $this->connection->affected_rows;
if($rowCount < 1)
{
2024-05-29 01:59:54 -05:00
$msg = "Statement did not return any rows: " . $query;
return $this->unprocessableQueryResponse($msg);
2024-05-09 20:25:56 -05:00
}
} catch(Exception $e) {
2024-05-29 12:27:49 -05:00
$msg = $query . " " . $e->getMessage();
2024-05-29 01:59:54 -05:00
return $this->unprocessableQueryResponse($msg);
2024-05-09 20:25:56 -05:00
}
2024-05-29 12:27:49 -05:00
$this->connection->close();
return $rowCount;
2024-05-29 01:59:54 -05:00
}
private function unprocessableQueryResponse($msg)
{
$response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Query';
$response['body'] = json_encode([
2024-06-03 00:18:25 -05:00
'error' => 'Invalid query',
'message' => $msg
2024-05-29 01:59:54 -05:00
]);
2024-05-29 12:27:49 -05:00
$this->strErrorDesc = 'Unprocessable Query';
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Query';
2024-05-29 01:59:54 -05:00
$this->strErrorMessage = $msg;
return $response;
2024-05-09 20:25:56 -05:00
}
2024-04-15 11:38:59 -05:00
}