Skip to content

Commit be406f4

Browse files
committed
Fix native binary for PostgreSQL
1 parent ebfe694 commit be406f4

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/Persistence/Sql/BinaryTypeCompatibilityTypecastTrait.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ private function binaryTypeValueDecode(string $value): string
4747

4848
private function binaryTypeIsEncodeNeeded(Type $type): bool
4949
{
50-
// TODO PostgreSQL tests fail without binary compatibility typecast
50+
// values for PostgreSQL are stored natively, but we need to encode first
51+
// to hold the binary type info for PDO parameter type binding
52+
5153
$platform = $this->getDatabasePlatform();
5254
if ($platform instanceof PostgreSQL94Platform
5355
|| $platform instanceof SQLServer2012Platform
@@ -76,7 +78,10 @@ public function typecastLoadField(Field $field, $value)
7678
$value = parent::typecastLoadField($field, $value);
7779

7880
if ($value !== null && $this->binaryTypeIsEncodeNeeded($field->getTypeObject())) {
79-
$value = $this->binaryTypeValueDecode($value);
81+
if (!$this->getDatabasePlatform() instanceof PostgreSQL94Platform
82+
|| $this->binaryTypeValueIsEncoded($value)) {
83+
$value = $this->binaryTypeValueDecode($value);
84+
}
8085
}
8186

8287
return $value;

src/Persistence/Sql/Expression.php

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Atk4\Data\Persistence\Sql;
66

77
use Atk4\Core\WarnDynamicPropertyTrait;
8+
use Atk4\Data\Persistence\Sql as SqlPersistence;
89
use Doctrine\DBAL\Connection as DbalConnection;
910
use Doctrine\DBAL\Exception as DbalException;
1011
use Doctrine\DBAL\ParameterType;
@@ -528,6 +529,14 @@ public function execute(object $connection = null): object
528529
$type = ParameterType::STRING;
529530
} elseif (is_string($val)) {
530531
$type = ParameterType::STRING;
532+
533+
if ($this->connection->getDatabasePlatform() instanceof PostgreSQL94Platform) {
534+
$dummySqlPersistence = new SqlPersistence($this->connection);
535+
if (\Closure::bind(fn () => $dummySqlPersistence->binaryTypeValueIsEncoded($val), null, SqlPersistence::class)()) {
536+
$val = \Closure::bind(fn () => $dummySqlPersistence->binaryTypeValueDecode($val), null, SqlPersistence::class)();
537+
$type = ParameterType::BINARY;
538+
}
539+
}
531540
} elseif (is_resource($val)) {
532541
throw new Exception('Resource type is not supported, set value as string instead');
533542
} else {

0 commit comments

Comments
 (0)