1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Pgsql\Schema; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Expression\ArrayExpression; |
8
|
|
|
use Yiisoft\Db\Expression\ExpressionInterface; |
9
|
|
|
use Yiisoft\Db\Expression\JsonExpression; |
10
|
|
|
use Yiisoft\Db\Pgsql\Query\ArrayParser; |
11
|
|
|
use Yiisoft\Db\Schema\ColumnSchema as AbstractColumnSchema; |
12
|
|
|
|
13
|
|
|
class ColumnSchema extends AbstractColumnSchema |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var int the dimension of array. Defaults to 0, means this column is not an array. |
17
|
|
|
*/ |
18
|
|
|
private int $dimension = 0; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string name of associated sequence if column is auto-incremental. |
22
|
|
|
*/ |
23
|
|
|
private ?string $sequenceName = null; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Converts the input value according to {@see type} and {@see dbType} for use in a db query. |
27
|
|
|
* |
28
|
|
|
* If the value is null or an {@see Expression}, it will not be converted. |
29
|
|
|
* |
30
|
|
|
* @param mixed $value input value |
31
|
49 |
|
* |
32
|
|
|
* @return mixed converted value. This may also be an array containing the value as the first element and the PDO |
33
|
49 |
|
* type as the second element. |
34
|
5 |
|
*/ |
35
|
|
|
public function dbTypecast($value) |
36
|
|
|
{ |
37
|
49 |
|
if ($value === null) { |
38
|
10 |
|
return $value; |
39
|
|
|
} |
40
|
|
|
|
41
|
44 |
|
if ($value instanceof ExpressionInterface) { |
42
|
|
|
return $value; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($this->dimension > 0) { |
46
|
|
|
return $this->disableArraySupport |
|
|
|
|
47
|
44 |
|
? (string) $value |
48
|
1 |
|
: new ArrayExpression($value, $this->getDbType(), $this->dimension); |
49
|
|
|
} |
50
|
|
|
|
51
|
43 |
|
if (\in_array($this->getDbType(), [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) { |
52
|
|
|
return new JsonExpression($value, $this->getDbType()); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this->typecast($value); |
56
|
|
|
} |
57
|
29 |
|
|
58
|
|
|
/** |
59
|
29 |
|
* Converts the input value according to {@see phpType} after retrieval from the database. |
60
|
|
|
* |
61
|
|
|
* If the value is null or an {@see Expression}, it will not be converted. |
62
|
|
|
* |
63
|
|
|
* @param mixed $value input value |
64
|
|
|
* |
65
|
|
|
* @return mixed converted value |
66
|
|
|
*/ |
67
|
|
|
public function phpTypecast($value) |
68
|
|
|
{ |
69
|
|
|
if ($this->dimension > 0) { |
70
|
|
|
if (!\is_array($value)) { |
71
|
|
|
$value = $this->getArrayParser()->parse($value); |
72
|
|
|
} |
73
|
|
|
if (\is_array($value)) { |
|
|
|
|
74
|
29 |
|
\array_walk_recursive($value, function (&$val, $key) { |
75
|
|
|
$val = $this->phpTypecastValue($val); |
76
|
|
|
}); |
77
|
|
|
} elseif ($value === null) { |
78
|
|
|
return null; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return $value; |
82
|
|
|
} |
83
|
|
|
|
84
|
29 |
|
return $this->phpTypecastValue($value); |
85
|
|
|
} |
86
|
29 |
|
|
87
|
|
|
/** |
88
|
|
|
* Casts $value after retrieving from the DBMS to PHP representation. |
89
|
|
|
* |
90
|
29 |
|
* @param string|null $value |
91
|
|
|
* |
92
|
|
|
* @return bool|mixed|null |
93
|
|
|
*/ |
94
|
|
|
protected function phpTypecastValue(?string $value) |
95
|
|
|
{ |
96
|
|
|
if ($value === null) { |
97
|
|
|
return null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
switch ($this->getType()) { |
101
|
|
|
case Schema::TYPE_BOOLEAN: |
102
|
|
|
$value = \is_string($value) ? \strtolower($value) : $value; |
|
|
|
|
103
|
|
|
|
104
|
|
|
switch ($value) { |
105
|
27 |
|
case 't': |
106
|
|
|
case 'true': |
107
|
|
|
return true; |
108
|
29 |
|
case 'f': |
109
|
|
|
case 'false': |
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return (bool) $value; |
114
|
|
|
case Schema::TYPE_JSON: |
115
|
|
|
return \json_decode($value, true, 512, JSON_THROW_ON_ERROR); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return parent::phpTypecast($value); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Creates instance of ArrayParser |
123
|
|
|
* |
124
|
|
|
* @return ArrayParser |
125
|
|
|
*/ |
126
|
|
|
protected function getArrayParser(): ArrayParser |
127
|
|
|
{ |
128
|
|
|
static $parser = null; |
129
|
|
|
|
130
|
|
|
if ($parser === null) { |
131
|
|
|
$parser = new ArrayParser(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return $parser; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function getDimension(): int |
138
|
|
|
{ |
139
|
|
|
return $this->dimension; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
public function getSequenceName(): ?string |
143
|
|
|
{ |
144
|
|
|
return $this->sequenceName; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
public function dimension(int $dimension): void |
148
|
|
|
{ |
149
|
|
|
$this->dimension = $dimension; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function sequenceName(?string $sequenceName): void |
153
|
|
|
{ |
154
|
|
|
$this->sequenceName = $sequenceName; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|