1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\db; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* SchemaBuilderTrait contains shortcut methods to create instances of [[ColumnSchemaBuilder]]. |
12
|
|
|
* |
13
|
|
|
* These can be used in database migrations to define database schema types using a PHP interface. |
14
|
|
|
* This is useful to define a schema in a DBMS independent way so that the application may run on |
15
|
|
|
* different DBMS the same way. |
16
|
|
|
* |
17
|
|
|
* For example you may use the following code inside your migration files: |
18
|
|
|
* |
19
|
|
|
* ```php |
20
|
|
|
* $this->createTable('example_table', [ |
21
|
|
|
* 'id' => $this->primaryKey(), |
22
|
|
|
* 'name' => $this->string(64)->notNull(), |
23
|
|
|
* 'type' => $this->integer()->notNull()->defaultValue(10), |
24
|
|
|
* 'description' => $this->text(), |
25
|
|
|
* 'rule_name' => $this->string(64), |
26
|
|
|
* 'data' => $this->text(), |
27
|
|
|
* 'created_at' => $this->datetime()->notNull(), |
28
|
|
|
* 'updated_at' => $this->datetime(), |
29
|
|
|
* ]); |
30
|
|
|
* ``` |
31
|
|
|
* |
32
|
|
|
* @author Vasenin Matvey <[email protected]> |
33
|
|
|
* @since 2.0.6 |
34
|
|
|
*/ |
35
|
|
|
trait SchemaBuilderTrait |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @return Connection the database connection to be used for schema building. |
39
|
|
|
*/ |
40
|
|
|
abstract protected function getDb(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates a primary key column. |
44
|
|
|
* @param int $length column size or precision definition. |
45
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
46
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
47
|
|
|
* @since 2.0.6 |
48
|
|
|
*/ |
49
|
6 |
|
public function primaryKey($length = null) |
50
|
|
|
{ |
51
|
6 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_PK, $length); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Creates a big primary key column. |
56
|
|
|
* @param int $length column size or precision definition. |
57
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
58
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
59
|
|
|
* @since 2.0.6 |
60
|
|
|
*/ |
61
|
12 |
|
public function bigPrimaryKey($length = null) |
62
|
|
|
{ |
63
|
12 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BIGPK, $length); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Creates a char column. |
68
|
|
|
* @param int $length column size definition i.e. the maximum string length. |
69
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
70
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
71
|
|
|
* @since 2.0.8 |
72
|
|
|
*/ |
73
|
6 |
|
public function char($length = null) |
74
|
|
|
{ |
75
|
6 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_CHAR, $length); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Creates a string column. |
80
|
|
|
* @param int $length column size definition i.e. the maximum string length. |
81
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
82
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
83
|
|
|
* @since 2.0.6 |
84
|
|
|
*/ |
85
|
33 |
|
public function string($length = null) |
86
|
|
|
{ |
87
|
33 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_STRING, $length); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Creates a text column. |
92
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
93
|
|
|
* @since 2.0.6 |
94
|
|
|
*/ |
95
|
12 |
|
public function text() |
96
|
|
|
{ |
97
|
12 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TEXT); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Creates a tinyint column. If tinyint is not supported by the DBMS, smallint will be used. |
102
|
|
|
* @param int $length column size or precision definition. |
103
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
104
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
105
|
|
|
* @since 2.0.14 |
106
|
|
|
*/ |
107
|
6 |
|
public function tinyInteger($length = null) |
108
|
|
|
{ |
109
|
6 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TINYINT, $length); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Creates a smallint column. |
114
|
|
|
* @param int $length column size or precision definition. |
115
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
116
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
117
|
|
|
* @since 2.0.6 |
118
|
|
|
*/ |
119
|
6 |
|
public function smallInteger($length = null) |
120
|
|
|
{ |
121
|
6 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_SMALLINT, $length); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Creates an integer column. |
126
|
|
|
* @param int $length column size or precision definition. |
127
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
128
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
129
|
|
|
* @since 2.0.6 |
130
|
|
|
*/ |
131
|
33 |
|
public function integer($length = null) |
132
|
|
|
{ |
133
|
33 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_INTEGER, $length); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Creates a bigint column. |
138
|
|
|
* @param int $length column size or precision definition. |
139
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
140
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
141
|
|
|
* @since 2.0.6 |
142
|
|
|
*/ |
143
|
6 |
|
public function bigInteger($length = null) |
144
|
|
|
{ |
145
|
6 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BIGINT, $length); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Creates a float column. |
150
|
|
|
* @param int $precision column value precision. First parameter passed to the column type, e.g. FLOAT(precision). |
151
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
152
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
153
|
|
|
* @since 2.0.6 |
154
|
|
|
*/ |
155
|
6 |
|
public function float($precision = null) |
156
|
|
|
{ |
157
|
6 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_FLOAT, $precision); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Creates a double column. |
162
|
|
|
* @param int $precision column value precision. First parameter passed to the column type, e.g. DOUBLE(precision). |
163
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
164
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
165
|
|
|
* @since 2.0.6 |
166
|
|
|
*/ |
167
|
12 |
|
public function double($precision = null) |
168
|
|
|
{ |
169
|
12 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DOUBLE, $precision); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Creates a decimal column. |
174
|
|
|
* @param int $precision column value precision, which is usually the total number of digits. |
175
|
|
|
* First parameter passed to the column type, e.g. DECIMAL(precision, scale). |
176
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
177
|
|
|
* @param int $scale column value scale, which is usually the number of digits after the decimal point. |
178
|
|
|
* Second parameter passed to the column type, e.g. DECIMAL(precision, scale). |
179
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
180
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
181
|
|
|
* @since 2.0.6 |
182
|
|
|
*/ |
183
|
6 |
|
public function decimal($precision = null, $scale = null) |
184
|
|
|
{ |
185
|
6 |
|
$length = []; |
186
|
6 |
|
if ($precision !== null) { |
187
|
6 |
|
$length[] = $precision; |
188
|
|
|
} |
189
|
6 |
|
if ($scale !== null) { |
190
|
6 |
|
$length[] = $scale; |
191
|
|
|
} |
192
|
|
|
|
193
|
6 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DECIMAL, $length); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Creates a datetime column. |
198
|
|
|
* @param int $precision column value precision. First parameter passed to the column type, e.g. DATETIME(precision). |
199
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
200
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
201
|
|
|
* @since 2.0.6 |
202
|
|
|
*/ |
203
|
6 |
|
public function dateTime($precision = null) |
204
|
|
|
{ |
205
|
6 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DATETIME, $precision); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Creates a timestamp column. |
210
|
|
|
* @param int $precision column value precision. First parameter passed to the column type, e.g. TIMESTAMP(precision). |
211
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
212
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
213
|
|
|
* @since 2.0.6 |
214
|
|
|
*/ |
215
|
6 |
|
public function timestamp($precision = null) |
216
|
|
|
{ |
217
|
6 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TIMESTAMP, $precision); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Creates a time column. |
222
|
|
|
* @param int $precision column value precision. First parameter passed to the column type, e.g. TIME(precision). |
223
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
224
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
225
|
|
|
* @since 2.0.6 |
226
|
|
|
*/ |
227
|
6 |
|
public function time($precision = null) |
228
|
|
|
{ |
229
|
6 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_TIME, $precision); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Creates a date column. |
234
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
235
|
|
|
* @since 2.0.6 |
236
|
|
|
*/ |
237
|
6 |
|
public function date() |
238
|
|
|
{ |
239
|
6 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_DATE); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Creates a binary column. |
244
|
|
|
* @param int $length column size or precision definition. |
245
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
246
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
247
|
|
|
* @since 2.0.6 |
248
|
|
|
*/ |
249
|
27 |
|
public function binary($length = null) |
250
|
|
|
{ |
251
|
27 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BINARY, $length); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Creates a boolean column. |
256
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
257
|
|
|
* @since 2.0.6 |
258
|
|
|
*/ |
259
|
6 |
|
public function boolean() |
260
|
|
|
{ |
261
|
6 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_BOOLEAN); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Creates a money column. |
266
|
|
|
* @param int $precision column value precision, which is usually the total number of digits. |
267
|
|
|
* First parameter passed to the column type, e.g. DECIMAL(precision, scale). |
268
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
269
|
|
|
* @param int $scale column value scale, which is usually the number of digits after the decimal point. |
270
|
|
|
* Second parameter passed to the column type, e.g. DECIMAL(precision, scale). |
271
|
|
|
* This parameter will be ignored if not supported by the DBMS. |
272
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
273
|
|
|
* @since 2.0.6 |
274
|
|
|
*/ |
275
|
6 |
|
public function money($precision = null, $scale = null) |
276
|
|
|
{ |
277
|
6 |
|
$length = []; |
278
|
6 |
|
if ($precision !== null) { |
279
|
6 |
|
$length[] = $precision; |
280
|
|
|
} |
281
|
6 |
|
if ($scale !== null) { |
282
|
6 |
|
$length[] = $scale; |
283
|
|
|
} |
284
|
|
|
|
285
|
6 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_MONEY, $length); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Creates a JSON column. |
290
|
|
|
* @return ColumnSchemaBuilder the column instance which can be further customized. |
291
|
|
|
* @since 2.0.14 |
292
|
|
|
* @throws \yii\base\Exception |
293
|
|
|
*/ |
294
|
4 |
|
public function json() |
295
|
|
|
{ |
296
|
|
|
/* |
297
|
|
|
* TODO Remove in Yii 2.1 |
298
|
|
|
* |
299
|
|
|
* Disabled due to bug in MySQL extension |
300
|
|
|
* @link https://bugs.php.net/bug.php?id=70384 |
301
|
|
|
*/ |
302
|
4 |
|
if (version_compare(PHP_VERSION, '5.6', '<') && $this->getDb()->getDriverName() === 'mysql') { |
303
|
|
|
throw new \yii\base\Exception('JSON column type is not supported in PHP < 5.6'); |
304
|
|
|
} |
305
|
|
|
|
306
|
4 |
|
return $this->getDb()->getSchema()->createColumnSchemaBuilder(Schema::TYPE_JSON); |
307
|
|
|
} |
308
|
|
|
} |
309
|
|
|
|