Skip to content

Commit

Permalink
fix:laravel8 9 Schema::getTables() 不存在
Browse files Browse the repository at this point in the history
  • Loading branch information
wenjy committed Feb 1, 2024
1 parent c20aa94 commit 442d3ef
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,7 @@ class TestComment extends Model
}

```

### 注意

laravel 8、laravel 9 只支持MySql(其它的数据库没有把SQL复制过来),laravel 10原生支持`Schema::getColumns` `Schema::getTables()`
43 changes: 41 additions & 2 deletions src/Generators/Model/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,29 @@ protected function generateProperties(array $columns)
protected function getTableSchemas(): array
{
if (empty($this->tableSchemas)) {
$this->tableSchemas = Arr::pluck(Schema::getTables(), null, 'name');
$this->tableSchemas = Arr::pluck($this->getTables(), null, 'name');
}

return $this->tableSchemas;
}

protected function getTables(): array
{
if (version_compare('10.0.0', app()->version()) === 1) {
$sql = sprintf(
'select table_name as `name`, (data_length + index_length) as `size`, '
.'table_comment as `comment`, engine as `engine`, table_collation as `collation` '
."from information_schema.tables where table_schema = '%s' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') "
.'order by table_name', $this->getDbConnection()->getDatabaseName());
$tables = DB::select($sql);
foreach ($tables as &$table) {
$table = (array)$table;
}
return $tables;
}
return Schema::getTables();
}

protected function getTableSchema(string $table): array
{
return $this->getTableSchemas()[$table] ?? [];
Expand All @@ -136,10 +153,32 @@ protected function getTableColumn(string $table): array
if (!empty($this->tableColumns[$table])) {
return $this->tableColumns[$table];
}
$this->tableColumns[$table] = Arr::pluck(Schema::getColumns($table), null, 'name');
$this->tableColumns[$table] = Arr::pluck($this->getColumns($table), null, 'name');
return $this->tableColumns[$table];
}

protected function getColumns(string $table): array
{
if (version_compare('10.0.0', app()->version()) === 1) {
$sql = sprintf(
'select column_name as `name`, data_type as `type_name`, column_type as `type`, '
.'collation_name as `collation`, is_nullable as `nullable`, '
.'column_default as `default`, column_comment as `comment`, extra as `extra` '
."from information_schema.columns where table_schema = '%s' and table_name = '%s' "
.'order by ordinal_position asc', DB::connection()->getDatabaseName(), $table);

$columns = DB::select($sql);
foreach ($columns as &$column) {
$column = (array)$column;
$column['nullable'] = $column['nullable'] == 'YES';
$column['auto_increment'] = $column['extra'] == 'auto_increment';
unset($column['extra']);
}
return $columns;
}
return Schema::getColumns($table);
}

/**
* @return array the table names that match the pattern specified by [[tableName]].
*/
Expand Down
36 changes: 36 additions & 0 deletions tests/ModelGenerateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use WenGen\Commands\ModelGen;

Expand All @@ -23,4 +24,39 @@ public function testGetAllTable()
var_dump(Arr::pluck(Schema::getColumns('test_comments'), null, 'name'));
$this->assertTrue(true);
}

public function testGetTables()
{
$sql = sprintf(
'select table_name as `name`, (data_length + index_length) as `size`, '
.'table_comment as `comment`, engine as `engine`, table_collation as `collation` '
."from information_schema.tables where table_schema = '%s' and table_type in ('BASE TABLE', 'SYSTEM VERSIONED') "
.'order by table_name', DB::connection()->getDatabaseName());
$tables = DB::select($sql);
foreach ($tables as &$table) {
$table = (array)$table;
}
var_dump($tables);
$this->assertTrue(true);
}

public function testGetColumns()
{
$sql = sprintf(
'select column_name as `name`, data_type as `type_name`, column_type as `type`, '
.'collation_name as `collation`, is_nullable as `nullable`, '
.'column_default as `default`, column_comment as `comment`, extra as `extra` '
."from information_schema.columns where table_schema = '%s' and table_name = '%s' "
.'order by ordinal_position asc', DB::connection()->getDatabaseName(), 'test_comments');

$columns = DB::select($sql);
foreach ($columns as &$column) {
$column = (array)$column;
$column['nullable'] = $column['nullable'] == 'YES';
$column['auto_increment'] = $column['extra'] == 'auto_increment';
unset($column['extra']);
}
var_dump($columns);
$this->assertTrue(true);
}
}

0 comments on commit 442d3ef

Please sign in to comment.