/
TableColumn.php
269 lines (249 loc) · 7.19 KB
/
TableColumn.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
<?php
namespace bizley\migration\table;
use yii\base\Object;
use yii\db\Expression;
/**
* Class TableColumn
* @package bizley\migration\table
*
* @property-read int|string $length
*/
class TableColumn extends Object
{
/**
* @var string
*/
public $name;
/**
* @var string
*/
public $type;
/**
* @var bool|null
*/
public $isNotNull;
/**
* @var int
*/
public $size;
/**
* @var int
*/
public $precision;
/**
* @var int
*/
public $scale;
/**
* @var bool
*/
public $isUnique = false;
/**
* @var bool
*/
public $isUnsigned = false;
/**
* @var string
*/
public $check;
/**
* @var mixed
*/
public $default;
/**
* @var bool
*/
public $isPrimaryKey;
/**
* @var bool
*/
public $autoIncrement;
/**
* @var string
*/
public $append;
/**
* @var string
*/
public $comment;
/**
* List of all properties to be checked.
* @return array
*/
public static function properties()
{
return ['type', 'isNotNull', 'size', 'precision', 'scale', 'isUnique', 'isUnsigned', 'default', 'append', 'comment'];
}
/**
* Returns length of the column.
* @return int|string
*/
public function getLength()
{
return $this->size;
}
/**
* Sets length of the column.
* @param $value
*/
public function setLength($value)
{
$this->size = $value;
$this->precision = $value;
}
protected function buildSpecificDefinition($table) {}
protected $definition = [];
protected $isUnsignedPossible = true;
protected $isNotNullPossible = true;
protected $isPkPossible = true;
/**
* Builds general methods chain for column definition.
* @param TableStructure $table
*/
protected function buildGeneralDefinition($table)
{
array_unshift($this->definition, '$this');
if ($this->isUnsignedPossible && $this->isUnsigned) {
$this->definition[] = 'unsigned()';
}
if ($this->isNotNullPossible && $this->isNotNull) {
$this->definition[] = 'notNull()';
}
if ($this->default !== null) {
if ($this->default instanceof Expression) {
$this->definition[] = "defaultExpression('" . $this->escapeQuotes($this->default->expression) . "')";
} else {
$this->definition[] = "defaultValue('" . $this->escapeQuotes($this->default) . "')";
}
}
if ($this->isPkPossible && !$table->primaryKey->isComposite() && $this->isColumnInPK($table->primaryKey)) {
$append = $this->prepareSchemaAppend($table, true, $this->autoIncrement);
if (!empty($this->append)) {
$append .= ' ' . $this->append;
}
$this->definition[] = "append('" . $this->escapeQuotes($append) . "')";
} elseif (!empty($this->append)) {
$this->definition[] = "append('" . $this->escapeQuotes($this->append) . "')";
}
if ($this->comment) {
$this->definition[] = "comment('" . $this->escapeQuotes($this->comment) . "')";
}
}
/**
* Renders column definition.
* @param TableStructure $table
* @return string
*/
public function renderDefinition($table)
{
$this->buildSpecificDefinition($table);
$this->buildGeneralDefinition($table);
return implode('->', $this->definition);
}
/**
* Renders the column.
* @param TableStructure $table
* @param int $indent
* @return string
*/
public function render($table, $indent = 12)
{
return str_repeat(' ', $indent) . "'{$this->name}' => " . $this->renderDefinition($table) . ',';
}
/**
* Checks if column is a part of primary key.
* @param TablePrimaryKey $pk
* @return bool
*/
public function isColumnInPK($pk)
{
return in_array($this->name, $pk->columns, true);
}
/**
* Checks if information of primary key is set in append property.
* @param string $schema
* @return bool
*/
public function isColumnAppendPK($schema)
{
if (empty($this->append)) {
return false;
}
if ($schema === TableStructure::SCHEMA_MSSQL) {
if (stripos($this->append, 'IDENTITY') !== false && stripos($this->append, 'PRIMARY KEY') !== false) {
return true;
}
} else {
if (stripos($this->append, 'PRIMARY KEY') !== false) {
return true;
}
}
return false;
}
/**
* Prepares append SQL based on schema.
* @param TableStructure $table
* @param bool $primaryKey
* @param bool $autoIncrement
* @return string
*/
public function prepareSchemaAppend($table, $primaryKey, $autoIncrement)
{
switch ($table->schema) {
case TableStructure::SCHEMA_MSSQL:
$append = $primaryKey ? 'IDENTITY PRIMARY KEY' : '';
break;
case TableStructure::SCHEMA_OCI:
case TableStructure::SCHEMA_PGSQL:
$append = $primaryKey ? 'PRIMARY KEY' : '';
break;
case TableStructure::SCHEMA_SQLITE:
$append = trim(($primaryKey ? 'PRIMARY KEY ' : '') . ($autoIncrement ? 'AUTOINCREMENT' : ''));
break;
case TableStructure::SCHEMA_CUBRID:
case TableStructure::SCHEMA_MYSQL:
default:
$append = trim(($autoIncrement ? 'AUTO_INCREMENT ' : '') . ($primaryKey ? 'PRIMARY KEY' : ''));
}
return empty($append) ? null : $append;
}
/**
* Escapes single quotes.
* @param $value
* @return mixed
*/
public function escapeQuotes($value)
{
return str_replace('\'', '\\\'', $value);
}
/**
* Removes information of primary key in append property.
* @param $schema
* @return null|string
*/
public function removePKAppend($schema)
{
if (!$this->isColumnAppendPK($schema)) {
return null;
}
$uppercaseAppend = preg_replace('/\s+/', ' ', mb_strtoupper($this->append, 'UTF-8'));
switch ($schema) {
case TableStructure::SCHEMA_MSSQL:
$formattedAppend = str_replace(['PRIMARY KEY', 'IDENTITY'], '', $uppercaseAppend);
break;
case TableStructure::SCHEMA_OCI:
case TableStructure::SCHEMA_PGSQL:
$formattedAppend = str_replace('PRIMARY KEY', '', $uppercaseAppend);
break;
case TableStructure::SCHEMA_SQLITE:
$formattedAppend = str_replace(['PRIMARY KEY', 'AUTOINCREMENT'], '', $uppercaseAppend);
break;
case TableStructure::SCHEMA_CUBRID:
case TableStructure::SCHEMA_MYSQL:
default:
$formattedAppend = str_replace(['PRIMARY KEY', 'AUTO_INCREMENT'], '', $uppercaseAppend);
}
$formattedAppend = trim($formattedAppend);
return !empty($formattedAppend) ? $formattedAppend : null;
}
}