Skip to content

Commit

Permalink
Merge pull request #268 from utopia-php/feat-base-exception
Browse files Browse the repository at this point in the history
Feat base exception
  • Loading branch information
abnegate committed May 22, 2023
2 parents 69a844c + 1911052 commit 3e7c866
Show file tree
Hide file tree
Showing 26 changed files with 428 additions and 340 deletions.
2 changes: 0 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@
"utopia-php/mongo": "0.2.*"
},
"require-dev": {
"ext-redis": "*",
"ext-mongodb": "*",
"fakerphp/faker": "^1.14",
"phpunit/phpunit": "^9.4",
"pcov/clobber": "^2.0",
Expand Down
31 changes: 14 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/add-new-adapter.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Create your `NewDB.php` file in `src/Database/Adapter/` and extend the parent cl

namespace Utopia\Database\Adapter;

use Exception;
use Utopia\Database\Exception;
use Utopia\Database\Adapter;
use Utopia\Database\Database;
use Utopia\Database\Document;
Expand Down
13 changes: 7 additions & 6 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Utopia\Database;

use Exception;
use Utopia\Database\Exception as DatabaseException;

abstract class Adapter
{
Expand Down Expand Up @@ -66,7 +67,7 @@ public function resetDebug(): self
public function setNamespace(string $namespace): bool
{
if (empty($namespace)) {
throw new Exception('Missing namespace');
throw new DatabaseException('Missing namespace');
}

$this->namespace = $this->filter($namespace);
Expand All @@ -80,13 +81,13 @@ public function setNamespace(string $namespace): bool
* Get namespace of current set scope
*
* @return string
* @throws Exception
* @throws DatabaseException
*
*/
public function getNamespace(): string
{
if (empty($this->namespace)) {
throw new Exception('Missing namespace');
throw new DatabaseException('Missing namespace');
}

return $this->namespace;
Expand All @@ -106,7 +107,7 @@ public function getNamespace(): string
public function setDefaultDatabase(string $name, bool $reset = false): bool
{
if (empty($name) && $reset === false) {
throw new Exception('Missing database');
throw new DatabaseException('Missing database');
}

$this->defaultDatabase = ($reset) ? '' : $this->filter($name);
Expand All @@ -126,7 +127,7 @@ public function setDefaultDatabase(string $name, bool $reset = false): bool
public function getDefaultDatabase(): string
{
if (empty($this->defaultDatabase)) {
throw new Exception('Missing default database');
throw new DatabaseException('Missing default database');
}

return $this->defaultDatabase;
Expand Down Expand Up @@ -589,7 +590,7 @@ public function filter(string $value): string
$value = preg_replace("/[^A-Za-z0-9\_\-]/", '', $value);

if (\is_null($value)) {
throw new Exception('Failed to filter key');
throw new DatabaseException('Failed to filter key');
}

return $value;
Expand Down
32 changes: 17 additions & 15 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use PDO;
use PDOException;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Exception\Duplicate;
Expand Down Expand Up @@ -297,7 +298,7 @@ public function createRelationship(
case Database::RELATION_MANY_TO_MANY:
return true;
default:
throw new Exception('Invalid relationship type.');
throw new DatabaseException('Invalid relationship type');
}

return $this->getPDO()
Expand Down Expand Up @@ -376,7 +377,7 @@ public function updateRelationship(
}
break;
default:
throw new Exception('Invalid relationship type.');
throw new DatabaseException('Invalid relationship type');
}

if (empty($sql)) {
Expand Down Expand Up @@ -442,7 +443,7 @@ public function deleteRelationship(
$sql = "DROP TABLE {$junction}; DROP TABLE {$perms}";
break;
default:
throw new Exception('Invalid relationship type.');
throw new DatabaseException('Invalid relationship type');
}

if (empty($sql)) {
Expand Down Expand Up @@ -624,7 +625,7 @@ public function createDocument(string $collection, Document $document): Document
}

if (!$this->getPDO()->commit()) {
throw new Exception('Failed to commit transaction');
throw new DatabaseException('Failed to commit transaction');
}

return $document;
Expand Down Expand Up @@ -810,7 +811,7 @@ public function updateDocument(string $collection, Document $document): Document
}

if (!$this->getPDO()->commit()) {
throw new Exception('Failed to commit transaction');
throw new DatabaseException('Failed to commit transaction');
}

return $document;
Expand Down Expand Up @@ -841,7 +842,7 @@ public function increaseDocumentAttribute(string $collection, string $id, string
$stmt->bindValue(':_uid', $id);
$stmt->bindValue(':val', $value);

$stmt->execute() || throw new Exception('Failed to update Attribute');
$stmt->execute() || throw new DatabaseException('Failed to update attribute');
return true;
}

Expand All @@ -867,15 +868,15 @@ public function deleteDocument(string $collection, string $id): bool
$stmtPermissions->bindValue(':_uid', $id);

try {
$stmt->execute() || throw new Exception('Failed to delete document');
$stmtPermissions->execute() || throw new Exception('Failed to clean permissions');
$stmt->execute() || throw new DatabaseException('Failed to delete document');
$stmtPermissions->execute() || throw new DatabaseException('Failed to clean permissions');
} catch (\Throwable $th) {
$this->getPDO()->rollBack();
throw new Exception($th->getMessage());
throw new DatabaseException($th->getMessage());
}

if (!$this->getPDO()->commit()) {
throw new Exception('Failed to commit transaction');
throw new DatabaseException('Failed to commit transaction');
}

return true;
Expand Down Expand Up @@ -1024,7 +1025,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
};

if (is_null($cursor[$attribute] ?? null)) {
throw new Exception("Order attribute '{$attribute}' is empty.");
throw new DatabaseException("Order attribute '{$attribute}' is empty");
}
$stmt->bindValue(':cursor', $cursor[$attribute], $this->getPDOType($cursor[$attribute]));
}
Expand Down Expand Up @@ -1305,7 +1306,7 @@ protected function getSQLType(string $type, int $size, bool $signed = true): str
return 'DATETIME(3)';

default:
throw new Exception('Unknown Type');
throw new DatabaseException('Unknown type: ' . $type . '. Must be one of ' . Database::VAR_STRING . ', ' . Database::VAR_INTEGER . ', ' . Database::VAR_FLOAT . ', ' . Database::VAR_BOOLEAN . ', ' . Database::VAR_DATETIME . ', ' . Database::VAR_RELATIONSHIP);
}
}

Expand All @@ -1322,10 +1323,11 @@ protected function getSQLType(string $type, int $size, bool $signed = true): str
protected function getSQLIndex(string $collection, string $id, string $type, array $attributes): string
{
$type = match ($type) {
Database::INDEX_KEY, Database::INDEX_ARRAY => 'INDEX',
Database::INDEX_KEY,
Database::INDEX_ARRAY => 'INDEX',
Database::INDEX_UNIQUE => 'UNIQUE INDEX',
Database::INDEX_FULLTEXT => 'FULLTEXT INDEX',
default => throw new Exception('Unknown Index Type:' . $type),
default => throw new DatabaseException('Unknown index type: ' . $type . '. Must be one of ' . Database::INDEX_KEY . ', ' . Database::INDEX_UNIQUE . ', ' . Database::INDEX_ARRAY . ', ' . Database::INDEX_FULLTEXT),
};

return "CREATE {$type} `{$id}` ON {$this->getSQLTable($collection)} ( " . implode(', ', $attributes) . " )";
Expand All @@ -1344,7 +1346,7 @@ protected function getPDOType(mixed $value): int
'double', 'string' => PDO::PARAM_STR,
'integer', 'boolean' => PDO::PARAM_INT,
'NULL' => PDO::PARAM_NULL,
default => throw new Exception('Unknown PDO Type for ' . gettype($value)),
default => throw new DatabaseException('Unknown PDO Type for ' . \gettype($value)),
};
}

Expand Down
Loading

0 comments on commit 3e7c866

Please sign in to comment.