|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* _ __ __ _____ _____ ___ ____ _____ |
|
5
|
|
|
* | | / // // ___//_ _// || __||_ _| |
|
6
|
|
|
* | |/ // /(__ ) / / / /| || | | | |
|
7
|
|
|
* |___//_//____/ /_/ /_/ |_||_| |_| |
|
8
|
|
|
* @link https://vistart.me/ |
|
9
|
|
|
* @copyright Copyright (c) 2016 - 2017 vistart |
|
10
|
|
|
* @license https://vistart.me/license/ |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace rhosocial\user\migrations\mysql; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @version 1.0 |
|
17
|
|
|
* @author vistart <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class ColumnSchemaBuilder extends \yii\db\mysql\ColumnSchemaBuilder |
|
20
|
|
|
{ |
|
21
|
|
|
public $collate; |
|
22
|
|
|
|
|
23
|
|
|
public $charset; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @inheritdoc |
|
27
|
|
|
*/ |
|
28
|
|
|
public $categoryMap = [ |
|
29
|
|
|
Schema::TYPE_PK => self::CATEGORY_PK, |
|
30
|
|
|
Schema::TYPE_UPK => self::CATEGORY_PK, |
|
31
|
|
|
Schema::TYPE_BIGPK => self::CATEGORY_PK, |
|
32
|
|
|
Schema::TYPE_UBIGPK => self::CATEGORY_PK, |
|
33
|
|
|
Schema::TYPE_CHAR => self::CATEGORY_STRING, |
|
34
|
|
|
Schema::TYPE_VARCHAR => self::CATEGORY_STRING, |
|
35
|
|
|
Schema::TYPE_STRING => self::CATEGORY_STRING, |
|
36
|
|
|
Schema::TYPE_TEXT => self::CATEGORY_STRING, |
|
37
|
|
|
Schema::TYPE_SMALLINT => self::CATEGORY_NUMERIC, |
|
38
|
|
|
Schema::TYPE_INTEGER => self::CATEGORY_NUMERIC, |
|
39
|
|
|
Schema::TYPE_BIGINT => self::CATEGORY_NUMERIC, |
|
40
|
|
|
Schema::TYPE_FLOAT => self::CATEGORY_NUMERIC, |
|
41
|
|
|
Schema::TYPE_DOUBLE => self::CATEGORY_NUMERIC, |
|
42
|
|
|
Schema::TYPE_DECIMAL => self::CATEGORY_NUMERIC, |
|
43
|
|
|
Schema::TYPE_DATETIME => self::CATEGORY_TIME, |
|
44
|
|
|
Schema::TYPE_TIMESTAMP => self::CATEGORY_TIME, |
|
45
|
|
|
Schema::TYPE_TIME => self::CATEGORY_TIME, |
|
46
|
|
|
Schema::TYPE_DATE => self::CATEGORY_TIME, |
|
47
|
|
|
Schema::TYPE_BINARY => self::CATEGORY_OTHER, |
|
48
|
|
|
Schema::TYPE_BINARY_PK => self::CATEGORY_PK, |
|
49
|
|
|
Schema::TYPE_BOOLEAN => self::CATEGORY_NUMERIC, |
|
50
|
|
|
Schema::TYPE_MONEY => self::CATEGORY_NUMERIC, |
|
51
|
|
|
]; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
|
|
public function __toString() |
|
57
|
|
|
{ |
|
58
|
|
|
switch ($this->getTypeCategory()) { |
|
59
|
|
|
case self::CATEGORY_PK: |
|
60
|
|
|
$format = '{type}{length}{check}{comment}{append}{pos}'; |
|
61
|
|
|
break; |
|
62
|
|
|
case self::CATEGORY_NUMERIC: |
|
63
|
|
|
$format = '{type}{length}{unsigned}{notnull}{unique}{default}{check}{comment}{append}{pos}'; |
|
64
|
|
|
break; |
|
65
|
|
|
default: |
|
66
|
|
|
$format = '{type}{length}{charset}{collate}{notnull}{unique}{default}{check}{comment}{append}{pos}'; |
|
67
|
|
|
} |
|
68
|
|
|
return $this->buildCompleteString($format); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
protected function buildCharsetString() |
|
72
|
|
|
{ |
|
73
|
|
|
return $this->charset ? " CHARACTER SET $this->charset" : ""; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
protected function buildCollateString() |
|
77
|
|
|
{ |
|
78
|
|
|
return $this->collate ? " COLLATE $this->collate" : ""; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Returns the complete column definition from input format |
|
83
|
|
|
* @param string $format the format of the definition. |
|
84
|
|
|
* @return string a string containing the complete column definition. |
|
85
|
|
|
* @since 2.0.8 |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function buildCompleteString($format) |
|
88
|
|
|
{ |
|
89
|
|
|
$placeholderValues = [ |
|
90
|
|
|
'{type}' => $this->type, |
|
91
|
|
|
'{length}' => $this->buildLengthString(), |
|
92
|
|
|
'{unsigned}' => $this->buildUnsignedString(), |
|
93
|
|
|
'{notnull}' => $this->buildNotNullString(), |
|
94
|
|
|
'{charset}' => $this->buildCharsetString(), |
|
95
|
|
|
'{collate}' => $this->buildCollateString(), |
|
96
|
|
|
'{unique}' => $this->buildUniqueString(), |
|
97
|
|
|
'{default}' => $this->buildDefaultString(), |
|
98
|
|
|
'{check}' => $this->buildCheckString(), |
|
99
|
|
|
'{comment}' => $this->buildCommentString(), |
|
100
|
|
|
'{pos}' => $this->isFirst ? $this->buildFirstString() : $this->buildAfterString(), |
|
101
|
|
|
'{append}' => $this->buildAppendString(), |
|
102
|
|
|
]; |
|
103
|
|
|
return strtr($format, $placeholderValues); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Add `collate` constraint. |
|
108
|
|
|
* @param string $collate |
|
109
|
|
|
* @return $this |
|
110
|
|
|
*/ |
|
111
|
|
|
public function collate($collate) |
|
112
|
|
|
{ |
|
113
|
|
|
$this->collate = $collate; |
|
114
|
|
|
return $this; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Add `character set` constraint. |
|
119
|
|
|
* @param string $charset |
|
120
|
|
|
* @return $this |
|
121
|
|
|
*/ |
|
122
|
|
|
public function charset($charset) |
|
123
|
|
|
{ |
|
124
|
|
|
$this->charset = $charset; |
|
125
|
|
|
return $this; |
|
126
|
|
|
} |
|
127
|
|
|
} |