|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Quantum PHP Framework |
|
5
|
|
|
* |
|
6
|
|
|
* An open source software development framework for PHP |
|
7
|
|
|
* |
|
8
|
|
|
* @package Quantum |
|
9
|
|
|
* @author Arman Ag. <[email protected]> |
|
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
|
11
|
|
|
* @link http://quantum.softberg.org/ |
|
12
|
|
|
* @since 2.7.0 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Database\Schema; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Column |
|
19
|
|
|
* @package Quantum\Libraries\Database |
|
20
|
|
|
*/ |
|
21
|
|
|
class Column |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Action add |
|
26
|
|
|
*/ |
|
27
|
|
|
const ADD = 'ADD'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Action modify |
|
31
|
|
|
*/ |
|
32
|
|
|
const MODIFY = 'MODIFY'; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Action rename |
|
36
|
|
|
*/ |
|
37
|
|
|
const RENAME = 'RENAME'; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Action drop |
|
41
|
|
|
*/ |
|
42
|
|
|
const DROP = 'DROP'; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Action add index |
|
46
|
|
|
*/ |
|
47
|
|
|
const ADD_INDEX = 'ADD_INDEX'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Action drop index |
|
51
|
|
|
*/ |
|
52
|
|
|
const DROP_INDEX = 'DROP_INDEX'; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Name property |
|
56
|
|
|
*/ |
|
57
|
|
|
const NAME = 'name'; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* New Name property |
|
61
|
|
|
*/ |
|
62
|
|
|
const NEW_NAME = 'newName'; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Type property |
|
66
|
|
|
*/ |
|
67
|
|
|
const TYPE = 'type'; |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Constraint property |
|
71
|
|
|
*/ |
|
72
|
|
|
const CONSTRAINT = 'constraint'; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Attribute property |
|
76
|
|
|
*/ |
|
77
|
|
|
const ATTRIBUTE = 'attribute'; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Nullable property |
|
81
|
|
|
*/ |
|
82
|
|
|
const NULLABLE = 'nullable'; |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Default property |
|
86
|
|
|
*/ |
|
87
|
|
|
const DEFAULT = 'default'; |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* After column property |
|
91
|
|
|
*/ |
|
92
|
|
|
const AFTER = 'afterColumn'; |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Comment property |
|
96
|
|
|
*/ |
|
97
|
|
|
const COMMENT = 'comment'; |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Auto increment property |
|
101
|
|
|
*/ |
|
102
|
|
|
const AUTO_INCREMENT = 'autoincrement'; |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Attribute binary |
|
106
|
|
|
*/ |
|
107
|
|
|
const ATTR_BINARY = 'BINARY'; |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Attribute unsigned |
|
111
|
|
|
*/ |
|
112
|
|
|
const ATTR_UNSIGNED = 'UNSIGNED'; |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Attribute zero fill |
|
116
|
|
|
*/ |
|
117
|
|
|
const ATTR_ZEROFILL = 'UNSIGNED ZEROFILL'; |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @var string |
|
121
|
|
|
*/ |
|
122
|
|
|
private $name; |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @var string |
|
126
|
|
|
*/ |
|
127
|
|
|
private $newName; |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @var string |
|
131
|
|
|
*/ |
|
132
|
|
|
private $type; |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @var mixed |
|
136
|
|
|
*/ |
|
137
|
|
|
private $constraint; |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @var string |
|
141
|
|
|
*/ |
|
142
|
|
|
private $attribute = null; |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @var string |
|
146
|
|
|
*/ |
|
147
|
|
|
private $nullable = 'NOT NULL'; |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @var mixed |
|
151
|
|
|
*/ |
|
152
|
|
|
private $default; |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @var bool |
|
156
|
|
|
*/ |
|
157
|
|
|
private $defaultQuoted = true; |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @var string |
|
161
|
|
|
*/ |
|
162
|
|
|
private $autoincrement = null; |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @var string |
|
166
|
|
|
*/ |
|
167
|
|
|
private $indexKey = null; |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @var string |
|
171
|
|
|
*/ |
|
172
|
|
|
private $indexName = null; |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @var string |
|
176
|
|
|
*/ |
|
177
|
|
|
private $indexDrop = null; |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* @var string |
|
181
|
|
|
*/ |
|
182
|
|
|
private $comment; |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @var string |
|
186
|
|
|
*/ |
|
187
|
|
|
private $afterColumn; |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Column constructor. |
|
191
|
|
|
* @param string $name |
|
192
|
|
|
* @param string $type |
|
193
|
|
|
* @param mixed $constraint |
|
194
|
|
|
*/ |
|
195
|
|
|
public function __construct(string $name, string $type = null, $constraint = null) |
|
196
|
|
|
{ |
|
197
|
|
|
$this->name = $name; |
|
198
|
|
|
|
|
199
|
|
|
if ($type) { |
|
200
|
|
|
$this->type = strtoupper($type); |
|
201
|
|
|
$this->constraint = $constraint; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Renames the column |
|
207
|
|
|
* @param string $newName |
|
208
|
|
|
* @return $this |
|
209
|
|
|
*/ |
|
210
|
|
|
public function renameTo(string $newName): Column |
|
211
|
|
|
{ |
|
212
|
|
|
$this->newName = $newName; |
|
213
|
|
|
return $this; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
public function indexDrop(string $indexName) |
|
217
|
|
|
{ |
|
218
|
|
|
$this->indexDrop = $indexName; |
|
219
|
|
|
return $this; |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* Gets the column property |
|
224
|
|
|
* @param string $property |
|
225
|
|
|
* @param string $action |
|
226
|
|
|
* @return mixed |
|
227
|
|
|
*/ |
|
228
|
|
|
public function get(string $property, string $action = null) |
|
229
|
|
|
{ |
|
230
|
|
|
return isset($this->$property) && ($action != self::RENAME && $action != self::DROP) ? $this->$property : null; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Makes the column auto incremental |
|
235
|
|
|
*/ |
|
236
|
|
|
public function autoIncrement() |
|
237
|
|
|
{ |
|
238
|
|
|
$this->autoincrement = 'AUTO_INCREMENT'; |
|
239
|
|
|
$this->primary(); |
|
240
|
|
|
} |
|
241
|
|
|
|
|
242
|
|
|
/** |
|
243
|
|
|
* Adds a primary key the column |
|
244
|
|
|
*/ |
|
245
|
|
|
public function primary() |
|
246
|
|
|
{ |
|
247
|
|
|
$this->indexKey = Key::PRIMARY; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Adds an index key to the column |
|
252
|
|
|
* @param string $name |
|
253
|
|
|
*/ |
|
254
|
|
|
public function index(string $name = null) |
|
255
|
|
|
{ |
|
256
|
|
|
$this->indexKey = Key::INDEX; |
|
257
|
|
|
|
|
258
|
|
|
if ($name) { |
|
259
|
|
|
$this->indexName = $name; |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Adds unique key to the column |
|
265
|
|
|
* @param string $name |
|
266
|
|
|
*/ |
|
267
|
|
|
public function unique(string $name = null) |
|
268
|
|
|
{ |
|
269
|
|
|
$this->indexKey = Key::UNIQUE; |
|
270
|
|
|
|
|
271
|
|
|
if ($name) { |
|
272
|
|
|
$this->indexName = $name; |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
|
|
276
|
|
|
/** |
|
277
|
|
|
* Adds a fulltext key the column |
|
278
|
|
|
* @param string $name |
|
279
|
|
|
*/ |
|
280
|
|
|
public function fulltext(string $name = null) |
|
281
|
|
|
{ |
|
282
|
|
|
$this->indexKey = Key::FULLTEXT; |
|
283
|
|
|
|
|
284
|
|
|
if ($name) { |
|
285
|
|
|
$this->indexName = $name; |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
/** |
|
290
|
|
|
* Adds a spatial key the column |
|
291
|
|
|
* @param string $name |
|
292
|
|
|
*/ |
|
293
|
|
|
public function spatial(string $name = null) |
|
294
|
|
|
{ |
|
295
|
|
|
$this->indexKey = Key::SPATIAL; |
|
296
|
|
|
|
|
297
|
|
|
if ($name) { |
|
298
|
|
|
$this->indexName = $name; |
|
299
|
|
|
} |
|
300
|
|
|
} |
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* Adds a type to the column |
|
304
|
|
|
* @param string $type |
|
305
|
|
|
* @param type $constraint |
|
306
|
|
|
*/ |
|
307
|
|
|
public function type(string $type, $constraint = null) |
|
308
|
|
|
{ |
|
309
|
|
|
$this->type = strtoupper($type); |
|
310
|
|
|
$this->constraint = $constraint; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
/** |
|
314
|
|
|
* Adds or removes nullable property |
|
315
|
|
|
* @param bool $indeed |
|
316
|
|
|
*/ |
|
317
|
|
|
public function nullable(bool $indeed = true) |
|
318
|
|
|
{ |
|
319
|
|
|
$this->nullable = (!$indeed ? 'NOT ' : '') . 'NULL'; |
|
320
|
|
|
} |
|
321
|
|
|
|
|
322
|
|
|
/** |
|
323
|
|
|
* Adds default value to the column |
|
324
|
|
|
* @param mixed $value |
|
325
|
|
|
* @param bool $quoted |
|
326
|
|
|
*/ |
|
327
|
|
|
public function default($value, bool $quoted = true) |
|
328
|
|
|
{ |
|
329
|
|
|
$this->default = $value; |
|
330
|
|
|
$this->defaultQuoted = $quoted; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
/** |
|
334
|
|
|
* Adds or removes attribute to the column |
|
335
|
|
|
* @param string $value |
|
336
|
|
|
*/ |
|
337
|
|
|
public function attribute(?string $value) |
|
338
|
|
|
{ |
|
339
|
|
|
$this->attribute = $value ? strtoupper($value) : null; |
|
340
|
|
|
} |
|
341
|
|
|
|
|
342
|
|
|
/** |
|
343
|
|
|
* Adds or removes comment to the column |
|
344
|
|
|
* @param string $comment |
|
345
|
|
|
*/ |
|
346
|
|
|
public function comment(?string $comment) |
|
347
|
|
|
{ |
|
348
|
|
|
$this->comment = $comment; |
|
349
|
|
|
} |
|
350
|
|
|
|
|
351
|
|
|
/** |
|
352
|
|
|
* Adds column after a given column |
|
353
|
|
|
* @param string $columnName |
|
354
|
|
|
*/ |
|
355
|
|
|
public function after(string $columnName) |
|
356
|
|
|
{ |
|
357
|
|
|
$this->afterColumn = $columnName; |
|
358
|
|
|
} |
|
359
|
|
|
|
|
360
|
|
|
/** |
|
361
|
|
|
* Adds quotes on default value |
|
362
|
|
|
* @return bool |
|
363
|
|
|
*/ |
|
364
|
|
|
public function defaultQuoted(): bool |
|
365
|
|
|
{ |
|
366
|
|
|
return $this->defaultQuoted; |
|
367
|
|
|
} |
|
368
|
|
|
|
|
369
|
|
|
} |
|
370
|
|
|
|