Skip to content

Commit

Permalink
Merge branch 'main' into feat-resizing-string-attributes
Browse files Browse the repository at this point in the history
# Conflicts:
#	src/Database/Adapter/MariaDB.php
#	src/Database/Adapter/MySQL.php
  • Loading branch information
PineappleIOnic committed Aug 29, 2024
2 parents 8a72170 + 56da33b commit 91487bf
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 41 deletions.
24 changes: 21 additions & 3 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,9 +708,14 @@ public function createIndex(string $collection, string $id, string $type, array
$sql = "CREATE {$sqlType} `{$id}` ON {$this->getSQLTable($collection->getId())} ({$attributes})";
$sql = $this->trigger(Database::EVENT_INDEX_CREATE, $sql);

return $this->getPDO()
->prepare($sql)
->execute();
try {
return $this->getPDO()
->prepare($sql)
->execute();
} catch (PDOException $e) {
$this->processException($e);
return false;
}
}

/**
Expand Down Expand Up @@ -1651,6 +1656,8 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
$where = [];
$orders = [];

$queries = array_map(fn ($query) => clone $query, $queries);

$orderAttributes = \array_map(fn ($orderAttribute) => match ($orderAttribute) {
'$id' => '_uid',
'$internalId' => '_id',
Expand Down Expand Up @@ -1852,6 +1859,8 @@ public function count(string $collection, array $queries = [], ?int $max = null)
$where = [];
$limit = \is_null($max) ? '' : 'LIMIT :max';

$queries = array_map(fn ($query) => clone $query, $queries);

$conditions = $this->getSQLConditions($queries);
if(!empty($conditions)) {
$where[] = $conditions;
Expand Down Expand Up @@ -1923,6 +1932,8 @@ public function sum(string $collection, string $attribute, array $queries = [],
$where = [];
$limit = \is_null($max) ? '' : 'LIMIT :max';

$queries = array_map(fn ($query) => clone $query, $queries);

foreach ($queries as $query) {
$where[] = $this->getSQLCondition($query);
}
Expand Down Expand Up @@ -2242,6 +2253,13 @@ protected function processException(PDOException $e): void
throw new DatabaseException('Resize would result in data truncation', $e->getCode(), $e);
}

// Duplicate index
if ($e->getCode() === '42000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1061) {
throw new DuplicateException($e->getMessage(), $e->getCode(), $e);
} elseif ($e->getCode() === 1061 && isset($e->errorInfo[0]) && $e->errorInfo[0] === '42000') {
throw new DuplicateException($e->getMessage(), $e->getCode(), $e);
}

throw $e;
}
}
4 changes: 4 additions & 0 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,7 @@ public function updateAttribute(string $collection, string $id, string $type, in
public function find(string $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER): array
{
$name = $this->getNamespace() . '_' . $this->filter($collection);
$queries = array_map(fn ($query) => clone $query, $queries);

$filters = $this->buildFilters($queries);

Expand Down Expand Up @@ -1212,6 +1213,8 @@ public function count(string $collection, array $queries = [], ?int $max = null)
{
$name = $this->getNamespace() . '_' . $this->filter($collection);

$queries = array_map(fn ($query) => clone $query, $queries);

$filters = [];
$options = [];

Expand Down Expand Up @@ -1252,6 +1255,7 @@ public function sum(string $collection, string $attribute, array $queries = [],
$name = $this->getNamespace() . '_' . $this->filter($collection);

// queries
$queries = array_map(fn ($query) => clone $query, $queries);
$filters = $this->buildFilters($queries);

// permissions
Expand Down
71 changes: 40 additions & 31 deletions src/Database/Adapter/MySQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,37 +35,6 @@ public function setTimeout(int $milliseconds, string $event = Database::EVENT_AL
});
}

/**
* @param PDOException $e
* @throws TimeoutException
* @throws DuplicateException
*/
protected function processException(PDOException $e): void
{
/**
* PDO and Swoole PDOProxy swap error codes and errorInfo
*/

if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 3024) {
throw new TimeoutException($e->getMessage(), $e->getCode(), $e);
} elseif ($e->getCode() === 3024 && isset($e->errorInfo[0]) && $e->errorInfo[0] === "HY000") {
throw new TimeoutException($e->getMessage(), $e->getCode(), $e);
}

if ($e->getCode() === '42S21' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1060) {
throw new DuplicateException($e->getMessage(), $e->getCode(), $e);
} elseif ($e->getCode() === 1060 && isset($e->errorInfo[0]) && $e->errorInfo[0] === '42S21') {
throw new DuplicateException($e->getMessage(), $e->getCode(), $e);
}

// Data is too big for column resize
if ($e->getCode() === '22001' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1406) {
throw new DatabaseException('Resize would result in data truncation', $e->getCode(), $e);
}

throw $e;
}

