1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Database; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\DB; |
6
|
|
|
use Illuminate\Support\LazyCollection; |
7
|
|
|
|
8
|
|
|
final class DatabaseSchema |
9
|
|
|
{ |
10
|
|
|
private static $schema; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Return the schema for the table. |
14
|
|
|
* |
15
|
|
|
* @param string $connection |
16
|
|
|
* @param string $table |
17
|
|
|
*/ |
18
|
|
|
public static function getForTable(string $connection, string $table) |
19
|
|
|
{ |
20
|
|
|
self::generateDatabaseSchema($connection, $table); |
21
|
|
|
|
22
|
|
|
return self::$schema[$connection][$table] ?? null; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function listTableColumnsNames(string $connection, string $table) |
26
|
|
|
{ |
27
|
|
|
$table = self::getForTable($connection, $table); |
28
|
|
|
|
29
|
|
|
return array_keys($table->getColumns()); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function listTableIndexes(string $connection, string $table) |
33
|
|
|
{ |
34
|
|
|
return self::getIndexColumnNames($connection, $table); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Generates and store the database schema. |
39
|
|
|
* |
40
|
|
|
* @param string $connection |
41
|
|
|
* @param string $table |
42
|
|
|
* @return void |
43
|
|
|
*/ |
44
|
|
|
private static function generateDatabaseSchema(string $connection, string $table) |
45
|
|
|
{ |
46
|
|
|
if (! isset(self::$schema[$connection]) || ! isset(self::$schema[$connection][$table])) { |
47
|
|
|
self::$schema[$connection] = self::mapTables($connection); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Map the tables from raw db values into an usable array. |
53
|
|
|
* |
54
|
|
|
* @param string $connection |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
private static function mapTables(string $connection) |
58
|
|
|
{ |
59
|
|
|
return LazyCollection::make(self::getCreateSchema($connection)->getTables())->mapWithKeys(function ($table, $key) use ($connection) { |
|
|
|
|
60
|
|
|
$tableName = is_array($table) ? $table['name'] : $table->getName(); |
61
|
|
|
|
62
|
|
|
if (self::$schema[$connection][$tableName] ?? false) { |
63
|
|
|
return [$tableName => self::$schema[$connection][$tableName]]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (is_array($table)) { |
67
|
|
|
$table = new Table(self::mapTableColumns($connection, $tableName)); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return [$tableName => $table]; |
71
|
|
|
})->toArray(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private static function getIndexColumnNames(string $connection, string $table) |
75
|
|
|
{ |
76
|
|
|
$schemaManager = self::getSchemaManager($connection); |
77
|
|
|
$indexes = method_exists($schemaManager, 'listTableIndexes') ? $schemaManager->listTableIndexes($table) : $schemaManager->getIndexes($table); |
|
|
|
|
78
|
|
|
|
79
|
|
|
$indexes = array_map(function ($index) { |
80
|
|
|
return is_array($index) ? $index['columns'] : $index->getColumns(); |
81
|
|
|
}, $indexes); |
82
|
|
|
|
83
|
|
|
$indexes = \Illuminate\Support\Arr::flatten($indexes); |
84
|
|
|
|
85
|
|
|
return array_unique($indexes); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
private static function mapTableColumns(string $connection, string $table) |
89
|
|
|
{ |
90
|
|
|
$indexedColumns = self::getIndexColumnNames($connection, $table); |
91
|
|
|
|
92
|
|
|
return LazyCollection::make(self::getSchemaManager($connection)->getColumns($table))->mapWithKeys(function ($column, $key) use ($indexedColumns) { |
|
|
|
|
93
|
|
|
$column['index'] = array_key_exists($column['name'], $indexedColumns) ? true : false; |
94
|
|
|
|
95
|
|
|
return [$column['name'] => $column]; |
96
|
|
|
})->toArray(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
private static function getCreateSchema(string $connection) |
100
|
|
|
{ |
101
|
|
|
$schemaManager = self::getSchemaManager($connection); |
102
|
|
|
|
103
|
|
|
return method_exists($schemaManager, 'createSchema') ? $schemaManager->createSchema() : $schemaManager; |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private static function getSchemaManager(string $connection) |
107
|
|
|
{ |
108
|
|
|
$connection = DB::connection($connection); |
109
|
|
|
|
110
|
|
|
return method_exists($connection, 'getDoctrineSchemaManager') ? $connection->getDoctrineSchemaManager() : $connection->getSchemaBuilder(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|