platine-php /
database
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * Platine Database |
||
| 5 | * |
||
| 6 | * Platine Database is the abstraction layer using PDO with support of query and schema builder |
||
| 7 | * |
||
| 8 | * This content is released under the MIT License (MIT) |
||
| 9 | * |
||
| 10 | * Copyright (c) 2020 Platine Database |
||
| 11 | * |
||
| 12 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
||
| 13 | * of this software and associated documentation files (the "Software"), to deal |
||
| 14 | * in the Software without restriction, including without limitation the rights |
||
| 15 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
||
| 16 | * copies of the Software, and to permit persons to whom the Software is |
||
| 17 | * furnished to do so, subject to the following conditions: |
||
| 18 | * |
||
| 19 | * The above copyright notice and this permission notice shall be included in all |
||
| 20 | * copies or substantial portions of the Software. |
||
| 21 | * |
||
| 22 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
||
| 23 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
||
| 24 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
||
| 25 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
||
| 26 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
||
| 27 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
||
| 28 | * SOFTWARE. |
||
| 29 | */ |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @file CreateTable.php |
||
| 33 | * |
||
| 34 | * The create table schema class |
||
| 35 | * |
||
| 36 | * @package Platine\Database\Schema |
||
| 37 | * @author Platine Developers Team |
||
| 38 | * @copyright Copyright (c) 2020 |
||
| 39 | * @license http://opensource.org/licenses/MIT MIT License |
||
| 40 | * @link https://www.platine-php.com |
||
| 41 | * @version 1.0.0 |
||
| 42 | * @filesource |
||
| 43 | */ |
||
| 44 | |||
| 45 | declare(strict_types=1); |
||
| 46 | |||
| 47 | namespace Platine\Database\Schema; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @class CreateTable |
||
| 51 | * @package Platine\Database\Schema |
||
| 52 | */ |
||
| 53 | class CreateTable |
||
| 54 | { |
||
| 55 | /** |
||
| 56 | * The list of CreateColumn |
||
| 57 | * @var array<string, CreateColumn> |
||
| 58 | */ |
||
| 59 | protected array $columns = []; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The primary or list of primary key |
||
| 63 | * @var array<string, mixed> |
||
| 64 | */ |
||
| 65 | protected array $primaryKey = []; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * The list of unique keys |
||
| 69 | * @var array<string, array<int, mixed>> |
||
| 70 | */ |
||
| 71 | protected array $uniqueKeys = []; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * The list of indexes keys |
||
| 75 | * @var array<string, array<int, mixed>> |
||
| 76 | */ |
||
| 77 | protected array $indexes = []; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The list of ForeignKey |
||
| 81 | * @var array<string, ForeignKey> |
||
| 82 | */ |
||
| 83 | protected array $foreignKeys = []; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * The engine for the table |
||
| 87 | * @var string|null |
||
| 88 | */ |
||
| 89 | protected ?string $engine = null; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The auto increment name |
||
| 93 | * @var bool|null |
||
| 94 | */ |
||
| 95 | protected ?bool $autoincrement = null; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Class constructor |
||
| 99 | * @param string $table The name of the table |
||
| 100 | */ |
||
| 101 | public function __construct(protected string $table) |
||
| 102 | { |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * |
||
| 107 | * @return string |
||
| 108 | */ |
||
| 109 | public function getTableName(): string |
||
| 110 | { |
||
| 111 | return $this->table; |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * |
||
| 116 | * @return array<string, CreateColumn> |
||
| 117 | */ |
||
| 118 | public function getColumns(): array |
||
| 119 | { |
||
| 120 | return $this->columns; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * |
||
| 125 | * @return array<string, mixed> |
||
| 126 | */ |
||
| 127 | public function getPrimaryKey(): array |
||
| 128 | { |
||
| 129 | return $this->primaryKey; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * |
||
| 134 | * @return array<string, array<int, mixed>> |
||
| 135 | */ |
||
| 136 | public function getUniqueKeys(): array |
||
| 137 | { |
||
| 138 | return $this->uniqueKeys; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * |
||
| 143 | * @return array<string, array<int, mixed>> |
||
| 144 | */ |
||
| 145 | public function getIndexes(): array |
||
| 146 | { |
||
| 147 | return $this->indexes; |
||
| 148 | } |
||
| 149 | |||
| 150 | /** |
||
| 151 | * |
||
| 152 | * @return array<string, ForeignKey> |
||
| 153 | */ |
||
| 154 | public function getForeignKeys(): array |
||
| 155 | { |
||
| 156 | return $this->foreignKeys; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * |
||
| 161 | * @return string|null |
||
| 162 | */ |
||
| 163 | public function getEngine(): ?string |
||
| 164 | { |
||
| 165 | return $this->engine; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * |
||
| 170 | * @return bool|null |
||
| 171 | */ |
||
| 172 | public function getAutoincrement(): ?bool |
||
| 173 | { |
||
| 174 | return $this->autoincrement; |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * |
||
| 179 | * @param string|null $name |
||
| 180 | * @return $this |
||
| 181 | */ |
||
| 182 | public function engine(?string $name): self |
||
| 183 | { |
||
| 184 | $this->engine = $name; |
||
| 185 | |||
| 186 | return $this; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * |
||
| 191 | * @param string|array<int, string> $columns |
||
| 192 | * @param string|null $name |
||
| 193 | * @return $this |
||
| 194 | */ |
||
| 195 | public function primary(string|array $columns, ?string $name = null): self |
||
| 196 | { |
||
| 197 | if (!is_array($columns)) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 198 | $columns = [$columns]; |
||
| 199 | } |
||
| 200 | |||
| 201 | if ($name === null) { |
||
| 202 | $name = sprintf('%s_pk_%s', $this->table, implode('_', $columns)); |
||
| 203 | } |
||
| 204 | |||
| 205 | $this->primaryKey = [ |
||
| 206 | 'name' => $name, |
||
| 207 | 'columns' => $columns |
||
| 208 | ]; |
||
| 209 | |||
| 210 | return $this; |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * |
||
| 215 | * @param string|array<int, string> $columns |
||
| 216 | * @param string|null $name |
||
| 217 | * @return $this |
||
| 218 | */ |
||
| 219 | public function unique(string|array $columns, ?string $name = null): self |
||
| 220 | { |
||
| 221 | if (!is_array($columns)) { |
||
|
0 ignored issues
–
show
|
|||
| 222 | $columns = [$columns]; |
||
| 223 | } |
||
| 224 | |||
| 225 | if ($name === null) { |
||
| 226 | $name = sprintf('%s_uk_%s', $this->table, implode('_', $columns)); |
||
| 227 | } |
||
| 228 | |||
| 229 | $this->uniqueKeys[$name] = $columns; |
||
| 230 | |||
| 231 | return $this; |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * |
||
| 236 | * @param string|array<int, string> $columns |
||
| 237 | * @param string|null $name |
||
| 238 | * @return $this |
||
| 239 | */ |
||
| 240 | public function index(string|array $columns, ?string $name = null): self |
||
| 241 | { |
||
| 242 | if (!is_array($columns)) { |
||
|
0 ignored issues
–
show
|
|||
| 243 | $columns = [$columns]; |
||
| 244 | } |
||
| 245 | |||
| 246 | if ($name === null) { |
||
| 247 | $name = sprintf('%s_ik_%s', $this->table, implode('_', $columns)); |
||
| 248 | } |
||
| 249 | |||
| 250 | $this->indexes[$name] = $columns; |
||
| 251 | |||
| 252 | return $this; |
||
| 253 | } |
||
| 254 | |||
| 255 | /** |
||
| 256 | * |
||
| 257 | * @param string|array<int, string> $columns |
||
| 258 | * @param string|null $name |
||
| 259 | * @return ForeignKey |
||
| 260 | */ |
||
| 261 | public function foreign(string|array $columns, ?string $name = null): ForeignKey |
||
| 262 | { |
||
| 263 | if (!is_array($columns)) { |
||
|
0 ignored issues
–
show
|
|||
| 264 | $columns = [$columns]; |
||
| 265 | } |
||
| 266 | |||
| 267 | if ($name === null) { |
||
| 268 | $name = sprintf('%s_fk_%s', $this->table, implode('_', $columns)); |
||
| 269 | } |
||
| 270 | |||
| 271 | return $this->foreignKeys[$name] = new ForeignKey($columns); |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * |
||
| 276 | * @param CreateColumn $column |
||
| 277 | * @param string|null $name |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function autoincrement(CreateColumn $column, ?string $name = null): self |
||
| 281 | { |
||
| 282 | if ($column->getType() !== 'integer') { |
||
| 283 | return $this; |
||
| 284 | } |
||
| 285 | |||
| 286 | $this->autoincrement = true; |
||
| 287 | |||
| 288 | $column->set('autoincrement', true); |
||
| 289 | |||
| 290 | return $this->primary($column->getName(), $name); |
||
| 291 | } |
||
| 292 | |||
| 293 | /** |
||
| 294 | * |
||
| 295 | * @param string $name |
||
| 296 | * @return CreateColumn |
||
| 297 | */ |
||
| 298 | public function integer(string $name): CreateColumn |
||
| 299 | { |
||
| 300 | return $this->addColumn($name, 'integer'); |
||
| 301 | } |
||
| 302 | |||
| 303 | /** |
||
| 304 | * |
||
| 305 | * @param string $name |
||
| 306 | * @return CreateColumn |
||
| 307 | */ |
||
| 308 | public function float(string $name): CreateColumn |
||
| 309 | { |
||
| 310 | return $this->addColumn($name, 'float'); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * |
||
| 315 | * @param string $name |
||
| 316 | * @return CreateColumn |
||
| 317 | */ |
||
| 318 | public function double(string $name): CreateColumn |
||
| 319 | { |
||
| 320 | return $this->addColumn($name, 'double'); |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * |
||
| 325 | * @param string $name |
||
| 326 | * @param int|null $length |
||
| 327 | * @param int|null $precision |
||
| 328 | * @return CreateColumn |
||
| 329 | */ |
||
| 330 | public function decimal( |
||
| 331 | string $name, |
||
| 332 | ?int $length = null, |
||
| 333 | ?int $precision = null |
||
| 334 | ): CreateColumn { |
||
| 335 | return $this->addColumn($name, 'decimal') |
||
| 336 | ->length($length) |
||
| 337 | ->set('precision', $precision); |
||
| 338 | } |
||
| 339 | |||
| 340 | /** |
||
| 341 | * |
||
| 342 | * @param string $name |
||
| 343 | * @return CreateColumn |
||
| 344 | */ |
||
| 345 | public function boolean(string $name): CreateColumn |
||
| 346 | { |
||
| 347 | return $this->addColumn($name, 'boolean'); |
||
| 348 | } |
||
| 349 | |||
| 350 | /** |
||
| 351 | * |
||
| 352 | * @param string $name |
||
| 353 | * @return CreateColumn |
||
| 354 | */ |
||
| 355 | public function binary(string $name): CreateColumn |
||
| 356 | { |
||
| 357 | return $this->addColumn($name, 'binary'); |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * |
||
| 362 | * @param string $name |
||
| 363 | * @param int $length |
||
| 364 | * @return CreateColumn |
||
| 365 | */ |
||
| 366 | public function string(string $name, int $length = 255): CreateColumn |
||
| 367 | { |
||
| 368 | return $this->addColumn($name, 'string') |
||
| 369 | ->length($length); |
||
| 370 | } |
||
| 371 | |||
| 372 | /** |
||
| 373 | * |
||
| 374 | * @param string $name |
||
| 375 | * @param int $length |
||
| 376 | * @return CreateColumn |
||
| 377 | */ |
||
| 378 | public function fixed(string $name, int $length = 255): CreateColumn |
||
| 379 | { |
||
| 380 | return $this->addColumn($name, 'fixed') |
||
| 381 | ->length($length); |
||
| 382 | } |
||
| 383 | |||
| 384 | /** |
||
| 385 | * |
||
| 386 | * @param string $name |
||
| 387 | * @param array<mixed> $values |
||
| 388 | * @return CreateColumn |
||
| 389 | */ |
||
| 390 | public function enum( |
||
| 391 | string $name, |
||
| 392 | array $values |
||
| 393 | ): CreateColumn { |
||
| 394 | return $this->addColumn($name, 'enum') |
||
| 395 | ->set('values', $values); |
||
| 396 | } |
||
| 397 | |||
| 398 | /** |
||
| 399 | * |
||
| 400 | * @param string $name |
||
| 401 | * @return CreateColumn |
||
| 402 | */ |
||
| 403 | public function text(string $name): CreateColumn |
||
| 404 | { |
||
| 405 | return $this->addColumn($name, 'text'); |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * |
||
| 410 | * @param string $name |
||
| 411 | * @return CreateColumn |
||
| 412 | */ |
||
| 413 | public function time(string $name): CreateColumn |
||
| 414 | { |
||
| 415 | return $this->addColumn($name, 'time'); |
||
| 416 | } |
||
| 417 | |||
| 418 | /** |
||
| 419 | * |
||
| 420 | * @param string $name |
||
| 421 | * @return CreateColumn |
||
| 422 | */ |
||
| 423 | public function timestamp(string $name): CreateColumn |
||
| 424 | { |
||
| 425 | return $this->addColumn($name, 'timestamp'); |
||
| 426 | } |
||
| 427 | |||
| 428 | /** |
||
| 429 | * |
||
| 430 | * @param string $name |
||
| 431 | * @return CreateColumn |
||
| 432 | */ |
||
| 433 | public function date(string $name): CreateColumn |
||
| 434 | { |
||
| 435 | return $this->addColumn($name, 'date'); |
||
| 436 | } |
||
| 437 | |||
| 438 | /** |
||
| 439 | * |
||
| 440 | * @param string $name |
||
| 441 | * @return CreateColumn |
||
| 442 | */ |
||
| 443 | public function datetime(string $name): CreateColumn |
||
| 444 | { |
||
| 445 | return $this->addColumn($name, 'datetime'); |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * |
||
| 450 | * @param string $column |
||
| 451 | * @return $this |
||
| 452 | */ |
||
| 453 | public function softDelete(string $column = 'deleted_at'): self |
||
| 454 | { |
||
| 455 | $this->datetime($column); |
||
| 456 | |||
| 457 | return $this; |
||
| 458 | } |
||
| 459 | |||
| 460 | /** |
||
| 461 | * |
||
| 462 | * @param string $createColumn |
||
| 463 | * @param string $updateColumn |
||
| 464 | * @return $this |
||
| 465 | */ |
||
| 466 | public function timestamps( |
||
| 467 | string $createColumn = 'created_at', |
||
| 468 | string $updateColumn = 'updated_at' |
||
| 469 | ): self { |
||
| 470 | $this->datetime($createColumn)->notNull(); |
||
| 471 | $this->datetime($updateColumn); |
||
| 472 | |||
| 473 | return $this; |
||
| 474 | } |
||
| 475 | |||
| 476 | /** |
||
| 477 | * |
||
| 478 | * @param string $name |
||
| 479 | * @param string $type |
||
| 480 | * @return CreateColumn |
||
| 481 | */ |
||
| 482 | protected function addColumn(string $name, string $type): CreateColumn |
||
| 483 | { |
||
| 484 | $column = new CreateColumn($this, $name, $type); |
||
| 485 | $this->columns[$name] = $column; |
||
| 486 | |||
| 487 | return $column; |
||
| 488 | } |
||
| 489 | } |
||
| 490 |