|
1
|
|
|
<?php |
|
2
|
|
|
namespace Wandu\Database\Query; |
|
3
|
|
|
|
|
4
|
|
|
use Wandu\Database\Contracts\ExpressionInterface; |
|
5
|
|
|
use Wandu\Database\Contracts\QueryInterface; |
|
6
|
|
|
use Wandu\Database\Query\Expression\ColumnExpression; |
|
7
|
|
|
use Wandu\Database\Query\Expression\ConstraintExpression; |
|
8
|
|
|
use Wandu\Database\Support\Attributes; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* CREATE TABLE [IF NOT EXISTS] $table |
|
12
|
|
|
* (create_definition,...) |
|
13
|
|
|
* [table_options] |
|
14
|
|
|
* |
|
15
|
|
|
* create_definition: |
|
16
|
|
|
* ColumnExpression |
|
17
|
|
|
* | ConstraintExpression |
|
18
|
|
|
* |
|
19
|
|
|
* table_option: |
|
20
|
|
|
* ENGINE [=] engine_name |
|
21
|
|
|
* | [DEFAULT] CHARACTER SET [=] charset_name |
|
22
|
|
|
* | [DEFAULT] COLLATE [=] collation_name |
|
23
|
|
|
* |
|
24
|
|
|
* @see http://dev.mysql.com/doc/refman/5.7/en/create-table.html |
|
25
|
|
|
* @method \Wandu\Database\Query\CreateQuery ifNotExists() |
|
26
|
|
|
* @method \Wandu\Database\Query\CreateQuery engine(string $engine) |
|
27
|
|
|
* @method \Wandu\Database\Query\CreateQuery charset(string $charset) |
|
28
|
|
|
* @method \Wandu\Database\Query\CreateQuery collation(string $collation) |
|
29
|
|
|
*/ |
|
30
|
|
|
class CreateQuery implements QueryInterface |
|
31
|
|
|
{ |
|
32
|
|
|
use Attributes; |
|
33
|
|
|
|
|
34
|
|
|
/** @var string */ |
|
35
|
|
|
protected $table; |
|
36
|
|
|
|
|
37
|
|
|
/** @var array|\Wandu\Database\Contracts\ExpressionInterface[] */ |
|
38
|
|
|
protected $columns = []; |
|
39
|
|
|
|
|
40
|
|
|
/** @var array|\Wandu\Database\Contracts\ExpressionInterface[] */ |
|
41
|
|
|
protected $constraints = []; |
|
42
|
|
|
|
|
43
|
|
|
/** @var array */ |
|
44
|
|
|
protected $options; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param string $table |
|
48
|
|
|
* @param callable $defineHandler |
|
49
|
|
|
* @param array $options |
|
50
|
|
|
*/ |
|
51
|
2 |
|
public function __construct($table, callable $defineHandler = null, array $options = []) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
2 |
|
$this->table = $table; |
|
54
|
2 |
|
call_user_func($defineHandler, $this); |
|
55
|
2 |
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $name |
|
59
|
|
|
* @param int $length |
|
60
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
61
|
|
|
*/ |
|
62
|
|
|
public function bit($name, $length = 1) |
|
63
|
|
|
{ |
|
64
|
|
|
return $this->addColumn($name, 'bit', [ |
|
65
|
|
|
'length' => $length, |
|
66
|
|
|
]); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param string $name |
|
71
|
|
|
* @param int $length |
|
72
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
73
|
|
|
*/ |
|
74
|
|
|
public function tinyInteger($name, $length = 4) |
|
75
|
|
|
{ |
|
76
|
|
|
return $this->addColumn($name, 'tinyint', [ |
|
77
|
|
|
'length' => $length, |
|
78
|
|
|
]); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @param string $name |
|
83
|
|
|
* @param int $length |
|
84
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
85
|
|
|
*/ |
|
86
|
|
|
public function smallInteger($name, $length = 6) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->addColumn($name, 'smallint', [ |
|
89
|
|
|
'length' => $length, |
|
90
|
|
|
]); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $name |
|
95
|
|
|
* @param int $length |
|
96
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
97
|
|
|
*/ |
|
98
|
|
|
public function mediumInteger($name, $length = 9) |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->addColumn($name, 'mediumint', [ |
|
101
|
|
|
'length' => $length, |
|
102
|
|
|
]); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param string $name |
|
107
|
|
|
* @param int $length |
|
108
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
109
|
|
|
*/ |
|
110
|
|
|
public function integer($name, $length = 11) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->addColumn($name, 'int', [ |
|
113
|
|
|
'length' => $length, |
|
114
|
|
|
]); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* @param string $name |
|
119
|
|
|
* @param int $length |
|
120
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
121
|
|
|
*/ |
|
122
|
2 |
|
public function bigInteger($name, $length = 20) |
|
123
|
|
|
{ |
|
124
|
2 |
|
return $this->addColumn($name, 'bigint', [ |
|
125
|
2 |
|
'length' => $length, |
|
126
|
|
|
]); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @param string $name |
|
131
|
|
|
* @param int $length |
|
132
|
|
|
* @param int $decimal |
|
133
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
134
|
|
|
*/ |
|
135
|
|
|
public function double($name, $length = null, $decimal = null) |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->addColumn($name, 'real', [ |
|
138
|
|
|
'length' => $length, |
|
139
|
|
|
'decimal' => $decimal, |
|
140
|
|
|
]); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param string $name |
|
145
|
|
|
* @param int $length |
|
146
|
|
|
* @param int $decimal |
|
147
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
148
|
|
|
*/ |
|
149
|
|
|
public function float($name, $length = null, $decimal = null) |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->addColumn($name, 'real', [ |
|
152
|
|
|
'length' => $length, |
|
153
|
|
|
'decimal' => $decimal, |
|
154
|
|
|
]); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param string $name |
|
159
|
|
|
* @param int $fsp |
|
160
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
161
|
|
|
*/ |
|
162
|
|
|
public function date($name, $fsp = null) |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->addColumn($name, 'date', [ |
|
165
|
|
|
'fsp' => $fsp, |
|
166
|
|
|
]); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param string $name |
|
171
|
|
|
* @param int $fsp |
|
172
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
173
|
|
|
*/ |
|
174
|
|
|
public function time($name, $fsp = null) |
|
175
|
|
|
{ |
|
176
|
|
|
return $this->addColumn($name, 'time', [ |
|
177
|
|
|
'fsp' => $fsp, |
|
178
|
|
|
]); |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @param string $name |
|
183
|
|
|
* @param int $fsp |
|
184
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
185
|
|
|
*/ |
|
186
|
2 |
|
public function timestamp($name, $fsp = null) |
|
187
|
|
|
{ |
|
188
|
2 |
|
return $this->addColumn($name, 'timestamp', [ |
|
189
|
2 |
|
'fsp' => $fsp, |
|
190
|
|
|
]); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @param string $name |
|
195
|
|
|
* @param int $fsp |
|
196
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
197
|
|
|
*/ |
|
198
|
|
|
public function datetime($name, $fsp = null) |
|
199
|
|
|
{ |
|
200
|
|
|
return $this->addColumn($name, 'datetime', [ |
|
201
|
|
|
'fsp' => $fsp, |
|
202
|
|
|
]); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* @param string $name |
|
207
|
|
|
* @param int $length |
|
208
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
209
|
|
|
*/ |
|
210
|
2 |
|
public function string($name, $length = 255) |
|
211
|
|
|
{ |
|
212
|
2 |
|
return $this->addColumn($name, 'varchar', [ |
|
213
|
2 |
|
'length' => $length, |
|
214
|
|
|
]); |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param string $name |
|
219
|
|
|
* @param int $length |
|
220
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
221
|
|
|
*/ |
|
222
|
|
|
public function char($name, $length = 1) |
|
223
|
|
|
{ |
|
224
|
|
|
return $this->addColumn($name, 'char', [ |
|
225
|
|
|
'length' => $length, |
|
226
|
|
|
]); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param string $name |
|
231
|
|
|
* @param int $length |
|
232
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
233
|
|
|
*/ |
|
234
|
1 |
|
public function varchar($name, $length) |
|
235
|
|
|
{ |
|
236
|
1 |
|
return $this->addColumn($name, 'varchar', [ |
|
237
|
1 |
|
'length' => $length, |
|
238
|
|
|
]); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
/** |
|
242
|
|
|
* @param string $name |
|
243
|
|
|
* @param int $length |
|
244
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
245
|
|
|
*/ |
|
246
|
|
|
public function binary($name, $length = 1) |
|
247
|
|
|
{ |
|
248
|
|
|
return $this->addColumn($name, 'binary', [ |
|
249
|
|
|
'length' => $length, |
|
250
|
|
|
]); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @param string $name |
|
255
|
|
|
* @param int $length |
|
256
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
257
|
|
|
*/ |
|
258
|
1 |
|
public function varbinary($name, $length) |
|
259
|
|
|
{ |
|
260
|
1 |
|
return $this->addColumn($name, 'varbinary', [ |
|
261
|
1 |
|
'length' => $length, |
|
262
|
|
|
]); |
|
263
|
|
|
} |
|
264
|
|
|
|
|
265
|
|
|
/** |
|
266
|
|
|
* @param string $name |
|
267
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
268
|
|
|
*/ |
|
269
|
|
|
public function tinyBlob($name) |
|
270
|
|
|
{ |
|
271
|
|
|
return $this->addColumn($name, 'tinyblob'); |
|
272
|
|
|
} |
|
273
|
|
|
|
|
274
|
|
|
/** |
|
275
|
|
|
* @param string $name |
|
276
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
277
|
|
|
*/ |
|
278
|
1 |
|
public function blob($name) |
|
279
|
|
|
{ |
|
280
|
1 |
|
return $this->addColumn($name, 'blob'); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
/** |
|
284
|
|
|
* @param string $name |
|
285
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
286
|
|
|
*/ |
|
287
|
|
|
public function mediumBlob($name) |
|
288
|
|
|
{ |
|
289
|
|
|
return $this->addColumn($name, 'mediumblob'); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* @param string $name |
|
294
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
295
|
|
|
*/ |
|
296
|
|
|
public function longBlob($name) |
|
297
|
|
|
{ |
|
298
|
|
|
return $this->addColumn($name, 'longblob'); |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
/** |
|
302
|
|
|
* @param string $name |
|
303
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
304
|
|
|
*/ |
|
305
|
|
|
public function tinyText($name) |
|
306
|
|
|
{ |
|
307
|
|
|
return $this->addColumn($name, 'tinytext'); |
|
308
|
|
|
} |
|
309
|
|
|
|
|
310
|
|
|
/** |
|
311
|
|
|
* @param string $name |
|
312
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
313
|
|
|
*/ |
|
314
|
|
|
public function text($name) |
|
315
|
|
|
{ |
|
316
|
|
|
return $this->addColumn($name, 'text'); |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* @param string $name |
|
321
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
322
|
|
|
*/ |
|
323
|
|
|
public function mediumText($name) |
|
324
|
|
|
{ |
|
325
|
|
|
return $this->addColumn($name, 'mediumtext'); |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
/** |
|
329
|
|
|
* @param string $name |
|
330
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
331
|
|
|
*/ |
|
332
|
1 |
|
public function longText($name) |
|
333
|
|
|
{ |
|
334
|
1 |
|
return $this->addColumn($name, 'longText'); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* @param string $name |
|
339
|
|
|
* @param array $values |
|
340
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
341
|
|
|
*/ |
|
342
|
1 |
|
public function enum($name, array $values = []) |
|
343
|
|
|
{ |
|
344
|
1 |
|
return $this->addColumn($name, 'enum', [ |
|
345
|
1 |
|
'values' => $values, |
|
346
|
|
|
]); |
|
347
|
|
|
} |
|
348
|
|
|
|
|
349
|
|
|
/** |
|
350
|
|
|
* @param string $name |
|
351
|
|
|
* @param array $values |
|
352
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
353
|
|
|
*/ |
|
354
|
|
|
public function set($name, array $values = []) |
|
355
|
|
|
{ |
|
356
|
|
|
return $this->addColumn($name, 'set', [ |
|
357
|
|
|
'values' => $values, |
|
358
|
|
|
]); |
|
359
|
|
|
} |
|
360
|
|
|
|
|
361
|
|
|
/** |
|
362
|
|
|
* @param string $name |
|
363
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression |
|
364
|
|
|
*/ |
|
365
|
|
|
public function json($name) |
|
366
|
|
|
{ |
|
367
|
|
|
return $this->addColumn($name, 'json'); |
|
368
|
|
|
} |
|
369
|
|
|
|
|
370
|
|
|
/** |
|
371
|
|
|
* @param string|\Wandu\Database\Contracts\ExpressionInterface $name |
|
372
|
|
|
* @param string $type |
|
373
|
|
|
* @param array $attributes |
|
374
|
|
|
* @return \Wandu\Database\Query\Expression\ColumnExpression|\Wandu\Database\Contracts\ExpressionInterface |
|
375
|
|
|
*/ |
|
376
|
2 |
|
public function addColumn($name, $type = null, array $attributes = []) |
|
377
|
|
|
{ |
|
378
|
2 |
|
if ($name instanceof ExpressionInterface) { |
|
379
|
1 |
|
return $this->columns[] = $name; |
|
380
|
|
|
} |
|
381
|
2 |
|
return $this->columns[] = new ColumnExpression($name, $type, $attributes); |
|
382
|
|
|
} |
|
383
|
|
|
|
|
384
|
|
|
/** |
|
385
|
|
|
* @param array|string $column |
|
386
|
|
|
* @return \Wandu\Database\Query\Expression\ConstraintExpression |
|
387
|
|
|
*/ |
|
388
|
1 |
|
public function primaryKey($column) |
|
389
|
|
|
{ |
|
390
|
1 |
|
return $this->addConstraint($column, null, [ |
|
391
|
1 |
|
'key_type' => ConstraintExpression::KEY_TYPE_PRIMARY, |
|
392
|
|
|
]); |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
/** |
|
396
|
|
|
* @param array|string $column |
|
397
|
|
|
* @param string $name |
|
398
|
|
|
* @return \Wandu\Database\Query\Expression\ConstraintExpression |
|
399
|
|
|
*/ |
|
400
|
1 |
|
public function uniqueKey($column, $name = null) |
|
401
|
|
|
{ |
|
402
|
1 |
|
return $this->addConstraint($column, $name, [ |
|
403
|
1 |
|
'key_type' => ConstraintExpression::KEY_TYPE_UNIQUE, |
|
404
|
|
|
]); |
|
405
|
|
|
} |
|
406
|
|
|
|
|
407
|
|
|
/** |
|
408
|
|
|
* @param array|string $column |
|
409
|
|
|
* @param string $name |
|
410
|
|
|
* @return \Wandu\Database\Query\Expression\ConstraintExpression |
|
411
|
|
|
*/ |
|
412
|
1 |
|
public function foreignKey($column, $name = null) |
|
413
|
|
|
{ |
|
414
|
1 |
|
return $this->addConstraint($column, $name, [ |
|
415
|
1 |
|
'key_type' => ConstraintExpression::KEY_TYPE_FOREIGN, |
|
416
|
|
|
]); |
|
417
|
|
|
} |
|
418
|
|
|
|
|
419
|
|
|
/** |
|
420
|
|
|
* @param array|string $column |
|
421
|
|
|
* @param string $name |
|
422
|
|
|
* @return \Wandu\Database\Query\Expression\ConstraintExpression |
|
423
|
|
|
*/ |
|
424
|
1 |
|
public function index($column, $name = null) |
|
425
|
|
|
{ |
|
426
|
1 |
|
return $this->addConstraint($column, $name); |
|
427
|
|
|
} |
|
428
|
|
|
|
|
429
|
|
|
/** |
|
430
|
|
|
* @param array|string|\Wandu\Database\Contracts\ExpressionInterface $column |
|
431
|
|
|
* @param string $name |
|
432
|
|
|
* @param array $attributes |
|
433
|
|
|
* @return \Wandu\Database\Query\Expression\ConstraintExpression|\Wandu\Database\Contracts\ExpressionInterface |
|
434
|
|
|
*/ |
|
435
|
1 |
|
public function addConstraint($column, $name = null, array $attributes = []) |
|
436
|
|
|
{ |
|
437
|
1 |
|
if ($column instanceof ExpressionInterface) { |
|
438
|
1 |
|
return $this->constraints[] = $column; |
|
439
|
|
|
} |
|
440
|
1 |
|
return $this->constraints[] = new ConstraintExpression( |
|
441
|
1 |
|
is_array($column) ? $column : [$column], |
|
442
|
1 |
|
$name, |
|
443
|
1 |
|
$attributes |
|
444
|
|
|
); |
|
445
|
|
|
} |
|
446
|
|
|
|
|
447
|
2 |
|
public function toSql() |
|
448
|
|
|
{ |
|
449
|
2 |
|
$sql = "CREATE TABLE"; |
|
450
|
2 |
|
if (isset($this->attributes['if_not_exists'])) { |
|
451
|
2 |
|
$sql .= ' IF NOT EXISTS'; |
|
452
|
|
|
} |
|
453
|
2 |
|
$sql .= " `{$this->table}` (\n "; |
|
454
|
2 |
|
$sql .= implode(",\n ", array_reduce(array_merge($this->columns, $this->constraints), function ($carry, ExpressionInterface $definition) { |
|
455
|
2 |
|
$definitionAsString = $definition->toSql(); |
|
456
|
2 |
|
if ($definitionAsString) { |
|
457
|
2 |
|
$carry[] = $definitionAsString; |
|
458
|
|
|
} |
|
459
|
2 |
|
return $carry; |
|
460
|
2 |
|
}, [])); |
|
461
|
2 |
|
$sql .= "\n)"; |
|
462
|
2 |
|
if (isset($this->attributes['engine'])) { |
|
463
|
1 |
|
$sql .= " ENGINE={$this->attributes['engine']}"; |
|
464
|
|
|
} |
|
465
|
2 |
|
if (isset($this->attributes['charset'])) { |
|
466
|
1 |
|
$sql .= " CHARSET={$this->attributes['charset']}"; |
|
467
|
|
|
} |
|
468
|
2 |
|
if (isset($this->attributes['collation'])) { |
|
469
|
1 |
|
$sql .= " COLLATE={$this->attributes['collation']}"; |
|
470
|
|
|
} |
|
471
|
2 |
|
return $sql; |
|
472
|
|
|
} |
|
473
|
|
|
|
|
474
|
|
|
/** |
|
475
|
|
|
* {@inheritdoc} |
|
476
|
|
|
*/ |
|
477
|
|
|
public function getBindings() |
|
478
|
|
|
{ |
|
479
|
|
|
return []; |
|
480
|
|
|
} |
|
481
|
|
|
} |
|
482
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.