1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Library\Database; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
use Illuminate\Support\Facades\DB; |
7
|
|
|
use Illuminate\Support\LazyCollection; |
8
|
|
|
|
9
|
|
|
final class DatabaseSchema |
10
|
|
|
{ |
11
|
|
|
private static $schema; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Return the schema for the table. |
15
|
|
|
*/ |
16
|
|
|
public static function getForTable(string $table, string $connection) |
17
|
|
|
{ |
18
|
|
|
$connection = $connection ?: config('database.default'); |
19
|
|
|
|
20
|
|
|
self::generateDatabaseSchema($connection, $table); |
21
|
|
|
|
22
|
|
|
return self::$schema[$connection][$table] ?? null; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function getTables(string $connection = null): array |
26
|
|
|
{ |
27
|
|
|
$connection = $connection ?: config('database.default'); |
28
|
|
|
|
29
|
|
|
self::$schema[$connection] = LazyCollection::make(self::getCreateSchema($connection)->getTables())->mapWithKeys(function ($table, $key) use ($connection) { |
|
|
|
|
30
|
|
|
$tableName = is_array($table) ? $table['name'] : $table->getName(); |
31
|
|
|
|
32
|
|
|
if ($existingTable = self::$schema[$connection][$tableName] ?? false) { |
33
|
|
|
return [$tableName => $existingTable]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
$table = self::mapTable($connection, $tableName); |
37
|
|
|
|
38
|
|
|
return [$tableName => $table]; |
39
|
|
|
})->toArray(); |
40
|
|
|
|
41
|
|
|
return self::$schema[$connection]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function listTableColumnsNames(string $connection, string $table) |
45
|
|
|
{ |
46
|
|
|
$table = self::getForTable($table, $connection); |
47
|
|
|
|
48
|
|
|
return $table ? array_keys($table->getColumns()) : []; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function listTableIndexes(string $connection, string $table) |
52
|
|
|
{ |
53
|
|
|
return self::getIndexColumnNames($connection, $table); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getManager(string $connection = null) |
57
|
|
|
{ |
58
|
|
|
$connection = $connection ?: config('database.default'); |
59
|
|
|
|
60
|
|
|
return self::getSchemaManager($connection); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Generates and store the database schema. |
65
|
|
|
*/ |
66
|
|
|
private static function generateDatabaseSchema(string $connection, string $table) |
67
|
|
|
{ |
68
|
|
|
if (! isset(self::$schema[$connection][$table])) { |
69
|
|
|
self::$schema[$connection][$table] = self::mapTable($connection, $table); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private static function mapTable(string $connection, string $tableName) |
74
|
|
|
{ |
75
|
|
|
try { |
76
|
|
|
$table = method_exists(self::getCreateSchema($connection), 'getTable') ? |
77
|
|
|
self::getCreateSchema($connection)->getTable($tableName) : |
|
|
|
|
78
|
|
|
self::getCreateSchema($connection)->getColumns($tableName); |
|
|
|
|
79
|
|
|
} catch (\Exception $e) { |
80
|
|
|
return new Table($tableName, []); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if (! is_array($table)) { |
84
|
|
|
return $table; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (empty($table)) { |
88
|
|
|
return new Table($tableName, []); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$schemaManager = self::getSchemaManager($connection); |
92
|
|
|
$indexes = $schemaManager->getIndexes($tableName); |
|
|
|
|
93
|
|
|
|
94
|
|
|
$indexes = array_map(function ($index) { |
95
|
|
|
return $index['columns']; |
96
|
|
|
}, $indexes); |
97
|
|
|
|
98
|
|
|
$table = new Table($tableName, $table); |
99
|
|
|
|
100
|
|
|
$indexes = Arr::flatten($indexes); |
101
|
|
|
$table->setIndexes(array_unique($indexes)); |
102
|
|
|
|
103
|
|
|
return $table; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
private static function getIndexColumnNames(string $connection, string $table) |
107
|
|
|
{ |
108
|
|
|
self::generateDatabaseSchema($connection, $table); |
109
|
|
|
|
110
|
|
|
$indexes = self::$schema[$connection][$table]->getIndexes(); |
111
|
|
|
|
112
|
|
|
$indexes = \Illuminate\Support\Arr::flatten(array_map(function ($index) { |
113
|
|
|
return is_string($index) ? $index : $index->getColumns(); |
114
|
|
|
}, $indexes)); |
115
|
|
|
|
116
|
|
|
return array_unique($indexes); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
private static function getCreateSchema(string $connection) |
120
|
|
|
{ |
121
|
|
|
$schemaManager = self::getSchemaManager($connection); |
122
|
|
|
|
123
|
|
|
return method_exists($schemaManager, 'createSchema') ? $schemaManager->createSchema() : $schemaManager; |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private static function dbalTypes() |
127
|
|
|
{ |
128
|
|
|
return [ |
129
|
|
|
'enum' => \Doctrine\DBAL\Types\Types::STRING, |
130
|
|
|
'jsonb' => \Doctrine\DBAL\Types\Types::JSON, |
131
|
|
|
'geometry' => \Doctrine\DBAL\Types\Types::STRING, |
132
|
|
|
'point' => \Doctrine\DBAL\Types\Types::STRING, |
133
|
|
|
'lineString' => \Doctrine\DBAL\Types\Types::STRING, |
134
|
|
|
'polygon' => \Doctrine\DBAL\Types\Types::STRING, |
135
|
|
|
'multiPoint' => \Doctrine\DBAL\Types\Types::STRING, |
136
|
|
|
'multiLineString' => \Doctrine\DBAL\Types\Types::STRING, |
137
|
|
|
'multiPolygon' => \Doctrine\DBAL\Types\Types::STRING, |
138
|
|
|
'geometryCollection' => \Doctrine\DBAL\Types\Types::STRING, |
139
|
|
|
]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private static function getSchemaManager(string $connection) |
143
|
|
|
{ |
144
|
|
|
$connection = DB::connection($connection); |
145
|
|
|
|
146
|
|
|
if (method_exists($connection, 'getDoctrineSchemaManager')) { |
147
|
|
|
foreach (self::dbalTypes() as $key => $value) { |
148
|
|
|
$connection->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping($key, $value); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $connection->getDoctrineSchemaManager(); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $connection->getSchemaBuilder(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.