From 46019cc3715026f5697ccb09b03051a0c7302de3 Mon Sep 17 00:00:00 2001 From: Yurun Date: Fri, 26 May 2023 09:02:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E6=8C=81=20PostgreSQL=20=E7=94=9F?= =?UTF-8?q?=E6=88=90=E6=A8=A1=E5=9E=8B=E9=85=8D=E7=BD=AE=20bean=20?= =?UTF-8?q?=E5=92=8C=20incrUpdate=20=E5=8F=82=E6=95=B0=20(#524)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 优化代码 * 支持 PostgreSQL 生成模型配置 bean 和 incrUpdate 参数 * 生成测试模型 * 修复 --- .../src/Model/Cli/Model/ModelGenerate.php | 9 ++++-- .../src/Model/Cli/Model/base-template.tpl | 32 +++++++++++-------- src/Components/pgsql/src/Model/PgModel.php | 9 ------ .../pgsql/tests/Model/Base/ArticleBase.php | 2 +- .../pgsql/tests/Model/Base/MemberBase.php | 2 +- .../tests/Model/Base/PerformanceBase.php | 2 +- .../pgsql/tests/Model/Base/TestJsonBase.php | 2 +- .../tests/Model/Base/TestSoftDeleteBase.php | 2 +- .../pgsql/tests/Model/Base/TreeBase.php | 2 +- .../pgsql/tests/Model/Base/UpdateTimeBase.php | 2 +- .../tests/Model/Base/VirtualColumnBase.php | 2 +- src/Model/SoftDelete/Traits/TSoftDelete.php | 4 +-- 12 files changed, 36 insertions(+), 34 deletions(-) diff --git a/src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php b/src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php index 8faac70c29..a9043d2471 100644 --- a/src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php +++ b/src/Components/pgsql/src/Model/Cli/Model/ModelGenerate.php @@ -42,11 +42,13 @@ class ModelGenerate extends BaseCommand * @Option(name="basePath", type=ArgType::STRING, default=null, comments="指定命名空间对应的基准路径,可选") * @Option(name="entity", type=ArgType::BOOLEAN, default=true, comments="序列化时是否使用驼峰命名(true or false),默认true,可选") * @Option(name="lengthCheck", type=ArgType::BOOLEAN, default=false, comments="是否检查字符串字段长度,可选") + * @Option(name="bean", type=ArgType::BOOL, comments="模型对象是否作为 bean 类使用", default=true) + * @Option(name="incrUpdate", type=ArgType::BOOL, comments="模型是否启用增量更新", default=false) * * @param string|bool $override * @param string|bool $config */ - public function generate(string $namespace, string $baseClass, ?string $database, ?string $poolName, array $prefix, array $include, array $exclude, $override, $config, ?string $basePath, bool $entity, bool $lengthCheck): void + public function generate(string $namespace, string $baseClass, ?string $database, ?string $poolName, array $prefix, array $include, array $exclude, $override, $config, ?string $basePath, bool $entity, bool $lengthCheck, bool $bean, bool $incrUpdate): void { $db = Db::getInstance($poolName); $tablePrefix = $db->getOption()['prefix'] ?? ''; @@ -161,9 +163,10 @@ public function generate(string $namespace, string $baseClass, ?string $database $hasResult = false; $fileName = ''; $modelNamespace = ''; + $tableConfig = null; foreach ($configData['namespace'] ?? [] as $namespaceName => $namespaceItem) { - if (\in_array($table, $namespaceItem['tables'] ?? [])) + if (($tableConfig = ($namespaceItem['tables'][$table] ?? null)) || \in_array($table, $namespaceItem['tables'] ?? [])) { $modelNamespace = $namespaceName; $path = Imi::getNamespacePath($modelNamespace, true); @@ -217,6 +220,8 @@ public function generate(string $namespace, string $baseClass, ?string $database ], 'fields' => [], 'entity' => $entity, + 'bean' => $tableConfig['bean'] ?? $bean, + 'incrUpdate' => $tableConfig['incrUpdate'] ?? $incrUpdate, 'poolName' => $poolName, 'tableComment' => $tableComment, 'lengthCheck' => $lengthCheck, 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 686c35566a..cb955b46b0 100644 --- a/src/Components/pgsql/src/Model/Cli/Model/base-template.tpl +++ b/src/Components/pgsql/src/Model/Cli/Model/base-template.tpl @@ -15,13 +15,13 @@ use as Model; /** * 基类. * - * @Entity@Entity(false) - - * @Table(name=@ConfigValue(name="@app.models.\.name", default=""), usePrefix=, id={}, dbPoolName=@ConfigValue(name="@app.models.\.poolName", default="")) + * @Entity(camel=, bean=, incrUpdate=) + * @Table(name=@ConfigValue(name="@app.models.\.name", default=""), usePrefix=, id={}, dbPoolName=@ConfigValue(name="@app.models.\.poolName", default="")) * - + * @property $ @@ -43,11 +43,12 @@ abstract class Base extends Model { ?> /** - + * . +else +{ ?> * . * @@ -57,7 +58,8 @@ abstract class Base extends Model * @var */ - protected $ = ; + protected $ = ; /** * 获取 . @@ -65,7 +67,9 @@ abstract class Base extends Model * @return */ - public function & get(): + public function & get(): { return $this->; @@ -78,7 +82,8 @@ abstract class Base extends Model * @return static */ - public function set( $) + public function set( $) { $field['length'], @@ -88,7 +93,8 @@ abstract class Base extends Model 'bit' => $field['length'], 'bit varying' => $field['length'], 'varbit' => $field['length'], -][$field['type']] ?? null) { ?> +][$field['type']] ?? null) +{ ?> if (is_string($) && mb_strlen($) > ) { throw new \InvalidArgumentException('The maximum length of $ is '); diff --git a/src/Components/pgsql/src/Model/PgModel.php b/src/Components/pgsql/src/Model/PgModel.php index 4ed59f2a2d..8f860b8703 100644 --- a/src/Components/pgsql/src/Model/PgModel.php +++ b/src/Components/pgsql/src/Model/PgModel.php @@ -4,7 +4,6 @@ namespace Imi\Pgsql\Model; -use Imi\Model\Contract\IModelQuery; use Imi\Model\Model; /** @@ -14,14 +13,6 @@ class PgModel extends Model { public const DEFAULT_QUERY_CLASS = ModelQuery::class; - /** - * {@inheritDoc} - */ - public static function query(?string $poolName = null, ?int $queryType = null, ?string $queryClass = null, ?string $alias = null): IModelQuery - { - return parent::query($poolName, $queryType, $queryClass, $alias); - } - /** * @param bool|int $timeAccuracy * diff --git a/src/Components/pgsql/tests/Model/Base/ArticleBase.php b/src/Components/pgsql/tests/Model/Base/ArticleBase.php index adbab198aa..5043ac8bbd 100644 --- a/src/Components/pgsql/tests/Model/Base/ArticleBase.php +++ b/src/Components/pgsql/tests/Model/Base/ArticleBase.php @@ -13,7 +13,7 @@ /** * tb_article 基类. * - * @Entity + * @Entity(camel=true, bean=true, incrUpdate=false) * * @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Article.name", default="tb_article"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Article.poolName")) * diff --git a/src/Components/pgsql/tests/Model/Base/MemberBase.php b/src/Components/pgsql/tests/Model/Base/MemberBase.php index 3db44b5315..358341cb35 100644 --- a/src/Components/pgsql/tests/Model/Base/MemberBase.php +++ b/src/Components/pgsql/tests/Model/Base/MemberBase.php @@ -13,7 +13,7 @@ /** * tb_member 基类. * - * @Entity + * @Entity(camel=true, bean=true, incrUpdate=false) * * @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Member.name", default="tb_member"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Member.poolName")) * diff --git a/src/Components/pgsql/tests/Model/Base/PerformanceBase.php b/src/Components/pgsql/tests/Model/Base/PerformanceBase.php index eb69c5bdb4..7cddcf6822 100644 --- a/src/Components/pgsql/tests/Model/Base/PerformanceBase.php +++ b/src/Components/pgsql/tests/Model/Base/PerformanceBase.php @@ -13,7 +13,7 @@ /** * tb_performance 基类. * - * @Entity + * @Entity(camel=true, bean=true, incrUpdate=false) * * @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Performance.name", default="tb_performance"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Performance.poolName")) * diff --git a/src/Components/pgsql/tests/Model/Base/TestJsonBase.php b/src/Components/pgsql/tests/Model/Base/TestJsonBase.php index ee8202c281..6d1a0c1ef0 100644 --- a/src/Components/pgsql/tests/Model/Base/TestJsonBase.php +++ b/src/Components/pgsql/tests/Model/Base/TestJsonBase.php @@ -13,7 +13,7 @@ /** * test 基类. * - * @Entity + * @Entity(camel=true, bean=true, incrUpdate=false) * * @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\TestJson.name", default="tb_test_json"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\TestJson.poolName")) * diff --git a/src/Components/pgsql/tests/Model/Base/TestSoftDeleteBase.php b/src/Components/pgsql/tests/Model/Base/TestSoftDeleteBase.php index ac36891f0b..5e3c6ead44 100644 --- a/src/Components/pgsql/tests/Model/Base/TestSoftDeleteBase.php +++ b/src/Components/pgsql/tests/Model/Base/TestSoftDeleteBase.php @@ -13,7 +13,7 @@ /** * tb_test_soft_delete 基类. * - * @Entity + * @Entity(camel=true, bean=true, incrUpdate=false) * * @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\TestSoftDelete.name", default="tb_test_soft_delete"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\TestSoftDelete.poolName")) * diff --git a/src/Components/pgsql/tests/Model/Base/TreeBase.php b/src/Components/pgsql/tests/Model/Base/TreeBase.php index e04b32daa7..684c30723f 100644 --- a/src/Components/pgsql/tests/Model/Base/TreeBase.php +++ b/src/Components/pgsql/tests/Model/Base/TreeBase.php @@ -13,7 +13,7 @@ /** * tb_tree 基类. * - * @Entity + * @Entity(camel=true, bean=true, incrUpdate=false) * * @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Tree.name", default="tb_tree"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\Tree.poolName")) * diff --git a/src/Components/pgsql/tests/Model/Base/UpdateTimeBase.php b/src/Components/pgsql/tests/Model/Base/UpdateTimeBase.php index 918f2a5afd..6217d4a81c 100644 --- a/src/Components/pgsql/tests/Model/Base/UpdateTimeBase.php +++ b/src/Components/pgsql/tests/Model/Base/UpdateTimeBase.php @@ -13,7 +13,7 @@ /** * tb_update_time 基类. * - * @Entity + * @Entity(camel=true, bean=true, incrUpdate=false) * * @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\UpdateTime.name", default="tb_update_time"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\UpdateTime.poolName")) * diff --git a/src/Components/pgsql/tests/Model/Base/VirtualColumnBase.php b/src/Components/pgsql/tests/Model/Base/VirtualColumnBase.php index 0732d7727e..d2c42c9270 100644 --- a/src/Components/pgsql/tests/Model/Base/VirtualColumnBase.php +++ b/src/Components/pgsql/tests/Model/Base/VirtualColumnBase.php @@ -13,7 +13,7 @@ /** * tb_virtual_column 基类. * - * @Entity + * @Entity(camel=true, bean=true, incrUpdate=false) * * @Table(name=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\VirtualColumn.name", default="tb_virtual_column"), usePrefix=false, id={"id"}, dbPoolName=@ConfigValue(name="@app.models.Imi\Pgsql\Test\Model\VirtualColumn.poolName")) * diff --git a/src/Model/SoftDelete/Traits/TSoftDelete.php b/src/Model/SoftDelete/Traits/TSoftDelete.php index e7e9dd692e..d35557a92a 100644 --- a/src/Model/SoftDelete/Traits/TSoftDelete.php +++ b/src/Model/SoftDelete/Traits/TSoftDelete.php @@ -102,9 +102,9 @@ public static function query(?string $poolName = null, ?int $queryType = null, ? * @param string|null $poolName 连接池名,为null则取默认 * @param int|null $queryType 查询类型;Imi\Db\Query\QueryType::READ/WRITE */ - public static function originQuery(?string $poolName = null, ?int $queryType = null, string $queryClass = self::DEFAULT_QUERY_CLASS): IModelQuery + public static function originQuery(?string $poolName = null, ?int $queryType = null, ?string $queryClass = null, ?string $alias = null): IModelQuery { - return parent::query($poolName, $queryType, $queryClass); + return parent::query($poolName, $queryType, $queryClass, $alias); } /**