1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Schema; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
8
|
|
|
use Yiisoft\Db\Helper\DbStringHelper; |
9
|
|
|
|
10
|
|
|
use function gettype; |
11
|
|
|
use function in_array; |
12
|
|
|
use function is_bool; |
13
|
|
|
use function is_float; |
14
|
|
|
use function is_resource; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Represents the metadata of a column in a database table. |
18
|
|
|
* |
19
|
|
|
* It provides information about the column's name, type, size, precision, and other details. |
20
|
|
|
* |
21
|
|
|
* The `ColumnSchema` class is used to store and retrieve metadata about a column in a database table. |
22
|
|
|
* |
23
|
|
|
* It's typically used in conjunction with the TableSchema class, which represents the metadata of a database table as a |
24
|
|
|
* whole. |
25
|
|
|
* |
26
|
|
|
* Here is an example of how to use the `ColumnSchema` class: |
27
|
|
|
* |
28
|
|
|
* ```php |
29
|
|
|
* use Yiisoft\Db\Schema\ColumnSchema; |
30
|
|
|
* |
31
|
|
|
* $column = new ColumnSchema(); |
32
|
|
|
* $column->name('id'); |
33
|
|
|
* $column->allowNull(false); |
34
|
|
|
* $column->dbType('int(11)'); |
35
|
|
|
* $column->phpType('integer'); |
36
|
|
|
* $column->type('integer'); |
37
|
|
|
* $column->defaultValue(0); |
38
|
|
|
* $column->autoIncrement(true); |
39
|
|
|
* $column->primaryKey(true); |
40
|
|
|
* `` |
41
|
|
|
*/ |
42
|
|
|
abstract class AbstractColumnSchema implements ColumnSchemaInterface |
43
|
|
|
{ |
44
|
|
|
public function __construct(string $name) |
45
|
|
|
{ |
46
|
|
|
$this->name = $name; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
private bool $allowNull = false; |
50
|
|
|
private bool $autoIncrement = false; |
51
|
|
|
private string|null $comment = null; |
52
|
|
|
private bool $computed = false; |
53
|
|
|
private string|null $dbType = null; |
54
|
|
|
private mixed $defaultValue = null; |
55
|
|
|
private array|null $enumValues = null; |
56
|
|
|
private string|null $extra = null; |
57
|
|
|
private bool $isPrimaryKey = false; |
58
|
|
|
private string $name; |
59
|
|
|
private string|null $phpType = null; |
60
|
|
|
private int|null $precision = null; |
61
|
|
|
private int|null $scale = null; |
62
|
|
|
private int|null $size = null; |
63
|
|
|
private string $type = ''; |
64
|
|
|
private bool $unsigned = false; |
65
|
|
|
|
66
|
|
|
public function allowNull(bool $value): void |
67
|
|
|
{ |
68
|
|
|
$this->allowNull = $value; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function autoIncrement(bool $value): void |
72
|
|
|
{ |
73
|
|
|
$this->autoIncrement = $value; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function comment(string|null $value): void |
77
|
|
|
{ |
78
|
|
|
$this->comment = $value; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function computed(bool $value): void |
82
|
|
|
{ |
83
|
|
|
$this->computed = $value; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function dbType(string|null $value): void |
87
|
|
|
{ |
88
|
|
|
$this->dbType = $value; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function dbTypecast(mixed $value): mixed |
92
|
|
|
{ |
93
|
|
|
/** |
94
|
|
|
* The default implementation does the same as casting for PHP, but it should be possible to override this with |
95
|
|
|
* annotation of an explicit PDO type. |
96
|
|
|
*/ |
97
|
|
|
return $this->typecast($value); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function defaultValue(mixed $value): void |
101
|
|
|
{ |
102
|
|
|
$this->defaultValue = $value; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function enumValues(array|null $value): void |
106
|
|
|
{ |
107
|
|
|
$this->enumValues = $value; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function extra(string|null $value): void |
111
|
|
|
{ |
112
|
|
|
$this->extra = $value; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getComment(): string|null |
116
|
|
|
{ |
117
|
|
|
return $this->comment; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function getDbType(): string|null |
121
|
|
|
{ |
122
|
|
|
return $this->dbType; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function getDefaultValue(): mixed |
126
|
|
|
{ |
127
|
|
|
return $this->defaultValue; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getEnumValues(): array|null |
131
|
|
|
{ |
132
|
|
|
return $this->enumValues; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function getExtra(): string|null |
136
|
|
|
{ |
137
|
|
|
return $this->extra; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function getName(): string |
141
|
|
|
{ |
142
|
|
|
return $this->name; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getPrecision(): int|null |
146
|
|
|
{ |
147
|
|
|
return $this->precision; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function getPhpType(): string|null |
151
|
|
|
{ |
152
|
|
|
return $this->phpType; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
public function getScale(): int|null |
156
|
|
|
{ |
157
|
|
|
return $this->scale; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function getSize(): int|null |
161
|
|
|
{ |
162
|
|
|
return $this->size; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
public function getType(): string |
166
|
|
|
{ |
167
|
|
|
return $this->type; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
public function isAllowNull(): bool |
171
|
|
|
{ |
172
|
|
|
return $this->allowNull; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
public function isAutoIncrement(): bool |
176
|
|
|
{ |
177
|
|
|
return $this->autoIncrement; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
public function isComputed(): bool |
181
|
|
|
{ |
182
|
|
|
return $this->computed; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
public function isPrimaryKey(): bool |
186
|
|
|
{ |
187
|
|
|
return $this->isPrimaryKey; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
public function isUnsigned(): bool |
191
|
|
|
{ |
192
|
|
|
return $this->unsigned; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
public function phpType(string|null $value): void |
196
|
|
|
{ |
197
|
|
|
$this->phpType = $value; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
public function phpTypecast(mixed $value): mixed |
201
|
|
|
{ |
202
|
|
|
return $this->typecast($value); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
public function precision(int|null $value): void |
206
|
|
|
{ |
207
|
|
|
$this->precision = $value; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
public function primaryKey(bool $value): void |
211
|
|
|
{ |
212
|
|
|
$this->isPrimaryKey = $value; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
public function scale(int|null $value): void |
216
|
|
|
{ |
217
|
|
|
$this->scale = $value; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
public function size(int|null $value): void |
221
|
|
|
{ |
222
|
|
|
$this->size = $value; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
public function type(string $value): void |
226
|
|
|
{ |
227
|
|
|
$this->type = $value; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
public function unsigned(bool $value): void |
231
|
|
|
{ |
232
|
|
|
$this->unsigned = $value; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Converts the input value according to {@see phpType} after retrieval from the database. |
237
|
|
|
* |
238
|
|
|
* If the value is null or an {@see Expression}, it won't be converted. |
239
|
|
|
* |
240
|
|
|
* @param mixed $value The value to be converted. |
241
|
|
|
* |
242
|
|
|
* @return mixed The converted value. |
243
|
|
|
*/ |
244
|
|
|
protected function typecast(mixed $value): mixed |
245
|
|
|
{ |
246
|
|
|
if ( |
247
|
|
|
$value === '' |
248
|
|
|
&& !in_array( |
249
|
|
|
$this->type, |
250
|
|
|
[ |
251
|
|
|
SchemaInterface::TYPE_TEXT, |
252
|
|
|
SchemaInterface::TYPE_STRING, |
253
|
|
|
SchemaInterface::TYPE_BINARY, |
254
|
|
|
SchemaInterface::TYPE_CHAR, |
255
|
|
|
], |
256
|
|
|
true |
257
|
|
|
) |
258
|
|
|
) { |
259
|
|
|
return null; |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
if ( |
263
|
|
|
$value === null |
264
|
|
|
|| $value instanceof ExpressionInterface |
265
|
|
|
|| gettype($value) === $this->phpType |
266
|
|
|
) { |
267
|
|
|
return $value; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
switch ($this->phpType) { |
271
|
|
|
case SchemaInterface::PHP_TYPE_RESOURCE: |
272
|
|
|
case SchemaInterface::PHP_TYPE_STRING: |
273
|
|
|
if (is_resource($value)) { |
274
|
|
|
return $value; |
275
|
|
|
} |
276
|
|
|
|
277
|
|
|
if (is_float($value)) { |
278
|
|
|
/** ensure type cast always has . as decimal separator in all locales */ |
279
|
|
|
return DbStringHelper::normalizeFloat($value); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if (is_bool($value)) { |
283
|
|
|
return $value ? '1' : '0'; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
return (string) $value; |
287
|
|
|
case SchemaInterface::PHP_TYPE_INTEGER: |
288
|
|
|
return (int) $value; |
289
|
|
|
case SchemaInterface::PHP_TYPE_BOOLEAN: |
290
|
|
|
/** |
291
|
|
|
* Treating a 0-bit value as false too. |
292
|
|
|
* |
293
|
|
|
* @link https://github.com/yiisoft/yii2/issues/9006 |
294
|
|
|
*/ |
295
|
|
|
return (bool) $value && $value !== "\0"; |
296
|
|
|
case SchemaInterface::PHP_TYPE_DOUBLE: |
297
|
|
|
return (float) $value; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
return $value; |
301
|
|
|
} |
302
|
|
|
} |
303
|
|
|
|