/**
* Get Collection Size
* @param string $collection
Expand Down Expand Up @@ -113,4 +82,44 @@ public function castIndexArray(): bool
{
return true;
}

/**
* @param PDOException $e
* @throws TimeoutException
* @throws DuplicateException
*/
protected function processException(PDOException $e): void
{
/**
* PDO and Swoole PDOProxy swap error codes and errorInfo
*/

// Timeout
if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 3024) {
throw new TimeoutException($e->getMessage(), $e->getCode(), $e);
} elseif ($e->getCode() === 3024 && isset($e->errorInfo[0]) && $e->errorInfo[0] === "HY000") {
throw new TimeoutException($e->getMessage(), $e->getCode(), $e);
}

// Duplicate column
if ($e->getCode() === '42S21' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1060) {
throw new DuplicateException($e->getMessage(), $e->getCode(), $e);
} elseif ($e->getCode() === 1060 && isset($e->errorInfo[0]) && $e->errorInfo[0] === '42S21') {
throw new DuplicateException($e->getMessage(), $e->getCode(), $e);
}

// Duplicate index
if ($e->getCode() === '42000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1061) {
throw new DuplicateException($e->getMessage(), $e->getCode(), $e);
} elseif ($e->getCode() === 1061 && isset($e->errorInfo[0]) && $e->errorInfo[0] === '42000') {
throw new DuplicateException($e->getMessage(), $e->getCode(), $e);
}

// Data is too big for column resize
if ($e->getCode() === '22001' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1406) {
throw new DatabaseException('Resize would result in data truncation', $e->getCode(), $e);
}

throw $e;
}
}
17 changes: 14 additions & 3 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -686,9 +686,14 @@ public function createIndex(string $collection, string $id, string $type, array
$sql = "CREATE {$sqlType} {$key} ON {$this->getSQLTable($collection)} ({$attributes});";
$sql = $this->trigger(Database::EVENT_INDEX_CREATE, $sql);

return $this->getPDO()
->prepare($sql)
->execute();
try {
return $this->getPDO()
->prepare($sql)
->execute();
} catch (PDOException $e) {
$this->processException($e);
return false;
}
}

/**
Expand Down Expand Up @@ -1578,6 +1583,8 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
$where = [];
$orders = [];

$queries = array_map(fn ($query) => clone $query, $queries);

$orderAttributes = \array_map(fn ($orderAttribute) => match ($orderAttribute) {
'$id' => '_uid',
'$internalId' => '_id',
Expand Down Expand Up @@ -1773,6 +1780,8 @@ public function count(string $collection, array $queries = [], ?int $max = null)
$where = [];
$limit = \is_null($max) ? '' : 'LIMIT :max';

$queries = array_map(fn ($query) => clone $query, $queries);

$conditions = $this->getSQLConditions($queries);
if(!empty($conditions)) {
$where[] = $conditions;
Expand Down Expand Up @@ -1837,6 +1846,8 @@ public function sum(string $collection, string $attribute, array $queries = [],
$where = [];
$limit = \is_null($max) ? '' : 'LIMIT :max';

$queries = array_map(fn ($query) => clone $query, $queries);

foreach ($queries as $query) {
$where[] = $this->getSQLCondition($query);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Database/Adapter/SQLite.php
Original file line number Diff line number Diff line change
Expand Up @@ -1367,12 +1367,14 @@ protected function processException(PDOException $e): void
* PDO and Swoole PDOProxy swap error codes and errorInfo
*/

// Timeout
if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 3024) {
throw new TimeoutException($e->getMessage(), $e->getCode(), $e);
} elseif ($e->getCode() === 3024 && isset($e->errorInfo[0]) && $e->errorInfo[0] === "HY000") {
throw new TimeoutException($e->getMessage(), $e->getCode(), $e);
}

// Duplicate
if ($e->getCode() === 'HY000' && isset($e->errorInfo[1]) && $e->errorInfo[1] === 1) {
throw new DuplicateException($e->getMessage(), $e->getCode(), $e);
} elseif ($e->getCode() === 1 && isset($e->errorInfo[0]) && $e->errorInfo[0] === 'HY000') {
Expand Down
15 changes: 13 additions & 2 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2660,15 +2660,26 @@ public function createIndex(string $collection, string $id, string $type, array
}
}

$index = $this->adapter->createIndex($collection->getId(), $id, $type, $attributes, $lengths, $orders);
try {
$created = $this->adapter->createIndex($collection->getId(), $id, $type, $attributes, $lengths, $orders);

if (!$created) {
throw new DatabaseException('Failed to create index');
}
} catch (DuplicateException $e) {
// HACK: Metadata should still be updated, can be removed when null tenant collections are supported.
if (!$this->adapter->getSharedTables()) {
throw $e;
}
}

if ($collection->getId() !== self::METADATA) {
$this->silent(fn () => $this->updateDocument(self::METADATA, $collection->getId(), $collection));
}

$this->trigger(self::EVENT_INDEX_CREATE, $index);

return $index;
return true;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions src/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ public function __construct(string $method, string $attribute = '', array $value
$this->values = $values;
}

public function __clone(): void
{
foreach ($this->values as $index => $value) {
if ($value instanceof self) {
$this->values[$index] = clone $value;
}
}
}

/**
* @return string
*/
Expand Down
Loading

0 comments on commit 91487bf

Please sign in to comment.