Skip to content

Commit

Permalink
Merge branch '2.x' of https://github.com/easy-swoole/fast-db into 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Jun 17, 2024
2 parents 9621576 + e47950e commit 872ff4e
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 46 deletions.
80 changes: 40 additions & 40 deletions src/Commands/GenModelAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use EasySwoole\Command\Color;
use EasySwoole\Command\CommandManager;
use EasySwoole\EasySwoole\Command\CommandInterface;
use EasySwoole\FastDb\Config;
use EasySwoole\FastDb\Exception\RuntimeError;
use EasySwoole\FastDb\FastDb;
Expand All @@ -23,46 +22,48 @@

class GenModelAction implements ActionInterface
{
public function run(): ?string
private function runAction()
{
Coroutine\run(function () {
$commandManager = CommandManager::getInstance();
$table = $commandManager->getOpt('table');
if (!$table) {
return Color::danger("The option param 'table' missed!");
}
$connectionName = $commandManager->getOpt('db-connection');
if (!$connectionName) {
$connectionName = 'default';
}
$path = $commandManager->getOpt('path');
if (!$path) {
$path = 'App/Model';
}
try {
$columns = $this->formatColumns($this->getColumnTypeListing($connectionName, $table));
} catch (\Throwable $throwable) {
echo Color::red($throwable->getMessage()) . "\n";
echo Color::red("Stack trace:") . "\n";
echo Color::red($throwable->getTraceAsString()) . "\n";
echo Color::red(" thrown in " . $throwable->getFile() . " on line " . $throwable->getLine()) . "\n";
Timer::clearAll();
return null;
}
$commandManager = CommandManager::getInstance();
$table = $commandManager->getOpt('table');
if (!$table) {
return Color::danger("The option param 'table' missed!");
}

$project = new Project();
$class = $this->studly($table);
$class = $project->getNamespace($path) . $class;
$filepath = getcwd() . DIRECTORY_SEPARATOR . $project->path($class);
if (!file_exists($filepath)) {
$this->mkdir($filepath);
}
$connectionName = $commandManager->getOpt('db-connection');
if (!$connectionName) {
$connectionName = 'default';
}

file_put_contents($filepath, $this->buildClass($table, $class, $connectionName, $columns));
echo Color::success("Model {$class} was created.") . "\n";
Timer::clearAll();
return null;
});
$path = $commandManager->getOpt('path');
if (!$path) {
$path = 'App/Model';
}

$columns = $this->formatColumns($this->getColumnTypeListing($connectionName, $table));
$project = new Project();
$class = $this->studly($table);
$class = $project->getNamespace($path) . $class;
$filepath = getcwd() . DIRECTORY_SEPARATOR . $project->path($class);
if (!file_exists($filepath)) {
$this->mkdir($filepath);
}

file_put_contents($filepath, $this->buildClass($table, $class, $connectionName, $columns));
echo Color::success("Model {$class} was created.") . "\n";
Timer::clearAll();
return null;
}

public function run(): ?string
{
if (Coroutine::getCid() > 0) {
$this->runAction();
} else {
Coroutine\run(function () {
$this->runAction();
});
}

return null;
}
Expand Down Expand Up @@ -102,8 +103,7 @@ private function getColumnTypeListing(string $connectionName, string $table)
$sql = 'select `column_key` as `column_key`, `column_name` as `column_name`, `data_type` as `data_type`, `is_nullable` as `is_nullable`, `column_comment` as `column_comment`, `extra` as `extra`, `column_type` as `column_type` from information_schema.columns where `table_schema` = ? and `table_name` = ? order by ORDINAL_POSITION';
$builder = new QueryBuilder();
$builder->raw($sql, [$fastDbConfig->getDatabase(), $table]);

return FastDb::getInstance()->selectConnection($connectionName)->query($builder)->getResult();
return FastDb::getInstance()->query($builder)->getResult();
}

