Added force index for hyperf/database. (#4260)

This commit is contained in:
李铭昕 2021-11-16 12:03:12 +08:00 committed by GitHub
parent ee98ef979a
commit f7cc4cc027
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 0 deletions

View File

@ -1,5 +1,9 @@
# v2.2.17 - TBD
## Added
- [#4260](https://github.com/hyperf/hyperf/pull/4260) Added force index for `hyperf/database`.
# v2.2.16 - 2021-11-15
## Added

View File

@ -103,6 +103,13 @@ class Builder
*/
public $from;
/**
* The force indexes.
*
* @var string[]
*/
public $forceIndexes = [];
/**
* The table joins for the query.
*
@ -383,6 +390,17 @@ class Builder
return $this;
}
/**
* Set the force indexes which the query should be used.
*
* @return $this
*/
public function forceIndexes(array $forceIndexes)
{
$this->forceIndexes = $forceIndexes;
return $this;
}
/**
* Add a join clause to the query.
*

View File

@ -396,6 +396,14 @@ class Grammar extends BaseGrammar
*/
protected function compileFrom(Builder $query, $table)
{
if ($query->forceIndexes) {
$forceIndexes = [];
foreach ($query->forceIndexes as $forceIndex) {
$forceIndexes[] = $this->wrapValue($forceIndex);
}
return 'from ' . $this->wrapTable($table) . ' force index (' . implode(',', $forceIndexes) . ')';
}
return 'from ' . $this->wrapTable($table);
}

View File

@ -126,6 +126,24 @@ class ModelRealBuilderTest extends TestCase
}
}
public function testForceIndexes()
{
$this->getContainer();
User::query()->get();
User::query()->forceIndexes(['PRIMARY'])->where('id', '>', 1)->get();
$sqls = [
['select * from `user`', []],
['select * from `user` force index (`PRIMARY`) where `id` > ?', [1]],
];
while ($event = $this->channel->pop(0.001)) {
if ($event instanceof QueryExecuted) {
$this->assertSame([$event->sql, $event->bindings], array_shift($sqls));
}
}
}
public function testIncrement()
{
$this->getContainer();