1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
<?php
namespace Icinga\Module\Director\Db;
use gipfl\ZfDb\Adapter\Adapter;
use gipfl\ZfDb\Adapter\Pdo\Pgsql;
use gipfl\ZfDb\Expr;
use Zend_Db_Adapter_Abstract;
use Zend_Db_Adapter_Pdo_Pgsql;
use Zend_Db_Expr;
use function bin2hex;
use function is_array;
use function is_resource;
use function stream_get_contents;
class DbUtil
{
public static function binaryResult($value)
{
if (is_resource($value)) {
return stream_get_contents($value);
}
return $value;
}
/**
* @param string|array $binary
* @param Zend_Db_Adapter_Abstract $db
* @return Zend_Db_Expr|Zend_Db_Expr[]
*/
public static function quoteBinaryLegacy($binary, $db)
{
if (is_array($binary)) {
return static::quoteArray($binary, 'quoteBinaryLegacy', $db);
}
if ($binary === null) {
return null;
}
if ($db instanceof Zend_Db_Adapter_Pdo_Pgsql) {
return new Zend_Db_Expr("'\\x" . bin2hex($binary) . "'");
}
return new Zend_Db_Expr('0x' . bin2hex($binary));
}
/**
* @param string|array $binary
* @param Adapter $db
* @return Expr|Expr[]
*/
public static function quoteBinary($binary, $db)
{
if (is_array($binary)) {
return static::quoteArray($binary, 'quoteBinary', $db);
}
if ($binary === null) {
return null;
}
if ($db instanceof Pgsql) {
return new Expr("'\\x" . bin2hex($binary) . "'");
}
return new Expr('0x' . bin2hex($binary));
}
/**
* @param string|array $binary
* @param Adapter|Zend_Db_Adapter_Abstract $db
* @return Expr|Zend_Db_Expr|Expr[]|Zend_Db_Expr[]
*/
public static function quoteBinaryCompat($binary, $db)
{
if ($db instanceof Adapter) {
return static::quoteBinary($binary, $db);
}
return static::quoteBinaryLegacy($binary, $db);
}
protected static function quoteArray($array, $method, $db)
{
$result = [];
foreach ($array as $bin) {
$quoted = static::$method($bin, $db);
$result[] = $quoted;
}
return $result;
}
}
|