Add ability to configure index hint with variable

See #1560
This commit is contained in:
Kijin Sung 2021-01-14 00:40:38 +09:00
parent 0c6bc8fbb1
commit 03e5909036
6 changed files with 81 additions and 4 deletions

View file

@ -11,4 +11,5 @@ class IndexHint
public $hint_type = '';
public $index_name = '';
public $table_name = '';
public $var;
}

View file

@ -469,7 +469,14 @@ class Query extends VariableBase
{
$key = $index_hint->hint_type ?: 'USE';
$index_list[$key] = $index_list[$key] ?? [];
$index_list[$key][] = $index_hint->index_name;
if ($index_hint->var && isset($this->_args[$index_hint->var]))
{
$index_list[$key][] = self::quoteName($this->_args[$index_hint->var]);
}
elseif ($index_hint->index_name)
{
$index_list[$key][] = self::quoteName($index_hint->index_name);
}
}
}
@ -477,7 +484,10 @@ class Query extends VariableBase
$result = [];
foreach ($index_list as $key => $val)
{
$result[] = sprintf('%s INDEX (%s)', $key, implode(', ', $val));
if (count($val))
{
$result[] = sprintf('%s INDEX (%s)', $key, implode(', ', $val));
}
}
return implode(' ', $result);
}

View file

@ -101,7 +101,15 @@ class DBQueryParser extends BaseParser
$index_hint->hint_type = strtoupper(trim($tag['type'])) ?: 'USE';
$index_hint->index_name = trim($tag['name']) ?: '';
$index_hint->table_name = trim($tag['table']) ?: '';
if ($index_hint->index_name)
if (isset($tag['var']) && trim($tag['var']))
{
$index_hint->var = trim($tag['var']);
}
if (isset($tag['default']) && trim($tag['default']))
{
$index_hint->index_name = trim($tag['default']);
}
if ($index_hint->index_name || $index_hint->var)
{
$query->index_hints[] = $index_hint;
}