Skip to content

Commit dfd5c3c

Browse files
committed
Fix decoding
1 parent e18746a commit dfd5c3c

3 files changed

Lines changed: 74 additions & 50 deletions

File tree

src/Database/Database.php

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,8 +3030,8 @@ public function getDocument(string $collection, string $id, array $queries = [],
30303030
$collectionCacheKey = $this->cacheName . '-cache-' . $this->getNamespace() . ':' . $this->adapter->getTenant() . ':collection:' . $collection->getId();
30313031
$documentCacheKey = $documentCacheHash = $collectionCacheKey . ':' . $id;
30323032

3033-
if (!empty($selections)) {
3034-
$documentCacheHash .= ':' . \md5(\implode($selections));
3033+
if (!empty($selects)) {
3034+
$documentCacheHash .= ':' . \md5(\serialize($selects));
30353035
}
30363036

30373037
try {
@@ -3081,7 +3081,7 @@ public function getDocument(string $collection, string $id, array $queries = [],
30813081
}
30823082

30833083
$document = $this->casting($collection, $document);
3084-
$document = $this->decodeV2($context, $document, $selections);
3084+
$document = $this->decodeV2($context, $document, $selects);
30853085
$this->map = [];
30863086

30873087
if ($this->resolveRelationships && (empty($selects) || !empty($nestedSelections))) {
@@ -5701,12 +5701,6 @@ public function find(string $collection, array $queries = [], string $forPermiss
57015701
throw new AuthorizationException($authorization->getDescription());
57025702
}
57035703

5704-
var_dump('############');
5705-
var_dump($skipAuth);
5706-
var_dump($forPermission);
5707-
var_dump($_collection->getId());
5708-
var_dump('############');
5709-
57105704
$context->addSkipAuth($this->adapter->filter($_collection->getId()), $forPermission, $skipAuth);
57115705
}
57125706

@@ -5857,11 +5851,12 @@ public function find(string $collection, array $queries = [], string $forPermiss
58575851
//$results = $skipAuth ? Authorization::skip($getResults) : $getResults();
58585852

58595853
foreach ($results as $index => $node) {
5854+
$node = $this->casting($collection, $node);
5855+
$node = $this->decodeV2($context, $node, $selects);
5856+
58605857
if ($this->resolveRelationships && (empty($selects) || !empty($nestedSelections))) {
58615858
$node = $this->silent(fn () => $this->populateDocumentRelationships($collection, $node, $nestedSelections));
58625859
}
5863-
$node = $this->casting($collection, $node);
5864-
$node = $this->decode($collection, $node, $selections);
58655860

58665861
if (!$node->isEmpty()) {
58675862
$node->setAttribute('$collection', $collection->getId());
@@ -6265,20 +6260,19 @@ public function decode(Document $collection, Document $document, array $selectio
62656260
* @return Document
62666261
* @throws DatabaseException
62676262
*/
6268-
public function decodeV2(QueryContext $context, Document $document, array $selections = []): Document
6263+
public function decodeV2(QueryContext $context, Document $document, array $selects = []): Document
62696264
{
62706265
$schema = [];
62716266

62726267
foreach ($context->getCollections() as $collection) {
6273-
62746268
foreach ($collection->getAttribute('attributes', []) as $attribute) {
62756269
$key = $attribute->getAttribute('key', $attribute->getAttribute('$id'));
62766270
$key = $this->adapter->filter($key);
62776271
$schema[$collection->getId()][$key] = $attribute->getArrayCopy();
62786272
}
62796273

62806274
foreach (Database::INTERNAL_ATTRIBUTES as $attribute) {
6281-
$schema[$collection->getId()][$attribute['$id']] = new Document($attribute);
6275+
$schema[$collection->getId()][$attribute['$id']] = $attribute;
62826276
}
62836277
}
62846278

@@ -6287,12 +6281,23 @@ public function decodeV2(QueryContext $context, Document $document, array $selec
62876281
foreach ($document as $key => $value) {
62886282
$alias = Query::DEFAULT_ALIAS;
62896283

6284+
foreach ($selects as $select) {
6285+
if($this->adapter->filter($select->getAttribute()) == $key){
6286+
$alias = $select->getAlias();
6287+
break;
6288+
}
6289+
}
6290+
62906291
$collection = $context->getCollectionByAlias($alias);
62916292
if ($collection->isEmpty()) {
62926293
throw new \Exception('Invalid query: Unknown Alias context');
62936294
}
62946295

6295-
$attribute = $schema[$collection->getId()][$key];
6296+
$attribute = $schema[$collection->getId()][$key] ?? null;
6297+
6298+
if($attribute === null){
6299+
continue;
6300+
}
62966301

62976302
$array = $attribute['array'] ?? false;
62986303
$filters = $attribute['filters'] ?? [];
@@ -6306,21 +6311,21 @@ public function decodeV2(QueryContext $context, Document $document, array $selec
63066311
}
63076312
}
63086313

6309-
if (empty($selections) || \in_array($key, $selections) || \in_array('*', $selections)) {
6310-
if (
6311-
empty($selections)
6312-
|| \in_array($key, $selections)
6313-
|| \in_array('*', $selections)
6314-
|| \in_array($key, ['$createdAt', '$updatedAt'])
6315-
) {
6316-
// Prevent null values being set for createdAt and updatedAt
6317-
if (\in_array($key, ['$createdAt', '$updatedAt']) && $value[0] === null) {
6318-
//continue;
6319-
} else {
6320-
//$document->setAttribute($key, ($array) ? $value : $value[0]);
6321-
}
6322-
}
6323-
}
6314+
// if (empty($selections) || \in_array($key, $selections) || \in_array('*', $selections)) {
6315+
// if (
6316+
// empty($selections)
6317+
// || \in_array($key, $selections)
6318+
// || \in_array('*', $selections)
6319+
// || \in_array($key, ['$createdAt', '$updatedAt'])
6320+
// ) {
6321+
// // Prevent null values being set for createdAt and updatedAt
6322+
// if (\in_array($key, ['$createdAt', '$updatedAt']) && $value[0] === null) {
6323+
// continue;
6324+
// } else {
6325+
// $document->setAttribute($key, ($array) ? $value : $value[0]);
6326+
// }
6327+
// }
6328+
// }
63246329

63256330
$value = ($array) ? $value : $value[0];
63266331

@@ -6354,7 +6359,8 @@ public function casting(Document $collection, Document $document): Document
63546359
if (is_null($value)) {
63556360
continue;
63566361
}
6357-
6362+
var_dump('############# casting');
6363+
var_dump($type);
63586364
if ($array) {
63596365
$value = !is_string($value)
63606366
? $value

src/Database/Query.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,9 @@ public static function equal(string $attribute, array $values, string $alias = '
462462
* @param string|int|float|bool $value
463463
* @return Query
464464
*/
465-
public static function notEqual(string $attribute, string|int|float|bool $value): self
465+
public static function notEqual(string $attribute, string|int|float|bool $value, string $alias = ''): self
466466
{
467-
return new self(self::TYPE_NOT_EQUAL, $attribute, [$value]);
467+
return new self(self::TYPE_NOT_EQUAL, $attribute, [$value], alias: $alias);
468468
}
469469

470470
/**
@@ -474,9 +474,9 @@ public static function notEqual(string $attribute, string|int|float|bool $value)
474474
* @param string|int|float|bool $value
475475
* @return Query
476476
*/
477-
public static function lessThan(string $attribute, string|int|float|bool $value): self
477+
public static function lessThan(string $attribute, string|int|float|bool $value, string $alias = ''): self
478478
{
479-
return new self(self::TYPE_LESSER, $attribute, [$value]);
479+
return new self(self::TYPE_LESSER, $attribute, [$value], alias: $alias);
480480
}
481481

482482
/**
@@ -486,9 +486,9 @@ public static function lessThan(string $attribute, string|int|float|bool $value)
486486
* @param string|int|float|bool $value
487487
* @return Query
488488
*/
489-
public static function lessThanEqual(string $attribute, string|int|float|bool $value): self
489+
public static function lessThanEqual(string $attribute, string|int|float|bool $value, string $alias = ''): self
490490
{
491-
return new self(self::TYPE_LESSER_EQUAL, $attribute, [$value]);
491+
return new self(self::TYPE_LESSER_EQUAL, $attribute, [$value], alias: $alias);
492492
}
493493

494494
/**
@@ -498,9 +498,9 @@ public static function lessThanEqual(string $attribute, string|int|float|bool $v
498498
* @param string|int|float|bool $value
499499
* @return Query
500500
*/
501-
public static function greaterThan(string $attribute, string|int|float|bool $value): self
501+
public static function greaterThan(string $attribute, string|int|float|bool $value, string $alias = ''): self
502502
{
503-
return new self(self::TYPE_GREATER, $attribute, [$value]);
503+
return new self(self::TYPE_GREATER, $attribute, [$value], alias: $alias);
504504
}
505505

506506
/**
@@ -510,9 +510,9 @@ public static function greaterThan(string $attribute, string|int|float|bool $val
510510
* @param string|int|float|bool $value
511511
* @return Query
512512
*/
513-
public static function greaterThanEqual(string $attribute, string|int|float|bool $value): self
513+
public static function greaterThanEqual(string $attribute, string|int|float|bool $value, string $alias = ''): self
514514
{
515-
return new self(self::TYPE_GREATER_EQUAL, $attribute, [$value]);
515+
return new self(self::TYPE_GREATER_EQUAL, $attribute, [$value], alias: $alias);
516516
}
517517

518518
/**
@@ -535,9 +535,9 @@ public static function contains(string $attribute, array $values): self
535535
* @param string|int|float|bool $end
536536
* @return Query
537537
*/
538-
public static function between(string $attribute, string|int|float|bool $start, string|int|float|bool $end): self
538+
public static function between(string $attribute, string|int|float|bool $start, string|int|float|bool $end, string $alias = ''): self
539539
{
540-
return new self(self::TYPE_BETWEEN, $attribute, [$start, $end]);
540+
return new self(self::TYPE_BETWEEN, $attribute, [$start, $end], alias: $alias);
541541
}
542542

543543
/**

tests/e2e/Adapter/Base.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,8 @@ public function testJoin()
231231

232232
static::getDatabase()->createAttribute('__users', 'username', Database::VAR_STRING, 100, false);
233233
static::getDatabase()->createAttribute('__sessions', 'user_id', Database::VAR_STRING, 100, false);
234+
static::getDatabase()->createAttribute('__sessions', 'boolean', Database::VAR_BOOLEAN, 0, false);
235+
static::getDatabase()->createAttribute('__sessions', 'float', Database::VAR_FLOAT, 0, false);
234236

235237
$user1 = static::getDatabase()->createDocument('__users', new Document([
236238
'username' => 'Donald',
@@ -268,6 +270,8 @@ public function testJoin()
268270
'$permissions' => [
269271
Permission::read(Role::any()),
270272
],
273+
'boolean' => false,
274+
'float' => 10.5,
271275
]));
272276

273277
$user2 = static::getDatabase()->createDocument('__users', new Document([
@@ -283,6 +287,8 @@ public function testJoin()
283287
'$permissions' => [
284288
Permission::read(Role::any()),
285289
],
290+
'boolean' => false,
291+
'float' => 5.5,
286292
]));
287293

288294
/**
@@ -511,22 +517,34 @@ public function testJoin()
511517
'__users',
512518
[
513519
Query::select('*', 'main'),
514-
Query::select('*', 'U'),
515520
Query::select('$id', 'main'),
516-
Query::select('user_id', 'U', as: 'user_id'),
521+
Query::select('user_id', 'S', as: 'we need to support this'),
522+
Query::select('float', 'S'),
523+
Query::select('boolean', 'S'),
524+
Query::select('*', 'S'),
517525
Query::join(
518526
'__sessions',
519-
'U',
527+
'S',
520528
[
521-
Query::relationEqual('', '$id', 'U', 'user_id'),
522-
//Query::equal('$id', [$session1->getId()], 'U'),
529+
Query::relationEqual('', '$id', 'S', 'user_id'),
530+
Query::greaterThan('float', 1.1, 'S'),
523531
]
524532
),
525533
]
526534
);
527535

528-
var_dump($documents);
529-
//$this->assertEquals('shmuel1', 'shmuel2');
536+
$document = end($documents);
537+
538+
// $this->assertIsFloat($document->getAttribute('float_unsigned'));
539+
// $this->assertEquals(5.55, $document->getAttribute('float_unsigned'));
540+
//
541+
// $this->assertIsBool($document->getAttribute('boolean'));
542+
// $this->assertEquals(true, $document->getAttribute('boolean'));
543+
// //$this->assertIsArray($document->getAttribute('colors'));
544+
// //$this->assertEquals(['pink', 'green', 'blue'], $document->getAttribute('colors'));
545+
//
546+
// var_dump($document);
547+
$this->assertEquals('shmuel1', 'shmuel2');
530548
}
531549

532550
public function testDeleteRelatedCollection(): void

0 commit comments

Comments
 (0)