1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class CreateTable |
4
|
|
|
* |
5
|
|
|
* @filesource CreateTable.php |
6
|
|
|
* @created 03.06.2017 |
7
|
|
|
* @package chillerlan\Database\Query\Dialects\MySQL |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\Database\Query\Dialects\MySQL; |
14
|
|
|
|
15
|
|
|
use chillerlan\Database\Query\CreateTableAbstract; |
16
|
|
|
use chillerlan\Database\Query\QueryException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @link https://dev.mysql.com/doc/refman/5.7/en/create-table.html |
20
|
|
|
*/ |
21
|
|
|
class CreateTable extends CreateTableAbstract{ |
22
|
|
|
|
23
|
|
|
public function sql():string{ |
24
|
|
|
|
25
|
|
|
if(empty($this->name)){ |
26
|
|
|
throw new QueryException('no name specified'); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
$sql = 'CREATE '; |
30
|
|
|
$sql .= $this->temp ? 'TEMPORARY ' : ''; |
31
|
|
|
$sql .= 'TABLE '; |
32
|
|
|
$sql .= $this->ifNotExists ? 'IF NOT EXISTS ' : ''; |
33
|
|
|
$sql .= $this->quote($this->name); |
34
|
|
|
|
35
|
|
|
if(!empty($this->cols)){ |
36
|
|
|
$sql .= ' ('.PHP_EOL."\t".implode(','.PHP_EOL."\t", $this->cols); |
37
|
|
|
|
38
|
|
|
if($this->primaryKey){ |
39
|
|
|
$sql .=','.PHP_EOL."\t".'PRIMARY KEY ('.$this->quote($this->primaryKey).')'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$sql .= PHP_EOL.')'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$sql .= ''; |
46
|
|
|
|
47
|
|
|
return $sql; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
protected function fieldspec( |
51
|
|
|
string $name, |
52
|
|
|
string $type, |
53
|
|
|
$length = null, |
54
|
|
|
string $attribute = null, |
55
|
|
|
string $collation = null, |
56
|
|
|
bool $isNull = false, |
57
|
|
|
string $defaultType = null, |
58
|
|
|
$defaultValue = null, |
59
|
|
|
string $extra = null |
60
|
|
|
){ |
61
|
|
|
$name = trim($name); |
62
|
|
|
$type = strtoupper(trim($type)); |
63
|
|
|
|
64
|
|
|
$field = [$this->quote($name)]; |
65
|
|
|
|
66
|
|
|
// @todo: whitelist types? |
67
|
|
|
$nolengthtypes = ['DATE', 'TINYBLOB', 'TINYTEXT', 'BLOB', 'TEXT', 'MEDIUMBLOB', |
68
|
|
|
'MEDIUMTEXT', 'LONGBLOB', 'LONGTEXT', 'SERIAL', 'BOOLEAN', 'UUID']; |
69
|
|
|
|
70
|
|
|
$field[] = (is_int($length) || is_string($length) && count(explode(',', $length)) === 2) && !in_array($type, $nolengthtypes) |
71
|
|
|
? $type.'('. $length . ')' |
72
|
|
|
: $type; |
73
|
|
|
|
74
|
|
|
if($attribute){ |
|
|
|
|
75
|
|
|
$field[] = strtoupper($attribute); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$collationtypes = ['TINYTEXT', 'TEXT', 'MEDIUMTEXT', 'LONGTEXT', 'VARCHAR', 'CHAR', 'ENUM', 'SET']; |
79
|
|
|
if($collation && in_array($type, $collationtypes)){ |
|
|
|
|
80
|
|
|
list($charset) = explode('_', $collation); |
81
|
|
|
|
82
|
|
|
$field[] = 'CHARACTER SET '.$charset; |
83
|
|
|
|
84
|
|
|
if($charset !== $collation){ |
85
|
|
|
$field[] = 'COLLATE '.$collation; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$field[] = $isNull ? 'NULL' : 'NOT NULL'; |
91
|
|
|
|
92
|
|
|
$defaultType = strtoupper($defaultType); |
93
|
|
|
|
94
|
|
|
if($defaultType === 'USER_DEFINED'){ |
95
|
|
|
|
96
|
|
|
switch(true){ |
97
|
|
|
case $type === 'TIMESTAMP' && intval($defaultValue) === 0: |
98
|
|
|
$field[] = 'DEFAULT 0'; |
99
|
|
|
break; |
100
|
|
|
case $type === 'BIT': |
101
|
|
|
$field[] = 'DEFAULT b\''.preg_replace('/[^01]/', '0', $defaultValue).'\''; |
102
|
|
|
break; |
103
|
|
|
case $type === 'BOOLEAN': |
104
|
|
|
$field[] = 'DEFAULT '.preg_match('/^1|T|TRUE|YES$/i', $defaultValue) ? 'TRUE' : 'FALSE'; |
105
|
|
|
break; |
106
|
|
|
case $type === 'BINARY' || $type === 'VARBINARY': |
107
|
|
|
$field[] = 'DEFAULT 0x'.$defaultValue; |
108
|
|
|
break; |
109
|
|
|
case strtoupper($defaultValue) === 'NULL' && $isNull: |
110
|
|
|
$field[] = 'DEFAULT NULL'; |
111
|
|
|
break; |
112
|
|
|
default: |
113
|
|
|
$field[] = 'DEFAULT \''.$defaultValue.'\''; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
} |
117
|
|
View Code Duplication |
else if($defaultType === 'CURRENT_TIMESTAMP'){ |
|
|
|
|
118
|
|
|
$field[] = 'DEFAULT CURRENT_TIMESTAMP'; |
119
|
|
|
} |
120
|
|
|
else if($defaultType === 'NULL' && $isNull){ |
121
|
|
|
$field[] = 'DEFAULT NULL'; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
|
125
|
|
|
if($extra){ |
|
|
|
|
126
|
|
|
$field[] = $extra; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return implode(' ', $field); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: