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
11 changes: 4 additions & 7 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -1479,11 +1479,10 @@ protected function handleSpatialQueries(Query $query, array &$binds, string $att
*
* @param Query $query
* @param array<string, mixed> $binds
* @param array<mixed> $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()));

Expand All @@ -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()) {
Expand All @@ -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());
Expand Down
8 changes: 3 additions & 5 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -1575,11 +1575,10 @@ protected function handleSpatialQueries(Query $query, array &$binds, string $att
*
* @param Query $query
* @param array<string, mixed> $binds
* @param array<mixed> $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()));

Expand All @@ -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);
}

Expand All @@ -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());
Expand Down
36 changes: 7 additions & 29 deletions src/Database/Adapter/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -1793,21 +1793,19 @@ public function getMaxIndexLength(): int
/**
* @param Query $query
* @param array<string, mixed> $binds
* @param array<mixed> $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<Query> $queries
* @param array<string, mixed> $binds
* @param string $separator
* @param array<mixed> $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) {
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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<mixed> $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
*
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 25 additions & 0 deletions src/Database/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class Query

protected string $method = '';
protected string $attribute = '';
protected string $attributeType = '';
protected bool $onArray = false;

/**
Expand Down Expand Up @@ -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

/**
Expand Down