1
|
|
|
<?php |
2
|
|
|
namespace Wandu\Database\Query\Expression; |
3
|
|
|
|
4
|
|
|
use Wandu\Database\Contracts\ExpressionInterface; |
5
|
|
|
use Wandu\Database\Support\Attributes; |
6
|
|
|
use Wandu\Database\Support\Helper; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* @see http://dev.mysql.com/doc/refman/5.7/en/create-table.html |
10
|
|
|
* |
11
|
|
|
* ColumnExpression: |
12
|
|
|
* col_name column_definition |
13
|
|
|
* [FIRST | AFTER col_name ] ............. (for alter table) |
14
|
|
|
* |
15
|
|
|
* column_definition: |
16
|
|
|
* data_type [NOT NULL | NULL] [DEFAULT default_value] |
17
|
|
|
* [AUTO_INCREMENT] [UNIQUE [KEY] | [PRIMARY] KEY] |
18
|
|
|
* [COMMENT 'string'] |
19
|
|
|
* [ReferenceExpression] |
20
|
|
|
* |
21
|
|
|
* data_type: |
22
|
|
|
* BIT[(length)] |
23
|
|
|
* | TINYINT[(length)] [UNSIGNED] [ZEROFILL] |
24
|
|
|
* | SMALLINT[(length)] [UNSIGNED] [ZEROFILL] |
25
|
|
|
* | MEDIUMINT[(length)] [UNSIGNED] [ZEROFILL] |
26
|
|
|
* | INT[(length)] [UNSIGNED] [ZEROFILL] |
27
|
|
|
* | INTEGER[(length)] [UNSIGNED] [ZEROFILL] |
28
|
|
|
* | BIGINT[(length)] [UNSIGNED] [ZEROFILL] |
29
|
|
|
* | REAL[(length,decimals)] [UNSIGNED] [ZEROFILL] |
30
|
|
|
* | DOUBLE[(length,decimals)] [UNSIGNED] [ZEROFILL] |
31
|
|
|
* | FLOAT[(length,decimals)] [UNSIGNED] [ZEROFILL] |
32
|
|
|
* | DECIMAL[(length[,decimals])] [UNSIGNED] [ZEROFILL] |
33
|
|
|
* | NUMERIC[(length[,decimals])] [UNSIGNED] [ZEROFILL] |
34
|
|
|
* | DATE |
35
|
|
|
* | TIME[(fsp)] |
36
|
|
|
* | TIMESTAMP[(fsp)] |
37
|
|
|
* | DATETIME[(fsp)] |
38
|
|
|
* | YEAR |
39
|
|
|
* | CHAR[(length)] [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] |
40
|
|
|
* | VARCHAR(length) [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] |
41
|
|
|
* | BINARY[(length)] |
42
|
|
|
* | VARBINARY(length) |
43
|
|
|
* | TINYBLOB |
44
|
|
|
* | BLOB |
45
|
|
|
* | MEDIUMBLOB |
46
|
|
|
* | LONGBLOB |
47
|
|
|
* | TINYTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] |
48
|
|
|
* | TEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] |
49
|
|
|
* | MEDIUMTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] |
50
|
|
|
* | LONGTEXT [BINARY] [CHARACTER SET charset_name] [COLLATE collation_name] |
51
|
|
|
* | ENUM(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name] |
52
|
|
|
* | SET(value1,value2,value3,...) [CHARACTER SET charset_name] [COLLATE collation_name] |
53
|
|
|
* | JSON |
54
|
|
|
* | spatial_type |
55
|
|
|
* |
56
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression nullable() |
57
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression default(mixed $value) |
58
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression autoIncrement() |
59
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression unique() |
60
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression primary() |
61
|
|
|
* |
62
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression length(int $length) |
63
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression decimal(int $decimal) |
64
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression fsp($fsp) |
65
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression values(array $values) |
66
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression unsigned() |
67
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression binary() |
68
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression charset() |
69
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression collation() |
70
|
|
|
* |
71
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression first() |
72
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression after(string $column) |
73
|
|
|
* |
74
|
|
|
* @method \Wandu\Database\Query\Expression\ColumnExpression comment(string $comment) |
75
|
|
|
*/ |
76
|
|
|
class ColumnExpression implements ExpressionInterface |
77
|
|
|
{ |
78
|
|
|
use Attributes; |
79
|
|
|
|
80
|
|
|
/** @var array */ |
81
|
|
|
protected static $typesHavingLength = [ |
82
|
|
|
'bit', |
83
|
|
|
'tinyint', |
84
|
|
|
'smallint', |
85
|
|
|
'mediumint', |
86
|
|
|
'int', |
87
|
|
|
'integer', |
88
|
|
|
'bigint', |
89
|
|
|
'char', |
90
|
|
|
'varchar', |
91
|
|
|
'binary', |
92
|
|
|
'varbinary', |
93
|
|
|
]; |
94
|
|
|
|
95
|
|
|
/** @var array */ |
96
|
|
|
protected static $typesHavingUnsigned = [ |
97
|
|
|
'bit', |
98
|
|
|
'tinyint', |
99
|
|
|
'smallint', |
100
|
|
|
'mediumint', |
101
|
|
|
'int', |
102
|
|
|
'integer', |
103
|
|
|
'bigint', |
104
|
|
|
'real', |
105
|
|
|
'double', |
106
|
|
|
'float', |
107
|
|
|
'decimal', |
108
|
|
|
'numeric', |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
/** @var array */ |
112
|
|
|
protected static $typesHavingDecimal = [ |
113
|
|
|
'real', |
114
|
|
|
'double', |
115
|
|
|
'float', |
116
|
|
|
'decimal', |
117
|
|
|
'numeric', |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
/** @var array */ |
121
|
|
|
protected static $typesHavingBinary = [ |
122
|
|
|
'char', |
123
|
|
|
'varchar', |
124
|
|
|
'tinytext', |
125
|
|
|
'text', |
126
|
|
|
'mediumtext', |
127
|
|
|
'longtext', |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
/** @var array */ |
131
|
|
|
protected static $typesHavingFsp = [ |
132
|
|
|
'time', |
133
|
|
|
'timestamp', |
134
|
|
|
'datetime', |
135
|
|
|
]; |
136
|
|
|
|
137
|
|
|
/** @var array */ |
138
|
|
|
protected static $typesHavingValues = [ |
139
|
|
|
'enum', |
140
|
|
|
'set', |
141
|
|
|
]; |
142
|
|
|
|
143
|
|
|
/** @var string */ |
144
|
|
|
protected $name; |
145
|
|
|
|
146
|
|
|
/** @var string */ |
147
|
|
|
protected $type; |
148
|
|
|
|
149
|
|
|
/** @var \Wandu\Database\Query\Expression\ReferenceExpression */ |
150
|
|
|
protected $reference; |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string $name |
154
|
|
|
* @param string $type |
155
|
|
|
* @param array $attributes |
156
|
|
|
*/ |
157
|
2 |
|
public function __construct($name, $type, array $attributes = []) |
158
|
|
|
{ |
159
|
2 |
|
$this->name = $name; |
160
|
2 |
|
$this->type = $type; |
161
|
2 |
|
$this->attributes = $attributes; |
162
|
2 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param string $table |
166
|
|
|
* @param string|array $column |
167
|
|
|
* @return \Wandu\Database\Query\Expression\ReferenceExpression |
168
|
|
|
*/ |
169
|
1 |
|
public function reference($table, $column) |
170
|
|
|
{ |
171
|
1 |
|
return $this->reference = new ReferenceExpression($table, is_array($column) ? $column : [$column]); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* {@inheritdoc} |
176
|
|
|
*/ |
177
|
2 |
|
public function toSql() |
178
|
|
|
{ |
179
|
2 |
|
$stringToReturn = "`{$this->name}` " . $this->getTypeString(); |
180
|
2 |
|
if (isset($this->attributes['nullable']) && $this->attributes['nullable']) { |
181
|
2 |
|
$stringToReturn .= ' NULL'; |
182
|
|
|
} else { |
183
|
2 |
|
$stringToReturn .= ' NOT NULL'; |
184
|
|
|
} |
185
|
2 |
|
if (isset($this->attributes['default'])) { |
186
|
2 |
|
$stringToReturn .= " DEFAULT "; |
187
|
2 |
|
if ($this->attributes['default'] instanceof ExpressionInterface) { |
188
|
2 |
|
$stringToReturn .= $this->attributes['default']->toSql(); |
189
|
|
|
} else { |
190
|
1 |
|
$stringToReturn .= "'" . addslashes($this->attributes['default']) . "'"; |
191
|
|
|
} |
192
|
|
|
} |
193
|
2 |
|
if (isset($this->attributes['auto_increment'])) { |
194
|
2 |
|
$stringToReturn .= " AUTO_INCREMENT"; |
195
|
|
|
} |
196
|
2 |
|
if (isset($this->attributes['unique'])) { |
197
|
|
|
$stringToReturn .= ' UNIQUE KEY'; |
198
|
|
|
} |
199
|
2 |
|
if (isset($this->attributes['primary'])) { |
200
|
1 |
|
$stringToReturn .= ' PRIMARY KEY'; |
201
|
|
|
} |
202
|
2 |
|
if (isset($this->attributes['comment'])) { |
203
|
|
|
$comment = str_replace("'", "''", $this->attributes['comment']); |
204
|
|
|
$stringToReturn .= " COMMENT '{$comment}'"; |
205
|
|
|
} |
206
|
2 |
|
if (isset($this->reference)) { |
207
|
1 |
|
$referenceString = $this->reference->toSql(); |
208
|
1 |
|
if ($referenceString) { |
209
|
1 |
|
$stringToReturn .= " {$referenceString}"; |
210
|
|
|
} |
211
|
|
|
} |
212
|
2 |
|
return $stringToReturn; |
213
|
|
|
} |
214
|
|
|
|
215
|
2 |
|
protected function getTypeString() |
216
|
|
|
{ |
217
|
2 |
|
$stringToReturn = strtoupper($this->type); |
218
|
2 |
|
$lowerType = strtolower($this->type); |
219
|
2 |
|
$enableLength = in_array($lowerType, static::$typesHavingLength) && isset($this->attributes['length']); |
220
|
2 |
|
$enableDecimal = in_array($lowerType, static::$typesHavingDecimal) && isset($this->attributes['decimal']); |
221
|
2 |
|
$enableFsp = in_array($lowerType, static::$typesHavingFsp) && isset($this->attributes['fsp']); |
222
|
2 |
|
$enableValues = in_array($lowerType, static::$typesHavingValues) && isset($this->attributes['values']); |
223
|
2 |
|
if ($enableLength || $enableDecimal || $enableFsp || $enableValues) { |
224
|
2 |
|
$stringToReturn .= '('; |
225
|
2 |
|
if ($enableLength && $enableDecimal) { |
226
|
|
|
$stringToReturn .= $this->attributes['length'] . ", " . $this->attributes['decimal']; |
227
|
2 |
|
} elseif ($enableLength) { |
228
|
2 |
|
$stringToReturn .= $this->attributes['length']; |
229
|
1 |
|
} elseif ($enableFsp) { |
230
|
|
|
$stringToReturn .= $this->attributes['fsp']; |
231
|
1 |
|
} elseif ($enableValues) { |
232
|
1 |
|
$stringToReturn .= Helper::arrayImplode(", ", $this->attributes['values'], '\'', '\''); |
233
|
|
|
} |
234
|
2 |
|
$stringToReturn .= ')'; |
235
|
|
|
} |
236
|
2 |
|
if (in_array($lowerType, static::$typesHavingUnsigned) && isset($this->attributes['unsigned'])) { |
237
|
2 |
|
$stringToReturn .= " UNSIGNED"; |
238
|
|
|
} |
239
|
2 |
|
if (in_array($lowerType, static::$typesHavingBinary)) { |
240
|
2 |
|
if (isset($this->attributes['binary'])) { |
241
|
|
|
$stringToReturn .= " BINARY"; |
242
|
|
|
} |
243
|
2 |
|
if (isset($this->attributes['charset'])) { |
244
|
|
|
$stringToReturn .= " CHARACTER SET {$this->attributes['charset']}"; |
245
|
|
|
} |
246
|
2 |
|
if (isset($this->attributes['collation'])) { |
247
|
|
|
$stringToReturn .= " COLLATE {$this->attributes['collation']}"; |
248
|
|
|
} |
249
|
|
|
} |
250
|
2 |
|
if (isset($this->attributes['first']) && $this->attributes['first']) { |
251
|
|
|
$stringToReturn .= ' FIRST'; |
252
|
|
|
} |
253
|
2 |
|
if (isset($this->attributes['after'])) { |
254
|
|
|
$stringToReturn .= " AFTER `{$this->attributes['after']}`"; |
255
|
|
|
} |
256
|
2 |
|
return $stringToReturn; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* {@inheritdoc} |
261
|
|
|
*/ |
262
|
|
|
public function getBindings() |
263
|
|
|
{ |
264
|
|
|
return []; |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|