Skip to content

Commit

Permalink
up: update some parser class
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Oct 18, 2023
1 parent d43170d commit c7bbbd6
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
34 changes: 30 additions & 4 deletions app/Lib/Parser/DBSchemeSQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,34 @@ public function parse(string $createSQL): DBTable
throw new InvalidArgumentException('invalid create table SQL for parse');
}

// match end \s*\)\s*(\w+\s*=.*);
$createSQL .= ";";

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

$tableComment = '';
$tableEngine = array_pop($tableRows);
if (($pos = stripos($tableEngine, ' comment')) !== false) {
$tableComment = trim(substr($tableEngine, $pos + 9), '=;\'"');
$tableInfo = $this->parseTableMeta($tableEngine);
$tableComment = $tableInfo['comment'];
}

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

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

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

Expand Down Expand Up @@ -107,7 +113,14 @@ public function parseLine(string $row): array
}

if (($pos = stripos($other, 'comment ')) !== false) {
$comment = trim(substr($other, $pos + 9), '\'"');
$endPos = stripos($other, 'COLLATE ');
// vdump($other, $endPos);
if ($endPos > 0) {
$comment = trim(substr($other, $pos + 9, $endPos - $pos - 11), '\'"');
} else {
$comment = trim(substr($other, $pos + 9), '\'"');
}

$other = substr($other, 0, $pos);
} else {
$comment = ucfirst(str_replace('_', ' ', $field));
Expand Down Expand Up @@ -172,4 +185,17 @@ protected function isIndexLine(string $row): bool
return false;
}

protected function parseTableMeta(string $str): array
{
$info = [];
$kvNodes = explode(' ', trim($str, " );\n"));

foreach ($kvNodes as $value) {
[$k, $v] = explode('=', $value, 2);

$info[strtolower($k)] = trim($v, ' "\'');
}
return $info;
}

}
2 changes: 1 addition & 1 deletion app/Lib/Parser/Item/FieldItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function javaType(): string
*/
public function toJavaType(string $type, string $name): string
{
if (Str::hasSuffixIC($this->name, 'id')) {
if ($type === Type::INTEGER && Str::hasSuffixIC($this->name, 'id')) {
return JavaType::LONG;
}

Expand Down

0 comments on commit c7bbbd6

Please sign in to comment.