|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
/** |
|
4
|
|
|
* Spiral Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @author Anton Titov (Wolfy-J) |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Cycle\Schema\Visitor; |
|
11
|
|
|
|
|
12
|
|
|
use Cycle\Schema\Builder; |
|
13
|
|
|
use Cycle\Schema\Definition\Entity; |
|
14
|
|
|
use Cycle\Schema\VisitorInterface; |
|
15
|
|
|
use Spiral\Database\Schema\AbstractColumn; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Generate table columns based on entity definition. |
|
19
|
|
|
*/ |
|
20
|
|
|
class RenderTable implements VisitorInterface |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* Generate table schema based on given entity definition. |
|
24
|
|
|
* |
|
25
|
|
|
* @param Builder $builder |
|
26
|
|
|
* @param Entity $entity |
|
27
|
|
|
*/ |
|
28
|
|
|
public function compute(Builder $builder, Entity $entity) |
|
29
|
|
|
{ |
|
30
|
|
|
// todo: readonly |
|
31
|
|
|
$table = $builder->getTable($entity); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
foreach ($entity->getFields() as $field) { |
|
34
|
|
|
|
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
// todo: primary columns |
|
38
|
|
|
// todo: rendering (!), see tablerenderer |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Cast default value based on column type. Required to prevent conflicts when not nullable |
|
43
|
|
|
* column added to existed table with data in. |
|
44
|
|
|
* |
|
45
|
|
|
* @param AbstractColumn $column |
|
46
|
|
|
* @return mixed |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function castDefault(AbstractColumn $column) |
|
49
|
|
|
{ |
|
50
|
|
|
if (in_array($column->getAbstractType(), ['timestamp', 'datetime', 'time', 'date'])) { |
|
51
|
|
|
return 0; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
if ($column->getAbstractType() == 'enum') { |
|
55
|
|
|
// we can use first enum value as default |
|
56
|
|
|
return $column->getEnumValues()[0]; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
switch ($column->getType()) { |
|
60
|
|
|
case AbstractColumn::INT: |
|
61
|
|
|
return 0; |
|
62
|
|
|
case AbstractColumn::FLOAT: |
|
63
|
|
|
return 0.0; |
|
64
|
|
|
case AbstractColumn::BOOL: |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return ''; |
|
69
|
|
|
} |
|
70
|
|
|
} |