private function studly(string $value, string $gap = ''): string
Expand Down
28 changes: 24 additions & 4 deletions tests/FastDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,20 @@ public function testTestDb()
} catch (\Throwable $throwable) {
if (USE_MYSQLI) {
$this->assertInstanceOf(\mysqli_sql_exception::class, $throwable);
$this->assertSame("Access denied for user 'error'@'localhost' (using password: YES)", $throwable->getMessage());
$err = $throwable->getMessage();
if (str_contains($err, 'using password: YES')) {
$this->assertSame("Access denied for user 'error'@'localhost' (using password: YES)", $err);
} else {
$this->assertSame("Connection refused", $err);
}
} else {
$this->assertInstanceOf(RuntimeError::class, $throwable);
$this->assertSame("SQLSTATE[28000] [1045] Access denied for user 'error'@'localhost' (using password: YES)", $throwable->getMessage());
$err = $throwable->getMessage();
if (str_contains($err, 'using password: YES')) {
$this->assertSame("connection error error case initObject fail after 3 times case connection [[email protected]] connect error: SQLSTATE[28000] [1045] Access denied for user 'error'@'localhost' (using password: YES)", $err);
} else {
$this->assertSame("SQLSTATE[HY000] [2002] Connection refused", $err);
}
}
}
}
Expand Down Expand Up @@ -216,9 +226,19 @@ public function testBegin()
} catch (\Throwable $throwable) {
$this->assertInstanceOf(RuntimeError::class, $throwable);
if (USE_MYSQLI) {
$this->assertSame("connection error error case initObject fail after 3 times case Access denied for user 'error'@'localhost' (using password: YES)", $throwable->getMessage());
$err = $throwable->getMessage();
if (str_contains($err, '(using password: YES)')) {
$this->assertSame("connection error error case initObject fail after 3 times case Access denied for user 'error'@'localhost' (using password: YES)", $err);
} else {
$this->assertSame("connection error error case initObject fail after 3 times case Connection refused", $err);
}
} else {
$this->assertSame("connection error error case initObject fail after 3 times case connection [[email protected]] connect error: SQLSTATE[28000] [1045] Access denied for user 'error'@'localhost' (using password: YES)", $throwable->getMessage());
$err = $throwable->getMessage();
if (str_contains($err, '(using password: YES)')) {
$this->assertSame("connection error error case initObject fail after 3 times case connection [[email protected]] connect error: SQLSTATE[28000] [1045] Access denied for user 'error'@'localhost' (using password: YES)", $err);
} else {
$this->assertSame("connection error error case initObject fail after 3 times case connection [[email protected]] connect error: SQLSTATE[HY000] [2002] Connection refused", $err);
}
}
$errorFastDb->rollback();
}
Expand Down
79 changes: 79 additions & 0 deletions tests/GenModelActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);
/**
* This file is part of EasySwoole.
*
* @link https://www.easyswoole.com
* @document https://www.easyswoole.com
* @contact https://www.easyswoole.com/Preface/contact.html
* @license https://github.com/easy-swoole/easyswoole/blob/3.x/LICENSE
*/

namespace EasySwoole\FastDb\Tests;

use EasySwoole\Command\CommandManager;
use EasySwoole\FastDb\Commands\GenModelAction;
use EasySwoole\FastDb\Config;
use EasySwoole\FastDb\FastDb;
use EasySwoole\FastDb\Mysql\QueryResult;
use EasySwoole\Mysqli\QueryBuilder;
use EasySwoole\Spl\SplBean;

final class GenModelActionTest extends BaseTestCase
{
protected $tableName = 'easyswoole_user';

protected function setUp(): void
{
parent::setUp();
$configObj = new Config(MYSQL_CONFIG);
FastDb::getInstance()->addDb($configObj);
FastDb::getInstance()->setOnQuery(function (QueryResult $queryResult) {
// if ($queryResult->getQueryBuilder()) {
// echo $queryResult->getQueryBuilder()->getLastQuery() . "\n";
// } else {
// echo $queryResult->getRawSql() . "\n";
// }
});

$this->createTestTable();
}

private function createTestTable()
{
$sql = <<<Sql
CREATE TABLE IF NOT EXISTS `{$this->tableName}`
(
`id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'increment id',
`name` varchar(100) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'name',
`status` tinyint unsigned DEFAULT '0' COMMENT 'status',
`score` int unsigned DEFAULT '0' COMMENT 'score',
`sex` tinyint unsigned DEFAULT '0' COMMENT 'sex',
`address` json DEFAULT NULL COMMENT 'address',
`email` varchar(150) COLLATE utf8mb4_general_ci DEFAULT NULL COMMENT 'email',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
Sql;
$builder = new QueryBuilder();
$builder->raw($sql);
FastDb::getInstance()->query($builder)->getResult();
}

public function testGenModel()
{
$file = __DIR__ . '/Model/EasyswooleUser.php';
@unlink($file);

$commandManager = CommandManager::getInstance();
$opts = [
'table' => 'easyswoole_user',
'path' => 'tests/Model',
'with-comments' => '',
];
$commandManager->setOpts($opts);
(new GenModelAction())->run();

$this->assertFileExists($file);
@unlink($file);
}
}
4 changes: 2 additions & 2 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ protected function setUp(): void
FastDb::getInstance()->addDb($configObj);
FastDb::getInstance()->setOnQuery(function (QueryResult $queryResult) {
if ($queryResult->getQueryBuilder()) {
echo $queryResult->getQueryBuilder()->getLastQuery() . "\n";
// echo $queryResult->getQueryBuilder()->getLastQuery() . "\n";
} else {
echo $queryResult->getRawSql() . "\n";
// echo $queryResult->getRawSql() . "\n";
}
});
}
Expand Down

0 comments on commit 872ff4e

Please sign in to comment.