1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class CreateTableAbstract |
4
|
|
|
* |
5
|
|
|
* @filesource CreateTableAbstract.php |
6
|
|
|
* @created 04.06.2017 |
7
|
|
|
* @package chillerlan\Database\Query\Dialects |
8
|
|
|
* @author Smiley <[email protected]> |
9
|
|
|
* @copyright 2017 Smiley |
10
|
|
|
* @license MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace chillerlan\Database\Query; |
14
|
|
|
|
15
|
|
|
abstract class CreateTableAbstract extends StatementAbstract implements CreateTableInterface{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $name; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var bool |
24
|
|
|
*/ |
25
|
|
|
protected $ifNotExists = false; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $temp = false; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $primaryKey; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $cols = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
protected $collate; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $collation |
49
|
|
|
* |
50
|
|
|
* @return \chillerlan\Database\Query\CreateTableInterface |
51
|
|
|
*/ |
52
|
|
View Code Duplication |
public function charset(string $collation):CreateTableInterface{ |
|
|
|
|
53
|
|
|
$collation = trim($collation); |
54
|
|
|
|
55
|
|
|
if(!empty($collation)){ |
56
|
|
|
$this->collate = $collation; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return \chillerlan\Database\Query\CreateTableInterface |
64
|
|
|
*/ |
65
|
|
|
public function temp():CreateTableInterface{ |
66
|
|
|
$this->temp = true; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return \chillerlan\Database\Query\CreateTableInterface |
73
|
|
|
*/ |
74
|
|
|
public function ifNotExists():CreateTableInterface{ |
75
|
|
|
$this->ifNotExists = true; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string|null $tablename |
82
|
|
|
* |
83
|
|
|
* @return \chillerlan\Database\Query\CreateTableInterface |
84
|
|
|
*/ |
85
|
|
|
public function name(string $tablename = null):CreateTableInterface{ |
86
|
|
|
$name = trim($tablename); |
87
|
|
|
|
88
|
|
|
if(!empty($name)){ |
89
|
|
|
$this->name = $tablename; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $field |
97
|
|
|
* |
98
|
|
|
* @return \chillerlan\Database\Query\CreateTableInterface |
99
|
|
|
*/ |
100
|
|
|
public function primaryKey(string $field):CreateTableInterface{ |
101
|
|
|
$this->primaryKey = $field; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param string $name |
108
|
|
|
* @param string $type |
109
|
|
|
* @param null $length |
110
|
|
|
* @param string|null $attribute |
111
|
|
|
* @param string|null $collation |
112
|
|
|
* @param bool|null $isNull |
113
|
|
|
* @param string|null $defaultType |
114
|
|
|
* @param null $defaultValue |
115
|
|
|
* @param string|null $extra |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
abstract protected function fieldspec( |
120
|
|
|
string $name, |
121
|
|
|
string $type, |
122
|
|
|
$length = null, |
123
|
|
|
string $attribute = null, |
124
|
|
|
string $collation = null, |
125
|
|
|
bool $isNull = null, |
126
|
|
|
string $defaultType = null, |
127
|
|
|
$defaultValue = null, |
128
|
|
|
string $extra = null |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $name |
133
|
|
|
* @param string $type |
134
|
|
|
* @param null $length |
135
|
|
|
* @param string|null $attribute |
136
|
|
|
* @param string|null $collation |
137
|
|
|
* @param bool|null $isNull |
138
|
|
|
* @param string|null $defaultType |
139
|
|
|
* @param null $defaultValue |
140
|
|
|
* @param string|null $extra |
141
|
|
|
* |
142
|
|
|
* @return \chillerlan\Database\Query\CreateTableInterface |
143
|
|
|
*/ |
144
|
|
|
public function field( |
145
|
|
|
string $name, |
146
|
|
|
string $type, |
147
|
|
|
$length = null, |
148
|
|
|
string $attribute = null, |
149
|
|
|
string $collation = null, |
150
|
|
|
bool $isNull = null, |
151
|
|
|
string $defaultType = null, |
152
|
|
|
$defaultValue = null, |
153
|
|
|
string $extra = null |
154
|
|
|
):CreateTableInterface { |
155
|
|
|
|
156
|
|
|
$this->cols[$name] = $this->fieldspec($name, $type, $length, $attribute, $collation, $isNull, $defaultType, $defaultValue, $extra); |
157
|
|
|
|
158
|
|
|
return $this; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* @param $name |
163
|
|
|
* |
164
|
|
|
* @return \chillerlan\Database\Query\CreateTableInterface |
165
|
|
|
*/ |
166
|
|
|
# public function index($name):CreateTableInterface{} |
|
|
|
|
167
|
|
|
|
168
|
|
|
// @todo: clean up this mess... |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param string $name |
172
|
|
|
* @param int|null $length |
173
|
|
|
* @param bool|null $isNull |
174
|
|
|
* @param int|null $defaultValue |
175
|
|
|
* @param string|null $attribute |
176
|
|
|
* |
177
|
|
|
* @return \chillerlan\Database\Query\CreateTableInterface |
178
|
|
|
*/ |
179
|
|
|
public function tinyint(string $name, int $length = null, int $defaultValue = null , bool $isNull = null, string $attribute = null):CreateTableInterface{ |
180
|
|
|
return $this->field($name, 'TINYINT', $length, null, null, !is_null($defaultValue) ? false : $isNull, !is_null($defaultValue) ? 'USER_DEFINED' : null, $defaultValue); |
|
|
|
|
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
|
184
|
|
|
public function int(string $name, int $length = null, int $defaultValue = null , bool $isNull = null, string $attribute = null):CreateTableInterface{ |
185
|
|
|
return $this->field($name, 'INT', $length, null, null, !is_null($defaultValue) ? false : $isNull, !is_null($defaultValue) ? 'USER_DEFINED' : null, $defaultValue); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $name |
191
|
|
|
* @param int|null $length |
|
|
|
|
192
|
|
|
* @param bool|null $isNull |
193
|
|
|
* @param int|null $defaultValue |
194
|
|
|
* |
195
|
|
|
* @return \chillerlan\Database\Query\CreateTableInterface |
196
|
|
|
*/ |
197
|
|
|
public function varchar(string $name, int $length, int $defaultValue = null , bool $isNull = null):CreateTableInterface{ |
198
|
|
|
return $this->field($name, 'VARCHAR', $length, null, null, !is_null($defaultValue) ? false : $isNull, !is_null($defaultValue) ? 'USER_DEFINED' : null, $defaultValue); |
|
|
|
|
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* @param string $name |
203
|
|
|
* @param string $length |
204
|
|
|
* @param bool|null $isNull |
205
|
|
|
* @param int|null $defaultValue |
206
|
|
|
* |
207
|
|
|
* @return \chillerlan\Database\Query\CreateTableInterface |
208
|
|
|
*/ |
209
|
|
|
public function decimal(string $name, string $length, int $defaultValue = null , bool $isNull = null):CreateTableInterface{ |
210
|
|
|
return $this->field($name, 'DECIMAL', $length, null, null, !is_null($defaultValue) ? false : $isNull, !is_null($defaultValue) ? 'USER_DEFINED' : null, $defaultValue); |
|
|
|
|
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @param string $name |
215
|
|
|
* @param bool $isNull |
216
|
|
|
* @param int|null $defaultValue |
217
|
|
|
* |
218
|
|
|
* @return \chillerlan\Database\Query\CreateTableInterface |
219
|
|
|
*/ |
220
|
|
|
public function tinytext(string $name, int $defaultValue = null , bool $isNull = true):CreateTableInterface{ |
221
|
|
|
return $this->field($name, 'TINYTEXT', null, null, null, !is_null($defaultValue) ? false : $isNull, !is_null($defaultValue) ? 'USER_DEFINED' : null, $defaultValue); |
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
|
225
|
|
|
public function text(string $name, int $defaultValue = null , bool $isNull = true):CreateTableInterface{ |
226
|
|
|
return $this->field($name, 'TEXT', null, null, null, !is_null($defaultValue) ? false : $isNull, !is_null($defaultValue) ? 'USER_DEFINED' : null, $defaultValue); |
|
|
|
|
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param string $name |
232
|
|
|
* @param array $values |
233
|
|
|
* @param null $defaultValue |
234
|
|
|
* @param bool|null $isNull |
235
|
|
|
* |
236
|
|
|
* @return \chillerlan\Database\Query\CreateTableInterface |
237
|
|
|
*/ |
238
|
|
|
public function enum(string $name, array $values, $defaultValue = null , bool $isNull = null):CreateTableInterface{ |
239
|
|
|
|
240
|
|
|
$field = $this->quote($name); |
241
|
|
|
$field .= 'ENUM (\''.implode('\', \'', $values).'\')'; |
242
|
|
|
|
243
|
|
|
if(is_bool($isNull)){ |
244
|
|
|
$field .= $isNull ? 'NULL' : 'NOT NULL'; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
if(in_array($defaultValue, $values, true)){ |
248
|
|
|
$field .= 'DEFAULT '.(is_int($defaultValue) || is_float($defaultValue) ? $defaultValue : '\''.$defaultValue.'\'') ; |
249
|
|
|
} |
250
|
|
|
elseif($isNull && strtolower($defaultValue) === 'null'){ |
251
|
|
|
$field .= 'DEFAULT NULL'; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$this->cols[$name] = $field; |
255
|
|
|
|
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
|
260
|
|
|
public function timestamp():CreateTableInterface{ |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
} |
266
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.