Skip to content

Commit

Permalink
Merge pull request #167 from wowthree/databaseColumn
Browse files Browse the repository at this point in the history
fix: 修改getDatabaseColumns方法,兼容sqlite数据库
  • Loading branch information
slowlyo authored Nov 28, 2024
2 parents f9e2027 + 5e6c158 commit 01a0076
Showing 1 changed file with 19 additions and 45 deletions.
64 changes: 19 additions & 45 deletions src/Support/CodeGenerator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Generator
'text' => 'text',
'mediumtext' => 'mediumText',
'longtext' => 'longText',
'integer' => 'integer',
];

public function needCreateOptions()
Expand Down Expand Up @@ -80,7 +81,7 @@ public function availableFieldTypes(): array
public function getDatabaseColumns($db = null, $tb = null)
{
$databases = Arr::where(config('database.connections', []), function ($value) {
$supports = ['mysql'];
$supports = ['mysql', 'sqlite'];

return in_array(strtolower(Arr::get($value, 'driver')), $supports);
});
Expand All @@ -91,53 +92,26 @@ public function getDatabaseColumns($db = null, $tb = null)
foreach ($databases as $connectName => $value) {
if ($db && $db != $value['database']) continue;

$sql = sprintf('SELECT * FROM information_schema.columns WHERE table_schema = "%s"',
$value['database']);

if ($tb) {
$p = Arr::get($value, 'prefix');

$sql .= " AND TABLE_NAME = '{$p}{$tb}'";
try{
$datebaseSchemaBuilder = Schema::connection($connectName);
$tables = array_column($datebaseSchemaBuilder->getTables(), 'name');
}catch(\Throwable $e){ // 连不上的跳过
continue;
}

$sql .= ' ORDER BY `ORDINAL_POSITION` ASC';

$tmp = DB::connection($connectName)->select($sql);

$collection = collect($tmp)->map(function ($v) use ($value) {
if (!$p = Arr::get($value, 'prefix')) {
return (array)$v;
}
$v = (array)$v;

$v['TABLE_NAME'] = Str::replaceFirst($p, '', $v['TABLE_NAME']);

return $v;
});

$data[$value['database']] = $collection->groupBy('TABLE_NAME')->map(function ($v) {
return collect($v)
->keyBy('COLUMN_NAME')
->where('COLUMN_KEY', '<>', 'PRI')
->whereNotIn('COLUMN_NAME', ['created_at', 'updated_at', 'deleted_at'])
// 键(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['COLUMN_TYPE'] = strtolower($v['COLUMN_TYPE']);
$v['DATA_TYPE'] = strtolower($v['DATA_TYPE']);

if (Str::contains($v['COLUMN_TYPE'], 'unsigned')) {
$v['DATA_TYPE'] .= '@unsigned';
}


return [
'name' => $v['COLUMN_NAME'],
'type' => Arr::get(Generator::$dataTypeMap, $v['DATA_TYPE'], 'string'),
'default' => $v['COLUMN_DEFAULT'],
'nullable' => $v['IS_NULLABLE'] == 'YES',
'comment' => $v['COLUMN_COMMENT'],
];
})
->values();
$v['type'] = Arr::get(Generator::$dataTypeMap, $v['type'], 'string');
$v['nullable'] = $v['nullable'] == 'YES';
return $v;
});

return $columns;
});
}
} catch (\Throwable $e) {
Expand Down

0 comments on commit 01a0076

Please sign in to comment.