Skip to content

Commit

Permalink
fix: fix some error for sql <=> md parse
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 4, 2021
1 parent 1e4e068 commit 3f4ee91
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 64 deletions.
3 changes: 2 additions & 1 deletion app/Lib/Parser/DBMdTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Inhere\Kite\Lib\Parser;

use Inhere\Kite\Lib\Parser\MySQL\TypeMap;
use function array_filter;
use function array_map;
use function array_values;
Expand Down Expand Up @@ -132,7 +133,7 @@ public function parseLine(string $line): array

// default value
if (isset($nodes[3])) {
if (DBTable::isNoDefault($upType)) {
if (TypeMap::isNoDefault($upType)) {
$defValue = '';
} else {
$defValue = $isInt ? (int)$nodes[3] : $nodes[3];
Expand Down
35 changes: 2 additions & 33 deletions app/Lib/Parser/DBTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Inhere\Kite\Lib\Parser;

use Inhere\Kite\Lib\Parser\MySQL\TypeMap;
use Inhere\Kite\Lib\Stream\MapStream;
use Toolkit\Stdlib\Str;
use function array_merge;
Expand Down Expand Up @@ -206,7 +207,7 @@ public function toCreateSQL(): string
}
// if ($this->isNoDefault($type)) {
// } else {
} elseif (!$meta['nullable'] && self::isStringType($type)) {
} elseif (!$meta['nullable'] && TypeMap::isStringType($type)) {
$nodes[] = "DEFAULT ''";
}

Expand Down Expand Up @@ -341,36 +342,4 @@ public function setTableComment(string $tableComment): void
{
$this->tableComment = $tableComment;
}

/**
* @param string $upperType
*
* @return bool
*/
public static function isStringType(string $upperType): bool
{
if (str_contains($upperType, 'TEXT')) {
return true;
}

if (str_contains($upperType, 'CHAR')) {
return true;
}

return false;
}

/**
* @param string $upperType
*
* @return bool
*/
public static function isNoDefault(string $upperType): bool
{
if ($upperType === 'JSON') {
return true;
}

return false;
}
}
14 changes: 14 additions & 0 deletions app/Lib/Parser/MySQL/TypeMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,20 @@ public static function isIntType(string $dbType): bool
return false;
}

/**
* @param string $type
*
* @return bool
*/
public static function isNoDefault(string $type): bool
{
if ($type === self::JSON) {
return true;
}

return false;
}

/**
* @param string $dbType
*
Expand Down
73 changes: 60 additions & 13 deletions app/Lib/Parser/TextParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Closure;
use Toolkit\Stdlib\Str;
use function array_filter;
use function array_merge;
use function array_slice;
use function count;
use function explode;
Expand All @@ -25,14 +26,19 @@ class TextParser
private string $text;

/**
* the custom settings
* - parsed from text header
*
* @var array
*/
private array $options = [];
private array $settings = [];

/**
* split the settings header and body
*
* @var string
*/
private string $headerSep = "\n###\n";
public string $headerSep = "\n###\n";

/**
* @var array
Expand All @@ -42,19 +48,24 @@ class TextParser
/**
* @var string
*/
private string $lineSep = "\n";
public string $lineSep = "\n";

/**
* field number of each line.
*
* @var int
*/
private int $fieldNum = 3;
public int $fieldNum = 3;

/**
* @var string
*/
private string $fieldSep = ' ';
public string $fieldSep = ' ';

/**
* @var array
*/
public array $fields = [];

/**
* @var callable(string): string
Expand All @@ -76,11 +87,6 @@ class TextParser
*/
private int $valueLtFieldNum = self::PREPEND;

/**
* @var array
*/
private array $fieldNames = [];

public static function new(): self
{
return new self();
Expand Down Expand Up @@ -178,13 +184,22 @@ protected function parseHeader(string $header): void
$this->fieldNum = (int)$value;
break;
case 'fields':
$this->fieldNames = Str::explode($value, ',');
$this->fields = Str::explode($value, ',');
break;
}
}

if ($this->fieldNames) {
$this->fieldNum = count($this->fieldNames);
$allowSettings = ['fieldNum', 'fields'];
$this->settings = IniParser::parseString($header);

foreach ($allowSettings as $prop) {
if (isset($this->settings[$prop])) {
$this->$prop = $this->settings[$prop];
}
}

if ($this->fields) {
$this->fieldNum = count($this->fields);
}
}

Expand Down Expand Up @@ -298,4 +313,36 @@ public function setValueLtFieldNum(int $valueLtFieldNum): TextParser
$this->valueLtFieldNum = $valueLtFieldNum;
return $this;
}

