Skip to content

Commit

Permalink
fix: 修复数据库表前缀导致的获取字段失败
Browse files Browse the repository at this point in the history
  • Loading branch information
slowlyo committed Dec 3, 2024
1 parent 01a0076 commit 388d25a
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/Support/CodeGenerator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,27 +92,33 @@ public function getDatabaseColumns($db = null, $tb = null)
foreach ($databases as $connectName => $value) {
if ($db && $db != $value['database']) continue;

try{
$datebaseSchemaBuilder = Schema::connection($connectName);
$tables = array_column($datebaseSchemaBuilder->getTables(), 'name');
}catch(\Throwable $e){ // 连不上的跳过
try {
$databaseSchemaBuilder = Schema::connection($connectName);

$tables = collect($databaseSchemaBuilder->getTables())
->pluck('name')
->map(fn($name) => Str::replaceStart(data_get($value, 'prefix', ''), '', $name))
->toArray();
} catch (\Throwable $e) { // 连不上的跳过
continue;
}

// 键(database名称)长度超过28个字符 amis 会获取字段信息失败(sqlite),截取一下
$databaseKey = strlen($value['database']) > 28 ? substr_replace($value['database'],'***', 10, -15) : $value['database'];

$data[$databaseKey] = collect($tables)->flip()->map(function ($_, $table) use ($datebaseSchemaBuilder) {
$columns = collect($datebaseSchemaBuilder->getColumns($table))
->whereNotIn('name', ['created_at', 'updated_at', 'deleted_at'])
->map(function ($v) {
$v['type'] = Arr::get(Generator::$dataTypeMap, $v['type'], 'string');
$v['nullable'] = $v['nullable'] == 'YES';
return $v;
});

return $columns;
});
$databaseKey = strlen($value['database']) > 28 ? substr_replace($value['database'], '***', 10, -15) : $value['database'];

$data[$databaseKey] = collect($tables)
->flip()
->map(function ($_, $table) use ($databaseSchemaBuilder, $connectName) {
return collect($databaseSchemaBuilder->getColumns($table))
->whereNotIn('name', ['id', 'created_at', 'updated_at', 'deleted_at'])
->values()
->map(function ($v) {
$v['type'] = Arr::get(Generator::$dataTypeMap, $v['type'], 'string');
$v['nullable'] = $v['nullable'] == 'YES';
$v['comment'] = filled($v['comment']) ? $v['comment'] : Str::studly($v['name']);
return $v;
});
});
}
} catch (\Throwable $e) {
}
Expand Down

0 comments on commit 388d25a

Please sign in to comment.