1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Starkerxp\DatabaseChecker\Structure; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Starkerxp\DatabaseChecker\Exception\TableHasNotDefinedException; |
7
|
|
|
|
8
|
|
|
class MysqlDatabaseColumn implements DatabaseInterface |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
private $table; |
12
|
|
|
private $name; |
13
|
|
|
private $type; |
14
|
|
|
private $length; |
15
|
|
|
private $nullable; |
16
|
|
|
private $defaultValue; |
17
|
|
|
private $extra; |
18
|
|
|
private $collate; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* DatabaseColumnStructure constructor. |
22
|
|
|
* |
23
|
|
|
* @param $name |
24
|
|
|
* |
25
|
|
|
* @param $type |
26
|
|
|
* @param $length |
27
|
|
|
* @param $nullable |
28
|
|
|
* @param $defaultValue |
29
|
|
|
* @param $extra |
30
|
|
|
* |
31
|
|
|
* @throws \RuntimeException |
32
|
|
|
*/ |
33
|
|
|
public function __construct($name, $type, $length, $nullable, $defaultValue, $extra) |
34
|
|
|
{ |
35
|
|
|
if (empty($name)) { |
36
|
|
|
throw new \RuntimeException(''); |
37
|
|
|
} |
38
|
|
|
$this->name = $name; |
39
|
|
|
$this->setType($type); |
40
|
|
|
$this->length = $length; |
41
|
|
|
$this->nullable = $nullable; |
42
|
|
|
$this->defaultValue = $defaultValue; |
43
|
|
|
$this->extra = $extra; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
|
|
public function getCollate() |
50
|
|
|
{ |
51
|
|
|
$type = $this->getType(); |
52
|
|
|
if (!in_array($type, ['char', 'varchar', 'enum', 'longtext', 'mediumtext', 'text', 'tinytext', 'varchar'], false)) { |
53
|
|
|
return ''; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->collate; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $collate |
61
|
|
|
*/ |
62
|
|
|
public function setCollate($collate) |
63
|
|
|
{ |
64
|
|
|
$this->collate = $collate; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function setType($type) |
68
|
|
|
{ |
69
|
|
|
$type = strtolower($type); |
70
|
|
|
$this->type = $type; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function optimizeType() |
74
|
|
|
{ |
75
|
|
|
$isEnum = explode('enum', $this->type); |
76
|
|
|
if (!empty($isEnum)) { |
77
|
|
|
$numberElements = substr_count(str_replace(['(', ')', "'",], '', $isEnum[1]), ',') + 1; |
78
|
|
|
if ($numberElements == 2) { |
79
|
|
|
$this->type = 'tinyint'; |
80
|
|
|
$this->length = 1; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function toArray() |
86
|
|
|
{ |
87
|
|
|
return get_object_vars($this); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getType() |
94
|
|
|
{ |
95
|
|
|
return $this->type; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
|
|
public function getLength() |
102
|
|
|
{ |
103
|
|
|
return $this->length; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return array |
108
|
|
|
* |
109
|
|
|
* @throws TableHasNotDefinedException |
110
|
|
|
*/ |
111
|
|
|
public function createStatement() |
112
|
|
|
{ |
113
|
|
|
if (!$this->getTable()) { |
114
|
|
|
throw new TableHasNotDefinedException('table not defined'); |
115
|
|
|
} |
116
|
|
|
$null = $this->getNullable() ? '' : 'NOT'; |
117
|
|
|
$default = $this->getDefaultValue() == false ? '' : ' DEFAULT ' . $this->getDefaultValue(); |
118
|
|
|
$collate = $this->getCollate()==''?'':sprintf("COLLATE '%s'", $this->getCollate()); |
119
|
|
|
$modification = sprintf('ALTER TABLE `%s` ADD COLUMN `%s` %s %s NULL %s %s %s;', $this->getTable(), $this->getName(), $this->getColonneType(), $null, $default, $this->getExtra(), $collate); |
120
|
|
|
|
121
|
|
|
return [str_replace([' ', ' ',], ' ', $modification)]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
|
|
public function getTable() |
128
|
|
|
{ |
129
|
|
|
return $this->table; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return mixed |
134
|
|
|
*/ |
135
|
|
|
public function getNullable() |
136
|
|
|
{ |
137
|
|
|
return $this->nullable; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
|
|
public function getDefaultValue() |
144
|
|
|
{ |
145
|
|
|
return $this->defaultValue; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getName() |
149
|
|
|
{ |
150
|
|
|
return $this->name; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getColonneType() |
154
|
|
|
{ |
155
|
|
|
$baseType = $this->type; |
156
|
|
|
if (in_array($baseType, ['int', 'mediumint', 'tinyint', 'smallint', 'binary', 'varchar', 'bigint', 'char', 'float'], false)) { |
157
|
|
|
$baseType = $baseType . '(' . $this->length . ')'; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
return strtoupper($baseType); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @return mixed |
165
|
|
|
*/ |
166
|
|
|
public function getExtra() |
167
|
|
|
{ |
168
|
|
|
return $this->extra; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param mixed $table |
173
|
|
|
*/ |
174
|
|
|
public function setTable($table) |
175
|
|
|
{ |
176
|
|
|
$this->table = $table; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* @return array |
181
|
|
|
* |
182
|
|
|
* @throws TableHasNotDefinedException |
183
|
|
|
*/ |
184
|
|
|
public function alterStatement() |
185
|
|
|
{ |
186
|
|
|
if (!$this->getTable()) { |
187
|
|
|
throw new TableHasNotDefinedException('table not defined'); |
188
|
|
|
} |
189
|
|
|
$null = $this->getNullable() ? '' : 'NOT'; |
190
|
|
|
$default = $this->getDefaultValue() == false ? '' : ' DEFAULT ' . $this->getDefaultValue(); |
191
|
|
|
$columnName = '`' . $this->getName() . '`'; |
192
|
|
|
$collate = $this->getCollate()==''?'':sprintf("COLLATE '%s'", $this->getCollate()); |
193
|
|
|
$modification = sprintf('ALTER TABLE `%s` CHANGE COLUMN %s %s %s %s NULL %s %s %s;', $this->getTable(), $columnName, $columnName, $this->getColonneType(), $null, $default, $this->getExtra(), $collate); |
194
|
|
|
|
195
|
|
|
return [str_replace([' ', ' ',], ' ', $modification)]; |
196
|
|
|
} |
197
|
|
|
} |
|
|
|
|
198
|
|
|
|
This check marks files that end in a newline character, i.e. an empy line.