From adbab90b341fe3239a9889be5b86b7f5eb0ea792 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 29 Sep 2025 16:30:24 +0300 Subject: [PATCH 1/4] Spatial attributes --- src/Database/Adapter/MariaDB.php | 11 +++---- src/Database/Adapter/Postgres.php | 8 ++--- src/Database/Adapter/SQL.php | 36 +++++------------------ src/Database/Database.php | 6 ++-- src/Database/Query.php | 25 ++++++++++++++++ tests/e2e/Adapter/Scopes/GeneralTests.php | 9 +++++- 6 files changed, 51 insertions(+), 44 deletions(-) diff --git a/src/Database/Adapter/MariaDB.php b/src/Database/Adapter/MariaDB.php index 5c16531ac..e5dd89c5b 100644 --- a/src/Database/Adapter/MariaDB.php +++ b/src/Database/Adapter/MariaDB.php @@ -1479,11 +1479,10 @@ protected function handleSpatialQueries(Query $query, array &$binds, string $att * * @param Query $query * @param array $binds - * @param array $attributes * @return string * @throws Exception */ - protected function getSQLCondition(Query $query, array &$binds, array $attributes = []): string + protected function getSQLCondition(Query $query, array &$binds): string { $query->setAttribute($this->getInternalKeyForAttribute($query->getAttribute())); @@ -1493,10 +1492,8 @@ protected function getSQLCondition(Query $query, array &$binds, array $attribute $alias = $this->quote(Query::DEFAULT_ALIAS); $placeholder = ID::unique(); - $attributeType = $this->getAttributeType($query->getAttribute(), $attributes); - - if (in_array($attributeType, Database::SPATIAL_TYPES)) { - return $this->handleSpatialQueries($query, $binds, $attribute, $attributeType, $alias, $placeholder); + if ($query->isSpatialAttribute()) { + return $this->handleSpatialQueries($query, $binds, $attribute, $query->getAttributeType(), $alias, $placeholder); } switch ($query->getMethod()) { @@ -1505,7 +1502,7 @@ protected function getSQLCondition(Query $query, array &$binds, array $attribute $conditions = []; /* @var $q Query */ foreach ($query->getValue() as $q) { - $conditions[] = $this->getSQLCondition($q, $binds, $attributes); + $conditions[] = $this->getSQLCondition($q, $binds); } $method = strtoupper($query->getMethod()); diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index 8fd1c88da..fe8214e9c 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -1575,11 +1575,10 @@ protected function handleSpatialQueries(Query $query, array &$binds, string $att * * @param Query $query * @param array $binds - * @param array $attributes * @return string * @throws Exception */ - protected function getSQLCondition(Query $query, array &$binds, array $attributes = []): string + protected function getSQLCondition(Query $query, array &$binds): string { $query->setAttribute($this->getInternalKeyForAttribute($query->getAttribute())); @@ -1588,10 +1587,9 @@ protected function getSQLCondition(Query $query, array &$binds, array $attribute $alias = $this->quote(Query::DEFAULT_ALIAS); $placeholder = ID::unique(); - $attributeType = $this->getAttributeType($query->getAttribute(), $attributes); $operator = null; - if (in_array($attributeType, Database::SPATIAL_TYPES)) { + if ($query->isSpatialAttribute()) { return $this->handleSpatialQueries($query, $binds, $attribute, $alias, $placeholder); } @@ -1601,7 +1599,7 @@ protected function getSQLCondition(Query $query, array &$binds, array $attribute $conditions = []; /* @var $q Query */ foreach ($query->getValue() as $q) { - $conditions[] = $this->getSQLCondition($q, $binds, $attributes); + $conditions[] = $this->getSQLCondition($q, $binds); } $method = strtoupper($query->getMethod()); diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 589419eb9..84fae6ce7 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -1793,21 +1793,19 @@ public function getMaxIndexLength(): int /** * @param Query $query * @param array $binds - * @param array $attributes * @return string * @throws Exception */ - abstract protected function getSQLCondition(Query $query, array &$binds, array $attributes = []): string; + abstract protected function getSQLCondition(Query $query, array &$binds): string; /** * @param array $queries * @param array $binds * @param string $separator - * @param array $attributes * @return string * @throws Exception */ - public function getSQLConditions(array $queries, array &$binds, string $separator = 'AND', array $attributes = []): string + public function getSQLConditions(array $queries, array &$binds, string $separator = 'AND'): string { $conditions = []; foreach ($queries as $query) { @@ -1816,9 +1814,9 @@ public function getSQLConditions(array $queries, array &$binds, string $separato } if ($query->isNested()) { - $conditions[] = $this->getSQLConditions($query->getValues(), $binds, $query->getMethod(), $attributes); + $conditions[] = $this->getSQLConditions($query->getValues(), $binds, $query->getMethod()); } else { - $conditions[] = $this->getSQLCondition($query, $binds, $attributes); + $conditions[] = $this->getSQLCondition($query, $binds); } } @@ -2316,26 +2314,6 @@ protected function convertArrayToWKT(array $geometry): string throw new DatabaseException('Unrecognized geometry array format'); } - /** - * Helper method to get attribute type from attributes array - * - * @param string $attributeName - * @param array $attributes - * @return string|null - */ - protected function getAttributeType(string $attributeName, array $attributes): ?string - { - foreach ($attributes as $attribute) { - if (isset($attribute['$id']) && $attribute['$id'] === $attributeName) { - return $attribute['type'] ?? null; - } - if (isset($attribute['key']) && $attribute['key'] === $attributeName) { - return $attribute['type'] ?? null; - } - } - return null; - } - /** * Find Documents * @@ -2438,7 +2416,7 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25 $where[] = '(' . implode(' OR ', $cursorWhere) . ')'; } - $conditions = $this->getSQLConditions($queries, $binds, attributes:$attributes); + $conditions = $this->getSQLConditions($queries, $binds); if (!empty($conditions)) { $where[] = $conditions; } @@ -2562,7 +2540,7 @@ public function count(Document $collection, array $queries = [], ?int $max = nul $queries = array_map(fn ($query) => clone $query, $queries); - $conditions = $this->getSQLConditions($queries, $binds, attributes:$attributes); + $conditions = $this->getSQLConditions($queries, $binds); if (!empty($conditions)) { $where[] = $conditions; } @@ -2638,7 +2616,7 @@ public function sum(Document $collection, string $attribute, array $queries = [] $queries = array_map(fn ($query) => clone $query, $queries); - $conditions = $this->getSQLConditions($queries, $binds, attributes:$collectionAttributes); + $conditions = $this->getSQLConditions($queries, $binds); if (!empty($conditions)) { $where[] = $conditions; } diff --git a/src/Database/Database.php b/src/Database/Database.php index 60853bc66..fb990bbf1 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1273,7 +1273,6 @@ public function create(?string $database = null): bool public function exists(?string $database = null, ?string $collection = null): bool { $database ??= $this->adapter->getDatabase(); - return $this->adapter->exists($database, $collection); } @@ -7112,9 +7111,12 @@ public static function convertQuery(Document $collection, Query $query): Query } if (!$attribute->isEmpty()) { + $type = $attribute->getAttribute('type'); + $query->setOnArray($attribute->getAttribute('array', false)); + $query->setAttributeType($type); - if ($attribute->getAttribute('type') == Database::VAR_DATETIME) { + if ($type == Database::VAR_DATETIME) { $values = $query->getValues(); foreach ($values as $valueIndex => $value) { try { diff --git a/src/Database/Query.php b/src/Database/Query.php index 96383efdb..47be58c12 100644 --- a/src/Database/Query.php +++ b/src/Database/Query.php @@ -110,6 +110,7 @@ class Query protected string $method = ''; protected string $attribute = ''; + protected string $attributeType = ''; protected bool $onArray = false; /** @@ -935,6 +936,30 @@ public function setOnArray(bool $bool): void $this->onArray = $bool; } + /** + * @param string $type + * @return void + */ + public function setAttributeType(string $type): void + { + $this->attributeType = $type; + } + + /** + * @return string + */ + public function getAttributeType(): string + { + return $this->attributeType; + } + /** + * @return bool + */ + public function isSpatialAttribute(): bool + { + return in_array($this->attributeType, Database::SPATIAL_TYPES); + } + // Spatial query methods /** diff --git a/tests/e2e/Adapter/Scopes/GeneralTests.php b/tests/e2e/Adapter/Scopes/GeneralTests.php index 1664273c1..0081a1f96 100644 --- a/tests/e2e/Adapter/Scopes/GeneralTests.php +++ b/tests/e2e/Adapter/Scopes/GeneralTests.php @@ -424,7 +424,9 @@ public function testNestedQueryValidation(): void public function testSharedTablesTenantPerDocument(): void { + /** @var Database $database */ $database = static::getDatabase(); + $sharedTables = $database->getSharedTables(); $tenantPerDocument = $database->getTenantPerDocument(); $namespace = $database->getNamespace(); @@ -435,8 +437,13 @@ public function testSharedTablesTenantPerDocument(): void return; } - if ($database->exists(__FUNCTION__)) { + try { + /** + * MirrorTest returning false $database->exists(__FUNCTION__) + */ $database->delete(__FUNCTION__); + } catch (\Throwable $e) { + } $database From 6b0720d5be762d4e2fcfe86c0d70e9c75c700476 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 29 Sep 2025 16:45:20 +0300 Subject: [PATCH 2/4] line --- src/Database/Database.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Database/Database.php b/src/Database/Database.php index fb990bbf1..1d82082e8 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1273,6 +1273,7 @@ public function create(?string $database = null): bool public function exists(?string $database = null, ?string $collection = null): bool { $database ??= $this->adapter->getDatabase(); + return $this->adapter->exists($database, $collection); } From 6cdfaf9bf99786ce63f3633e62c6e8f334d23369 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 29 Sep 2025 16:46:45 +0300 Subject: [PATCH 3/4] set type --- src/Database/Database.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 1d82082e8..8a1f80533 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -7112,12 +7112,10 @@ public static function convertQuery(Document $collection, Query $query): Query } if (!$attribute->isEmpty()) { - $type = $attribute->getAttribute('type'); - $query->setOnArray($attribute->getAttribute('array', false)); - $query->setAttributeType($type); + $query->setAttributeType($attribute->getAttribute('type')); - if ($type == Database::VAR_DATETIME) { + if ($attribute->getAttribute('type') == Database::VAR_DATETIME) { $values = $query->getValues(); foreach ($values as $valueIndex => $value) { try { From be5105fa43056feb7f4528abeb99e1dc40f98094 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 30 Sep 2025 08:16:36 +0300 Subject: [PATCH 4/4] Revert schema delete in test --- tests/e2e/Adapter/Scopes/GeneralTests.php | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/tests/e2e/Adapter/Scopes/GeneralTests.php b/tests/e2e/Adapter/Scopes/GeneralTests.php index 0081a1f96..1664273c1 100644 --- a/tests/e2e/Adapter/Scopes/GeneralTests.php +++ b/tests/e2e/Adapter/Scopes/GeneralTests.php @@ -424,9 +424,7 @@ public function testNestedQueryValidation(): void public function testSharedTablesTenantPerDocument(): void { - /** @var Database $database */ $database = static::getDatabase(); - $sharedTables = $database->getSharedTables(); $tenantPerDocument = $database->getTenantPerDocument(); $namespace = $database->getNamespace(); @@ -437,13 +435,8 @@ public function testSharedTablesTenantPerDocument(): void return; } - try { - /** - * MirrorTest returning false $database->exists(__FUNCTION__) - */ + if ($database->exists(__FUNCTION__)) { $database->delete(__FUNCTION__); - } catch (\Throwable $e) { - } $database