mirror of
https://gitee.com/hyperf/hyperf.git
synced 2024-12-01 19:27:39 +08:00
Added force index for hyperf/database
. (#4260)
This commit is contained in:
parent
ee98ef979a
commit
f7cc4cc027
@ -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
|
||||
|
@ -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.
|
||||
*
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user