|
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) || empty($table)) { |
|
84
|
|
|
return $table; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
$schemaManager = self::getSchemaManager($connection); |
|
88
|
|
|
$indexes = $schemaManager->getIndexes($tableName); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
$indexes = array_map(function ($index) { |
|
91
|
|
|
return $index['columns']; |
|
92
|
|
|
}, $indexes); |
|
93
|
|
|
|
|
94
|
|
|
$table = new Table($tableName, $table); |
|
95
|
|
|
|
|
96
|
|
|
$indexes = Arr::flatten($indexes); |
|
97
|
|
|
$table->setIndexes(array_unique($indexes)); |
|
98
|
|
|
|
|
99
|
|
|
return $table; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private static function getIndexColumnNames(string $connection, string $table) |
|
103
|
|
|
{ |
|
104
|
|
|
self::generateDatabaseSchema($connection, $table); |
|
105
|
|
|
|
|
106
|
|
|
$indexes = self::$schema[$connection][$table]->getIndexes(); |
|
107
|
|
|
|
|
108
|
|
|
$indexes = \Illuminate\Support\Arr::flatten(array_map(function ($index) { |
|
109
|
|
|
return is_string($index) ? $index : $index->getColumns(); |
|
110
|
|
|
}, $indexes)); |
|
111
|
|
|
|
|
112
|
|
|
return array_unique($indexes); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
private static function getCreateSchema(string $connection) |
|
116
|
|
|
{ |
|
117
|
|
|
$schemaManager = self::getSchemaManager($connection); |
|
118
|
|
|
|
|
119
|
|
|
return method_exists($schemaManager, 'createSchema') ? $schemaManager->createSchema() : $schemaManager; |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
private static function dbalTypes() |
|
123
|
|
|
{ |
|
124
|
|
|
return [ |
|
125
|
|
|
'enum' => \Doctrine\DBAL\Types\Types::STRING, |
|
126
|
|
|
'jsonb' => \Doctrine\DBAL\Types\Types::JSON, |
|
127
|
|
|
'geometry' => \Doctrine\DBAL\Types\Types::STRING, |
|
128
|
|
|
'point' => \Doctrine\DBAL\Types\Types::STRING, |
|
129
|
|
|
'lineString' => \Doctrine\DBAL\Types\Types::STRING, |
|
130
|
|
|
'polygon' => \Doctrine\DBAL\Types\Types::STRING, |
|
131
|
|
|
'multiPoint' => \Doctrine\DBAL\Types\Types::STRING, |
|
132
|
|
|
'multiLineString' => \Doctrine\DBAL\Types\Types::STRING, |
|
133
|
|
|
'multiPolygon' => \Doctrine\DBAL\Types\Types::STRING, |
|
134
|
|
|
'geometryCollection' => \Doctrine\DBAL\Types\Types::STRING, |
|
135
|
|
|
]; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
private static function getSchemaManager(string $connection) |
|
139
|
|
|
{ |
|
140
|
|
|
$connection = DB::connection($connection); |
|
141
|
|
|
|
|
142
|
|
|
if (method_exists($connection, 'getDoctrineSchemaManager')) { |
|
143
|
|
|
foreach (self::dbalTypes() as $key => $value) { |
|
144
|
|
|
$connection->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping($key, $value); |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
return $connection->getDoctrineSchemaManager(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $connection->getSchemaBuilder(); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.