Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
11 / 11 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| IndexDefinition | |
100.00% |
11 / 11 |
|
100.00% |
8 / 8 |
9 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| key | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| primaryKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| uniqueKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fulltextKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| foreignKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| spatialKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sql | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php declare(strict_types=1); |
| 2 | /* |
| 3 | * This file is part of Aplus Framework Database Library. |
| 4 | * |
| 5 | * (c) Natan Felles <natanfelles@gmail.com> |
| 6 | * |
| 7 | * For the full copyright and license information, please view the LICENSE |
| 8 | * file that was distributed with this source code. |
| 9 | */ |
| 10 | namespace Framework\Database\Definition\Table\Indexes; |
| 11 | |
| 12 | use Framework\Database\Database; |
| 13 | use Framework\Database\Definition\Table\DefinitionPart; |
| 14 | use Framework\Database\Definition\Table\Indexes\Keys\ForeignKey; |
| 15 | use Framework\Database\Definition\Table\Indexes\Keys\FulltextKey; |
| 16 | use Framework\Database\Definition\Table\Indexes\Keys\Key; |
| 17 | use Framework\Database\Definition\Table\Indexes\Keys\PrimaryKey; |
| 18 | use Framework\Database\Definition\Table\Indexes\Keys\SpatialKey; |
| 19 | use Framework\Database\Definition\Table\Indexes\Keys\UniqueKey; |
| 20 | use RuntimeException; |
| 21 | |
| 22 | /** |
| 23 | * Class IndexDefinition. |
| 24 | * |
| 25 | * @see https://mariadb.com/kb/en/create-table/#index-definitions |
| 26 | * @see https://mariadb.com/kb/en/optimization-and-indexes/ |
| 27 | * |
| 28 | * @package database |
| 29 | */ |
| 30 | class IndexDefinition extends DefinitionPart |
| 31 | { |
| 32 | protected Database $database; |
| 33 | protected ?string $name; |
| 34 | protected ?Index $index = null; |
| 35 | |
| 36 | public function __construct(Database $database, string $name = null) |
| 37 | { |
| 38 | $this->database = $database; |
| 39 | $this->name = $name; |
| 40 | } |
| 41 | |
| 42 | public function key(string $column, string ...$columns) : Key |
| 43 | { |
| 44 | return $this->index = new Key($this->database, $this->name, $column, ...$columns); |
| 45 | } |
| 46 | |
| 47 | public function primaryKey(string $column, string ...$columns) : PrimaryKey |
| 48 | { |
| 49 | return $this->index = new PrimaryKey($this->database, $this->name, $column, ...$columns); |
| 50 | } |
| 51 | |
| 52 | public function uniqueKey(string $column, string ...$columns) : UniqueKey |
| 53 | { |
| 54 | return $this->index = new UniqueKey($this->database, $this->name, $column, ...$columns); |
| 55 | } |
| 56 | |
| 57 | public function fulltextKey(string $column, string ...$columns) : FulltextKey |
| 58 | { |
| 59 | return $this->index = new FulltextKey($this->database, $this->name, $column, ...$columns); |
| 60 | } |
| 61 | |
| 62 | public function foreignKey(string $column, string ...$columns) : ForeignKey |
| 63 | { |
| 64 | return $this->index = new ForeignKey($this->database, $this->name, $column, ...$columns); |
| 65 | } |
| 66 | |
| 67 | public function spatialKey(string $column, string ...$columns) : SpatialKey |
| 68 | { |
| 69 | return $this->index = new SpatialKey($this->database, $this->name, $column, ...$columns); |
| 70 | } |
| 71 | |
| 72 | protected function sql() : string |
| 73 | { |
| 74 | if ( ! $this->index) { |
| 75 | throw new RuntimeException("Key type not set in index {$this->name}"); |
| 76 | } |
| 77 | return $this->index->sql(); |
| 78 | } |
| 79 | } |