Skip to content

Commit

Permalink
fix: fix parse error on sepcial create SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 5, 2023
1 parent b80194c commit f5d1b2c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 25 deletions.
23 changes: 14 additions & 9 deletions app/Lib/Parser/DBSchemeSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,30 +52,30 @@ public function parse(string $createSQL): DBTable
$createSQL .= ";";

$tableRows = explode("\n", $createSQL);
$tableName = trim(array_shift($tableRows), '( ');
$tableName = trim(array_shift($tableRows), " \t\n\r\0\x0B(");
$tableName = trim(substr($tableName, 12), " \t\n\r\0\x0B`");

$tableComment = '';
$tableEngine = array_pop($tableRows);
if (($pos = stripos($tableEngine, ' comment')) !== false) {
if (stripos($tableEngine, ' comment') !== false) {
$tableInfo = $this->parseTableMeta($tableEngine);
$tableComment = $tableInfo['comment'];
$dbt->setTableComment($tableInfo['comment']);
} else {
$tableRows[] = $tableEngine;
}

$dbt->setTableName($tableName);
$dbt->setTableComment($tableComment);

$indexes = [];
$endstr = '';
$endStr = '';
foreach ($tableRows as $row) {
$row = trim($row, ", \t\n\r\0\x0B");
$row = trim($row, ";, \t\n\r\0\x0B");
if (!$row) {
continue;
}

// eg: "(", ") ENGINE=some"
// eg: "(", ") ENGINE=some" table info 在多行
if ($row[0] === '(' || $row[0] === ')' || !str_contains($row, ' ')) {
$endstr .= $row;
$endStr .= ' ' . $row;
continue;
}

Expand All @@ -90,6 +90,11 @@ public function parse(string $createSQL): DBTable

$dbt->setIndexes($indexes);

if ($endStr && !$dbt->tableComment) {
$tableInfo = $this->parseTableMeta($endStr);
$dbt->setTableComment($tableInfo['comment']);
}

return $dbt;
}

Expand Down
3 changes: 1 addition & 2 deletions app/Lib/Parser/Item/FieldItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function javaType(): string
}

/**
* @param string $type
* @param string $type PHP type
* @param string $name
*
* @return string
Expand All @@ -121,7 +121,6 @@ public function toJavaType(string $type, string $name): string
if ($type === Type::OBJECT) {
return Str::upFirst($name);
}

return Str::upFirst($type);
}

Expand Down
10 changes: 7 additions & 3 deletions app/Lib/Parser/MySQL/DBType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Inhere\Kite\Lib\Parser\MySQL;

use Inhere\Kite\Lib\Generate\Java\JavaType;
use Toolkit\Stdlib\Str;
use Toolkit\Stdlib\Type;
use function strtolower;

Expand All @@ -12,6 +14,8 @@ class DBType
{
public const INT = 'int';

public const BIGINT = 'bigint';

public const CHAR = 'char';

public const VARCHAR = 'varchar';
Expand All @@ -28,11 +32,11 @@ class DBType
public static function isStringType(string $dbType): bool
{
if (str_contains($dbType, 'text')) {
return true;
return true;
}

if (str_contains($dbType, 'char')) {
return true;
return true;
}

return false;
Expand All @@ -46,7 +50,7 @@ public static function isStringType(string $dbType): bool
public static function isIntType(string $dbType): bool
{
if (str_contains($dbType, 'int')) {
return true;
return true;
}

return false;
Expand Down
14 changes: 3 additions & 11 deletions app/Lib/Parser/MySQL/TableField.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,19 @@ public function phpType(): string
}

/**
* @param string $type
* @param string $type DB type
* @param string $name
*
* @return string
*/
public function toJavaType(string $type, string $name): string
{
if (Str::hasSuffixIC($this->name, 'id')) {
return JavaType::LONG;
}

if (Str::hasSuffixIC($this->name, 'ids')) {
return sprintf('%s<%s>', JavaType::LIST, JavaType::LONG);
}

if ($type === DBType::JSON) {
// return JavaType::OBJECT;
return Str::upFirst($name);
}

return Str::upFirst(DBType::toPhpType($type));
$phpType = DBType::toPhpType($type);
return parent::toJavaType($phpType, $name);
}

/**
Expand Down

0 comments on commit f5d1b2c

Please sign in to comment.