Re-add missing support for index hints in XML query

This commit is contained in:
Kijin Sung 2020-12-15 22:36:55 +09:00
parent 2a5d99b2df
commit a908cd9291
3 changed files with 82 additions and 0 deletions

View file

@ -13,6 +13,7 @@ class Query extends VariableBase
public $name;
public $type;
public $tables = array();
public $index_hints = array();
public $columns = array();
public $conditions = array();
public $groupby = null;
@ -167,6 +168,14 @@ class Query extends VariableBase
$result .= ' FROM ' . $tables;
}
}
if (count($this->index_hints))
{
$index_hints = $this->_arrangeIndexHints($this->index_hints);
if ($index_hints !== '')
{
$result .= ' ' . $index_hints;
}
}
// Compose the WHERE clause.
if (count($this->conditions))
@ -418,6 +427,37 @@ class Query extends VariableBase
return implode('', $result);
}
/**
* Generate index hints.
*
* @param array $index_hints
* @return string
*/
protected function _arrangeIndexHints(array $index_hints): string
{
// Initialize the index list by type.
$index_list = [];
// Group each index hint by type.
foreach ($index_hints as $index_hint)
{
if (!count($index_hint->target_db) || isset($index_hint->target_db['mysql']))
{
$key = $index_hint->hint_type ?: 'USE';
$index_list[$key] = $index_list[$key] ?? [];
$index_list[$key][] = $index_hint->index_name;
}
}
// Generate a list of indexes for each group.
$result = [];
foreach ($index_list as $key => $val)
{
$result[] = sprintf('%s INDEX (%s)', $key, implode(', ', $val));
}
return implode(' ', $result);
}
/**
* Generate a WHERE clause from a list of conditions.
*