Skip to content

Commit 59150c4

Browse files
authored
兼容 pdo_pgsql float 值是 string (#596)
* 兼容 pdo_pgsql float 值是 string 优化数组类型字段的类型注释 * 修复 pgsql 模型对字符串数组字段进行了不应该的长度验证
1 parent fcacb93 commit 59150c4

13 files changed

+247
-96
lines changed

src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php

+29-24
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,15 @@ private function parseFields(?string $poolName, array $fields, ?array &$data, bo
316316
}
317317

318318
$isPk = $field['ordinal_position'] > 0;
319-
[$phpType, $phpDefinitionType] = $this->dbFieldTypeToPhp($field);
319+
[$phpType, $phpDefinitionType, $typeConvert] = $this->dbFieldTypeToPhp($field);
320320
$data['fields'][] = [
321321
'name' => $field['attname'],
322322
'varName' => Text::toCamelName($field['attname']),
323323
'type' => $type = ('_' === $field['typname'][0] ? substr($field['typname'], 1) : $field['typname']),
324324
'ndims' => $field['attndims'],
325325
'phpType' => $phpType . '|null',
326326
'phpDefinitionType' => $phpDefinitionType,
327+
'typeConvert' => $typeConvert,
327328
'length' => $length,
328329
'accuracy' => $accuracy,
329330
'nullable' => 'f' === $field['attnotnull'],
@@ -359,27 +360,27 @@ private function renderTemplate(string $template, array $data): string
359360
}
360361

361362
public const DB_FIELD_TYPE_MAP = [
362-
'int' => ['int', '?int'],
363-
'int2' => ['int', '?int'],
364-
'int4' => ['int', '?int'],
365-
'int8' => ['int', '?int'],
366-
'integer' => ['int', '?int'],
367-
'smallint' => ['int', '?int'],
368-
'bigint' => ['int', '?int'],
369-
'smallserial' => ['int', '?int'],
370-
'serial' => ['int', '?int'],
371-
'bigserial' => ['int', '?int'],
372-
'serial2' => ['int', '?int'],
373-
'serial4' => ['int', '?int'],
374-
'serial8' => ['int', '?int'],
375-
'bool' => ['bool', '?bool'],
376-
'boolean' => ['bool', '?bool'],
377-
'double' => ['float', '?float'],
378-
'float4' => ['float', '?float'],
379-
'float8' => ['float', '?float'],
363+
'int' => ['int', '?int', '(int)'],
364+
'int2' => ['int', '?int', '(int)'],
365+
'int4' => ['int', '?int', '(int)'],
366+
'int8' => ['int', '?int', '(int)'],
367+
'integer' => ['int', '?int', '(int)'],
368+
'smallint' => ['int', '?int', '(int)'],
369+
'bigint' => ['int', '?int', '(int)'],
370+
'smallserial' => ['int', '?int', '(int)'],
371+
'serial' => ['int', '?int', '(int)'],
372+
'bigserial' => ['int', '?int', '(int)'],
373+
'serial2' => ['int', '?int', '(int)'],
374+
'serial4' => ['int', '?int', '(int)'],
375+
'serial8' => ['int', '?int', '(int)'],
376+
'bool' => ['bool', '?bool', '(bool)'],
377+
'boolean' => ['bool', '?bool', '(bool)'],
378+
'double' => ['float', '?float', '(float)'],
379+
'float4' => ['float', '?float', '(float)'],
380+
'float8' => ['float', '?float', '(float)'],
380381
'numeric' => ['string|float|int', \PHP_VERSION_ID >= 80000 ? 'string|float|int|null' : '', ''],
381-
'json' => ['\\' . \Imi\Util\LazyArrayObject::class . '|array', ''],
382-
'jsonb' => ['\\' . \Imi\Util\LazyArrayObject::class . '|array', ''],
382+
'json' => ['\\' . \Imi\Util\LazyArrayObject::class . '|array', '', ''],
383+
'jsonb' => ['\\' . \Imi\Util\LazyArrayObject::class . '|array', '', ''],
383384
];
384385

385386
/**
@@ -399,11 +400,15 @@ private function dbFieldTypeToPhp(array $field): array
399400
$type = $field['typname'];
400401
}
401402

402-
$result = self::DB_FIELD_TYPE_MAP[$type] ?? ['string', '?string'];
403+
$result = self::DB_FIELD_TYPE_MAP[$type] ?? ['string', '?string', ''];
403404
if ($isArray)
404405
{
405-
$result[0] .= 'array';
406-
$result[1] = '?array';
406+
$count = $field['attndims'];
407+
$result = [
408+
str_repeat('array<', $count) . $result[0] . str_repeat('>', $count),
409+
'?array',
410+
'',
411+
];
407412
}
408413

409414
return $result;

src/Components/pgsql/src/Model/Cli/Model/base-template.tpl

+3-4
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,9 @@ else
8181

8282
* @return static
8383
*/
84-
public function set<?php echo ucfirst($field['varName']); ?>(<?php if ($field['typeDefinition'] && $field['phpDefinitionType'])
85-
{ ?><?php echo $field['phpDefinitionType']; ?> <?php } ?>$<?php echo $field['varName']; ?>)
84+
public function set<?php echo ucfirst($field['varName']); ?>($<?php echo $field['varName']; ?>)
8685
{
87-
<?php if ($lengthCheck && $length = [
86+
<?php if ($lengthCheck && 0 === $field['ndims'] && $length = [
8887
'character' => $field['length'],
8988
'char' => $field['length'],
9089
'varchar' => $field['length'],
@@ -99,7 +98,7 @@ else
9998
throw new \InvalidArgumentException('The maximum length of $<?php echo $field['varName']; ?> is <?php echo $length; ?>');
10099
}
101100
<?php } ?>
102-
$this-><?php echo $field['varName']; ?> = $<?php echo $field['varName']; ?>;
101+
$this-><?php echo $field['varName']; ?> = null === $<?php echo $field['varName']; ?> ? null : <?php echo $field['typeConvert']; ?>$<?php echo $field['varName']; ?>;
103102
return $this;
104103
}
105104

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Imi\Pgsql\Test\Model;
6+
7+
use Imi\Bean\Annotation\Inherit;
8+
use Imi\Pgsql\Test\Model\Base\ArrayTestBase;
9+
10+
/**
11+
* tb_array_test.
12+
*
13+
* @Inherit
14+
*/
15+
class ArrayTest extends ArrayTestBase
16+
{
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Imi\Pgsql\Test\Model\Base;
6+
7+
use Imi\Config\Annotation\ConfigValue;
8+
use Imi\Model\Annotation\Column;
9+
use Imi\Model\Annotation\Entity;
10+
use Imi\Model\Annotation\Table;
11+
use Imi\Pgsql\Model\PgModel as Model;
12+
13+
/**
14+
* tb_array_test 基类.
15+
*
16+
* @Entity(camel=true, bean=true, incrUpdate=false)
17+
*
18+
* @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\ArrayTest.name", default="tb_array_test"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\ArrayTest.poolName"))
19+
*
20+
* @property int|null $id
21+
* @property array<int>|null $arr1
22+
* @property array<array<string>>|null $arr2
23+
*/
24+
abstract class ArrayTestBase extends Model
25+
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public const PRIMARY_KEY = 'id';
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public const PRIMARY_KEYS = ['id'];
35+
36+
/**
37+
* id.
38+
*
39+
* @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false)
40+
*/
41+
protected ?int $id = null;
42+
43+
/**
44+
* 获取 id.
45+
*/
46+
public function getId(): ?int
47+
{
48+
return $this->id;
49+
}
50+
51+
/**
52+
* 赋值 id.
53+
*
54+
* @param int|null $id id
55+
*
56+
* @return static
57+
*/
58+
public function setId($id)
59+
{
60+
$this->id = null === $id ? null : (int) $id;
61+
62+
return $this;
63+
}
64+
65+
/**
66+
* arr1.
67+
*
68+
* @Column(name="arr1", type="int8", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false, ndims=1, virtual=false)
69+
*
70+
* @var array<int>|null
71+
*/
72+
protected ?array $arr1 = null;
73+
74+
/**
75+
* 获取 arr1.
76+
*
77+
* @return array<int>|null
78+
*/
79+
public function getArr1(): ?array
80+
{
81+
return $this->arr1;
82+
}
83+
84+
/**
85+
* 赋值 arr1.
86+
*
87+
* @param array<int>|null $arr1 arr1
88+
*
89+
* @return static
90+
*/
91+
public function setArr1($arr1)
92+
{
93+
$this->arr1 = null === $arr1 ? null : $arr1;
94+
95+
return $this;
96+
}
97+
98+
/**
99+
* arr2.
100+
*
101+
* @Column(name="arr2", type="varchar", length=255, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false, ndims=2, virtual=false)
102+
*
103+
* @var array<array<string>>|null
104+
*/
105+
protected ?array $arr2 = null;
106+
107+
/**
108+
* 获取 arr2.
109+
*
110+
* @return array<array<string>>|null
111+
*/
112+
public function getArr2(): ?array
113+
{
114+
return $this->arr2;
115+
}
116+
117+
/**
118+
* 赋值 arr2.
119+
*
120+
* @param array<array<string>>|null $arr2 arr2
121+
*
122+
* @return static
123+
*/
124+
public function setArr2($arr2)
125+
{
126+
$this->arr2 = null === $arr2 ? null : $arr2;
127+
128+
return $this;
129+
}
130+
}

src/Components/pgsql/tests/Model/Base/ArticleBase.php

+8-8
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ public function getId(): ?int
5656
*
5757
* @return static
5858
*/
59-
public function setId(?int $id)
59+
public function setId($id)
6060
{
61-
$this->id = $id;
61+
$this->id = null === $id ? null : (int) $id;
6262

6363
return $this;
6464
}
@@ -85,13 +85,13 @@ public function getTitle(): ?string
8585
*
8686
* @return static
8787
*/
88-
public function setTitle(?string $title)
88+
public function setTitle($title)
8989
{
9090
if (\is_string($title) && mb_strlen($title) > 255)
9191
{
9292
throw new \InvalidArgumentException('The maximum length of $title is 255');
9393
}
94-
$this->title = $title;
94+
$this->title = null === $title ? null : $title;
9595

9696
return $this;
9797
}
@@ -118,9 +118,9 @@ public function getContent(): ?string
118118
*
119119
* @return static
120120
*/
121-
public function setContent(?string $content)
121+
public function setContent($content)
122122
{
123-
$this->content = $content;
123+
$this->content = null === $content ? null : $content;
124124

125125
return $this;
126126
}
@@ -147,9 +147,9 @@ public function getTime(): ?string
147147
*
148148
* @return static
149149
*/
150-
public function setTime(?string $time)
150+
public function setTime($time)
151151
{
152-
$this->time = $time;
152+
$this->time = null === $time ? null : $time;
153153

154154
return $this;
155155
}

src/Components/pgsql/tests/Model/Base/MemberBase.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ public function getId(): ?int
5555
*
5656
* @return static
5757
*/
58-
public function setId(?int $id)
58+
public function setId($id)
5959
{
60-
$this->id = $id;
60+
$this->id = null === $id ? null : (int) $id;
6161

6262
return $this;
6363
}
@@ -85,13 +85,13 @@ public function getUsername(): ?string
8585
*
8686
* @return static
8787
*/
88-
public function setUsername(?string $username)
88+
public function setUsername($username)
8989
{
9090
if (\is_string($username) && mb_strlen($username) > 32)
9191
{
9292
throw new \InvalidArgumentException('The maximum length of $username is 32');
9393
}
94-
$this->username = $username;
94+
$this->username = null === $username ? null : $username;
9595

9696
return $this;
9797
}
@@ -119,13 +119,13 @@ public function getPassword(): ?string
119119
*
120120
* @return static
121121
*/
122-
public function setPassword(?string $password)
122+
public function setPassword($password)
123123
{
124124
if (\is_string($password) && mb_strlen($password) > 255)
125125
{
126126
throw new \InvalidArgumentException('The maximum length of $password is 255');
127127
}
128-
$this->password = $password;
128+
$this->password = null === $password ? null : $password;
129129

130130
return $this;
131131
}

src/Components/pgsql/tests/Model/Base/NoIncPkBase.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ public function getAId(): ?int
5555
*
5656
* @return static
5757
*/
58-
public function setAId(?int $aId)
58+
public function setAId($aId)
5959
{
60-
$this->aId = $aId;
60+
$this->aId = null === $aId ? null : (int) $aId;
6161

6262
return $this;
6363
}
@@ -84,9 +84,9 @@ public function getBId(): ?int
8484
*
8585
* @return static
8686
*/
87-
public function setBId(?int $bId)
87+
public function setBId($bId)
8888
{
89-
$this->bId = $bId;
89+
$this->bId = null === $bId ? null : (int) $bId;
9090

9191
return $this;
9292
}
@@ -113,13 +113,13 @@ public function getValue(): ?string
113113
*
114114
* @return static
115115
*/
116-
public function setValue(?string $value)
116+
public function setValue($value)
117117
{
118118
if (\is_string($value) && mb_strlen($value) > 255)
119119
{
120120
throw new \InvalidArgumentException('The maximum length of $value is 255');
121121
}
122-
$this->value = $value;
122+
$this->value = null === $value ? null : $value;
123123

124124
return $this;
125125
}

0 commit comments

Comments
 (0)