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