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..8a1f80533 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -7113,6 +7113,7 @@ public static function convertQuery(Document $collection, Query $query): Query if (!$attribute->isEmpty()) { $query->setOnArray($attribute->getAttribute('array', false)); + $query->setAttributeType($attribute->getAttribute('type')); if ($attribute->getAttribute('type') == Database::VAR_DATETIME) { $values = $query->getValues(); 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 /**