Skip to content

Commit 311555c

Browse files
committed
Init selects
1 parent 7e837ca commit 311555c

File tree

7 files changed

+132
-58
lines changed

7 files changed

+132
-58
lines changed

src/Database/Adapter/MariaDB.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1835,10 +1835,10 @@ public function find(
18351835
$sqlLimit .= ' OFFSET :offset';
18361836
}
18371837

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

18401840
$sql = "
1841-
SELECT {$this->getAttributeProjection($selections, $alias)}
1841+
SELECT {$this->getAttributeProjectionV2($selects, $alias)}
18421842
FROM {$this->getSQLTable($mainCollection)} AS {$this->quote($alias)}
18431843
{$sqlJoin}
18441844
{$sqlWhere}
@@ -2063,7 +2063,9 @@ protected function getSQLCondition(Query $query, array &$binds): string
20632063
$attribute = $query->getAttribute();
20642064
$attribute = $this->filter($attribute);
20652065
$attribute = $this->quote($attribute);
2066-
$alias = $this->quote($query->getAlias());
2066+
$alias = $query->getAlias();
2067+
$alias = $this->filter($alias);
2068+
$alias = $this->quote($alias);
20672069

20682070
//$placeholder = $this->getSQLPlaceholder($query);
20692071
$placeholder = ID::unique();

src/Database/Adapter/Postgres.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,9 +1904,9 @@ protected function getSQLCondition(Query $query, array &$binds): string
19041904

19051905
$attribute = $this->filter($query->getAttribute());
19061906
$attribute = $this->quote($attribute);
1907-
$alias = $this->quote($query->getAlias());
1908-
1909-
//$placeholder = $this->getSQLPlaceholder($query);
1907+
$alias = $query->getAlias();
1908+
$alias = $this->filter($alias);
1909+
$alias = $this->quote($alias);
19101910
$placeholder = ID::unique();
19111911
$operator = null;
19121912

src/Database/Adapter/SQL.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,43 @@ public function getTenantQuery(
15251525
return "{$condition} ({$alias}{$dot}_tenant IN ({$bindings}) {$orIsNull})";
15261526
}
15271527

1528+
/**
1529+
* Get the SQL projection given the selected attributes
1530+
*
1531+
* @param array<Query> $selects
1532+
* @return string
1533+
* @throws Exception
1534+
*/
1535+
protected function getAttributeProjectionV2(array $selects): string
1536+
{
1537+
if (empty($selects)) {
1538+
return Query::DEFAULT_ALIAS.'.*';
1539+
}
1540+
1541+
$string = '';
1542+
foreach ($selects as $select) {
1543+
var_dump($select->getAttribute());
1544+
var_dump($select->getAlias());
1545+
if(!empty($string)){
1546+
$string .= ', ';
1547+
}
1548+
1549+
$alias = $this->filter($select->getAlias());
1550+
1551+
$attribute = $select->getAttribute();
1552+
$attribute = $this->getInternalKeyForAttribute($attribute);
1553+
1554+
if ($attribute !== '*'){
1555+
$attribute = $this->filter($attribute);
1556+
$attribute = $this->quote($attribute);
1557+
}
1558+
1559+
$string .= "{$this->quote($alias)}.{$attribute}";
1560+
}
1561+
1562+
return $string;
1563+
}
1564+
15281565
/**
15291566
* Get the SQL projection given the selected attributes
15301567
*

src/Database/Database.php

Lines changed: 73 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -5746,57 +5746,91 @@ public function find(string $collection, array $queries = [], string $forPermiss
57465746

57475747
//$filters = self::convertQueries($collection, $filters);
57485748

5749-
/** @var array<Query> $queries */
5750-
$queries = \array_merge(
5751-
$selects,
5752-
$filters
5753-
);
5749+
// /** @var array<Query> $queries */
5750+
// $queries = \array_merge(
5751+
// $selects,
5752+
// $filters
5753+
// );
57545754

57555755
$selections = $this->validateSelections($collection, $selects);
57565756
$nestedSelections = [];
57575757

