1 | <?php |
||
73 | class ColumnExpression implements ExpressionInterface |
||
74 | { |
||
75 | use Attributes; |
||
76 | |||
77 | /** @var array */ |
||
78 | protected static $typesHavingLength = [ |
||
79 | 'bit', |
||
80 | 'tinyint', |
||
81 | 'smallint', |
||
82 | 'mediumint', |
||
83 | 'int', |
||
84 | 'integer', |
||
85 | 'bigint', |
||
86 | 'char', |
||
87 | 'varchar', |
||
88 | 'binary', |
||
89 | 'varbinary', |
||
90 | ]; |
||
91 | |||
92 | /** @var array */ |
||
93 | protected static $typesHavingUnsigned = [ |
||
94 | 'bit', |
||
95 | 'tinyint', |
||
96 | 'smallint', |
||
97 | 'mediumint', |
||
98 | 'int', |
||
99 | 'integer', |
||
100 | 'bigint', |
||
101 | 'real', |
||
102 | 'double', |
||
103 | 'float', |
||
104 | 'decimal', |
||
105 | 'numeric', |
||
106 | ]; |
||
107 | |||
108 | /** @var array */ |
||
109 | protected static $typesHavingDecimal = [ |
||
110 | 'real', |
||
111 | 'double', |
||
112 | 'float', |
||
113 | 'decimal', |
||
114 | 'numeric', |
||
115 | ]; |
||
116 | |||
117 | /** @var array */ |
||
118 | protected static $typesHavingBinary = [ |
||
119 | 'char', |
||
120 | 'varchar', |
||
121 | 'tinytext', |
||
122 | 'text', |
||
123 | 'mediumtext', |
||
124 | 'longtext', |
||
125 | ]; |
||
126 | |||
127 | /** @var array */ |
||
128 | protected static $typesHavingFsp = [ |
||
129 | 'time', |
||
130 | 'timestamp', |
||
131 | 'datetime', |
||
132 | ]; |
||
133 | |||
134 | /** @var array */ |
||
135 | protected static $typesHavingValues = [ |
||
136 | 'enum', |
||
137 | 'set', |
||
138 | ]; |
||
139 | |||
140 | /** @var string */ |
||
141 | protected $name; |
||
142 | |||
143 | /** @var string */ |
||
144 | protected $type; |
||
145 | |||
146 | /** @var \Wandu\Database\Query\Expression\ReferenceExpression */ |
||
147 | protected $reference; |
||
148 | |||
149 | /** |
||
150 | * @param string $name |
||
151 | * @param string $type |
||
152 | * @param array $attributes |
||
153 | */ |
||
154 | 2 | public function __construct($name, $type, array $attributes = []) |
|
160 | |||
161 | /** |
||
162 | * @param string $table |
||
163 | * @param string|array $column |
||
164 | * @return \Wandu\Database\Query\Expression\ReferenceExpression |
||
165 | */ |
||
166 | 1 | public function reference($table, $column) |
|
170 | |||
171 | /** |
||
172 | * {@inheritdoc} |
||
173 | */ |
||
174 | 2 | public function toSql() |
|
175 | { |
||
176 | 2 | $stringToReturn = "`{$this->name}` " . $this->getTypeString(); |
|
177 | 2 | if (isset($this->attributes['nullable']) && $this->attributes['nullable']) { |
|
178 | 2 | $stringToReturn .= ' NULL'; |
|
179 | } else { |
||
180 | 2 | $stringToReturn .= ' NOT NULL'; |
|
181 | } |
||
182 | 2 | if (isset($this->attributes['default'])) { |
|
183 | 2 | $stringToReturn .= " DEFAULT "; |
|
184 | 2 | if ($this->attributes['default'] instanceof ExpressionInterface) { |
|
185 | 2 | $stringToReturn .= $this->attributes['default']->toSql(); |
|
186 | } else { |
||
187 | 1 | $stringToReturn .= "'" . addslashes($this->attributes['default']) . "'"; |
|
188 | } |
||
189 | } |
||
190 | 2 | if (isset($this->attributes['auto_increment'])) { |
|
191 | 2 | $stringToReturn .= " AUTO_INCREMENT"; |
|
192 | } |
||
193 | 2 | if (isset($this->attributes['unique'])) { |
|
194 | $stringToReturn .= ' UNIQUE KEY'; |
||
195 | } |
||
196 | 2 | if (isset($this->attributes['primary'])) { |
|
197 | 1 | $stringToReturn .= ' PRIMARY KEY'; |
|
198 | } |
||
199 | 2 | if (isset($this->reference)) { |
|
200 | 1 | $referenceString = $this->reference->toSql(); |
|
201 | 1 | if ($referenceString) { |
|
202 | 1 | $stringToReturn .= " {$referenceString}"; |
|
203 | } |
||
204 | } |
||
205 | 2 | return $stringToReturn; |
|
206 | } |
||
207 | |||
208 | 2 | protected function getTypeString() |
|
209 | { |
||
210 | 2 | $stringToReturn = strtoupper($this->type); |
|
211 | 2 | $lowerType = strtolower($this->type); |
|
212 | 2 | $enableLength = in_array($lowerType, static::$typesHavingLength) && isset($this->attributes['length']); |
|
213 | 2 | $enableDecimal = in_array($lowerType, static::$typesHavingDecimal) && isset($this->attributes['decimal']); |
|
214 | 2 | $enableFsp = in_array($lowerType, static::$typesHavingFsp) && isset($this->attributes['fsp']); |
|
215 | 2 | $enableValues = in_array($lowerType, static::$typesHavingValues) && isset($this->attributes['values']); |
|
216 | 2 | if ($enableLength || $enableDecimal || $enableFsp || $enableValues) { |
|
217 | 2 | $stringToReturn .= '('; |
|
218 | 2 | if ($enableLength && $enableDecimal) { |
|
219 | $stringToReturn .= $this->attributes['length'] . ", " . $this->attributes['decimal']; |
||
220 | 2 | } elseif ($enableLength) { |
|
221 | 2 | $stringToReturn .= $this->attributes['length']; |
|
222 | 1 | } elseif ($enableFsp) { |
|
223 | $stringToReturn .= $this->attributes['fsp']; |
||
224 | 1 | } elseif ($enableValues) { |
|
225 | 1 | $stringToReturn .= Helper::arrayImplode(", ", $this->attributes['values'], '\'', '\''); |
|
226 | } |
||
227 | 2 | $stringToReturn .= ')'; |
|
228 | } |
||
229 | 2 | if (in_array($lowerType, static::$typesHavingUnsigned) && isset($this->attributes['unsigned'])) { |
|
230 | 2 | $stringToReturn .= " UNSIGNED"; |
|
231 | } |
||
232 | 2 | if (in_array($lowerType, static::$typesHavingBinary)) { |
|
233 | 2 | if (isset($this->attributes['binary'])) { |
|
234 | $stringToReturn .= " BINARY"; |
||
235 | } |
||
236 | 2 | if (isset($this->attributes['charset'])) { |
|
237 | $stringToReturn .= " CHARACTER SET {$this->attributes['charset']}"; |
||
238 | } |
||
239 | 2 | if (isset($this->attributes['collation'])) { |
|
240 | $stringToReturn .= " COLLATE {$this->attributes['collation']}"; |
||
241 | } |
||
242 | } |
||
243 | 2 | if (isset($this->attributes['first']) && $this->attributes['first']) { |
|
244 | $stringToReturn .= ' FIRST'; |
||
245 | } |
||
246 | 2 | if (isset($this->attributes['after'])) { |
|
247 | $stringToReturn .= " AFTER `{$this->attributes['after']}`"; |
||
248 | } |
||
249 | 2 | return $stringToReturn; |
|
250 | } |
||
251 | |||
252 | /** |
||
253 | * {@inheritdoc} |
||
254 | */ |
||
255 | public function getBindings() |
||
259 | } |
||
260 |