Skip to content

Commit

Permalink
兼容 pdo_pgsql float 值是 string (#596)
Browse files Browse the repository at this point in the history
* 兼容 pdo_pgsql float 值是 string
优化数组类型字段的类型注释

* 修复 pgsql 模型对字符串数组字段进行了不应该的长度验证
  • Loading branch information
Yurunsoft authored Oct 19, 2023
1 parent fcacb93 commit 59150c4
Show file tree
Hide file tree
Showing 13 changed files with 247 additions and 96 deletions.
53 changes: 29 additions & 24 deletions src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,15 @@ 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']),
'type' => $type = ('_' === $field['typname'][0] ? substr($field['typname'], 1) : $field['typname']),
'ndims' => $field['attndims'],
'phpType' => $phpType . '|null',
'phpDefinitionType' => $phpDefinitionType,
'typeConvert' => $typeConvert,
'length' => $length,
'accuracy' => $accuracy,
'nullable' => 'f' === $field['attnotnull'],
Expand Down Expand Up @@ -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', '', ''],
];

/**
Expand All @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions src/Components/pgsql/src/Model/Cli/Model/base-template.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,9 @@ else

* @return static
*/
public function set<?php echo ucfirst($field['varName']); ?>(<?php if ($field['typeDefinition'] && $field['phpDefinitionType'])
{ ?><?php echo $field['phpDefinitionType']; ?> <?php } ?>$<?php echo $field['varName']; ?>)
public function set<?php echo ucfirst($field['varName']); ?>($<?php echo $field['varName']; ?>)
{
<?php if ($lengthCheck && $length = [
<?php if ($lengthCheck && 0 === $field['ndims'] && $length = [
'character' => $field['length'],
'char' => $field['length'],
'varchar' => $field['length'],
Expand All @@ -99,7 +98,7 @@ else
throw new \InvalidArgumentException('The maximum length of $<?php echo $field['varName']; ?> is <?php echo $length; ?>');
}
<?php } ?>
$this-><?php echo $field['varName']; ?> = $<?php echo $field['varName']; ?>;
$this-><?php echo $field['varName']; ?> = null === $<?php echo $field['varName']; ?> ? null : <?php echo $field['typeConvert']; ?>$<?php echo $field['varName']; ?>;
return $this;
}

Expand Down
17 changes: 17 additions & 0 deletions src/Components/pgsql/tests/Model/ArrayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Imi\Pgsql\Test\Model;

use Imi\Bean\Annotation\Inherit;
use Imi\Pgsql\Test\Model\Base\ArrayTestBase;

/**
* tb_array_test.
*
* @Inherit
*/
class ArrayTest extends ArrayTestBase
{
}
130 changes: 130 additions & 0 deletions src/Components/pgsql/tests/Model/Base/ArrayTestBase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?php

declare(strict_types=1);

namespace Imi\Pgsql\Test\Model\Base;

use Imi\Config\Annotation\ConfigValue;
use Imi\Model\Annotation\Column;
use Imi\Model\Annotation\Entity;
use Imi\Model\Annotation\Table;
use Imi\Pgsql\Model\PgModel as Model;

/**
* tb_array_test 基类.
*
* @Entity(camel=true, bean=true, incrUpdate=false)
*
* @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"))
*
* @property int|null $id
* @property array<int>|null $arr1
* @property array<array<string>>|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<int>|null
*/
protected ?array $arr1 = null;

/**
* 获取 arr1.
*
* @return array<int>|null
*/
public function getArr1(): ?array
{
return $this->arr1;
}

/**
* 赋值 arr1.
*
* @param array<int>|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<array<string>>|null
*/
protected ?array $arr2 = null;

/**
* 获取 arr2.
*
* @return array<array<string>>|null
*/
public function getArr2(): ?array
{
return $this->arr2;
}

/**
* 赋值 arr2.
*
* @param array<array<string>>|null $arr2 arr2
*
* @return static
*/
public function setArr2($arr2)
{
$this->arr2 = null === $arr2 ? null : $arr2;

return $this;
}
}
16 changes: 8 additions & 8 deletions src/Components/pgsql/tests/Model/Base/ArticleBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Components/pgsql/tests/Model/Base/MemberBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 6 additions & 6 deletions src/Components/pgsql/tests/Model/Base/NoIncPkBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Loading

0 comments on commit 59150c4

Please sign in to comment.