5758-
foreach ($queries as $index => &$query) {
5759-
switch ($query->getMethod()) {
5760-
case Query::TYPE_SELECT:
5761-
$values = $query->getValues();
5762-
foreach ($values as $valueIndex => $value) {
5763-
if (\str_contains($value, '.')) {
5764-
// Shift the top level off the dot-path to pass the selection down the chain
5765-
// 'foo.bar.baz' becomes 'bar.baz'
5766-
$nestedSelections[] = Query::select([
5767-
\implode('.', \array_slice(\explode('.', $value), 1))
5768-
]);
5758+
foreach ($selects as $i => $q) {
5759+
var_dump($q->getAlias());
5760+
var_dump($q->getAttribute());
5761+
if (\str_contains($q->getAttribute(), '.')) {
5762+
$nestedSelections[] = Query::select(
5763+
\implode('.', \array_slice(\explode('.', $q->getAttribute()), 1))
5764+
);
57695765

5770-
$key = \explode('.', $value)[0];
5766+
$key = \explode('.', $q->getAttribute())[0];
57715767

5772-
foreach ($relationships as $relationship) {
5773-
if ($relationship->getAttribute('key') === $key) {
5774-
switch ($relationship->getAttribute('options')['relationType']) {
5775-
case Database::RELATION_MANY_TO_MANY:
5776-
case Database::RELATION_ONE_TO_MANY:
5777-
unset($values[$valueIndex]);
5778-
break;
5768+
var_dump('####################################');
5769+
var_dump($key);
5770+
var_dump('####################################');
5771+
foreach ($relationships as $relationship) {
5772+
if ($relationship->getAttribute('key') === $key) {
5773+
switch ($relationship->getAttribute('options')['relationType']) {
5774+
case Database::RELATION_MANY_TO_MANY:
5775+
case Database::RELATION_ONE_TO_MANY:
5776+
unset($selects[$i]);
5777+
break;
57795778

5780-
case Database::RELATION_MANY_TO_ONE:
5781-
case Database::RELATION_ONE_TO_ONE:
5782-
$values[$valueIndex] = $key;
5783-
break;
5784-
}
5785-
}
5786-
}
5779+
case Database::RELATION_MANY_TO_ONE:
5780+
case Database::RELATION_ONE_TO_ONE:
5781+
$q->setAttribute($key);
5782+
$selects[$i] = $q;
5783+
break;
57875784
}
57885785
}
5789-
$query->setValues(\array_values($values));
5790-
break;
5791-
default:
5792-
if (\str_contains($query->getAttribute(), '.')) {
5793-
unset($queries[$index]);
5794-
}
5795-
break;
5786+
}
57965787
}
57975788
}
57985789

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

58015835
$results = $this->adapter->find(
58025836
$context,

src/Database/Query.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class Query
2626

2727
public const TYPE_SELECT = 'select';
2828

29-
public const TYPE_SELECTION = 'selection';
29+
//public const TYPE_SELECTION = 'selection';
3030

3131
// Order methods
3232
public const TYPE_ORDER_DESC = 'orderDesc';
@@ -555,14 +555,14 @@ public static function search(string $attribute, string $value): self
555555
* @param array<string> $attributes
556556
* @return Query
557557
*/
558-
public static function select(array $attributes): self
558+
public static function select_old(array $attributes): self
559559
{
560560
return new self(self::TYPE_SELECT, values: $attributes);
561561
}
562562

563-
public static function selection(string $attribute, string $alias = '', string $as = '', string $function = ''): self
563+
public static function select(string $attribute, string $alias = '', string $as = '', string $function = ''): self
564564
{
565-
return new self(self::TYPE_SELECTION, $attribute, [], alias: $alias, as: $as);
565+
return new self(self::TYPE_SELECT, $attribute, [], alias: $alias, as: $as);
566566
}
567567

568568
/**

src/Database/Validator/Queries/V2.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ public function isValid($value, string $scope = ''): bool
249249
$this->validateSelect($query);
250250

251251
break;
252-
case Query::TYPE_SELECTION:
253-
$this->validateSelections($query);
252+
// case Query::TYPE_SELECTION:
253+
// $this->validateSelections($query);
254254

255255
break;
256256
case Query::TYPE_ORDER_ASC:

tests/e2e/Adapter/Base.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,10 @@ public function testJoin()
510510
$documents = static::getDatabase()->find(
511511
'__users',
512512
[
513-
Query::selection('*', 'main'),
514-
Query::selection('*', 'U'),
515-
Query::selection('$id', 'main'),
516-
Query::selection('user_id', 'U', as: 'user_id'),
513+
Query::select('*', 'main'),
514+
Query::select('*', 'U'),
515+
Query::select('$id', 'main'),
516+
Query::select('user_id', 'U', as: 'user_id'),
517517
Query::join(
518518
'__sessions',
519519
'U',
@@ -2012,7 +2012,7 @@ public function testAttributeNamesWithDots(): void
20122012
));
20132013

20142014
$document = static::getDatabase()->find('dots.parent', [
2015-
Query::select(['dots.name']),
2015+
Query::select('dots.name'),
20162016
]);
20172017
$this->assertEmpty($document);
20182018

@@ -2053,7 +2053,7 @@ public function testAttributeNamesWithDots(): void
20532053
]));
20542054

20552055
$documents = static::getDatabase()->find('dots.parent', [
2056-
Query::select(['*']),
2056+
Query::select('*'),
20572057
]);
20582058

20592059
$this->assertEquals('Bill clinton', $documents[0]['dots.name']);
@@ -3145,7 +3145,8 @@ public function testGetDocumentSelect(Document $document): Document
31453145
$documentId = $document->getId();
31463146

31473147
$document = static::getDatabase()->getDocument('documents', $documentId, [
3148-
Query::select(['string', 'integer_signed']),
3148+
Query::select('string'),
3149+
Query::select('integer_signed'),
31493150
]);
31503151

31513152
$this->assertEmpty($document->getId());

0 commit comments

Comments
 (0)