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 AlterTable.php |
||
33 | * |
||
34 | * The alter 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 AlterTable |
||
51 | * @package Platine\Database\Schema |
||
52 | */ |
||
53 | class AlterTable |
||
54 | { |
||
55 | /** |
||
56 | * The list of commands |
||
57 | * @var array<int, array<string, mixed>> |
||
58 | */ |
||
59 | protected array $commands = []; |
||
60 | |||
61 | /** |
||
62 | * Class constructor |
||
63 | * @param string $table The name of table |
||
64 | */ |
||
65 | public function __construct(protected string $table) |
||
66 | { |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | public function getTableName(): string |
||
74 | { |
||
75 | return $this->table; |
||
76 | } |
||
77 | |||
78 | /** |
||
79 | * |
||
80 | * @return array<int, array<string, mixed>> |
||
81 | */ |
||
82 | public function getCommands(): array |
||
83 | { |
||
84 | return $this->commands; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * |
||
89 | * @param string $name |
||
90 | * @return $this |
||
91 | */ |
||
92 | public function dropIndex(string $name): self |
||
93 | { |
||
94 | return $this->addCommand('dropIndex', $name); |
||
95 | } |
||
96 | |||
97 | /** |
||
98 | * |
||
99 | * @param string $name |
||
100 | * @return $this |
||
101 | */ |
||
102 | public function dropUnique(string $name): self |
||
103 | { |
||
104 | return $this->addCommand('dropUniqueKey', $name); |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * |
||
109 | * @param string $name |
||
110 | * @return $this |
||
111 | */ |
||
112 | public function dropPrimary(string $name): self |
||
113 | { |
||
114 | return $this->addCommand('dropPrimaryKey', $name); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * |
||
119 | * @param string $name |
||
120 | * @return $this |
||
121 | */ |
||
122 | public function dropForeign(string $name): self |
||
123 | { |
||
124 | return $this->addCommand('dropForeignKey', $name); |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * |
||
129 | * @param string $name |
||
130 | * @return $this |
||
131 | */ |
||
132 | public function dropColumn(string $name): self |
||
133 | { |
||
134 | return $this->addCommand('dropColumn', $name); |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * |
||
139 | * @param string $column |
||
140 | * @return $this |
||
141 | */ |
||
142 | public function dropDefaultValue(string $column): self |
||
143 | { |
||
144 | return $this->addCommand('dropDefaultValue', $column); |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * |
||
149 | * @param string $oldName |
||
150 | * @param string $newName |
||
151 | * @return $this |
||
152 | */ |
||
153 | public function renameColumn(string $oldName, string $newName): self |
||
154 | { |
||
155 | return $this->addCommand('renameColumn', [ |
||
156 | 'from' => $oldName, |
||
157 | 'column' => new AlterColumn($this, $newName) |
||
158 | ]); |
||
159 | } |
||
160 | |||
161 | /** |
||
162 | * |
||
163 | * @param string|array<int, string> $columns |
||
164 | * @param string|null $name |
||
165 | * @return $this |
||
166 | */ |
||
167 | public function primary(string|array $columns, ?string $name = null): self |
||
168 | { |
||
169 | return $this->addKey('addPrimary', $columns, $name); |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * |
||
174 | * @param string|array<int, string> $columns |
||
175 | * @param string|null $name |
||
176 | * @return $this |
||
177 | */ |
||
178 | public function unique(string|array $columns, ?string $name = null): self |
||
179 | { |
||
180 | return $this->addKey('addUnique', $columns, $name); |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * |
||
185 | * @param string|array<int, string> $columns |
||
186 | * @param string|null $name |
||
187 | * @return $this |
||
188 | */ |
||
189 | public function index(string|array $columns, ?string $name = null): self |
||
190 | { |
||
191 | return $this->addKey('addIndex', $columns, $name); |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * |
||
196 | * @param string|array<int, string> $columns |
||
197 | * @param string|null $name |
||
198 | * @return ForeignKey |
||
199 | */ |
||
200 | public function foreign(string|array $columns, ?string $name = null): ForeignKey |
||
201 | { |
||
202 | if (!is_array($columns)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
203 | $columns = [$columns]; |
||
204 | } |
||
205 | if ($name === null) { |
||
206 | $name = $this->table . '_fk_' . implode('_', $columns); |
||
207 | } |
||
208 | $foreign = new ForeignKey($columns); |
||
209 | $this->addCommand('addForeign', [ |
||
210 | 'name' => $name, |
||
211 | 'foreign' => $foreign |
||
212 | ]); |
||
213 | |||
214 | return $foreign; |
||
215 | } |
||
216 | |||
217 | /** |
||
218 | * |
||
219 | * @param string $column |
||
220 | * @param mixed $value |
||
221 | * @return $this |
||
222 | */ |
||
223 | public function setDefaultValue(string $column, mixed $value): self |
||
224 | { |
||
225 | return $this->addCommand('setDefaultValue', [ |
||
226 | 'column' => $column, |
||
227 | 'value' => $value, |
||
228 | ]); |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * |
||
233 | * @param string $name |
||
234 | * @return AlterColumn |
||
235 | */ |
||
236 | public function integer(string $name): AlterColumn |
||
237 | { |
||
238 | return $this->addColumn($name, 'integer'); |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * |
||
243 | * @param string $name |
||
244 | * @return AlterColumn |
||
245 | */ |
||
246 | public function float(string $name): AlterColumn |
||
247 | { |
||
248 | return $this->addColumn($name, 'float'); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * |
||
253 | * @param string $name |
||
254 | * @return AlterColumn |
||
255 | */ |
||
256 | public function double(string $name): AlterColumn |
||
257 | { |
||
258 | return $this->addColumn($name, 'double'); |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * |
||
263 | * @param string $name |
||
264 | * @param int|null $length |
||
265 | * @param int|null $precision |
||
266 | * @return AlterColumn |
||
267 | */ |
||
268 | public function decimal( |
||
269 | string $name, |
||
270 | ?int $length = null, |
||
271 | ?int $precision = null |
||
272 | ): AlterColumn { |
||
273 | return $this->addColumn($name, 'decimal') |
||
274 | ->length($length) |
||
275 | ->set('precision', $precision); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * |
||
280 | * @param string $name |
||
281 | * @return AlterColumn |
||
282 | */ |
||
283 | public function boolean(string $name): AlterColumn |
||
284 | { |
||
285 | return $this->addColumn($name, 'boolean'); |
||
286 | } |
||
287 | |||
288 | /** |
||
289 | * |
||
290 | * @param string $name |
||
291 | * @return AlterColumn |
||
292 | */ |
||
293 | public function binary(string $name): AlterColumn |
||
294 | { |
||
295 | return $this->addColumn($name, 'binary'); |
||
296 | } |
||
297 | |||
298 | /** |
||
299 | * |
||
300 | * @param string $name |
||
301 | * @param int $length |
||
302 | * @return AlterColumn |
||
303 | */ |
||
304 | public function string(string $name, int $length = 255): AlterColumn |
||
305 | { |
||
306 | return $this->addColumn($name, 'string') |
||
307 | ->length($length); |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * |
||
312 | * @param string $name |
||
313 | * @param int $length |
||
314 | * @return AlterColumn |
||
315 | */ |
||
316 | public function fixed(string $name, int $length = 255): AlterColumn |
||
317 | { |
||
318 | return $this->addColumn($name, 'fixed') |
||
319 | ->length($length); |
||
320 | } |
||
321 | |||
322 | /** |
||
323 | * |
||
324 | * @param string $name |
||
325 | * @param array<mixed> $values |
||
326 | * @return AlterColumn |
||
327 | */ |
||
328 | public function enum( |
||
329 | string $name, |
||
330 | array $values |
||
331 | ): AlterColumn { |
||
332 | return $this->addColumn($name, 'enum') |
||
333 | ->set('values', $values); |
||
334 | } |
||
335 | |||
336 | /** |
||
337 | * |
||
338 | * @param string $name |
||
339 | * @return AlterColumn |
||
340 | */ |
||
341 | public function text(string $name): AlterColumn |
||
342 | { |
||
343 | return $this->addColumn($name, 'text'); |
||
344 | } |
||
345 | |||
346 | /** |
||
347 | * |
||
348 | * @param string $name |
||
349 | * @return AlterColumn |
||
350 | */ |
||
351 | public function time(string $name): AlterColumn |
||
352 | { |
||
353 | return $this->addColumn($name, 'time'); |
||
354 | } |
||
355 | |||
356 | /** |
||
357 | * |
||
358 | * @param string $name |
||
359 | * @return AlterColumn |
||
360 | */ |
||
361 | public function timestamp(string $name): AlterColumn |
||
362 | { |
||
363 | return $this->addColumn($name, 'timestamp'); |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * |
||
368 | * @param string $name |
||
369 | * @return AlterColumn |
||
370 | */ |
||
371 | public function date(string $name): AlterColumn |
||
372 | { |
||
373 | return $this->addColumn($name, 'date'); |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * |
||
378 | * @param string $name |
||
379 | * @return AlterColumn |
||
380 | */ |
||
381 | public function datetime(string $name): AlterColumn |
||
382 | { |
||
383 | return $this->addColumn($name, 'datetime'); |
||
384 | } |
||
385 | |||
386 | /** |
||
387 | * |
||
388 | * @param string $name |
||
389 | * @return AlterColumn |
||
390 | */ |
||
391 | public function toInteger(string $name): AlterColumn |
||
392 | { |
||
393 | return $this->modifyColumn($name, 'integer'); |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * |
||
398 | * @param string $name |
||
399 | * @return AlterColumn |
||
400 | */ |
||
401 | public function toFloat(string $name): AlterColumn |
||
402 | { |
||
403 | return $this->modifyColumn($name, 'float'); |
||
404 | } |
||
405 | |||
406 | /** |
||
407 | * |
||
408 | * @param string $name |
||
409 | * @return AlterColumn |
||
410 | */ |
||
411 | public function toDouble(string $name): AlterColumn |
||
412 | { |
||
413 | return $this->modifyColumn($name, 'double'); |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * |
||
418 | * @param string $name |
||
419 | * @param int|null $length |
||
420 | * @param int|null $precision |
||
421 | * @return AlterColumn |
||
422 | */ |
||
423 | public function toDecimal( |
||
424 | string $name, |
||
425 | ?int $length = null, |
||
426 | ?int $precision = null |
||
427 | ): AlterColumn { |
||
428 | return $this->modifyColumn($name, 'decimal') |
||
429 | ->length($length) |
||
430 | ->set('precision', $precision); |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * |
||
435 | * @param string $name |
||
436 | * @param array<mixed> $values |
||
437 | * @return AlterColumn |
||
438 | */ |
||
439 | public function toEnum( |
||
440 | string $name, |
||
441 | array $values |
||
442 | ): AlterColumn { |
||
443 | return $this->modifyColumn($name, 'enum') |
||
444 | ->set('values', $values); |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * |
||
449 | * @param string $name |
||
450 | * @return AlterColumn |
||
451 | */ |
||
452 | public function toBoolean(string $name): AlterColumn |
||
453 | { |
||
454 | return $this->modifyColumn($name, 'boolean'); |
||
455 | } |
||
456 | |||
457 | /** |
||
458 | * |
||
459 | * @param string $name |
||
460 | * @return AlterColumn |
||
461 | */ |
||
462 | public function toBinary(string $name): AlterColumn |
||
463 | { |
||
464 | return $this->modifyColumn($name, 'binary'); |
||
465 | } |
||
466 | |||
467 | /** |
||
468 | * |
||
469 | * @param string $name |
||
470 | * @return AlterColumn |
||
471 | */ |
||
472 | public function toString(string $name, int $length = 255): AlterColumn |
||
473 | { |
||
474 | return $this->modifyColumn($name, 'string') |
||
475 | ->length($length); |
||
476 | } |
||
477 | |||
478 | /** |
||
479 | * |
||
480 | * @param string $name |
||
481 | * @return AlterColumn |
||
482 | */ |
||
483 | public function toFixed(string $name, int $length = 255): AlterColumn |
||
484 | { |
||
485 | return $this->modifyColumn($name, 'fixed') |
||
486 | ->length($length); |
||
487 | } |
||
488 | |||
489 | /** |
||
490 | * |
||
491 | * @param string $name |
||
492 | * @return AlterColumn |
||
493 | */ |
||
494 | public function toText(string $name): AlterColumn |
||
495 | { |
||
496 | return $this->modifyColumn($name, 'text'); |
||
497 | } |
||
498 | |||
499 | /** |
||
500 | * |
||
501 | * @param string $name |
||
502 | * @return AlterColumn |
||
503 | */ |
||
504 | public function toTime(string $name): AlterColumn |
||
505 | { |
||
506 | return $this->modifyColumn($name, 'time'); |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * |
||
511 | * @param string $name |
||
512 | * @return AlterColumn |
||
513 | */ |
||
514 | public function toTimestamp(string $name): AlterColumn |
||
515 | { |
||
516 | return $this->modifyColumn($name, 'timestamp'); |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * |
||
521 | * @param string $name |
||
522 | * @return AlterColumn |
||
523 | */ |
||
524 | public function toDate(string $name): AlterColumn |
||
525 | { |
||
526 | return $this->modifyColumn($name, 'date'); |
||
527 | } |
||
528 | |||
529 | /** |
||
530 | * |
||
531 | * @param string $name |
||
532 | * @return AlterColumn |
||
533 | */ |
||
534 | public function toDatetime(string $name): AlterColumn |
||
535 | { |
||
536 | return $this->modifyColumn($name, 'datetime'); |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * |
||
541 | * @param string $name |
||
542 | * @param mixed $data |
||
543 | * @return $this |
||
544 | */ |
||
545 | protected function addCommand(string $name, mixed $data): self |
||
546 | { |
||
547 | $this->commands[] = [ |
||
548 | 'type' => $name, |
||
549 | 'data' => $data |
||
550 | ]; |
||
551 | |||
552 | return $this; |
||
553 | } |
||
554 | |||
555 | /** |
||
556 | * |
||
557 | * @param string $type |
||
558 | * @param string|array<int, string> $columns |
||
559 | * @param string|null $name |
||
560 | * @return $this |
||
561 | */ |
||
562 | protected function addKey( |
||
563 | string $type, |
||
564 | string|array $columns, |
||
565 | ?string $name = null |
||
566 | ): self { |
||
567 | static $maps = [ |
||
568 | 'addPrimary' => 'pk', |
||
569 | 'addUnique' => 'uk', |
||
570 | 'addForeignKey' => 'fk', |
||
571 | 'addIndex' => 'ik', |
||
572 | ]; |
||
573 | |||
574 | if (!is_array($columns)) { |
||
0 ignored issues
–
show
|
|||
575 | $columns = [$columns]; |
||
576 | } |
||
577 | |||
578 | if ($name === null) { |
||
579 | $name = sprintf( |
||
580 | '%s_%s_%s', |
||
581 | $this->table, |
||
582 | $maps[$type], |
||
583 | implode('_', $columns) |
||
584 | ); |
||
585 | } |
||
586 | |||
587 | return $this->addCommand($type, [ |
||
588 | 'name' => $name, |
||
589 | 'columns' => $columns |
||
590 | ]); |
||
591 | } |
||
592 | |||
593 | /** |
||
594 | * |
||
595 | * @param string $name |
||
596 | * @param string $type |
||
597 | * @return AlterColumn |
||
598 | */ |
||
599 | protected function addColumn(string $name, string $type): AlterColumn |
||
600 | { |
||
601 | $column = new AlterColumn($this, $name, $type); |
||
602 | $this->addCommand('addColumn', $column); |
||
603 | |||
604 | return $column; |
||
605 | } |
||
606 | |||
607 | /** |
||
608 | * |
||
609 | * @param string $name |
||
610 | * @param string $type |
||
611 | * @return AlterColumn |
||
612 | */ |
||
613 | protected function modifyColumn(string $name, string $type): AlterColumn |
||
614 | { |
||
615 | $column = new AlterColumn($this, $name, $type); |
||
616 | $column->set('handleDefault', false); |
||
617 | $this->addCommand('modifyColumn', $column); |
||
618 | |||
619 | return $column; |
||
620 | } |
||
621 | } |
||
622 |