From daee04fa2f4ca8fda50b8eb12ed7cc84413d03f2 Mon Sep 17 00:00:00 2001 From: Yurun Date: Thu, 19 Oct 2023 11:40:02 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=85=BC=E5=AE=B9=20pdo=5Fpgsql=20float=20?= =?UTF-8?q?=E5=80=BC=E6=98=AF=20string=20=E4=BC=98=E5=8C=96=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E7=B1=BB=E5=9E=8B=E5=AD=97=E6=AE=B5=E7=9A=84=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Model/Cli/Model/ModelGenerate.php | 53 +++---- .../src/Model/Cli/Model/base-template.tpl | 5 +- .../pgsql/tests/Model/ArrayTest.php | 17 +++ .../pgsql/tests/Model/Base/ArrayTestBase.php | 134 ++++++++++++++++++ .../pgsql/tests/Model/Base/ArticleBase.php | 16 +-- .../pgsql/tests/Model/Base/MemberBase.php | 12 +- .../pgsql/tests/Model/Base/NoIncPkBase.php | 12 +- .../tests/Model/Base/PerformanceBase.php | 8 +- .../pgsql/tests/Model/Base/TestJsonBase.php | 6 +- .../tests/Model/Base/TestSoftDeleteBase.php | 12 +- .../pgsql/tests/Model/Base/TreeBase.php | 12 +- .../pgsql/tests/Model/Base/UpdateTimeBase.php | 48 +++---- .../tests/Model/Base/VirtualColumnBase.php | 10 +- 13 files changed, 250 insertions(+), 95 deletions(-) create mode 100644 src/Components/pgsql/tests/Model/ArrayTest.php create mode 100644 src/Components/pgsql/tests/Model/Base/ArrayTestBase.php diff --git a/src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php b/src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php index 5a5e0571df..13bd4a27b7 100644 --- a/src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php +++ b/src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php @@ -316,7 +316,7 @@ private function parseFields(?string $poolName, array $fields, ?array &$data, bo } $isPk = $field['ordinal_position'] > 0; - [$phpType, $phpDefinitionType] = $this->dbFieldTypeToPhp($field); + [$phpType, $phpDefinitionType, $typeConvert] = $this->dbFieldTypeToPhp($field); $data['fields'][] = [ 'name' => $field['attname'], 'varName' => Text::toCamelName($field['attname']), @@ -324,6 +324,7 @@ private function parseFields(?string $poolName, array $fields, ?array &$data, bo 'ndims' => $field['attndims'], 'phpType' => $phpType . '|null', 'phpDefinitionType' => $phpDefinitionType, + 'typeConvert' => $typeConvert, 'length' => $length, 'accuracy' => $accuracy, 'nullable' => 'f' === $field['attnotnull'], @@ -359,27 +360,27 @@ private function renderTemplate(string $template, array $data): string } public const DB_FIELD_TYPE_MAP = [ - 'int' => ['int', '?int'], - 'int2' => ['int', '?int'], - 'int4' => ['int', '?int'], - 'int8' => ['int', '?int'], - 'integer' => ['int', '?int'], - 'smallint' => ['int', '?int'], - 'bigint' => ['int', '?int'], - 'smallserial' => ['int', '?int'], - 'serial' => ['int', '?int'], - 'bigserial' => ['int', '?int'], - 'serial2' => ['int', '?int'], - 'serial4' => ['int', '?int'], - 'serial8' => ['int', '?int'], - 'bool' => ['bool', '?bool'], - 'boolean' => ['bool', '?bool'], - 'double' => ['float', '?float'], - 'float4' => ['float', '?float'], - 'float8' => ['float', '?float'], + 'int' => ['int', '?int', '(int)'], + 'int2' => ['int', '?int', '(int)'], + 'int4' => ['int', '?int', '(int)'], + 'int8' => ['int', '?int', '(int)'], + 'integer' => ['int', '?int', '(int)'], + 'smallint' => ['int', '?int', '(int)'], + 'bigint' => ['int', '?int', '(int)'], + 'smallserial' => ['int', '?int', '(int)'], + 'serial' => ['int', '?int', '(int)'], + 'bigserial' => ['int', '?int', '(int)'], + 'serial2' => ['int', '?int', '(int)'], + 'serial4' => ['int', '?int', '(int)'], + 'serial8' => ['int', '?int', '(int)'], + 'bool' => ['bool', '?bool', '(bool)'], + 'boolean' => ['bool', '?bool', '(bool)'], + 'double' => ['float', '?float', '(float)'], + 'float4' => ['float', '?float', '(float)'], + 'float8' => ['float', '?float', '(float)'], 'numeric' => ['string|float|int', \PHP_VERSION_ID >= 80000 ? 'string|float|int|null' : '', ''], - 'json' => ['\\' . \Imi\Util\LazyArrayObject::class . '|array', ''], - 'jsonb' => ['\\' . \Imi\Util\LazyArrayObject::class . '|array', ''], + 'json' => ['\\' . \Imi\Util\LazyArrayObject::class . '|array', '', ''], + 'jsonb' => ['\\' . \Imi\Util\LazyArrayObject::class . '|array', '', ''], ]; /** @@ -399,11 +400,15 @@ private function dbFieldTypeToPhp(array $field): array $type = $field['typname']; } - $result = self::DB_FIELD_TYPE_MAP[$type] ?? ['string', '?string']; + $result = self::DB_FIELD_TYPE_MAP[$type] ?? ['string', '?string', '']; if ($isArray) { - $result[0] .= 'array'; - $result[1] = '?array'; + $count = $field['attndims']; + $result = [ + str_repeat('array<', $count) . $result[0] . str_repeat('>', $count), + '?array', + '', + ]; } return $result; diff --git a/src/Components/pgsql/src/Model/Cli/Model/base-template.tpl b/src/Components/pgsql/src/Model/Cli/Model/base-template.tpl index 65f0645316..b27d19c21c 100644 --- a/src/Components/pgsql/src/Model/Cli/Model/base-template.tpl +++ b/src/Components/pgsql/src/Model/Cli/Model/base-template.tpl @@ -81,8 +81,7 @@ else * @return static */ - public function set( $) + public function set($) { $field['length'], @@ -99,7 +98,7 @@ else throw new \InvalidArgumentException('The maximum length of $ is '); } - $this-> = $; + $this-> = null === $ ? null : $; return $this; } diff --git a/src/Components/pgsql/tests/Model/ArrayTest.php b/src/Components/pgsql/tests/Model/ArrayTest.php new file mode 100644 index 0000000000..19b4d0375a --- /dev/null +++ b/src/Components/pgsql/tests/Model/ArrayTest.php @@ -0,0 +1,17 @@ +|null $arr1 + * @property array>|null $arr2 + */ +abstract class ArrayTestBase extends Model +{ + /** + * {@inheritdoc} + */ + public const PRIMARY_KEY = 'id'; + + /** + * {@inheritdoc} + */ + public const PRIMARY_KEYS = ['id']; + + /** + * id. + * + * @Column(name="id", type="int4", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=true, primaryKeyIndex=0, isAutoIncrement=true, ndims=0, virtual=false) + */ + protected ?int $id = null; + + /** + * 获取 id. + */ + public function getId(): ?int + { + return $this->id; + } + + /** + * 赋值 id. + * + * @param int|null $id id + * + * @return static + */ + public function setId($id) + { + $this->id = null === $id ? null : (int) $id; + + return $this; + } + + /** + * arr1. + * + * @Column(name="arr1", type="int8", length=-1, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false, ndims=1, virtual=false) + * + * @var array|null + */ + protected ?array $arr1 = null; + + /** + * 获取 arr1. + * + * @return array|null + */ + public function getArr1(): ?array + { + return $this->arr1; + } + + /** + * 赋值 arr1. + * + * @param array|null $arr1 arr1 + * + * @return static + */ + public function setArr1($arr1) + { + $this->arr1 = null === $arr1 ? null : $arr1; + + return $this; + } + + /** + * arr2. + * + * @Column(name="arr2", type="varchar", length=255, accuracy=0, nullable=false, default="", isPrimaryKey=false, primaryKeyIndex=-1, isAutoIncrement=false, ndims=2, virtual=false) + * + * @var array>|null + */ + protected ?array $arr2 = null; + + /** + * 获取 arr2. + * + * @return array>|null + */ + public function getArr2(): ?array + { + return $this->arr2; + } + + /** + * 赋值 arr2. + * + * @param array>|null $arr2 arr2 + * + * @return static + */ + public function setArr2($arr2) + { + if (\is_string($arr2) && mb_strlen($arr2) > 255) + { + throw new \InvalidArgumentException('The maximum length of $arr2 is 255'); + } + $this->arr2 = null === $arr2 ? null : $arr2; + + return $this; + } +} diff --git a/src/Components/pgsql/tests/Model/Base/ArticleBase.php b/src/Components/pgsql/tests/Model/Base/ArticleBase.php index e4a78f8d1a..bba43bd460 100644 --- a/src/Components/pgsql/tests/Model/Base/ArticleBase.php +++ b/src/Components/pgsql/tests/Model/Base/ArticleBase.php @@ -56,9 +56,9 @@ public function getId(): ?int * * @return static */ - public function setId(?int $id) + public function setId($id) { - $this->id = $id; + $this->id = null === $id ? null : (int) $id; return $this; } @@ -85,13 +85,13 @@ public function getTitle(): ?string * * @return static */ - public function setTitle(?string $title) + public function setTitle($title) { if (\is_string($title) && mb_strlen($title) > 255) { throw new \InvalidArgumentException('The maximum length of $title is 255'); } - $this->title = $title; + $this->title = null === $title ? null : $title; return $this; } @@ -118,9 +118,9 @@ public function getContent(): ?string * * @return static */ - public function setContent(?string $content) + public function setContent($content) { - $this->content = $content; + $this->content = null === $content ? null : $content; return $this; } @@ -147,9 +147,9 @@ public function getTime(): ?string * * @return static */ - public function setTime(?string $time) + public function setTime($time) { - $this->time = $time; + $this->time = null === $time ? null : $time; return $this; } diff --git a/src/Components/pgsql/tests/Model/Base/MemberBase.php b/src/Components/pgsql/tests/Model/Base/MemberBase.php index 77f126702c..b872bac4a6 100644 --- a/src/Components/pgsql/tests/Model/Base/MemberBase.php +++ b/src/Components/pgsql/tests/Model/Base/MemberBase.php @@ -55,9 +55,9 @@ public function getId(): ?int * * @return static */ - public function setId(?int $id) + public function setId($id) { - $this->id = $id; + $this->id = null === $id ? null : (int) $id; return $this; } @@ -85,13 +85,13 @@ public function getUsername(): ?string * * @return static */ - public function setUsername(?string $username) + public function setUsername($username) { if (\is_string($username) && mb_strlen($username) > 32) { throw new \InvalidArgumentException('The maximum length of $username is 32'); } - $this->username = $username; + $this->username = null === $username ? null : $username; return $this; } @@ -119,13 +119,13 @@ public function getPassword(): ?string * * @return static */ - public function setPassword(?string $password) + public function setPassword($password) { if (\is_string($password) && mb_strlen($password) > 255) { throw new \InvalidArgumentException('The maximum length of $password is 255'); } - $this->password = $password; + $this->password = null === $password ? null : $password; return $this; } diff --git a/src/Components/pgsql/tests/Model/Base/NoIncPkBase.php b/src/Components/pgsql/tests/Model/Base/NoIncPkBase.php index 7a795ef88e..5f4036acb2 100644 --- a/src/Components/pgsql/tests/Model/Base/NoIncPkBase.php +++ b/src/Components/pgsql/tests/Model/Base/NoIncPkBase.php @@ -55,9 +55,9 @@ public function getAId(): ?int * * @return static */ - public function setAId(?int $aId) + public function setAId($aId) { - $this->aId = $aId; + $this->aId = null === $aId ? null : (int) $aId; return $this; } @@ -84,9 +84,9 @@ public function getBId(): ?int * * @return static */ - public function setBId(?int $bId) + public function setBId($bId) { - $this->bId = $bId; + $this->bId = null === $bId ? null : (int) $bId; return $this; } @@ -113,13 +113,13 @@ public function getValue(): ?string * * @return static */ - public function setValue(?string $value) + public function setValue($value) { if (\is_string($value) && mb_strlen($value) > 255) { throw new \InvalidArgumentException('The maximum length of $value is 255'); } - $this->value = $value; + $this->value = null === $value ? null : $value; return $this; } diff --git a/src/Components/pgsql/tests/Model/Base/PerformanceBase.php b/src/Components/pgsql/tests/Model/Base/PerformanceBase.php index 9dd96b6976..3c78636611 100644 --- a/src/Components/pgsql/tests/Model/Base/PerformanceBase.php +++ b/src/Components/pgsql/tests/Model/Base/PerformanceBase.php @@ -54,9 +54,9 @@ public function getId(): ?int * * @return static */ - public function setId(?int $id) + public function setId($id) { - $this->id = $id; + $this->id = null === $id ? null : (int) $id; return $this; } @@ -83,13 +83,13 @@ public function getValue(): ?string * * @return static */ - public function setValue(?string $value) + public function setValue($value) { if (\is_string($value) && mb_strlen($value) > 255) { throw new \InvalidArgumentException('The maximum length of $value is 255'); } - $this->value = $value; + $this->value = null === $value ? null : $value; return $this; } diff --git a/src/Components/pgsql/tests/Model/Base/TestJsonBase.php b/src/Components/pgsql/tests/Model/Base/TestJsonBase.php index d1b95bf416..f59787a48f 100644 --- a/src/Components/pgsql/tests/Model/Base/TestJsonBase.php +++ b/src/Components/pgsql/tests/Model/Base/TestJsonBase.php @@ -54,9 +54,9 @@ public function getId(): ?int * * @return static */ - public function setId(?int $id) + public function setId($id) { - $this->id = $id; + $this->id = null === $id ? null : (int) $id; return $this; } @@ -90,7 +90,7 @@ public function &getJsonData() */ public function setJsonData($jsonData) { - $this->jsonData = $jsonData; + $this->jsonData = null === $jsonData ? null : $jsonData; return $this; } diff --git a/src/Components/pgsql/tests/Model/Base/TestSoftDeleteBase.php b/src/Components/pgsql/tests/Model/Base/TestSoftDeleteBase.php index a965f69fec..879a739587 100644 --- a/src/Components/pgsql/tests/Model/Base/TestSoftDeleteBase.php +++ b/src/Components/pgsql/tests/Model/Base/TestSoftDeleteBase.php @@ -55,9 +55,9 @@ public function getId(): ?int * * @return static */ - public function setId(?int $id) + public function setId($id) { - $this->id = $id; + $this->id = null === $id ? null : (int) $id; return $this; } @@ -84,13 +84,13 @@ public function getTitle(): ?string * * @return static */ - public function setTitle(?string $title) + public function setTitle($title) { if (\is_string($title) && mb_strlen($title) > 255) { throw new \InvalidArgumentException('The maximum length of $title is 255'); } - $this->title = $title; + $this->title = null === $title ? null : $title; return $this; } @@ -117,9 +117,9 @@ public function getDeleteTime(): ?int * * @return static */ - public function setDeleteTime(?int $deleteTime) + public function setDeleteTime($deleteTime) { - $this->deleteTime = $deleteTime; + $this->deleteTime = null === $deleteTime ? null : (int) $deleteTime; return $this; } diff --git a/src/Components/pgsql/tests/Model/Base/TreeBase.php b/src/Components/pgsql/tests/Model/Base/TreeBase.php index 587c9e4921..09ef596796 100644 --- a/src/Components/pgsql/tests/Model/Base/TreeBase.php +++ b/src/Components/pgsql/tests/Model/Base/TreeBase.php @@ -55,9 +55,9 @@ public function getId(): ?int * * @return static */ - public function setId(?int $id) + public function setId($id) { - $this->id = $id; + $this->id = null === $id ? null : (int) $id; return $this; } @@ -84,9 +84,9 @@ public function getParentId(): ?int * * @return static */ - public function setParentId(?int $parentId) + public function setParentId($parentId) { - $this->parentId = $parentId; + $this->parentId = null === $parentId ? null : (int) $parentId; return $this; } @@ -113,13 +113,13 @@ public function getName(): ?string * * @return static */ - public function setName(?string $name) + public function setName($name) { if (\is_string($name) && mb_strlen($name) > 32) { throw new \InvalidArgumentException('The maximum length of $name is 32'); } - $this->name = $name; + $this->name = null === $name ? null : $name; return $this; } diff --git a/src/Components/pgsql/tests/Model/Base/UpdateTimeBase.php b/src/Components/pgsql/tests/Model/Base/UpdateTimeBase.php index 3dbbe137c4..1ddb25c804 100644 --- a/src/Components/pgsql/tests/Model/Base/UpdateTimeBase.php +++ b/src/Components/pgsql/tests/Model/Base/UpdateTimeBase.php @@ -64,9 +64,9 @@ public function getId(): ?int * * @return static */ - public function setId(?int $id) + public function setId($id) { - $this->id = $id; + $this->id = null === $id ? null : (int) $id; return $this; } @@ -93,9 +93,9 @@ public function getDate(): ?string * * @return static */ - public function setDate(?string $date) + public function setDate($date) { - $this->date = $date; + $this->date = null === $date ? null : $date; return $this; } @@ -122,9 +122,9 @@ public function getTime(): ?string * * @return static */ - public function setTime(?string $time) + public function setTime($time) { - $this->time = $time; + $this->time = null === $time ? null : $time; return $this; } @@ -151,9 +151,9 @@ public function getTimetz(): ?string * * @return static */ - public function setTimetz(?string $timetz) + public function setTimetz($timetz) { - $this->timetz = $timetz; + $this->timetz = null === $timetz ? null : $timetz; return $this; } @@ -180,9 +180,9 @@ public function getTime2(): ?string * * @return static */ - public function setTime2(?string $time2) + public function setTime2($time2) { - $this->time2 = $time2; + $this->time2 = null === $time2 ? null : $time2; return $this; } @@ -209,9 +209,9 @@ public function getTimetz2(): ?string * * @return static */ - public function setTimetz2(?string $timetz2) + public function setTimetz2($timetz2) { - $this->timetz2 = $timetz2; + $this->timetz2 = null === $timetz2 ? null : $timetz2; return $this; } @@ -238,9 +238,9 @@ public function getTimestamp(): ?string * * @return static */ - public function setTimestamp(?string $timestamp) + public function setTimestamp($timestamp) { - $this->timestamp = $timestamp; + $this->timestamp = null === $timestamp ? null : $timestamp; return $this; } @@ -267,9 +267,9 @@ public function getTimestamptz(): ?string * * @return static */ - public function setTimestamptz(?string $timestamptz) + public function setTimestamptz($timestamptz) { - $this->timestamptz = $timestamptz; + $this->timestamptz = null === $timestamptz ? null : $timestamptz; return $this; } @@ -296,9 +296,9 @@ public function getTimestamp2(): ?string * * @return static */ - public function setTimestamp2(?string $timestamp2) + public function setTimestamp2($timestamp2) { - $this->timestamp2 = $timestamp2; + $this->timestamp2 = null === $timestamp2 ? null : $timestamp2; return $this; } @@ -325,9 +325,9 @@ public function getTimestamptz2(): ?string * * @return static */ - public function setTimestamptz2(?string $timestamptz2) + public function setTimestamptz2($timestamptz2) { - $this->timestamptz2 = $timestamptz2; + $this->timestamptz2 = null === $timestamptz2 ? null : $timestamptz2; return $this; } @@ -354,9 +354,9 @@ public function getInt(): ?int * * @return static */ - public function setInt(?int $int) + public function setInt($int) { - $this->int = $int; + $this->int = null === $int ? null : (int) $int; return $this; } @@ -383,9 +383,9 @@ public function getBigint(): ?int * * @return static */ - public function setBigint(?int $bigint) + public function setBigint($bigint) { - $this->bigint = $bigint; + $this->bigint = null === $bigint ? null : (int) $bigint; return $this; } diff --git a/src/Components/pgsql/tests/Model/Base/VirtualColumnBase.php b/src/Components/pgsql/tests/Model/Base/VirtualColumnBase.php index 683116aff3..e3bfcedc2f 100644 --- a/src/Components/pgsql/tests/Model/Base/VirtualColumnBase.php +++ b/src/Components/pgsql/tests/Model/Base/VirtualColumnBase.php @@ -55,9 +55,9 @@ public function getId(): ?int * * @return static */ - public function setId(?int $id) + public function setId($id) { - $this->id = $id; + $this->id = null === $id ? null : (int) $id; return $this; } @@ -84,9 +84,9 @@ public function getAmount(): ?int * * @return static */ - public function setAmount(?int $amount) + public function setAmount($amount) { - $this->amount = $amount; + $this->amount = null === $amount ? null : (int) $amount; return $this; } @@ -119,7 +119,7 @@ public function getVirtualAmount() */ public function setVirtualAmount($virtualAmount) { - $this->virtualAmount = $virtualAmount; + $this->virtualAmount = null === $virtualAmount ? null : $virtualAmount; return $this; } From ae2e13a03d75e34f542a7bdeb174d97c3cb9f463 Mon Sep 17 00:00:00 2001 From: Yurun Date: Thu, 19 Oct 2023 11:49:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20pgsql=20=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E5=AF=B9=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=95=B0=E7=BB=84?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E8=BF=9B=E8=A1=8C=E4=BA=86=E4=B8=8D=E5=BA=94?= =?UTF-8?q?=E8=AF=A5=E7=9A=84=E9=95=BF=E5=BA=A6=E9=AA=8C=E8=AF=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Components/pgsql/src/Model/Cli/Model/base-template.tpl | 2 +- src/Components/pgsql/tests/Model/Base/ArrayTestBase.php | 4 ---- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Components/pgsql/src/Model/Cli/Model/base-template.tpl b/src/Components/pgsql/src/Model/Cli/Model/base-template.tpl index b27d19c21c..34115be01b 100644 --- a/src/Components/pgsql/src/Model/Cli/Model/base-template.tpl +++ b/src/Components/pgsql/src/Model/Cli/Model/base-template.tpl @@ -83,7 +83,7 @@ else */ public function set($) { - $field['length'], 'char' => $field['length'], 'varchar' => $field['length'], diff --git a/src/Components/pgsql/tests/Model/Base/ArrayTestBase.php b/src/Components/pgsql/tests/Model/Base/ArrayTestBase.php index 01e4ff8cba..75f7a882bc 100644 --- a/src/Components/pgsql/tests/Model/Base/ArrayTestBase.php +++ b/src/Components/pgsql/tests/Model/Base/ArrayTestBase.php @@ -123,10 +123,6 @@ public function getArr2(): ?array */ public function setArr2($arr2) { - if (\is_string($arr2) && mb_strlen($arr2) > 255) - { - throw new \InvalidArgumentException('The maximum length of $arr2 is 255'); - } $this->arr2 = null === $arr2 ? null : $arr2; return $this;