Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions src/Driver/Cassandra/Cassandra.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,22 +121,26 @@ protected function connect(): void
*
* @param string $query
* @return Rows
* @throws Exception
* @throws CassandraModelException if an error occurs while executing the query
*/
protected function rawQuery(string $query): Rows
{
$this->connect();

$statement = new SimpleStatement($query);
return $this->connection->execute($statement, null);
try {
return $this->connection->execute($statement, null);
} catch (Exception $e) {
throw CassandraModelException::wrapping($e);
}
}

/**
* Save the model
*
* @param ModelInterface $model
* @return bool
* @throws Exception
* @throws CassandraModelException if an error occurs while executing the query
*/
public function save(ModelInterface $model): bool
{
Expand All @@ -158,7 +162,7 @@ public function save(ModelInterface $model): bool
* @param mixed $id
* @param ModelInterface|null $model
* @return ModelInterface|null
* @throws Exception
* @throws CassandraModelException if an error occurs while executing the query
*/
public function get(string $modelClass, mixed $id, ?ModelInterface $model = null): ?ModelInterface
{
Expand All @@ -183,7 +187,7 @@ public function get(string $modelClass, mixed $id, ?ModelInterface $model = null
*
* @param ModelInterface $model
* @return bool
* @throws Exception
* @throws CassandraModelException if an error occurs while executing the query
*/
public function delete(ModelInterface $model): bool
{
Expand All @@ -200,7 +204,7 @@ public function delete(ModelInterface $model): bool
*
* @param Query $query
* @return QueryResult
* @throws Exception
* @throws CassandraModelException if an error occurs while executing the query
*/
public function query(Query $query): QueryResult
{
Expand Down Expand Up @@ -278,4 +282,4 @@ public function setKeyspace(string $keyspace): Cassandra
$this->keyspace = $keyspace;
return $this;
}
}
}
20 changes: 20 additions & 0 deletions src/Driver/Cassandra/CassandraModelException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php /** @noinspection PhpComposerExtensionStubsInspection */

namespace Aternos\Model\Driver\Cassandra;

use Aternos\Model\WrappingModelException;
use Throwable;

class CassandraModelException extends WrappingModelException
{
/**
* Wrap an existing exception into a CassandraModelException
* This is used to adapt exceptions from the cassandra extension to a ModelException
* @param Throwable $exception
* @return static
*/
static function wrapping(Throwable $exception): static
{
return new self($exception->getMessage(), $exception->getCode(), $exception);
}
}
13 changes: 13 additions & 0 deletions src/Driver/Mysqli/MysqlConnectionFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Aternos\Model\Driver\Mysqli;

use Throwable;

class MysqlConnectionFailedException extends MysqlException
{
public function __construct(Throwable $previous)
{
parent::__construct("Could not connect to Mysqli database", $previous?->getCode(), $previous);
}
}
38 changes: 38 additions & 0 deletions src/Driver/Mysqli/MysqlException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php /** @noinspection PhpComposerExtensionStubsInspection */

namespace Aternos\Model\Driver\Mysqli;

use Aternos\Model\ModelException;
use mysqli;
use Throwable;

class MysqlException extends ModelException
{
/**
* Check if a MySQLi connection has an error and throw an exception if it has
* @param mysqli $connection
* @return void
* @throws MysqlException if the connection has an error
*/
static function checkConnection(mysqli $connection): void
{
if (mysqli_error($connection)) {
throw static::fromConnection($connection);
}
}

/**
* Create an exception from a MySQLi connection with an error
* @param mysqli $connection
* @return static
*/
static function fromConnection(mysqli $connection): static
{
return new self("MySQLi Error #" . mysqli_errno($connection) . ": " . mysqli_error($connection));
}

public function __construct(string $message, int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
32 changes: 15 additions & 17 deletions src/Driver/Mysqli/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
Query\Generator\SQL,
Query\Query,
Query\QueryResult,
Query\UpdateQuery
};
use Exception;
Query\UpdateQuery};
use mysqli_result;

/**
Expand Down Expand Up @@ -99,15 +97,14 @@ public function __construct(?string $host = null, ?int $port = null, ?string $us

/**
* Connect to database
*
* @throws Exception
* @throws MysqlConnectionFailedException if connecting to the mysql database fails
*/
protected function connect(): void
{
if (!$this->connection || !@mysqli_ping($this->connection)) {
$this->connection = mysqli_connect($this->host, $this->username, $this->password, $this->database, $this->port, $this->socket);
if (!$this->connection) {
throw new Exception("Could not connect to Mysqli database. Error: " . mysqli_error($this->connection));
throw new MysqlConnectionFailedException(new MysqlException($this->connection));
}
}
}
Expand All @@ -117,17 +114,14 @@ protected function connect(): void
*
* @param string $query
* @return bool|mysqli_result
* @throws Exception
* @throws MysqlConnectionFailedException if connecting to the mysql database fails
* @throws MysqlException if a mysql error occurs while executing the query
*/
protected function rawQuery(string $query): mysqli_result|bool
{
$this->connect();
$result = mysqli_query($this->connection, $query);

if (mysqli_error($this->connection)) {
throw new Exception("MySQLi Error #" . mysqli_errno($this->connection) . ": " . mysqli_error($this->connection));
}

MysqlException::checkConnection($this->connection);
return $result;
}

Expand All @@ -136,7 +130,8 @@ protected function rawQuery(string $query): mysqli_result|bool
*
* @param ModelInterface $model
* @return bool
* @throws Exception
* @throws MysqlConnectionFailedException if connecting to the mysql database fails
* @throws MysqlException if a mysql error occurs while executing the query
*/
public function save(ModelInterface $model): bool
{
Expand Down Expand Up @@ -182,7 +177,8 @@ public function save(ModelInterface $model): bool
* @param mixed $id
* @param ModelInterface|null $model
* @return ModelInterface|null
* @throws Exception
* @throws MysqlConnectionFailedException if connecting to the mysql database fails
* @throws MysqlException if a mysql error occurs while executing the query
*/
public function get(string $modelClass, mixed $id, ?ModelInterface $model = null): ?ModelInterface
{
Expand All @@ -208,7 +204,8 @@ public function get(string $modelClass, mixed $id, ?ModelInterface $model = null
*
* @param ModelInterface $model
* @return bool
* @throws Exception
* @throws MysqlConnectionFailedException if connecting to the mysql database fails
* @throws MysqlException if a mysql error occurs while executing the query
*/
public function delete(ModelInterface $model): bool
{
Expand All @@ -227,7 +224,8 @@ public function delete(ModelInterface $model): bool
*
* @param Query $query
* @return QueryResult
* @throws Exception
* @throws MysqlConnectionFailedException if connecting to the mysql database fails
* @throws MysqlException if a mysql error occurs while executing the query
*/
public function query(Query $query): QueryResult
{
Expand Down Expand Up @@ -323,4 +321,4 @@ public function setDatabase(string $database): Mysqli
$this->database = $database;
return $this;
}
}
}
4 changes: 2 additions & 2 deletions src/Driver/OpenSearch/Exception/OpenSearchException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace Aternos\Model\Driver\OpenSearch\Exception;

use Exception;
use Aternos\Model\ModelException;

class OpenSearchException extends Exception
class OpenSearchException extends ModelException
{

}
77 changes: 39 additions & 38 deletions src/Driver/OpenSearch/OpenSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,9 @@ public function __construct(
* @param string $uri
* @param mixed|null $body
* @return stdClass
* @throws HttpErrorResponseException
* @throws HttpTransportException
* @throws SerializeException
* @throws OpenSearchException
* @throws HttpErrorResponseException If the response status code is not 2xx
* @throws HttpTransportException If an error happens while the http client processes the request
* @throws SerializeException If an error happens during (de-)serialization
*/
protected function request(string $method, string $uri, mixed $body = null): stdClass
{
Expand All @@ -96,6 +95,7 @@ protected function request(string $method, string $uri, mixed $body = null): std
throw $e;
}
}
/** @var OpenSearchException $lastError */
throw $lastError;
}

Expand All @@ -113,7 +113,9 @@ protected function buildUrl(string ...$path): string
*
* @param ModelInterface $model
* @return bool
* @throws OpenSearchException
* @throws HttpErrorResponseException If the response status code is not 2xx
* @throws HttpTransportException If an error happens while the http client processes the request
* @throws SerializeException If an error happens during (de-)serialization
*/
public function save(ModelInterface $model): bool
{
Expand All @@ -132,10 +134,9 @@ public function save(ModelInterface $model): bool
* @param mixed $id
* @param ModelInterface|null $model
* @return ModelInterface|null
* @throws HttpErrorResponseException
* @throws HttpTransportException
* @throws OpenSearchException
* @throws SerializeException
* @throws HttpErrorResponseException If the response status code is not 2xx
* @throws HttpTransportException If an error happens while the http client processes the request
* @throws SerializeException If an error happens during (de-)serialization
*/
public function get(string $modelClass, mixed $id, ?ModelInterface $model = null): ?ModelInterface
{
Expand All @@ -151,17 +152,7 @@ public function get(string $modelClass, mixed $id, ?ModelInterface $model = null
throw $e;
}

if (!isset($response->_id) || !is_string($response->_id)) {
throw new SerializeException("Received invalid document _id from OpenSearch");
}

if (isset($response->_source) && is_object($response->_source)) {
$data = get_object_vars($response->_source);
} else {
$data = [];
}

$data[$modelClass::getIdField()] = $response->_id;
$data = $this->getModelData($response, $modelClass);

if ($model) {
return $model->applyData($data);
Expand All @@ -174,7 +165,9 @@ public function get(string $modelClass, mixed $id, ?ModelInterface $model = null
*
* @param ModelInterface $model
* @return bool
* @throws OpenSearchException
* @throws HttpErrorResponseException If the response status code is not 2xx
* @throws HttpTransportException If an error happens while the http client processes the request
* @throws SerializeException If an error happens during (de-)serialization
*/
public function delete(ModelInterface $model): bool
{
Expand Down Expand Up @@ -208,10 +201,9 @@ protected function getHitCountRelation(string $name): ?CountRelation
/**
* @param Search $search
* @return SearchResult
* @throws HttpErrorResponseException
* @throws HttpTransportException
* @throws OpenSearchException
* @throws SerializeException
* @throws HttpErrorResponseException If the response status code is not 2xx
* @throws HttpTransportException If an error happens while the http client processes the request
* @throws SerializeException If an error happens during (de-)serialization
*/
public function search(Search $search): SearchResult
{
Expand Down Expand Up @@ -242,23 +234,32 @@ public function search(Search $search): SearchResult
}

foreach ($response->hits->hits as $resultDocument) {
if (!isset($resultDocument->_id) || !is_string($resultDocument->_id)) {
throw new SerializeException("Received invalid document _id from OpenSearch");
}

if (isset($resultDocument->_source) && is_object($resultDocument->_source)) {
$data = get_object_vars($resultDocument->_source);
} else {
$data = [];
}

$data[$modelClassName::getIdField()] = $resultDocument->_id;

/** @var ModelInterface $model */
$model = new $modelClassName();
$model->applyData($data);
$model->applyData($this->getModelData($resultDocument, $modelClassName));
$result->add($model);
}
return $result;
}

/**
* @param stdClass $response
* @param class-string<ModelInterface> $modelClass $modelClass
* @return array
*/
public function getModelData(stdClass $response, string $modelClass): array
{
if (!isset($response->_id) || !is_string($response->_id)) {
throw new SerializeException("Received invalid document _id from OpenSearch");
}

if (isset($response->_source) && is_object($response->_source)) {
$data = get_object_vars($response->_source);
} else {
$data = [];
}

$data[$modelClass::getIdField()] = $response->_id;
return $data;
}
}
Loading
Loading