Skip to content

Commit

Permalink
move typecasting from binary comp. trait for better BT
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jul 5, 2022
1 parent 9230213 commit 4e2c0fe
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
22 changes: 22 additions & 0 deletions src/Persistence/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,28 @@ protected function deleteRaw(Model $model, $idRaw): void
$model->hook(self::HOOK_AFTER_DELETE_QUERY, [$delete, $c]);
}

public function typecastSaveField(Field $field, $value)
{
$value = parent::typecastSaveField($field, $value);

if ($value !== null && $this->binaryTypeIsEncodeNeeded($field->getTypeObject())) {
$value = $this->binaryTypeValueEncode($value);
}

return $value;
}

public function typecastLoadField(Field $field, $value)
{
$value = parent::typecastLoadField($field, $value);

if ($value !== null && $this->binaryTypeIsDecodeNeeded($field->getTypeObject(), $value)) {
$value = $this->binaryTypeValueDecode($value);
}

return $value;
}

public function getFieldSqlExpression(Field $field, Expression $expression): Expression
{
if (isset($field->getOwner()->persistence_data['use_table_prefixes'])) {
Expand Down
25 changes: 7 additions & 18 deletions src/Persistence/Sql/BinaryTypeCompatibilityTypecastTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Atk4\Data\Persistence\Sql;

use Atk4\Data\Exception;
use Atk4\Data\Field;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
Expand Down Expand Up @@ -67,29 +66,19 @@ private function binaryTypeIsEncodeNeeded(Type $type): bool
return false;
}

public function typecastSaveField(Field $field, $value)
/**
* @param scalar $value
*/
private function binaryTypeIsDecodeNeeded(Type $type, $value): bool
{
$value = parent::typecastSaveField($field, $value);

if ($value !== null && $this->binaryTypeIsEncodeNeeded($field->getTypeObject())) {
$value = $this->binaryTypeValueEncode($value);
}

return $value;
}

public function typecastLoadField(Field $field, $value)
{
$value = parent::typecastLoadField($field, $value);

if ($value !== null && $this->binaryTypeIsEncodeNeeded($field->getTypeObject())) {
if ($this->binaryTypeIsEncodeNeeded($type)) {
// always decode for Oracle platform to assert the value is always encoded,
// on other platforms, binary values are stored natively
if ($this->getDatabasePlatform() instanceof OraclePlatform || $this->binaryTypeValueIsEncoded($value)) {
$value = $this->binaryTypeValueDecode($value);
return true;
}
}

return $value;
return false;
}
}

0 comments on commit 4e2c0fe

Please sign in to comment.