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
|
|
|
public static function getForTable(string $connection, string $table) |
16
|
|
|
{ |
17
|
|
|
$connection = $connection ?: config('database.default'); |
18
|
|
|
|
19
|
|
|
self::generateDatabaseSchema($connection); |
20
|
|
|
|
21
|
|
|
return self::$schema[$connection][$table] ?? null; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public static function getTables(string $connection = null): array |
25
|
|
|
{ |
26
|
|
|
$connection = $connection ?: config('database.default'); |
27
|
|
|
self::generateDatabaseSchema($connection); |
28
|
|
|
|
29
|
|
|
return self::$schema[$connection] ?? []; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function listTableColumnsNames(string $connection, string $table) |
33
|
|
|
{ |
34
|
|
|
$table = self::getForTable($connection, $table); |
35
|
|
|
|
36
|
|
|
return array_keys($table->getColumns()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function listTableIndexes(string $connection, string $table) |
40
|
|
|
{ |
41
|
|
|
return self::getIndexColumnNames($connection, $table); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function getManager(string $connection = null) |
45
|
|
|
{ |
46
|
|
|
$connection = $connection ?: config('database.default'); |
47
|
|
|
|
48
|
|
|
return self::getSchemaManager($connection); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Generates and store the database schema. |
53
|
|
|
*/ |
54
|
|
|
private static function generateDatabaseSchema(string $connection) |
55
|
|
|
{ |
56
|
|
|
if (! isset(self::$schema[$connection])) { |
57
|
|
|
self::$schema[$connection] = self::mapTables($connection); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Map the tables from raw db values into an usable array. |
63
|
|
|
* |
64
|
|
|
* @param string $connection |
65
|
|
|
* @return array |
66
|
|
|
*/ |
67
|
|
|
private static function mapTables(string $connection) |
68
|
|
|
{ |
69
|
|
|
return LazyCollection::make(self::getCreateSchema($connection)->getTables())->mapWithKeys(function ($table, $key) use ($connection) { |
|
|
|
|
70
|
|
|
$tableName = is_array($table) ? $table['name'] : $table->getName(); |
71
|
|
|
|
72
|
|
|
if (self::$schema[$connection][$tableName] ?? false) { |
73
|
|
|
return [$tableName => self::$schema[$connection][$tableName]]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (is_array($table)) { |
77
|
|
|
$table = new Table($tableName, self::mapTableColumns($connection, $tableName)); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return [$tableName => $table]; |
81
|
|
|
})->toArray(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private static function getIndexColumnNames(string $connection, string $table) |
85
|
|
|
{ |
86
|
|
|
$schemaManager = self::getSchemaManager($connection); |
87
|
|
|
$indexes = method_exists($schemaManager, 'listTableIndexes') ? $schemaManager->listTableIndexes($table) : $schemaManager->getIndexes($table); |
|
|
|
|
88
|
|
|
|
89
|
|
|
$indexes = array_map(function ($index) { |
90
|
|
|
return is_array($index) ? $index['columns'] : $index->getColumns(); |
91
|
|
|
}, $indexes); |
92
|
|
|
|
93
|
|
|
$indexes = \Illuminate\Support\Arr::flatten($indexes); |
94
|
|
|
|
95
|
|
|
return array_unique($indexes); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private static function mapTableColumns(string $connection, string $table) |
99
|
|
|
{ |
100
|
|
|
$indexedColumns = self::getIndexColumnNames($connection, $table); |
101
|
|
|
|
102
|
|
|
return LazyCollection::make(self::getSchemaManager($connection)->getColumns($table))->mapWithKeys(function ($column, $key) use ($indexedColumns) { |
|
|
|
|
103
|
|
|
$column['index'] = array_key_exists($column['name'], $indexedColumns) ? true : false; |
104
|
|
|
|
105
|
|
|
return [$column['name'] => $column]; |
106
|
|
|
})->toArray(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
private static function getCreateSchema(string $connection) |
110
|
|
|
{ |
111
|
|
|
$schemaManager = self::getSchemaManager($connection); |
112
|
|
|
|
113
|
|
|
return method_exists($schemaManager, 'createSchema') ? $schemaManager->createSchema() : $schemaManager; |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private static function dbalTypes() |
117
|
|
|
{ |
118
|
|
|
return [ |
119
|
|
|
'enum' => \Doctrine\DBAL\Types\Types::STRING, |
120
|
|
|
'jsonb' => \Doctrine\DBAL\Types\Types::JSON, |
121
|
|
|
'geometry' => \Doctrine\DBAL\Types\Types::STRING, |
122
|
|
|
'point' => \Doctrine\DBAL\Types\Types::STRING, |
123
|
|
|
'lineString' => \Doctrine\DBAL\Types\Types::STRING, |
124
|
|
|
'polygon' => \Doctrine\DBAL\Types\Types::STRING, |
125
|
|
|
'multiPoint' => \Doctrine\DBAL\Types\Types::STRING, |
126
|
|
|
'multiLineString' => \Doctrine\DBAL\Types\Types::STRING, |
127
|
|
|
'multiPolygon' => \Doctrine\DBAL\Types\Types::STRING, |
128
|
|
|
'geometryCollection' => \Doctrine\DBAL\Types\Types::STRING, |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private static function getSchemaManager(string $connection) |
133
|
|
|
{ |
134
|
|
|
$connection = DB::connection($connection); |
135
|
|
|
|
136
|
|
|
if (method_exists($connection, 'getDoctrineSchemaManager')) { |
137
|
|
|
foreach (self::dbalTypes() as $key => $value) { |
138
|
|
|
$connection->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping($key, $value); |
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $connection->getDoctrineSchemaManager(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $connection->getSchemaBuilder(); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.