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
|
|
|
private bool $allowNull = false; |
45
|
|
|
private bool $autoIncrement = false; |
46
|
|
|
private string|null $comment = null; |
47
|
|
|
private bool $computed = false; |
48
|
|
|
private string|null $dbType = null; |
49
|
|
|
private mixed $defaultValue = null; |
50
|
|
|
private array|null $enumValues = null; |
51
|
|
|
private string|null $extra = null; |
52
|
|
|
private bool $isPrimaryKey = false; |
53
|
|
|
private string|null $phpType = null; |
54
|
|
|
private int|null $precision = null; |
55
|
|
|
private int|null $scale = null; |
56
|
|
|
private int|null $size = null; |
57
|
|
|
private string $type = ''; |
58
|
|
|
private bool $unsigned = false; |
59
|
|
|
|
60
|
|
|
public function __construct(private string $name) |
61
|
|
|
{ |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function allowNull(bool $value): void |
65
|
|
|
{ |
66
|
|
|
$this->allowNull = $value; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function autoIncrement(bool $value): void |
70
|
|
|
{ |
71
|
|
|
$this->autoIncrement = $value; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function comment(string|null $value): void |
75
|
|
|
{ |
76
|
|
|
$this->comment = $value; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function computed(bool $value): void |
80
|
|
|
{ |
81
|
|
|
$this->computed = $value; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function dbType(string|null $value): void |
85
|
|
|
{ |
86
|
|
|
$this->dbType = $value; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function dbTypecast(mixed $value): mixed |
90
|
|
|
{ |
91
|
|
|
/** |
92
|
|
|
* The default implementation does the same as casting for PHP, but it should be possible to override this with |
93
|
|
|
* annotation of an explicit PDO type. |
94
|
|
|
*/ |
95
|
|
|
return $this->typecast($value); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function defaultValue(mixed $value): void |
99
|
|
|
{ |
100
|
|
|
$this->defaultValue = $value; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function enumValues(array|null $value): void |
104
|
|
|
{ |
105
|
|
|
$this->enumValues = $value; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function extra(string|null $value): void |
109
|
|
|
{ |
110
|
|
|
$this->extra = $value; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
public function getComment(): string|null |
114
|
|
|
{ |
115
|
|
|
return $this->comment; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function getDbType(): string|null |
119
|
|
|
{ |
120
|
|
|
return $this->dbType; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public function getDefaultValue(): mixed |
124
|
|
|
{ |
125
|
|
|
return $this->defaultValue; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getEnumValues(): array|null |
129
|
|
|
{ |
130
|
|
|
return $this->enumValues; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
public function getExtra(): string|null |
134
|
|
|
{ |
135
|
|
|
return $this->extra; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function getName(): string |
139
|
|
|
{ |
140
|
|
|
return $this->name; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function getPrecision(): int|null |
144
|
|
|
{ |
145
|
|
|
return $this->precision; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
public function getPhpType(): string|null |
149
|
|
|
{ |
150
|
|
|
return $this->phpType; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function getScale(): int|null |
154
|
|
|
{ |
155
|
|
|
return $this->scale; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function getSize(): int|null |
159
|
|
|
{ |
160
|
|
|
return $this->size; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getType(): string |
164
|
|
|
{ |
165
|
|
|
return $this->type; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function isAllowNull(): bool |
169
|
|
|
{ |
170
|
|
|
return $this->allowNull; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
public function isAutoIncrement(): bool |
174
|
|
|
{ |
175
|
|
|
return $this->autoIncrement; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function isComputed(): bool |
179
|
|
|
{ |
180
|
|
|
return $this->computed; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
public function isPrimaryKey(): bool |
184
|
|
|
{ |
185
|
|
|
return $this->isPrimaryKey; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
public function isUnsigned(): bool |
189
|
|
|
{ |
190
|
|
|
return $this->unsigned; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function phpType(string|null $value): void |
194
|
|
|
{ |
195
|
|
|
$this->phpType = $value; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
public function phpTypecast(mixed $value): mixed |
199
|
|
|
{ |
200
|
|
|
return $this->typecast($value); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
public function precision(int|null $value): void |
204
|
|
|
{ |
205
|
|
|
$this->precision = $value; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
public function primaryKey(bool $value): void |
209
|
|
|
{ |
210
|
|
|
$this->isPrimaryKey = $value; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function scale(int|null $value): void |
214
|
|
|
{ |
215
|
|
|
$this->scale = $value; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function size(int|null $value): void |
219
|
|
|
{ |
220
|
|
|
$this->size = $value; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function type(string $value): void |
224
|
|
|
{ |
225
|
|
|
$this->type = $value; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function unsigned(bool $value): void |
229
|
|
|
{ |
230
|
|
|
$this->unsigned = $value; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* Converts the input value according to {@see phpType} after retrieval from the database. |
235
|
|
|
* |
236
|
|
|
* If the value is null or an {@see Expression}, it won't be converted. |
237
|
|
|
* |
238
|
|
|
* @param mixed $value The value to be converted. |
239
|
|
|
* |
240
|
|
|
* @return mixed The converted value. |
241
|
|
|
*/ |
242
|
|
|
protected function typecast(mixed $value): mixed |
243
|
|
|
{ |
244
|
|
|
if ( |
245
|
|
|
$value === null |
246
|
|
|
|| $value === '' && !in_array($this->type, [ |
247
|
|
|
SchemaInterface::TYPE_TEXT, |
248
|
|
|
SchemaInterface::TYPE_STRING, |
249
|
|
|
SchemaInterface::TYPE_BINARY, |
250
|
|
|
SchemaInterface::TYPE_CHAR, |
251
|
|
|
], true) |
252
|
|
|
) { |
253
|
|
|
return null; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
if ($value instanceof ExpressionInterface) { |
257
|
|
|
return $value; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
return match ($this->phpType) { |
261
|
|
|
gettype($value) => $value, |
262
|
|
|
SchemaInterface::PHP_TYPE_RESOURCE, |
263
|
|
|
SchemaInterface::PHP_TYPE_STRING |
264
|
|
|
=> match (true) { |
265
|
|
|
is_resource($value) => $value, |
266
|
|
|
/** ensure type cast always has . as decimal separator in all locales */ |
267
|
|
|
is_float($value) => DbStringHelper::normalizeFloat($value), |
268
|
|
|
is_bool($value) => $value ? '1' : '0', |
269
|
|
|
default => (string) $value, |
270
|
|
|
}, |
271
|
|
|
SchemaInterface::PHP_TYPE_INTEGER => (int) $value, |
272
|
|
|
/** Treating a 0-bit value as false too (@link https://github.com/yiisoft/yii2/issues/9006) */ |
273
|
|
|
SchemaInterface::PHP_TYPE_BOOLEAN => $value && $value !== "\0", |
274
|
|
|
SchemaInterface::PHP_TYPE_DOUBLE => (float) $value, |
275
|
|
|
default => $value, |
276
|
|
|
}; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|