1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Db\Pgsql; |
6
|
|
|
|
7
|
|
|
use Yiisoft\Db\Expressions\ArrayExpression; |
8
|
|
|
use Yiisoft\Db\Expressions\ExpressionInterface; |
9
|
|
|
use Yiisoft\Db\Expressions\JsonExpression; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ColumnSchema for Postgres SQL database. |
13
|
|
|
*/ |
14
|
|
|
class ColumnSchema extends \Yiisoft\Db\Schemas\ColumnSchema |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var int the dimension of array. Defaults to 0, means this column is not an array. |
18
|
|
|
*/ |
19
|
|
|
public int $dimension = 0; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string name of associated sequence if column is auto-incremental. |
23
|
|
|
*/ |
24
|
|
|
public ?string $sequenceName = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function dbTypecast($value) |
30
|
|
|
{ |
31
|
|
|
if ($value === null) { |
32
|
|
|
return $value; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ($value instanceof ExpressionInterface) { |
36
|
|
|
return $value; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
if ($this->dimension > 0) { |
40
|
|
|
return $this->disableArraySupport |
|
|
|
|
41
|
|
|
? (string) $value |
42
|
|
|
: new ArrayExpression($value, $this->dbType, $this->dimension); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (\in_array($this->dbType, [Schema::TYPE_JSON, Schema::TYPE_JSONB], true)) { |
|
|
|
|
46
|
|
|
return new JsonExpression($value, $this->dbType); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->typecast($value); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritdoc} |
54
|
|
|
*/ |
55
|
|
|
public function phpTypecast($value) |
56
|
|
|
{ |
57
|
|
|
if ($this->dimension > 0) { |
58
|
|
|
if (!\is_array($value)) { |
59
|
|
|
$value = $this->getArrayParser()->parse($value); |
60
|
|
|
} |
61
|
|
|
if (\is_array($value)) { |
62
|
|
|
array_walk_recursive($value, function (&$val, $key) { |
63
|
|
|
$val = $this->phpTypecastValue($val); |
64
|
|
|
}); |
65
|
|
|
} elseif ($value === null) { |
|
|
|
|
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $value; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->phpTypecastValue($value); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Casts $value after retrieving from the DBMS to PHP representation. |
77
|
|
|
* |
78
|
|
|
* @param string|null $value |
79
|
|
|
* |
80
|
|
|
* @return bool|mixed|null |
81
|
|
|
*/ |
82
|
|
|
protected function phpTypecastValue($value) |
83
|
|
|
{ |
84
|
|
|
if ($value === null) { |
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
switch ($this->type) { |
89
|
|
|
case Schema::TYPE_BOOLEAN: |
90
|
|
|
$value = \is_string($value) ? strtolower($value) : $value; |
|
|
|
|
91
|
|
|
|
92
|
|
|
switch ($value) { |
93
|
|
|
case 't': |
94
|
|
|
case 'true': |
95
|
|
|
return true; |
96
|
|
|
case 'f': |
97
|
|
|
case 'false': |
98
|
|
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return (bool) $value; |
102
|
|
|
case Schema::TYPE_JSON: |
103
|
|
|
return \json_decode($value, true); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return parent::phpTypecast($value); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Creates instance of ArrayParser |
111
|
|
|
* |
112
|
|
|
* @return ArrayParser |
113
|
|
|
*/ |
114
|
|
|
protected function getArrayParser(): ArrayParser |
115
|
|
|
{ |
116
|
|
|
static $parser = null; |
117
|
|
|
|
118
|
|
|
if ($parser === null) { |
119
|
|
|
$parser = new ArrayParser(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $parser; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|