From f09ff81e94bc7696482fc19b52ef7ab5eca20c3c Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 8 Jul 2025 09:47:20 +0300 Subject: [PATCH 1/8] Remove old logic --- src/Database/Adapter/Postgres.php | 15 +++------------ src/Database/Database.php | 16 +++++++++++++++- tests/e2e/Adapter/Scopes/DocumentTests.php | 15 ++++++++++++++- 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/Database/Adapter/Postgres.php b/src/Database/Adapter/Postgres.php index aad33c42e..22f888d6d 100644 --- a/src/Database/Adapter/Postgres.php +++ b/src/Database/Adapter/Postgres.php @@ -1504,11 +1504,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25, try { $stmt = $this->getPDO()->prepare($sql); foreach ($binds as $key => $value) { - if ($key === ":sequence") { - $stmt->bindValue($key, $value, PDO::PARAM_INT); - } else { - $stmt->bindValue($key, $value, $this->getPDOType($value)); - } + $stmt->bindValue($key, $value, $this->getPDOType($value)); } $this->execute($stmt); @@ -1768,14 +1764,9 @@ protected function getSQLCondition(Query $query, array &$binds): string Query::TYPE_CONTAINS => $query->onArray() ? \json_encode($value) : '%' . $this->escapeWildcards($value) . '%', default => $value }; - if ($attribute === $this->quote("_id")) { - $binds[":sequence"] = $value; - $conditions[] = "{$alias}.{$attribute} {$operator} :sequence"; - } else { - $binds[":{$placeholder}_{$key}"] = $value; - $conditions[] = "{$alias}.{$attribute} {$operator} :{$placeholder}_{$key}"; - } + $binds[":{$placeholder}_{$key}"] = $value; + $conditions[] = "{$alias}.{$attribute} {$operator} :{$placeholder}_{$key}"; } return empty($conditions) ? '' : '(' . implode(' OR ', $conditions) . ')'; diff --git a/src/Database/Database.php b/src/Database/Database.php index 0658065cc..7c6e72680 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -6673,8 +6673,22 @@ public static function convertQueries(Document $collection, array $queries): arr } foreach ($attributes as $attribute) { - foreach ($queries as $query) { + foreach ($queries as $index => $query) { if ($query->getAttribute() === $attribute->getId()) { + if($query->getAttribute() === '$sequence'){ + /** + * Hack for Postgres, since bindParam does not convert '' on int attribute + */ + $values = $query->getValues(); + foreach ($values as $valueIndex => $value) { + if($value === ''){ + $values[$valueIndex] = '0'; + } + } + $query->setValues($values); + $queries[$index] = $query; + } + $query->setOnArray($attribute->getAttribute('array', false)); } } diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 50bbcba57..06533f9ef 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -1778,8 +1778,21 @@ public function testFindByInternalID(array $data): void $documents = $database->find('movies', [ Query::equal('$sequence', [$data['$sequence']]), ]); - $this->assertEquals(1, count($documents)); + + /** + * Test getSequence returns '' Postgres bindParam issue on empty string + * Hack in convertQueries to change $sequence empty string from '' => '0' + * We should not use this fix and use structure validations, or change using not querying on empty string + */ + $empty = new Document(); + + /** + * Check no exceptions are thrown as a temporary fix + */ + $database->find('movies', [ + Query::equal('$sequence', [$empty->getSequence()]), + ]); } public function testFindOrderBy(): void From 947ac5367766e39d611613a1c0a66faf2636574b Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 8 Jul 2025 10:11:34 +0300 Subject: [PATCH 2/8] Comment option 2 --- src/Database/Database.php | 26 +++++++++++----------- src/Database/Document.php | 7 +++++- tests/e2e/Adapter/Scopes/DocumentTests.php | 3 ++- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 7c6e72680..77058ae7c 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -6675,19 +6675,19 @@ public static function convertQueries(Document $collection, array $queries): arr foreach ($attributes as $attribute) { foreach ($queries as $index => $query) { if ($query->getAttribute() === $attribute->getId()) { - if($query->getAttribute() === '$sequence'){ - /** - * Hack for Postgres, since bindParam does not convert '' on int attribute - */ - $values = $query->getValues(); - foreach ($values as $valueIndex => $value) { - if($value === ''){ - $values[$valueIndex] = '0'; - } - } - $query->setValues($values); - $queries[$index] = $query; - } +// if($query->getAttribute() === '$sequence'){ +// /** +// * Hack for Postgres, since bindParam does not convert '' on int attribute +// */ +// $values = $query->getValues(); +// foreach ($values as $valueIndex => $value) { +// if($value === ''){ +// $values[$valueIndex] = '0'; +// } +// } +// $query->setValues($values); +// $queries[$index] = $query; +// } $query->setOnArray($attribute->getAttribute('array', false)); } diff --git a/src/Database/Document.php b/src/Database/Document.php index 2ed634f46..b75432d26 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -66,7 +66,12 @@ public function getId(): string */ public function getSequence(): string { - return $this->getAttribute('$sequence', ''); + /** + * This can be a better fix ? + * Since we always check logic by empty($document->getSequence() + */ + return $this->getAttribute('$sequence', '0'); + //return $this->getAttribute('$sequence', ''); } /** diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 06533f9ef..51e1315c8 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -1786,9 +1786,10 @@ public function testFindByInternalID(array $data): void * We should not use this fix and use structure validations, or change using not querying on empty string */ $empty = new Document(); + $this->assertEquals('0', $empty->getSequence()); /** - * Check no exceptions are thrown as a temporary fix + * Check no exceptions is thrown */ $database->find('movies', [ Query::equal('$sequence', [$empty->getSequence()]), From 1f1a97255d9ebffb4ca42f8bc430521575b3c04f Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 8 Jul 2025 17:08:14 +0300 Subject: [PATCH 3/8] formatting --- src/Database/Database.php | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 77058ae7c..265d05492 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -6675,19 +6675,19 @@ public static function convertQueries(Document $collection, array $queries): arr foreach ($attributes as $attribute) { foreach ($queries as $index => $query) { if ($query->getAttribute() === $attribute->getId()) { -// if($query->getAttribute() === '$sequence'){ -// /** -// * Hack for Postgres, since bindParam does not convert '' on int attribute -// */ -// $values = $query->getValues(); -// foreach ($values as $valueIndex => $value) { -// if($value === ''){ -// $values[$valueIndex] = '0'; -// } -// } -// $query->setValues($values); -// $queries[$index] = $query; -// } + // if($query->getAttribute() === '$sequence'){ + // /** + // * Hack for Postgres, since bindParam does not convert '' on int attribute + // */ + // $values = $query->getValues(); + // foreach ($values as $valueIndex => $value) { + // if($value === ''){ + // $values[$valueIndex] = '0'; + // } + // } + // $query->setValues($values); + // $queries[$index] = $query; + // } $query->setOnArray($attribute->getAttribute('array', false)); } From 749cb0606a88ad4282b182feaa7e380627126b86 Mon Sep 17 00:00:00 2001 From: fogelito Date: Tue, 29 Jul 2025 08:33:45 +0300 Subject: [PATCH 4/8] Set test --- src/Database/Database.php | 36 +++++++++++++++++++++++------------- src/Database/Document.php | 5 ----- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 6698aa583..2b960a172 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -6665,20 +6665,30 @@ public function convertQueries(Document $collection, array $queries): array foreach ($attributes as $attribute) { foreach ($queries as $index => $query) { if ($query->getAttribute() === $attribute->getId()) { - // if($query->getAttribute() === '$sequence'){ - // /** - // * Hack for Postgres, since bindParam does not convert '' on int attribute - // */ - // $values = $query->getValues(); - // foreach ($values as $valueIndex => $value) { - // if($value === ''){ - // $values[$valueIndex] = '0'; - // } - // } - // $query->setValues($values); - // $queries[$index] = $query; - // } + /** + * Hack test - Remove this: + */ + + if($query->getAttribute() === '$sequence'){ + /** + * Hack for Postgres, since bindParam does not convert '' on int attribute + */ + $values = $query->getValues(); + foreach ($values as $valueIndex => $value) { +// if($value === ''){ +// $values[$valueIndex] = '0'; +// } + if(empty($value)){ + throw new DatabaseException\Structure('Shmuel tests'); + } + } + $query->setValues($values); + $queries[$index] = $query; + } + /** + * End test + */ $query->setOnArray($attribute->getAttribute('array', false)); } } diff --git a/src/Database/Document.php b/src/Database/Document.php index 5b2d3e85b..d714e9c13 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -70,12 +70,7 @@ public function getId(): string */ public function getSequence(): string { - /** - * This can be a better fix ? - * Since we always check logic by empty($document->getSequence() - */ return $this->getAttribute('$sequence', '0'); - //return $this->getAttribute('$sequence', ''); } /** From b6ac173230080081ed3056c7bbd5808b79a32c66 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 31 Jul 2025 12:55:52 +0300 Subject: [PATCH 5/8] Revert convert queries --- src/Database/Database.php | 26 +------------------------- 1 file changed, 1 insertion(+), 25 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 2b960a172..cc1336625 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -6663,32 +6663,8 @@ public function convertQueries(Document $collection, array $queries): array } foreach ($attributes as $attribute) { - foreach ($queries as $index => $query) { + foreach ($queries as $query) { if ($query->getAttribute() === $attribute->getId()) { - /** - * Hack test - Remove this: - */ - - if($query->getAttribute() === '$sequence'){ - /** - * Hack for Postgres, since bindParam does not convert '' on int attribute - */ - $values = $query->getValues(); - foreach ($values as $valueIndex => $value) { -// if($value === ''){ -// $values[$valueIndex] = '0'; -// } - if(empty($value)){ - throw new DatabaseException\Structure('Shmuel tests'); - } - } - $query->setValues($values); - $queries[$index] = $query; - } - - /** - * End test - */ $query->setOnArray($attribute->getAttribute('array', false)); } } From 53f4ffd00fee1a4a8198f5c4272b689090a7cb46 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 31 Jul 2025 13:03:29 +0300 Subject: [PATCH 6/8] Remove comment --- tests/e2e/Adapter/Scopes/DocumentTests.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 0b98a909f..3c3ba12e2 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -1845,11 +1845,6 @@ public function testFindByInternalID(array $data): void ]); $this->assertEquals(1, count($documents)); - /** - * Test getSequence returns '' Postgres bindParam issue on empty string - * Hack in convertQueries to change $sequence empty string from '' => '0' - * We should not use this fix and use structure validations, or change using not querying on empty string - */ $empty = new Document(); $this->assertEquals('0', $empty->getSequence()); From 26a4dd7491f9bc3d8c2e5514508a0eb5a5e45fe3 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 31 Jul 2025 14:02:09 +0300 Subject: [PATCH 7/8] Fix tests --- src/Database/Document.php | 2 +- tests/e2e/Adapter/Scopes/DocumentTests.php | 9 +-------- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/src/Database/Document.php b/src/Database/Document.php index d714e9c13..6b82d73cf 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -70,7 +70,7 @@ public function getId(): string */ public function getSequence(): string { - return $this->getAttribute('$sequence', '0'); + return $this->getAttribute('$sequence', ''); } /** diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 3c3ba12e2..99a5de152 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -1846,14 +1846,7 @@ public function testFindByInternalID(array $data): void $this->assertEquals(1, count($documents)); $empty = new Document(); - $this->assertEquals('0', $empty->getSequence()); - - /** - * Check no exceptions is thrown - */ - $database->find('movies', [ - Query::equal('$sequence', [$empty->getSequence()]), - ]); + $this->assertEquals('', $empty->getSequence()); } public function testFindOrderBy(): void From 0e2549db086ad2d670d4ec43da38f0a287ced523 Mon Sep 17 00:00:00 2001 From: fogelito Date: Thu, 31 Jul 2025 14:02:52 +0300 Subject: [PATCH 8/8] line --- tests/e2e/Adapter/Scopes/DocumentTests.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 99a5de152..72fb78d3e 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -1843,6 +1843,7 @@ public function testFindByInternalID(array $data): void $documents = $database->find('movies', [ Query::equal('$sequence', [$data['$sequence']]), ]); + $this->assertEquals(1, count($documents)); $empty = new Document();