From 997517dc8d10dc6fbd1e59c63f1c9c2d0031908f Mon Sep 17 00:00:00 2001 From: Yonas Habteab Date: Fri, 20 May 2022 12:31:46 +0200 Subject: [PATCH] Database: Transform assembled binary values to hex format for pgsql --- library/Icinga/Common/Database.php | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/library/Icinga/Common/Database.php b/library/Icinga/Common/Database.php index 4c977653c..e39acfa0f 100644 --- a/library/Icinga/Common/Database.php +++ b/library/Icinga/Common/Database.php @@ -5,8 +5,12 @@ namespace Icinga\Common; use Icinga\Application\Config as IcingaConfig; use Icinga\Data\ResourceFactory; +use Icinga\Util\DBUtils; +use ipl\Sql\Adapter\Pgsql; use ipl\Sql\Config as SqlConfig; use ipl\Sql\Connection; +use ipl\Sql\Insert; +use ipl\Sql\QueryBuilder; use LogicException; use PDO; @@ -41,7 +45,29 @@ trait Database . ",NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION'"; } - return new Connection($config); + $conn = new Connection($config); + if ($conn->getAdapter() instanceof Pgsql) { + $valuesTransformer = function (&$values) { + DBUtils::transformValues($values); + }; + + $conn->getQueryBuilder() + ->on(QueryBuilder::ON_DELETE_ASSEMBLED, $valuesTransformer) + ->on(QueryBuilder::ON_UPDATE_ASSEMBLED, $valuesTransformer) + ->on(QueryBuilder::ON_ASSEMBLE_INSERT, function (Insert $insert) { + $values = $insert->getValues(); + foreach ($insert->getValues() as $key => $value) { + if (DBUtils::isBinary($value)) { + $values[$key] = DBUtils::getBinaryExpr($value); + } + } + + $insert->values(array_combine($insert->getColumns(), $values)); + }); + + } + + return $conn; } /**