diff --git a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php index fda2f188e..9371399bc 100644 --- a/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php +++ b/modules/monitoring/library/Monitoring/Backend/Ido/Query/IdoQuery.php @@ -86,6 +86,18 @@ abstract class IdoQuery extends DbQuery */ protected $customVars = array(); + /** + * Printf compatible string to joins custom vars + * + * - %1$s Source field, contain the object_id + * - %2$s Alias used for the relation + * - %3$s Name of the CustomVariable + * + * @var string + */ + private $customVarsJoinTemplate = + '%1$s = %2$s.object_id AND %2$s.varname = %3$s COLLATE latin1_general_ci'; + /** * An array with all 'virtual' tables that are already joined * @@ -351,6 +363,8 @@ abstract class IdoQuery extends DbQuery $this->object_id = $this->host_id = $this->service_id = $this->hostgroup_id = $this->servicegroup_id = $this->contact_id = $this->contactgroup_id = 'id'; + $this->customVarsJoinTemplate = + '%1$s = %2$s.object_id AND LOWER(%2$s.varname) = %3$s'; foreach ($this->columnMap as &$columns) { foreach ($columns as &$value) { $value = preg_replace('/UNIX_TIMESTAMP/', 'localts2unixts', $value); @@ -364,6 +378,8 @@ abstract class IdoQuery extends DbQuery */ private function initializeForPostgres() { + $this->customVarsJoinTemplate = + '%1$s = %2$s.object_id AND LOWER(%2$s.varname) = %3$s'; foreach ($this->columnMap as $table => & $columns) { foreach ($columns as $key => & $value) { $value = preg_replace('/ COLLATE .+$/', '', $value, -1, $count); @@ -393,14 +409,13 @@ abstract class IdoQuery extends DbQuery { parent::init(); $this->prefix = $this->ds->getTablePrefix(); - - if ($this->ds->getDbType() === 'oracle') { + $dbType = $this->ds->getDbType(); + if ($dbType === 'oracle') { $this->initializeForOracle(); - } elseif ($this->ds->getDbType() === 'pgsql') { + } elseif ($dbType === 'pgsql') { $this->initializeForPostgres(); } $this->dbSelect(); - $this->select->columns($this->columns); //$this->joinBaseTables(); $this->prepareAliasIndexes(); @@ -604,14 +619,14 @@ abstract class IdoQuery extends DbQuery protected function hasCustomvar($customvar) { - return array_key_exists($customvar, $this->customVars); + return array_key_exists(strtolower($customvar), $this->customVars); } protected function joinCustomvar($customvar) { // TODO: This is not generic enough yet list($type, $name) = $this->customvarNameToTypeName($customvar); - $alias = ($type === 'host' ? 'hcv_' : 'scv_') . strtolower($name); + $alias = ($type === 'host' ? 'hcv_' : 'scv_') . $name; $this->customVars[$customvar] = $alias; @@ -622,12 +637,12 @@ abstract class IdoQuery extends DbQuery } else { $leftcol = 'h.' . $type . '_object_id'; } + $joinOn = sprintf( - '%s = %s.object_id AND %s.varname = %s', + $this->customVarsJoinTemplate, $leftcol, $alias, - $alias, - $this->db->quote(strtoupper($name)) + $this->db->quote($name) ); $this->select->joinLeft( @@ -641,6 +656,7 @@ abstract class IdoQuery extends DbQuery protected function customvarNameToTypeName($customvar) { + $customvar = strtolower($customvar); // TODO: Improve this: if (! preg_match('~^_(host|service)_([a-zA-Z0-9_]+)$~', $customvar, $m)) { throw new ProgrammingError( @@ -658,7 +674,7 @@ abstract class IdoQuery extends DbQuery protected function getCustomvarColumnName($customvar) { - return $this->customVars[$customvar] . '.varvalue'; + return $this->customVars[strtolower($customvar)] . '.varvalue'; } public function aliasToColumnName($alias)