Skip to content

Commit ce0bf8f

Browse files
committed
Remove select duplications fix $collection issue
1 parent 7fea0b3 commit ce0bf8f

File tree

15 files changed

+386
-346
lines changed

15 files changed

+386
-346
lines changed

src/Database/Adapter/MariaDB.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1835,7 +1835,7 @@ public function find(
18351835
$sqlLimit .= ' OFFSET :offset';
18361836
}
18371837

1838-
//$selections = $this->getAttributeSelections($selects);
1838+
//$selections = $this->getAttributeSelections($selects);
18391839

18401840
$sql = "
18411841
SELECT {$this->getAttributeProjectionV2($selects)}

src/Database/Adapter/Pool.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ public function find(
268268
array $filters = [],
269269
array $joins = [],
270270
array $orderQueries = []
271-
): array
272-
{
271+
): array {
273272
return $this->delegate(__FUNCTION__, \func_get_args());
274273
}
275274

src/Database/Adapter/SQL.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,11 +1539,26 @@ protected function getAttributeProjectionV2(array $selects): string
15391539
return Query::DEFAULT_ALIAS.'.*';
15401540
}
15411541

1542+
$duplications = [];
1543+
15421544
$string = '';
15431545
foreach ($selects as $select) {
1546+
if($select->getAttribute() === '$collection'){
1547+
continue;
1548+
}
1549+
1550+
$needle = $select->getAlias().':'.$select->getAttribute();
1551+
1552+
if (in_array($needle, $duplications)){
1553+
continue;
1554+
}
1555+
1556+
$duplications[] = $needle;
1557+
15441558
$alias = $select->getAlias();
15451559
$alias = $this->filter($alias);
15461560
$attribute = $select->getAttribute();
1561+
15471562
$attribute = match ($attribute) {
15481563
'$id' => '_uid',
15491564
'$internalId' => '_id',
@@ -1554,12 +1569,12 @@ protected function getAttributeProjectionV2(array $selects): string
15541569
default => $attribute
15551570
};
15561571

1557-
if ($attribute !== '*'){
1572+
if ($attribute !== '*') {
15581573
$attribute = $this->filter($attribute);
15591574
$attribute = $this->quote($attribute);
15601575
}
15611576

1562-
if (!empty($string)){
1577+
if (!empty($string)) {
15631578
$string .= ', ';
15641579
}
15651580

src/Database/Database.php

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,10 +2961,19 @@ public function getDocument(string $collection, string $id, array $queries = [],
29612961
}
29622962

29632963
$selects = Query::getSelectQueries($queries);
2964-
if(count($selects) !== count($queries)){
2964+
if (count($selects) !== count($queries)) {
2965+
// Do we want this check?
29652966
throw new QueryException('Only select queries are allowed');
29662967
}
29672968

2969+
/**
2970+
* For security check
2971+
*/
2972+
if (!empty($selects)) {
2973+
//$selects[] = Query::select('$id'); // Do we need this?
2974+
$selects[] = Query::select('$permissions', system: true);
2975+
}
2976+
29682977
$context = new QueryContext();
29692978
$context->add($collection);
29702979

@@ -3098,7 +3107,10 @@ public function getDocument(string $collection, string $id, array $queries = [],
30983107
// $id, $permissions and $collection are the default selected attributes for (MariaDB, MySQL, SQLite, Postgres)
30993108
// All internal attributes are default selected attributes for (MongoDB)
31003109
if (!empty($selects)) {
3101-
$selectedAttributes = array_map(fn($q) => $q->getAttribute(), $selects);
3110+
$selectedAttributes = array_map(
3111+
fn ($q) => $q->getAttribute(),
3112+
array_filter($selects, fn ($q) => $q->isSystem() === false)
3113+
);
31023114

31033115
foreach ($this->getInternalAttributes() as $internalAttribute) {
31043116
if (!in_array($internalAttribute['$id'], $selectedAttributes, true)) {
@@ -5744,11 +5756,11 @@ public function find(string $collection, array $queries = [], string $forPermiss
57445756

57455757
//$filters = self::convertQueries($collection, $filters);
57465758

5747-
// /** @var array<Query> $queries */
5748-
// $queries = \array_merge(
5749-
// $selects,
5750-
// $filters
5751-
// );
5759+
// /** @var array<Query> $queries */
5760+
// $queries = \array_merge(
5761+
// $selects,
5762+
// $filters
5763+
// );
57525764

57535765
$selections = $this->validateSelections($collection, $selects);
57545766
$nestedSelections = [];
@@ -5781,48 +5793,48 @@ public function find(string $collection, array $queries = [], string $forPermiss
57815793

57825794
$selects = \array_values($selects); // Since we may unset above
57835795

5784-
// foreach ($queries as $index => &$query) {
5785-
// switch ($query->getMethod()) {
5786-
// case Query::TYPE_SELECT:
5787-
// $values = $query->getValues();
5788-
// foreach ($values as $valueIndex => $value) {
5789-
// if (\str_contains($value, '.')) {
5790-
// // Shift the top level off the dot-path to pass the selection down the chain
5791-
// // 'foo.bar.baz' becomes 'bar.baz'
5792-
// $nestedSelections[] = Query::select([
5793-
// \implode('.', \array_slice(\explode('.', $value), 1))
5794-
// ]);
5795-
//
5796-
// $key = \explode('.', $value)[0];
5797-
//
5798-
// foreach ($relationships as $relationship) {
5799-
// if ($relationship->getAttribute('key') === $key) {
5800-
// switch ($relationship->getAttribute('options')['relationType']) {
5801-
// case Database::RELATION_MANY_TO_MANY:
5802-
// case Database::RELATION_ONE_TO_MANY:
5803-
// unset($values[$valueIndex]);
5804-
// break;
5805-
//
5806-
// case Database::RELATION_MANY_TO_ONE:
5807-
// case Database::RELATION_ONE_TO_ONE:
5808-
// $values[$valueIndex] = $key;
5809-
// break;
5810-
// }
5811-
// }
5812-
// }
5813-
// }
5814-
// }
5815-
// $query->setValues(\array_values($values));
5816-
// break;
5817-
// default:
5818-
// if (\str_contains($query->getAttribute(), '.')) {
5819-
// unset($queries[$index]);
5820-
// }
5821-
// break;
5822-
// }
5823-
// }
5824-
//
5825-
// $queries = \array_values($queries);
5796+
// foreach ($queries as $index => &$query) {
5797+
// switch ($query->getMethod()) {
5798+
// case Query::TYPE_SELECT:
5799+
// $values = $query->getValues();
5800+
// foreach ($values as $valueIndex => $value) {
5801+
// if (\str_contains($value, '.')) {
5802+
// // Shift the top level off the dot-path to pass the selection down the chain
5803+
// // 'foo.bar.baz' becomes 'bar.baz'
5804+
// $nestedSelections[] = Query::select([
5805+
// \implode('.', \array_slice(\explode('.', $value), 1))
5806+
// ]);
5807+
//
5808+
// $key = \explode('.', $value)[0];
5809+
//
5810+
// foreach ($relationships as $relationship) {
5811+
// if ($relationship->getAttribute('key') === $key) {
5812+
// switch ($relationship->getAttribute('options')['relationType']) {
5813+
// case Database::RELATION_MANY_TO_MANY:
5814+
// case Database::RELATION_ONE_TO_MANY:
5815+
// unset($values[$valueIndex]);
5816+
// break;
5817+
//
5818+
// case Database::RELATION_MANY_TO_ONE:
5819+
// case Database::RELATION_ONE_TO_ONE:
5820+
// $values[$valueIndex] = $key;
5821+
// break;
5822+
// }
5823+
// }
5824+
// }
5825+
// }
5826+
// }
5827+
// $query->setValues(\array_values($values));
5828+
// break;
5829+
// default:
5830+
// if (\str_contains($query->getAttribute(), '.')) {
5831+
// unset($queries[$index]);
5832+
// }
5833+
// break;
5834+
// }
5835+
// }
5836+
//
5837+
// $queries = \array_values($queries);
58265838

58275839
$results = $this->adapter->find(
58285840
$context,
@@ -6267,7 +6279,7 @@ public function decodeV2(QueryContext $context, Document $document, array $selec
62676279
}
62686280
}
62696281

6270-
$new = new Document;
6282+
$new = new Document();
62716283

62726284
foreach ($document as $key => $value) {
62736285
$alias = Query::DEFAULT_ALIAS;

src/Database/Query.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ class Query
8888
protected string $aliasRight = '';
8989
protected string $attributeRight = '';
9090
protected string $as = '';
91+
protected bool $system = false;
9192
protected bool $onArray = false;
9293

9394
/**
@@ -111,6 +112,7 @@ protected function __construct(
111112
string $aliasRight = '',
112113
string $collection = '',
113114
string $as = '',
115+
bool $system = false,
114116
) {
115117
if ($attribute === '' && \in_array($method, [Query::TYPE_ORDER_ASC, Query::TYPE_ORDER_DESC])) {
116118
$attribute = '$internalId';
@@ -135,6 +137,7 @@ protected function __construct(
135137
$this->attributeRight = $attributeRight;
136138
$this->collection = $collection;
137139
$this->as = $as;
140+
$this->system = $system;
138141
}
139142

140143
public function __clone(): void
@@ -560,9 +563,9 @@ public static function select_old(array $attributes): self
560563
return new self(self::TYPE_SELECT, values: $attributes);
561564
}
562565

563-
public static function select(string $attribute, string $alias = '', string $as = '', string $function = ''): self
566+
public static function select(string $attribute, string $alias = '', string $as = '', string $function = '', bool $system = false): self
564567
{
565-
return new self(self::TYPE_SELECT, $attribute, [], alias: $alias, as: $as);
568+
return new self(self::TYPE_SELECT, $attribute, [], alias: $alias, as: $as, system: $system);
566569
}
567570

568571
/**
@@ -993,8 +996,6 @@ public function isJoin(): bool
993996
return false;
994997
}
995998

996-
997-
998999
public function onArray(): bool
9991000
{
10001001
return $this->onArray;
@@ -1008,4 +1009,12 @@ public function setOnArray(bool $bool): void
10081009
{
10091010
$this->onArray = $bool;
10101011
}
1012+
1013+
/**
1014+
* Is This query added by the system
1015+
*/
1016+
public function isSystem(): bool
1017+
{
1018+
return $this->system;
1019+
}
10111020
}

src/Database/Validator/IndexedQueries.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
//
34
//namespace Utopia\Database\Validator;
45
//

src/Database/Validator/Queries/Document.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
//
34
//namespace Utopia\Database\Validator\Queries;
45
//

src/Database/Validator/Queries/Documents.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
//
34
//namespace Utopia\Database\Validator\Queries;
45
//

src/Database/Validator/Queries/V2.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,7 @@ public function isValid($value, string $scope = ''): bool
134134
}
135135
}
136136

137-
echo PHP_EOL.PHP_EOL.PHP_EOL.PHP_EOL;
138-
var_dump($query->getMethod(), $query->getCollection(), $query->getAlias());
137+
//var_dump($query->getMethod(), $query->getCollection(), $query->getAlias());
139138

140139
$this->validateAlias($query);
141140

@@ -249,8 +248,8 @@ public function isValid($value, string $scope = ''): bool
249248
$this->validateSelect($query);
250249

251250
break;
252-
// case Query::TYPE_SELECTION:
253-
// $this->validateSelections($query);
251+
// case Query::TYPE_SELECTION:
252+
// $this->validateSelections($query);
254253

255254
break;
256255
case Query::TYPE_ORDER_ASC:

0 commit comments

Comments
 (0)