/**
* @param callable $filterFn
*/
public function setFilterFn(callable $filterFn): void
{
$this->filterFn = $filterFn;
}

/**
* @param callable $parserFn
*/
public function setParserFn(callable $parserFn): void
{
$this->parserFn = $parserFn;
}

/**
* @return array
*/
public function getSettings(): array
{
return $this->settings;
}

/**
* @param array $settings
*/
public function setSettings(array $settings): void
{
$this->settings = array_merge($this->settings, $settings);
}
}
4 changes: 2 additions & 2 deletions test/unittest/Common/ClientEnvReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Inhere\KiteTest\Common;

use Inhere\Kite\Common\IdeaHttp\ClientEnvReader;
use Inhere\KiteTest\BaseTestCase;
use Inhere\KiteTest\BaseKiteTestCase;

/**
* Class ClientEnvReaderTest
*
* @package Inhere\KiteTest\Common
*/
class ClientEnvReaderTest extends BaseTestCase
class ClientEnvReaderTest extends BaseKiteTestCase
{
public function testLoad(): void
{
Expand Down
4 changes: 2 additions & 2 deletions test/unittest/Common/ContentParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
namespace Inhere\KiteTest\Common;

use Inhere\Kite\Common\IdeaHttp\RequestSet;
use Inhere\KiteTest\BaseTestCase;
use Inhere\KiteTest\BaseKiteTestCase;

/**
* Class ContentParserTest
*
* @package Inhere\KiteTest\Common
*/
class ContentParserTest extends BaseTestCase
class ContentParserTest extends BaseKiteTestCase
{
public const oneRequest = <<<HTTP
### post example
Expand Down
4 changes: 2 additions & 2 deletions test/unittest/Common/RequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@

use Inhere\Kite\Common\IdeaHttp\Request;
use Inhere\Kite\Common\IdeaHttp\RequestSet;
use Inhere\KiteTest\BaseTestCase;
use Inhere\KiteTest\BaseKiteTestCase;

/**
* Class RequestTest
*
* @package Inhere\KiteTest\Common
*/
class RequestTest extends BaseTestCase
class RequestTest extends BaseKiteTestCase
{
public const oneRequest = <<<HTTP
### post example
Expand Down
13 changes: 6 additions & 7 deletions test/unittest/Helper/AppHelperTest.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
<?php declare(strict_types=1);

namespace unittest\Helper;
namespace Inhere\KiteTest\Helper;

use Inhere\Kite\Common\Clipboard;
use Inhere\Kite\Console\Component\Clipboard;
use PHPUnit\Framework\TestCase;
use function vdump;

/**
* Class AppHelperTest
*
* @package unittest\Helper
* @package Inhere\KiteTest\Helper
*/
class AppHelperTest extends TestCase
{
Expand All @@ -19,11 +18,11 @@ public function testClipboard(): void
self::assertNotEmpty($clip);

$current = __METHOD__;
vdump($current);
$clip->write($current);
// vdump($current);
$clip->write($current, true);

$text = $clip->read();
vdump($text);
// vdump($text);

self::assertNotEmpty($text);
self::assertSame($current, $text);
Expand Down
13 changes: 9 additions & 4 deletions test/unittest/Lib/Parser/DBTableTest.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<?php declare(strict_types=1);

namespace unittest\Lib\Parser;
namespace Inhere\KiteTest\Lib\Parser;

use Inhere\Kite\Lib\Parser\DBMdTable;
use Inhere\Kite\Lib\Parser\DBTable;
use Inhere\KiteTest\BaseTestCase;
use function vdump;
use Inhere\Kite\Lib\Parser\MySQL\TypeMap;
use Inhere\KiteTest\BaseKiteTestCase;

/**
* class DBTableTest
*/
class DBTableTest extends BaseTestCase
class DBTableTest extends BaseKiteTestCase
{
private $mdTable1 = <<<MD
### 用户的订单记录表 `user_order`
Expand Down Expand Up @@ -49,6 +49,11 @@ public function testFromMdTable(): void
$this->assertEquals('user_order', $dbt->getTableName());
$this->assertEquals('用户的订单记录表', $dbt->getTableComment());

$field = $dbt->getField('orderno');
$this->assertNotEmpty($field);
$this->assertEquals(TypeMap::VARCHAR, $field['type']);
$this->assertEquals(48, $field['typeLen']);

$this->assertNotEmpty($genSql = $dbt->toString());
$this->assertEquals($this->createSql1, $genSql);
// vdump($dbt->toString());
Expand Down

0 comments on commit 3f4ee91

Please sign in to comment.