Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| Index | |
100.00% |
20 / 20 |
|
100.00% |
6 / 6 |
10 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| renderType | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| renderName | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| renderColumns | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| renderTypeAttributes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sql | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| 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 LogicException; |
| 15 | |
| 16 | /** |
| 17 | * Class Index. |
| 18 | * |
| 19 | * @see https://mariadb.com/kb/en/getting-started-with-indexes/ |
| 20 | * |
| 21 | * @package database |
| 22 | */ |
| 23 | abstract class Index extends DefinitionPart |
| 24 | { |
| 25 | protected Database $database; |
| 26 | /** |
| 27 | * @var array<string> |
| 28 | */ |
| 29 | protected array $columns; |
| 30 | protected string $type = ''; |
| 31 | protected ?string $name; |
| 32 | |
| 33 | public function __construct(Database $database, ?string $name, string $column, string ...$columns) |
| 34 | { |
| 35 | $this->database = $database; |
| 36 | $this->name = $name; |
| 37 | $this->columns = $columns ? \array_merge([$column], $columns) : [$column]; |
| 38 | } |
| 39 | |
| 40 | protected function renderType() : string |
| 41 | { |
| 42 | if (empty($this->type)) { |
| 43 | throw new LogicException('Key type is empty'); |
| 44 | } |
| 45 | return " {$this->type}"; |
| 46 | } |
| 47 | |
| 48 | protected function renderName() : ?string |
| 49 | { |
| 50 | if ($this->name === null) { |
| 51 | return null; |
| 52 | } |
| 53 | return ' ' . $this->database->protectIdentifier($this->name); |
| 54 | } |
| 55 | |
| 56 | protected function renderColumns() : string |
| 57 | { |
| 58 | $columns = []; |
| 59 | foreach ($this->columns as $column) { |
| 60 | $columns[] = $this->database->protectIdentifier($column); |
| 61 | } |
| 62 | $columns = \implode(', ', $columns); |
| 63 | return " ({$columns})"; |
| 64 | } |
| 65 | |
| 66 | protected function renderTypeAttributes() : ?string |
| 67 | { |
| 68 | return null; |
| 69 | } |
| 70 | |
| 71 | protected function sql() : string |
| 72 | { |
| 73 | $sql = $this->renderType(); |
| 74 | $sql .= $this->renderName(); |
| 75 | $sql .= $this->renderColumns(); |
| 76 | $sql .= $this->renderTypeAttributes(); |
| 77 | return $sql; |
| 78 | } |
| 79 | } |