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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
<?php
namespace Icinga\Module\Director\Data\Db;
use Icinga\Data\Db\DbQuery;
use Icinga\Data\Filter\Filter;
use Icinga\Exception\NotFoundError;
use Icinga\Exception\NotImplementedError;
use Icinga\Module\Director\Db;
use Zend_Db_Expr as ZfDbExpr;
use Zend_Db_Select as ZfDbSelect;
class IcingaObjectQuery
{
const BASE_ALIAS = 'o';
/** @var Db */
protected $connection;
/** @var string */
protected $type;
/** @var \Zend_Db_Adapter_Abstract */
protected $db;
/** @var ZfDbSelect */
protected $query;
/** @var bool */
protected $resolved;
/** @var array joined tables, alias => table */
protected $requiredTables;
/** @var array maps table aliases, alias => table*/
protected $aliases;
/** @var DbQuery */
protected $dummyQuery;
/** @var array varname => alias */
protected $joinedVars = array();
protected $customVarTable;
protected $baseQuery;
/**
* IcingaObjectQuery constructor.
*
* @param string $type
* @param Db $connection
* @param bool $resolved
*/
public function __construct($type, Db $connection, $resolved = true)
{
$this->type = $type;
$this->connection = $connection;
$this->db = $connection->getDbAdapter();
$this->resolved = $resolved;
$baseTable = 'icinga_' . $type;
$this->baseQuery = $this->db->select()
->from(
array(self::BASE_ALIAS => $baseTable),
array('name' => 'object_name')
)->order(self::BASE_ALIAS . '.object_name');
}
public function joinVar($name)
{
if (! $this->hasJoinedVar($name)) {
$type = $this->type;
$alias = $this->safeVarAlias($name);
$varAlias = "v_$alias";
// TODO: optionally $varRelation = sprintf('icinga_%s_resolved_var', $type);
$varRelation = sprintf('icinga_%s_var', $type);
$idCol = sprintf('%s.%s_id', $alias, $type);
$joinOn = sprintf('%s = %s.id', $idCol, self::BASE_ALIAS);
$joinVarOn = $this->db->quoteInto(
sprintf('%s.checksum = %s.checksum AND %s.varname = ?', $alias, $varAlias, $alias),
$name
);
$this->baseQuery->join(
array($alias => $varRelation),
$joinOn,
array()
)->join(
array($varAlias => 'icinga_var'),
$joinVarOn,
array($alias => $varAlias . '.varvalue')
);
$this->joinedVars[$name] = $varAlias . '.varvalue';
}
return $this;
}
// Debug only
public function getSql()
{
return (string) $this->baseQuery;
}
public function listNames()
{
return $this->db->fetchCol(
$this->baseQuery
);
}
protected function hasJoinedVar($name)
{
return array_key_exists($name, $this->joinedVars);
}
public function getJoinedVarAlias($name)
{
return $this->joinedVars[$name];
}
// TODO: recheck this
protected function safeVarAlias($name)
{
$alias = preg_replace('/[^a-zA-Z0-9_]/', '', (string) $name);
$cnt = 1;
$checkAlias = $alias;
while (in_array($checkAlias, $this->joinedVars)) {
$cnt++;
$checkAlias = $alias . '_' . $cnt;
}
return $checkAlias;
}
public function escapedWhere($where)
{
$this->baseQuery->where(new ZfDbExpr($where));
}
/**
* @param $column
* @return string
* @throws NotFoundError
* @throws NotImplementedError
*/
public function getAliasForRequiredFilterColumn($column)
{
list($key, $sub) = $this->splitFilterKey($column);
if ($sub === null) {
return $key;
} else {
$objectType = $key;
}
if ($objectType === $this->type) {
list($key, $sub) = $this->splitFilterKey($sub);
if ($sub === null) {
return $key;
}
if ($key === 'vars') {
return $this->joinVar($sub)->getJoinedVarAlias($sub);
} else {
throw new NotFoundError('Not yet, my type: %s - %s', $objectType, $key);
}
} else {
throw new NotImplementedError('Not yet: %s - %s', $objectType, $sub);
}
}
protected function splitFilterKey($key)
{
$dot = strpos($key, '.');
if ($dot === false) {
return [$key, null];
} else {
return [substr($key, 0, $dot), substr($key, $dot + 1)];
}
}
protected function requireTable($name)
{
if ($alias = $this->getTableAliasFromQuery($name)) {
return $alias;
}
$this->joinTable($name);
}
protected function joinTable($name)
{
if (!array_key_exists($name, $this->requiredTables)) {
$alias = $this->makeAlias($name);
}
return $this->tableAliases($name);
}
protected function hasAlias($name)
{
return array_key_exists($name, $this->aliases);
}
protected function makeAlias($name)
{
if (substr($name, 0, 7) === 'icinga_') {
$shortName = substr($name, 7);
} else {
$shortName = $name;
}
$parts = preg_split('/_/', $shortName, -1);
$alias = '';
foreach ($parts as $part) {
$alias .= $part[0];
if (! $this->hasAlias($alias)) {
return $alias;
}
}
$cnt = 1;
do {
$cnt++;
if (! $this->hasAlias($alias . $cnt)) {
return $alias . $cnt;
}
} while (! $this->hasAlias($alias));
return $alias;
}
protected function getTableAliasFromQuery($table)
{
$tables = $this->query->getPart('from');
$key = array_search($table, $tables);
if ($key === null || $key === false) {
return false;
}
/*
'joinType' => $type,
'schema' => $schema,
'tableName' => $tableName,
'joinCondition' => $cond
*/
return $key;
}
public function whereToSql($col, $sign, $expression)
{
return $this->connection->renderFilter(Filter::expression($col, $sign, $expression));
}
}
|