1
|
|
|
<?php |
2
|
|
|
namespace suda\database\struct; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* 数据表字段创建工具 |
6
|
|
|
* 用于创建数据表字段 |
7
|
|
|
*/ |
8
|
|
|
class Field |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* 自增 AUTO_INCREMENT |
12
|
|
|
* |
13
|
|
|
* @var bool |
14
|
|
|
*/ |
15
|
|
|
protected $auto; |
16
|
|
|
// COMMENT |
17
|
|
|
/** |
18
|
|
|
* @var |
19
|
|
|
*/ |
20
|
|
|
protected $comment; |
21
|
|
|
/** |
22
|
|
|
* @var |
23
|
|
|
*/ |
24
|
|
|
protected $key; //primary unique index |
25
|
|
|
// foreign key |
26
|
|
|
/** |
27
|
|
|
* @var |
28
|
|
|
*/ |
29
|
|
|
protected $foreign; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $name; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $type; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int|string|array|null |
43
|
|
|
*/ |
44
|
|
|
protected $length; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var mixed |
48
|
|
|
*/ |
49
|
|
|
protected $default; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var bool |
53
|
|
|
*/ |
54
|
|
|
protected $hasDefault; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @var bool|null |
58
|
|
|
*/ |
59
|
|
|
protected $null; // isNullable |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @var |
63
|
|
|
*/ |
64
|
|
|
protected $attribute; // binary unsigned |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var |
68
|
|
|
*/ |
69
|
|
|
protected $collation; |
70
|
|
|
/** |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
protected $tableName; |
74
|
|
|
/** |
75
|
|
|
* @var |
76
|
|
|
*/ |
77
|
|
|
protected $charset; |
78
|
|
|
/** |
79
|
|
|
* @var string |
80
|
|
|
*/ |
81
|
|
|
protected $alias; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* |
85
|
|
|
*/ |
86
|
|
|
const BINARY = 'BINARY'; |
87
|
|
|
/** |
88
|
|
|
* |
89
|
|
|
*/ |
90
|
|
|
const UNSIGNED = 'UNSIGNED'; |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* |
94
|
|
|
*/ |
95
|
|
|
const UNIQUE = 'UNIQUE'; |
96
|
|
|
/** |
97
|
|
|
* |
98
|
|
|
*/ |
99
|
|
|
const PRIMARY = 'PRIMARY'; |
100
|
|
|
/** |
101
|
|
|
* |
102
|
|
|
*/ |
103
|
|
|
const INDEX = 'INDEX'; |
104
|
|
|
/** |
105
|
|
|
* |
106
|
|
|
*/ |
107
|
|
|
const KEY = 'KEY'; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Field constructor. |
111
|
|
|
* @param string $tableName |
112
|
|
|
* @param string $name |
113
|
|
|
* @param string $type |
114
|
|
|
* @param int|string|array|null $length |
115
|
|
|
*/ |
116
|
|
|
public function __construct(string $tableName, string $name, string $type, $length = null) |
117
|
|
|
{ |
118
|
|
|
$this->tableName = $tableName; |
119
|
|
|
$this->name = $name; |
120
|
|
|
$this->type = strtoupper($type); |
121
|
|
|
$this->length = $length; |
122
|
|
|
$this->hasDefault = false; |
123
|
|
|
$this->alias = $name; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $name |
128
|
|
|
* @return $this |
129
|
|
|
*/ |
130
|
|
|
public function alias(string $name) |
131
|
|
|
{ |
132
|
|
|
$this->alias = $name; |
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param string $charset |
138
|
|
|
* @return $this |
139
|
|
|
*/ |
140
|
|
|
public function charset(string $charset) |
141
|
|
|
{ |
142
|
|
|
$this->charset = 'CHARACTER SET ' . $charset; |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param string $comment |
148
|
|
|
* @return $this |
149
|
|
|
*/ |
150
|
|
|
public function comment(string $comment) |
151
|
|
|
{ |
152
|
|
|
$this->comment = $comment; |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param $length |
158
|
|
|
* @return $this |
159
|
|
|
*/ |
160
|
|
|
public function length($length) |
161
|
|
|
{ |
162
|
|
|
$this->length = $length; |
163
|
|
|
return $this; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return $this |
168
|
|
|
*/ |
169
|
|
|
public function key() |
170
|
|
|
{ |
171
|
|
|
$this->key = self::KEY; |
172
|
|
|
return $this; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @return $this |
177
|
|
|
*/ |
178
|
|
|
public function primary() |
179
|
|
|
{ |
180
|
|
|
$this->key = self::PRIMARY; |
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return $this |
186
|
|
|
*/ |
187
|
|
|
public function index() |
188
|
|
|
{ |
189
|
|
|
$this->key = self::INDEX; |
190
|
|
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
|
|
public function unique() |
197
|
|
|
{ |
198
|
|
|
$this->key = self::UNIQUE; |
199
|
|
|
return $this; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param string $collate |
204
|
|
|
* @return $this |
205
|
|
|
*/ |
206
|
|
|
public function collate(string $collate) |
207
|
|
|
{ |
208
|
|
|
$this->collation = $collate; |
209
|
|
|
return $this; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return $this |
214
|
|
|
*/ |
215
|
|
|
public function auto() |
216
|
|
|
{ |
217
|
|
|
$this->auto = true; |
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* @param Field $field |
223
|
|
|
* @return $this |
224
|
|
|
*/ |
225
|
|
|
public function foreign(Field $field) |
226
|
|
|
{ |
227
|
|
|
$this->foreign = $field; |
228
|
|
|
$this->type = $field->type; |
229
|
|
|
$this->length = $field->length; |
230
|
|
|
$this->default = null; |
231
|
|
|
if ($field->attribute) { |
232
|
|
|
$this->attribute = $field->attribute; |
233
|
|
|
} |
234
|
|
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* @param bool $set |
239
|
|
|
* @return $this |
240
|
|
|
*/ |
241
|
|
|
public function null(bool $set = true) |
242
|
|
|
{ |
243
|
|
|
$this->null = $set; |
244
|
|
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* @param $value |
249
|
|
|
* @return $this |
250
|
|
|
*/ |
251
|
|
|
public function default($value) |
252
|
|
|
{ |
253
|
|
|
$this->hasDefault = true; |
254
|
|
|
$this->default = $value; |
255
|
|
|
if (null === $value) { |
256
|
|
|
$this->null = true; |
257
|
|
|
} |
258
|
|
|
return $this; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* @return $this |
263
|
|
|
*/ |
264
|
|
|
public function binary() |
265
|
|
|
{ |
266
|
|
|
$this->attribute = self::BINARY; |
267
|
|
|
return $this; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* @return $this |
272
|
|
|
*/ |
273
|
|
|
public function unsigned() |
274
|
|
|
{ |
275
|
|
|
$this->attribute = self::UNSIGNED; |
276
|
|
|
return $this; |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Get the value of auto |
282
|
|
|
*/ |
283
|
|
|
public function getAuto() |
284
|
|
|
{ |
285
|
|
|
return $this->auto; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Get the value of comment |
290
|
|
|
*/ |
291
|
|
|
public function getComment() |
292
|
|
|
{ |
293
|
|
|
return $this->comment; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Get the value of key |
298
|
|
|
*/ |
299
|
|
|
public function getType() |
300
|
|
|
{ |
301
|
|
|
return $this->key; |
302
|
|
|
} |
303
|
|
|
|
304
|
|
|
/** |
305
|
|
|
* Get the value of foreign |
306
|
|
|
*/ |
307
|
|
|
public function getForeignKey() |
308
|
|
|
{ |
309
|
|
|
return $this->foreign; |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
/** |
313
|
|
|
* Get the value of name |
314
|
|
|
*/ |
315
|
|
|
public function getName() |
316
|
|
|
{ |
317
|
|
|
return $this->name; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* Get the value of type |
322
|
|
|
*/ |
323
|
|
|
public function getValueType() |
324
|
|
|
{ |
325
|
|
|
return $this->type; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Get the value of length |
330
|
|
|
*/ |
331
|
|
|
public function getLength() |
332
|
|
|
{ |
333
|
|
|
return $this->length; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* Get the value of default |
338
|
|
|
*/ |
339
|
|
|
public function getDefault() |
340
|
|
|
{ |
341
|
|
|
return $this->default; |
342
|
|
|
} |
343
|
|
|
|
344
|
|
|
/** |
345
|
|
|
* Get the value of hasDefault |
346
|
|
|
*/ |
347
|
|
|
public function hasDefault() |
348
|
|
|
{ |
349
|
|
|
return $this->hasDefault; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
/** |
353
|
|
|
* 是否为主键 |
354
|
|
|
* |
355
|
|
|
* @return boolean |
356
|
|
|
*/ |
357
|
|
|
public function isPrimary():bool |
358
|
|
|
{ |
359
|
|
|
return $this->key == self::PRIMARY; |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
/** |
363
|
|
|
* 是否为可空 |
364
|
|
|
* |
365
|
|
|
* @return boolean |
366
|
|
|
*/ |
367
|
|
|
public function isNullable(): ?bool |
368
|
|
|
{ |
369
|
|
|
return $this->null; |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* 是否支持 |
374
|
|
|
* |
375
|
|
|
* @return boolean |
376
|
|
|
*/ |
377
|
|
|
public function isAutoIncrement(): bool |
378
|
|
|
{ |
379
|
|
|
return $this->auto === true; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
/** |
383
|
|
|
* Get the value of attribute |
384
|
|
|
*/ |
385
|
|
|
public function getAttribute() |
386
|
|
|
{ |
387
|
|
|
return $this->attribute; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
/** |
391
|
|
|
* Get the value of collation |
392
|
|
|
*/ |
393
|
|
|
public function getCollation() |
394
|
|
|
{ |
395
|
|
|
return $this->collation; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* Get the value of tableName |
400
|
|
|
*/ |
401
|
|
|
public function getTableName() |
402
|
|
|
{ |
403
|
|
|
return $this->tableName; |
404
|
|
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Get the value of charset |
408
|
|
|
*/ |
409
|
|
|
public function getCharset() |
410
|
|
|
{ |
411
|
|
|
return $this->charset; |
412
|
|
|
} |
413
|
|
|
|
414
|
|
|
/** |
415
|
|
|
* Get the value of alias |
416
|
|
|
*/ |
417
|
|
|
public function getAlias() |
418
|
|
|
{ |
419
|
|
|
return $this->alias ?? $this->name; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
/** |
423
|
|
|
* @param Field $field |
424
|
|
|
* @return bool |
425
|
|
|
*/ |
426
|
|
|
public function equals(Field $field) { |
427
|
|
|
if ($this->name !== $field->name) { |
428
|
|
|
return false; |
429
|
|
|
} |
430
|
|
|
if ($this->type !== $field->type) { |
431
|
|
|
return false; |
432
|
|
|
} |
433
|
|
|
if ($this->length !== $field->length) { |
434
|
|
|
return false; |
435
|
|
|
} |
436
|
|
|
return true; |
437
|
|
|
} |
438
|
|
|
} |
439
|
|
|
|