2017-07-21 12:05:47 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Icinga\Module\Director\Web\Table;
|
|
|
|
|
2017-08-16 17:33:02 +02:00
|
|
|
use Zend_Db_Expr as Expr;
|
|
|
|
|
2017-07-21 12:05:47 +02:00
|
|
|
trait DbHelper
|
|
|
|
{
|
|
|
|
public function dbHexFunc($column)
|
|
|
|
{
|
|
|
|
if ($this->isPgsql()) {
|
|
|
|
return sprintf("LOWER(ENCODE(%s, 'hex'))", $column);
|
|
|
|
} else {
|
|
|
|
return sprintf("LOWER(HEX(%s))", $column);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function quoteBinary($binary)
|
|
|
|
{
|
2022-07-01 08:39:12 +02:00
|
|
|
if ($binary === '') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_array($binary)) {
|
|
|
|
return array_map([$this, 'quoteBinary'], $binary);
|
|
|
|
}
|
|
|
|
|
2017-07-21 12:05:47 +02:00
|
|
|
if ($this->isPgsql()) {
|
|
|
|
return new Expr("'\\x" . bin2hex($binary) . "'");
|
|
|
|
}
|
|
|
|
|
2022-07-01 08:39:12 +02:00
|
|
|
return new Expr('0x' . bin2hex($binary));
|
2017-07-21 12:05:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isPgsql()
|
|
|
|
{
|
2017-08-16 17:33:02 +02:00
|
|
|
return $this->db() instanceof \Zend_Db_Adapter_Pdo_Pgsql;
|
2017-07-21 12:05:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isMysql()
|
|
|
|
{
|
2017-08-16 17:33:02 +02:00
|
|
|
return $this->db() instanceof \Zend_Db_Adapter_Pdo_Mysql;
|
2017-07-21 12:05:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function wantBinaryValue($value)
|
|
|
|
{
|
|
|
|
if (is_resource($value)) {
|
|
|
|
return stream_get_contents($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
2018-03-11 13:49:59 +01:00
|
|
|
|
|
|
|
public function getChecksum($checksum)
|
|
|
|
{
|
|
|
|
return bin2hex($this->wantBinaryValue($checksum));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getShortChecksum($checksum)
|
|
|
|
{
|
2018-09-12 12:43:01 +02:00
|
|
|
if ($checksum === null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-03-11 13:49:59 +01:00
|
|
|
return substr($this->getChecksum($checksum), 0, 7);
|
|
|
|
}
|
2017-07-21 12:05:47 +02:00
|
|
|
}
|