1
|
|
|
<?php |
2
|
|
|
namespace yentu\database; |
3
|
|
|
|
4
|
|
|
use yentu\Parameters; |
5
|
|
|
|
6
|
|
|
class Column extends DatabaseItem implements Commitable |
7
|
|
|
{ |
8
|
|
|
private $type; |
9
|
|
|
private $table; |
10
|
|
|
private $length; |
11
|
|
|
|
12
|
|
|
// Description items |
13
|
|
|
private $nulls; |
14
|
|
|
private $name; |
15
|
|
|
private $default; |
16
|
|
|
|
17
|
|
|
#[\Override] |
18
|
|
|
protected function buildDescription() |
19
|
|
|
{ |
20
|
|
|
return array( |
21
|
|
|
'name' => $this->name, |
22
|
|
|
'type' => $this->type, |
23
|
|
|
'table' => $this->table->getName(), |
24
|
|
|
'schema' => $this->table->getSchema()->getName(), |
25
|
|
|
'nulls' => $this->nulls, |
26
|
|
|
'length' => $this->length, |
27
|
|
|
'default' => $this->default |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function __construct($name, $table) |
32
|
|
|
{ |
33
|
|
|
$this->table = $table; |
34
|
|
|
$this->name = $name; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
#[\Override] |
38
|
|
|
public function init() |
39
|
|
|
{ |
40
|
|
|
$column = Parameters::wrap($this->getDriver()->doesColumnExist( |
|
|
|
|
41
|
|
|
array( |
42
|
|
|
'table' => $this->table->getName(), |
43
|
|
|
'schema' => $this->table->getSchema()->getName(), |
44
|
|
|
'name' => $this->name |
45
|
|
|
) |
46
|
|
|
), |
47
|
|
|
['default', 'length', 'nulls', 'type'] |
48
|
|
|
); |
49
|
|
|
if($column === false) { |
50
|
|
|
$this->new = true; |
51
|
|
|
} else { |
52
|
|
|
$this->default = $column['default']; |
53
|
|
|
$this->length = $column['length']; |
54
|
|
|
$this->type = $column['type']; |
55
|
|
|
$this->nulls = $column['nulls']; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function type($type) |
60
|
|
|
{ |
61
|
|
|
$this->type = $type; |
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function nulls($nulls) |
66
|
|
|
{ |
67
|
|
|
return $this->addChange('nulls', 'nulls', $nulls); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function required(bool $required) |
71
|
|
|
{ |
72
|
|
|
return $this->addChange('nulls', 'nulls', !$required); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function defaultValue($default) |
76
|
|
|
{ |
77
|
|
|
return $this->addChange('default', 'default', $default); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
#[\Override] |
81
|
|
|
public function commitNew() |
82
|
|
|
{ |
83
|
|
|
$this->getDriver()->addColumn($this->buildDescription()); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function length($length) |
87
|
|
|
{ |
88
|
|
|
$this->length = $length; |
89
|
|
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function drop() |
93
|
|
|
{ |
94
|
|
|
$this->getDriver()->dropColumn($this->buildDescription()); |
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
|