Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
67 / 67 |
|
100.00% |
22 / 22 |
CRAP | |
100.00% |
1 / 1 |
| Column | |
100.00% |
67 / 67 |
|
100.00% |
22 / 22 |
34 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| renderType | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| renderLength | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| check | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| renderCheck | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| null | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| notNull | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| renderNull | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| default | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| renderDefault | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| comment | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| renderComment | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| primaryKey | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| renderPrimaryKey | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| uniqueKey | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| renderUniqueKey | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| first | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| renderFirst | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| after | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| renderAfter | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| renderTypeAttributes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| sql | |
100.00% |
12 / 12 |
|
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\Columns; |
| 11 | |
| 12 | use Closure; |
| 13 | use Framework\Database\Database; |
| 14 | use Framework\Database\Definition\Table\DefinitionPart; |
| 15 | use LogicException; |
| 16 | |
| 17 | /** |
| 18 | * Class Column. |
| 19 | * |
| 20 | * @package database |
| 21 | */ |
| 22 | abstract class Column extends DefinitionPart |
| 23 | { |
| 24 | protected Database $database; |
| 25 | protected string $type; |
| 26 | /** |
| 27 | * @var array<scalar|null> |
| 28 | */ |
| 29 | protected array $length; |
| 30 | protected bool $null = false; |
| 31 | protected bool $uniqueKey = false; |
| 32 | protected bool $primaryKey = false; |
| 33 | protected bool | Closure | float | int | string | null $default; |
| 34 | protected Closure $check; |
| 35 | protected ?string $comment; |
| 36 | protected bool $first = false; |
| 37 | protected ?string $after; |
| 38 | |
| 39 | /** |
| 40 | * Column constructor. |
| 41 | * |
| 42 | * @param Database $database |
| 43 | * @param bool|float|int|string|null ...$length |
| 44 | */ |
| 45 | public function __construct(Database $database, bool | float | int | string | null ...$length) |
| 46 | { |
| 47 | $this->database = $database; |
| 48 | $this->length = $length; |
| 49 | } |
| 50 | |
| 51 | protected function renderType() : string |
| 52 | { |
| 53 | if (empty($this->type)) { |
| 54 | throw new LogicException('Column type is empty'); |
| 55 | } |
| 56 | return ' ' . $this->type; |
| 57 | } |
| 58 | |
| 59 | protected function renderLength() : ?string |
| 60 | { |
| 61 | if ( ! isset($this->length[0])) { |
| 62 | return null; |
| 63 | } |
| 64 | $length = $this->database->quote($this->length[0]); |
| 65 | return "({$length})"; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * @param Closure $expression |
| 70 | * |
| 71 | * @return static |
| 72 | */ |
| 73 | public function check(Closure $expression) : static |
| 74 | { |
| 75 | $this->check = $expression; |
| 76 | return $this; |
| 77 | } |
| 78 | |
| 79 | protected function renderCheck() : ?string |
| 80 | { |
| 81 | if ( ! isset($this->check)) { |
| 82 | return null; |
| 83 | } |
| 84 | return ' CHECK (' . ($this->check)($this->database) . ')'; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @return static |
| 89 | */ |
| 90 | public function null() : static |
| 91 | { |
| 92 | $this->null = true; |
| 93 | return $this; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @return static |
| 98 | */ |
| 99 | public function notNull() : static |
| 100 | { |
| 101 | $this->null = false; |
| 102 | return $this; |
| 103 | } |
| 104 | |
| 105 | protected function renderNull() : ?string |
| 106 | { |
| 107 | return $this->null ? ' NULL' : ' NOT NULL'; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * @param bool|Closure|float|int|string|null $default |
| 112 | * |
| 113 | * @return static |
| 114 | */ |
| 115 | public function default(bool | Closure | float | int | string | null $default) : static |
| 116 | { |
| 117 | $this->default = $default; |
| 118 | return $this; |
| 119 | } |
| 120 | |
| 121 | protected function renderDefault() : ?string |
| 122 | { |
| 123 | if ( ! isset($this->default)) { |
| 124 | return null; |
| 125 | } |
| 126 | $default = $this->default instanceof Closure |
| 127 | ? '(' . ($this->default)($this->database) . ')' |
| 128 | : $this->database->quote($this->default); |
| 129 | return ' DEFAULT ' . $default; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @param string $comment |
| 134 | * |
| 135 | * @return static |
| 136 | */ |
| 137 | public function comment(string $comment) : static |
| 138 | { |
| 139 | $this->comment = $comment; |
| 140 | return $this; |
| 141 | } |
| 142 | |
| 143 | protected function renderComment() : ?string |
| 144 | { |
| 145 | if ( ! isset($this->comment)) { |
| 146 | return null; |
| 147 | } |
| 148 | return ' COMMENT ' . $this->database->quote($this->comment); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * @return static |
| 153 | */ |
| 154 | public function primaryKey() : static |
| 155 | { |
| 156 | $this->primaryKey = true; |
| 157 | return $this; |
| 158 | } |
| 159 | |
| 160 | protected function renderPrimaryKey() : ?string |
| 161 | { |
| 162 | if ( ! $this->primaryKey) { |
| 163 | return null; |
| 164 | } |
| 165 | return ' PRIMARY KEY'; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @return static |
| 170 | */ |
| 171 | public function uniqueKey() : static |
| 172 | { |
| 173 | $this->uniqueKey = true; |
| 174 | return $this; |
| 175 | } |
| 176 | |
| 177 | protected function renderUniqueKey() : ?string |
| 178 | { |
| 179 | if ( ! $this->uniqueKey) { |
| 180 | return null; |
| 181 | } |
| 182 | return ' UNIQUE KEY'; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * @return static |
| 187 | */ |
| 188 | public function first() : static |
| 189 | { |
| 190 | $this->first = true; |
| 191 | return $this; |
| 192 | } |
| 193 | |
| 194 | protected function renderFirst() : ?string |
| 195 | { |
| 196 | if ( ! $this->first) { |
| 197 | return null; |
| 198 | } |
| 199 | return ' FIRST'; |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @param string $column |
| 204 | * |
| 205 | * @return static |
| 206 | */ |
| 207 | public function after(string $column) : static |
| 208 | { |
| 209 | $this->after = $column; |
| 210 | return $this; |
| 211 | } |
| 212 | |
| 213 | protected function renderAfter() : ?string |
| 214 | { |
| 215 | if ( ! isset($this->after)) { |
| 216 | return null; |
| 217 | } |
| 218 | if ($this->first) { |
| 219 | throw new LogicException('Clauses FIRST and AFTER can not be used together'); |
| 220 | } |
| 221 | return ' AFTER ' . $this->database->protectIdentifier($this->after); |
| 222 | } |
| 223 | |
| 224 | protected function renderTypeAttributes() : ?string |
| 225 | { |
| 226 | return null; |
| 227 | } |
| 228 | |
| 229 | protected function sql() : string |
| 230 | { |
| 231 | $sql = $this->renderType(); |
| 232 | $sql .= $this->renderLength(); |
| 233 | $sql .= $this->renderTypeAttributes(); |
| 234 | $sql .= $this->renderNull(); |
| 235 | $sql .= $this->renderDefault(); |
| 236 | $sql .= $this->renderUniqueKey(); |
| 237 | $sql .= $this->renderPrimaryKey(); |
| 238 | $sql .= $this->renderComment(); |
| 239 | $sql .= $this->renderFirst(); |
| 240 | $sql .= $this->renderAfter(); |
| 241 | $sql .= $this->renderCheck(); |
| 242 | return $sql; |
| 243 | } |
| 244 | } |