1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Pgsql; |
6
|
|
|
|
7
|
|
|
use JsonException; |
8
|
|
|
use PDO; |
9
|
|
|
use Yiisoft\Db\Command\Param; |
10
|
|
|
use Yiisoft\Db\Expression\ArrayExpression; |
11
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
12
|
|
|
use Yiisoft\Db\Expression\JsonExpression; |
13
|
|
|
use Yiisoft\Db\Pgsql\Composite\CompositeExpression; |
14
|
|
|
use Yiisoft\Db\Pgsql\Composite\CompositeParser; |
15
|
|
|
use Yiisoft\Db\Schema\AbstractColumnSchema; |
16
|
|
|
use Yiisoft\Db\Schema\ColumnSchemaInterface; |
17
|
|
|
use Yiisoft\Db\Schema\SchemaInterface; |
18
|
|
|
|
19
|
|
|
use function array_walk_recursive; |
20
|
|
|
use function bindec; |
21
|
|
|
use function decbin; |
22
|
|
|
use function is_array; |
23
|
|
|
use function is_int; |
24
|
|
|
use function is_string; |
25
|
|
|
use function json_decode; |
26
|
|
|
use function str_pad; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Represents the metadata of a column in a database table for PostgreSQL Server. |
30
|
|
|
* |
31
|
|
|
* It provides information about the column's name, type, size, precision, and other details. |
32
|
|
|
* |
33
|
|
|
* It's used to store and retrieve metadata about a column in a database table and is typically used in conjunction with |
34
|
|
|
* the {@see TableSchema}, which represents the metadata of a database table as a whole. |
35
|
|
|
* |
36
|
|
|
* The following code shows how to use: |
37
|
|
|
* |
38
|
|
|
* ```php |
39
|
|
|
* use Yiisoft\Db\Pgsql\ColumnSchema; |
40
|
|
|
* |
41
|
|
|
* $column = new ColumnSchema(); |
42
|
|
|
* $column->name('id'); |
43
|
|
|
* $column->allowNull(false); |
44
|
|
|
* $column->dbType('integer'); |
45
|
|
|
* $column->phpType('integer'); |
46
|
|
|
* $column->type('integer'); |
47
|
|
|
* $column->defaultValue(0); |
48
|
|
|
* $column->autoIncrement(true); |
49
|
|
|
* $column->primaryKey(true); |
50
|
|
|
* ``` |
51
|
|
|
*/ |
52
|
|
|
final class ColumnSchema extends AbstractColumnSchema |
53
|
|
|
{ |
54
|
|
|
/** |
55
|
|
|
* @var int The dimension of array. Defaults to 0, means this column isn't an array. |
56
|
|
|
*/ |
57
|
|
|
private int $dimension = 0; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var string|null Name of an associated sequence if column is auto incremental. |
61
|
|
|
*/ |
62
|
|
|
private string|null $sequenceName = null; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var ColumnSchemaInterface[]|null Columns metadata of the composite type. |
66
|
|
|
* @psalm-var array<string, ColumnSchemaInterface>|null |
67
|
|
|
*/ |
68
|
|
|
private array|null $columns = null; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Converts the input value according to {@see type} and {@see dbType} for use in a db query. |
72
|
|
|
* |
73
|
|
|
* If the value is null or an {@see Expression}, it won't be converted. |
74
|
|
|
* |
75
|
|
|
* @param mixed $value input value |
76
|
|
|
* |
77
|
|
|
* @return mixed Converted value. |
78
|
|
|
*/ |
79
|
91 |
|
public function dbTypecast(mixed $value): mixed |
80
|
|
|
{ |
81
|
91 |
|
if ($this->dimension > 0) { |
82
|
4 |
|
if ($value === null || $value instanceof ExpressionInterface) { |
83
|
2 |
|
return $value; |
84
|
|
|
} |
85
|
|
|
|
86
|
3 |
|
if ($this->getType() === Schema::TYPE_COMPOSITE) { |
87
|
1 |
|
$value = $this->dbTypecastArray($value, $this->dimension); |
88
|
|
|
} |
89
|
|
|
|
90
|
3 |
|
return new ArrayExpression($value, $this->getDbType(), $this->dimension); |
91
|
|
|
} |
92
|
|
|
|
93
|
90 |
|
return $this->dbTypecastValue($value); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Recursively converts array values for use in a db query. |
98
|
|
|
* |
99
|
|
|
* @param mixed $value The array or iterable object. |
100
|
|
|
* @param int $dimension The array dimension. Should be more than 0. |
101
|
|
|
* |
102
|
|
|
* @return array|null Converted values. |
103
|
|
|
*/ |
104
|
1 |
|
private function dbTypecastArray(mixed $value, int $dimension): array|null |
105
|
|
|
{ |
106
|
1 |
|
if (!is_iterable($value)) { |
107
|
1 |
|
return []; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
$items = []; |
111
|
|
|
|
112
|
1 |
|
if ($dimension > 1) { |
113
|
|
|
/** @psalm-var mixed $val */ |
114
|
1 |
|
foreach ($value as $val) { |
115
|
1 |
|
$items[] = $this->dbTypecastArray($val, $dimension - 1); |
116
|
|
|
} |
117
|
|
|
} else { |
118
|
|
|
/** @psalm-var mixed $val */ |
119
|
1 |
|
foreach ($value as $val) { |
120
|
|
|
/** @psalm-suppress MixedAssignment */ |
121
|
1 |
|
$items[] = $this->dbTypecastValue($val); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
return $items; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Converts the input value for use in a db query. |
130
|
|
|
*/ |
131
|
90 |
|
private function dbTypecastValue(mixed $value): mixed |
132
|
|
|
{ |
133
|
90 |
|
if ($value === null || $value instanceof ExpressionInterface) { |
134
|
30 |
|
return $value; |
135
|
|
|
} |
136
|
|
|
|
137
|
85 |
|
return match ($this->getType()) { |
138
|
85 |
|
SchemaInterface::TYPE_JSON => new JsonExpression($value, $this->getDbType()), |
139
|
|
|
|
140
|
85 |
|
SchemaInterface::TYPE_BINARY => is_string($value) |
141
|
3 |
|
? new Param($value, PDO::PARAM_LOB) // explicitly setup PDO param type for binary column |
142
|
3 |
|
: $this->typecast($value), |
143
|
|
|
|
144
|
85 |
|
Schema::TYPE_BIT => is_int($value) |
145
|
1 |
|
? str_pad(decbin($value), (int) $this->getSize(), '0', STR_PAD_LEFT) |
146
|
2 |
|
: (string) $value, |
147
|
|
|
|
148
|
85 |
|
Schema::TYPE_COMPOSITE => new CompositeExpression($value, $this->getDbType(), $this->columns), |
149
|
|
|
|
150
|
85 |
|
default => $this->typecast($value), |
151
|
85 |
|
}; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Converts the input value according to {@see phpType} after retrieval from the database. |
156
|
|
|
* |
157
|
|
|
* If the value is null or an {@see Expression}, it won't be converted. |
158
|
|
|
* |
159
|
|
|
* @param mixed $value The input value |
160
|
|
|
* |
161
|
|
|
* @throws JsonException |
162
|
|
|
* |
163
|
|
|
* @return mixed The converted value |
164
|
|
|
*/ |
165
|
98 |
|
public function phpTypecast(mixed $value): mixed |
166
|
|
|
{ |
167
|
98 |
|
if ($this->dimension > 0) { |
168
|
12 |
|
if (is_string($value)) { |
169
|
12 |
|
$value = $this->getArrayParser()->parse($value); |
170
|
|
|
} |
171
|
|
|
|
172
|
12 |
|
if (!is_array($value)) { |
173
|
1 |
|
return null; |
174
|
|
|
} |
175
|
|
|
|
176
|
12 |
|
array_walk_recursive($value, function (mixed &$val) { |
177
|
|
|
/** @psalm-var mixed $val */ |
178
|
12 |
|
$val = $this->phpTypecastValue($val); |
179
|
12 |
|
}); |
180
|
|
|
|
181
|
12 |
|
return $value; |
182
|
|
|
} |
183
|
|
|
|
184
|
92 |
|
return $this->phpTypecastValue($value); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Casts $value after retrieving from the DBMS to PHP representation. |
189
|
|
|
* |
190
|
|
|
* @throws JsonException |
191
|
|
|
*/ |
192
|
98 |
|
private function phpTypecastValue(mixed $value): mixed |
193
|
|
|
{ |
194
|
98 |
|
if ($value === null) { |
195
|
11 |
|
return null; |
196
|
|
|
} |
197
|
|
|
|
198
|
98 |
|
return match ($this->getType()) { |
199
|
98 |
|
Schema::TYPE_BIT => is_string($value) ? bindec($value) : $value, |
200
|
|
|
|
201
|
98 |
|
SchemaInterface::TYPE_BOOLEAN => $value && $value !== 'f', |
202
|
|
|
|
203
|
98 |
|
SchemaInterface::TYPE_JSON |
204
|
98 |
|
=> json_decode((string) $value, true, 512, JSON_THROW_ON_ERROR), |
205
|
|
|
|
206
|
98 |
|
Schema::TYPE_COMPOSITE => $this->phpTypecastComposite($value), |
207
|
|
|
|
208
|
98 |
|
default => parent::phpTypecast($value), |
209
|
98 |
|
}; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Converts the input value according to the composite type after retrieval from the database. |
214
|
|
|
*/ |
215
|
5 |
|
private function phpTypecastComposite(mixed $value): array|null |
216
|
|
|
{ |
217
|
5 |
|
if (is_string($value)) { |
218
|
5 |
|
$value = (new CompositeParser())->parse($value); |
219
|
|
|
} |
220
|
|
|
|
221
|
5 |
|
if (!is_iterable($value)) { |
222
|
1 |
|
return null; |
223
|
|
|
} |
224
|
|
|
|
225
|
5 |
|
$fields = []; |
226
|
5 |
|
$columns = (array) $this->columns; |
227
|
5 |
|
$columnNames = array_keys($columns); |
228
|
|
|
|
229
|
|
|
/** |
230
|
|
|
* @psalm-var int|string $columnName |
231
|
|
|
* @psalm-var mixed $item |
232
|
|
|
*/ |
233
|
5 |
|
foreach ($value as $columnName => $item) { |
234
|
5 |
|
$columnName = $columnNames[$columnName] ?? $columnName; |
235
|
|
|
|
236
|
5 |
|
if (isset($columns[$columnName])) { |
237
|
|
|
/** @psalm-var mixed $item */ |
238
|
5 |
|
$item = $columns[$columnName]->phpTypecast($item); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
/** @psalm-suppress MixedAssignment */ |
242
|
5 |
|
$fields[$columnName] = $item; |
243
|
|
|
} |
244
|
|
|
|
245
|
5 |
|
return $fields; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Creates instance of ArrayParser. |
250
|
|
|
*/ |
251
|
12 |
|
private function getArrayParser(): ArrayParser |
252
|
|
|
{ |
253
|
12 |
|
return new ArrayParser(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return int Get the dimension of the array. |
258
|
|
|
* |
259
|
|
|
* Defaults to 0, means this column isn't an array. |
260
|
|
|
*/ |
261
|
6 |
|
public function getDimension(): int |
262
|
|
|
{ |
263
|
6 |
|
return $this->dimension; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return string|null name of an associated sequence if column is auto incremental. |
268
|
|
|
*/ |
269
|
100 |
|
public function getSequenceName(): string|null |
270
|
|
|
{ |
271
|
100 |
|
return $this->sequenceName; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
/** |
275
|
|
|
* Set dimension of an array. |
276
|
|
|
* |
277
|
|
|
* Defaults to 0, means this column isn't an array. |
278
|
|
|
*/ |
279
|
161 |
|
public function dimension(int $dimension): void |
280
|
|
|
{ |
281
|
161 |
|
$this->dimension = $dimension; |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* Set the name of an associated sequence if a column is auto incremental. |
286
|
|
|
*/ |
287
|
84 |
|
public function sequenceName(string|null $sequenceName): void |
288
|
|
|
{ |
289
|
84 |
|
$this->sequenceName = $sequenceName; |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Set columns of the composite type. |
294
|
|
|
* |
295
|
|
|
* @param ColumnSchemaInterface[]|null $columns The metadata of the composite type columns. |
296
|
|
|
* @psalm-param array<string, ColumnSchemaInterface>|null $columns |
297
|
|
|
*/ |
298
|
5 |
|
public function columns(array|null $columns): void |
299
|
|
|
{ |
300
|
5 |
|
$this->columns = $columns; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Get the metadata of the composite type columns. |
305
|
|
|
* |
306
|
|
|
* @return ColumnSchemaInterface[]|null |
307
|
|
|
*/ |
308
|
5 |
|
public function getColumns(): array|null |
309
|
|
|
{ |
310
|
5 |
|
return $this->columns; |
311
|
|
|
} |
312
|
|
|
} |
313
|
|
|
|