Merge branch 'bugfix/customvars-case-insensitive-8696'

fixes #8696
This commit is contained in:
Marius Hein 2015-05-21 16:51:31 +02:00
commit dd654980b7
1 changed files with 26 additions and 10 deletions

View File

@ -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)