Skip to content

Commit adbab90

Browse files
committed
Spatial attributes
1 parent 19a3ab2 commit adbab90

File tree

6 files changed

+51
-44
lines changed

6 files changed

+51
-44
lines changed

src/Database/Adapter/MariaDB.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,11 +1479,10 @@ protected function handleSpatialQueries(Query $query, array &$binds, string $att
14791479
*
14801480
* @param Query $query
14811481
* @param array<string, mixed> $binds
1482-
* @param array<mixed> $attributes
14831482
* @return string
14841483
* @throws Exception
14851484
*/
1486-
protected function getSQLCondition(Query $query, array &$binds, array $attributes = []): string
1485+
protected function getSQLCondition(Query $query, array &$binds): string
14871486
{
14881487
$query->setAttribute($this->getInternalKeyForAttribute($query->getAttribute()));
14891488

@@ -1493,10 +1492,8 @@ protected function getSQLCondition(Query $query, array &$binds, array $attribute
14931492
$alias = $this->quote(Query::DEFAULT_ALIAS);
14941493
$placeholder = ID::unique();
14951494

1496-
$attributeType = $this->getAttributeType($query->getAttribute(), $attributes);
1497-
1498-
if (in_array($attributeType, Database::SPATIAL_TYPES)) {
1499-
return $this->handleSpatialQueries($query, $binds, $attribute, $attributeType, $alias, $placeholder);
1495+
if ($query->isSpatialAttribute()) {
1496+
return $this->handleSpatialQueries($query, $binds, $attribute, $query->getAttributeType(), $alias, $placeholder);
15001497
}
15011498

15021499
switch ($query->getMethod()) {
@@ -1505,7 +1502,7 @@ protected function getSQLCondition(Query $query, array &$binds, array $attribute
15051502
$conditions = [];
15061503
/* @var $q Query */
15071504
foreach ($query->getValue() as $q) {
1508-
$conditions[] = $this->getSQLCondition($q, $binds, $attributes);
1505+
$conditions[] = $this->getSQLCondition($q, $binds);
15091506
}
15101507

15111508
$method = strtoupper($query->getMethod());

src/Database/Adapter/Postgres.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,11 +1575,10 @@ protected function handleSpatialQueries(Query $query, array &$binds, string $att
15751575
*
15761576
* @param Query $query
15771577
* @param array<string, mixed> $binds
1578-
* @param array<mixed> $attributes
15791578
* @return string
15801579
* @throws Exception
15811580
*/
1582-
protected function getSQLCondition(Query $query, array &$binds, array $attributes = []): string
1581+
protected function getSQLCondition(Query $query, array &$binds): string
15831582
{
15841583
$query->setAttribute($this->getInternalKeyForAttribute($query->getAttribute()));
15851584

@@ -1588,10 +1587,9 @@ protected function getSQLCondition(Query $query, array &$binds, array $attribute
15881587
$alias = $this->quote(Query::DEFAULT_ALIAS);
15891588
$placeholder = ID::unique();
15901589

1591-
$attributeType = $this->getAttributeType($query->getAttribute(), $attributes);
15921590
$operator = null;
15931591

1594-
if (in_array($attributeType, Database::SPATIAL_TYPES)) {
1592+
if ($query->isSpatialAttribute()) {
15951593
return $this->handleSpatialQueries($query, $binds, $attribute, $alias, $placeholder);
15961594
}
15971595

@@ -1601,7 +1599,7 @@ protected function getSQLCondition(Query $query, array &$binds, array $attribute
16011599
$conditions = [];
16021600
/* @var $q Query */
16031601
foreach ($query->getValue() as $q) {
1604-
$conditions[] = $this->getSQLCondition($q, $binds, $attributes);
1602+
$conditions[] = $this->getSQLCondition($q, $binds);
16051603
}
16061604

16071605
$method = strtoupper($query->getMethod());

src/Database/Adapter/SQL.php

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1793,21 +1793,19 @@ public function getMaxIndexLength(): int
17931793
/**
17941794
* @param Query $query
17951795
* @param array<string, mixed> $binds
1796-
* @param array<mixed> $attributes
17971796
* @return string
17981797
* @throws Exception
17991798
*/
1800-
abstract protected function getSQLCondition(Query $query, array &$binds, array $attributes = []): string;
1799+
abstract protected function getSQLCondition(Query $query, array &$binds): string;
18011800

18021801
/**
18031802
* @param array<Query> $queries
18041803
* @param array<string, mixed> $binds
18051804
* @param string $separator
1806-
* @param array<mixed> $attributes
18071805
* @return string
18081806
* @throws Exception
18091807
*/
1810-
public function getSQLConditions(array $queries, array &$binds, string $separator = 'AND', array $attributes = []): string
1808+
public function getSQLConditions(array $queries, array &$binds, string $separator = 'AND'): string
18111809
{
18121810
$conditions = [];
18131811
foreach ($queries as $query) {
@@ -1816,9 +1814,9 @@ public function getSQLConditions(array $queries, array &$binds, string $separato
18161814
}
18171815

18181816
if ($query->isNested()) {
1819-
$conditions[] = $this->getSQLConditions($query->getValues(), $binds, $query->getMethod(), $attributes);
1817+
$conditions[] = $this->getSQLConditions($query->getValues(), $binds, $query->getMethod());
18201818
} else {
1821-
$conditions[] = $this->getSQLCondition($query, $binds, $attributes);
1819+
$conditions[] = $this->getSQLCondition($query, $binds);
18221820
}
18231821
}
18241822

@@ -2316,26 +2314,6 @@ protected function convertArrayToWKT(array $geometry): string
23162314
throw new DatabaseException('Unrecognized geometry array format');
23172315
}
23182316

2319-
/**
2320-
* Helper method to get attribute type from attributes array
2321-
*
2322-
* @param string $attributeName
2323-
* @param array<mixed> $attributes
2324-
* @return string|null
2325-
*/
2326-
protected function getAttributeType(string $attributeName, array $attributes): ?string
2327-
{
2328-
foreach ($attributes as $attribute) {
2329-
if (isset($attribute['$id']) && $attribute['$id'] === $attributeName) {
2330-
return $attribute['type'] ?? null;
2331-
}
2332-
if (isset($attribute['key']) && $attribute['key'] === $attributeName) {
2333-
return $attribute['type'] ?? null;
2334-
}
2335-
}
2336-
return null;
2337-
}
2338-
23392317
/**
23402318
* Find Documents
23412319
*
@@ -2438,7 +2416,7 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25
24382416
$where[] = '(' . implode(' OR ', $cursorWhere) . ')';
24392417
}
24402418

2441-
$conditions = $this->getSQLConditions($queries, $binds, attributes:$attributes);
2419+
$conditions = $this->getSQLConditions($queries, $binds);
24422420
if (!empty($conditions)) {
24432421
$where[] = $conditions;
24442422
}
@@ -2562,7 +2540,7 @@ public function count(Document $collection, array $queries = [], ?int $max = nul
25622540

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

2565-
$conditions = $this->getSQLConditions($queries, $binds, attributes:$attributes);
2543+
$conditions = $this->getSQLConditions($queries, $binds);
25662544
if (!empty($conditions)) {
25672545
$where[] = $conditions;
25682546
}
@@ -2638,7 +2616,7 @@ public function sum(Document $collection, string $attribute, array $queries = []
26382616

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

2641-
$conditions = $this->getSQLConditions($queries, $binds, attributes:$collectionAttributes);
2619+
$conditions = $this->getSQLConditions($queries, $binds);
26422620
if (!empty($conditions)) {
26432621
$where[] = $conditions;
26442622
}

src/Database/Database.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,6 @@ public function create(?string $database = null): bool
12731273
public function exists(?string $database = null, ?string $collection = null): bool
12741274
{
12751275
$database ??= $this->adapter->getDatabase();
1276-
12771276
return $this->adapter->exists($database, $collection);
12781277
}
12791278

@@ -7112,9 +7111,12 @@ public static function convertQuery(Document $collection, Query $query): Query
71127111
}
71137112

71147113
if (!$attribute->isEmpty()) {
7114+
$type = $attribute->getAttribute('type');
7115+
71157116
$query->setOnArray($attribute->getAttribute('array', false));
7117+
$query->setAttributeType($type);
71167118

7117-
if ($attribute->getAttribute('type') == Database::VAR_DATETIME) {
7119+
if ($type == Database::VAR_DATETIME) {
71187120
$values = $query->getValues();
71197121
foreach ($values as $valueIndex => $value) {
71207122
try {

src/Database/Query.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ class Query
110110

111111
protected string $method = '';
112112
protected string $attribute = '';
113+
protected string $attributeType = '';
113114
protected bool $onArray = false;
114115

115116
/**
@@ -935,6 +936,30 @@ public function setOnArray(bool $bool): void
935936
$this->onArray = $bool;
936937
}
937938

939+
/**
940+
* @param string $type
941+
* @return void
942+
*/
943+
public function setAttributeType(string $type): void
944+
{
945+
$this->attributeType = $type;
946+
}
947+
948+
/**
949+
* @return string
950+
*/
951+
public function getAttributeType(): string
952+
{
953+
return $this->attributeType;
954+
}
955+
/**
956+
* @return bool
957+
*/
958+
public function isSpatialAttribute(): bool
959+
{
960+
return in_array($this->attributeType, Database::SPATIAL_TYPES);
961+
}
962+
938963
// Spatial query methods
939964

940965
/**

tests/e2e/Adapter/Scopes/GeneralTests.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,9 @@ public function testNestedQueryValidation(): void
424424

425425
public function testSharedTablesTenantPerDocument(): void
426426
{
427+
/** @var Database $database */
427428
$database = static::getDatabase();
429+
428430
$sharedTables = $database->getSharedTables();
429431
$tenantPerDocument = $database->getTenantPerDocument();
430432
$namespace = $database->getNamespace();
@@ -435,8 +437,13 @@ public function testSharedTablesTenantPerDocument(): void
435437
return;
436438
}
437439

438-
if ($database->exists(__FUNCTION__)) {
440+
try {
441+
/**
442+
* MirrorTest returning false $database->exists(__FUNCTION__)
443+
*/
439444
$database->delete(__FUNCTION__);
445+
} catch (\Throwable $e) {
446+
440447
}
441448

442449
$database

0 commit comments

Comments
 (0)