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
|
|
|
public function registerDbalTypes(array $types, string $connection = null) |
52
|
|
|
{ |
53
|
|
|
$connection = $connection ?: config('database.default'); |
54
|
|
|
$manager = self::getSchemaManager($connection); |
55
|
|
|
|
56
|
|
|
if(!method_exists($manager, 'getDatabasePlatform')) { |
57
|
|
|
return; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$platform = $manager->getDatabasePlatform(); |
|
|
|
|
61
|
|
|
|
62
|
|
|
foreach ($types as $key => $value) { |
63
|
|
|
$platform->registerDoctrineTypeMapping($key, $value); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public static function dbalTypes() |
68
|
|
|
{ |
69
|
|
|
if(!method_exists(self::getSchemaManager(), 'getDatabasePlatform')) { |
|
|
|
|
70
|
|
|
return []; |
71
|
|
|
} |
72
|
|
|
return [ |
73
|
|
|
'enum' => \Doctrine\DBAL\Types\Types::STRING, |
74
|
|
|
'geometry' => \Doctrine\DBAL\Types\Types::STRING, |
75
|
|
|
'point' => \Doctrine\DBAL\Types\Types::STRING, |
76
|
|
|
'lineString' => \Doctrine\DBAL\Types\Types::STRING, |
77
|
|
|
'polygon' => \Doctrine\DBAL\Types\Types::STRING, |
78
|
|
|
'multiPoint' => \Doctrine\DBAL\Types\Types::STRING, |
79
|
|
|
'multiLineString' => \Doctrine\DBAL\Types\Types::STRING, |
80
|
|
|
'multiPolygon' => \Doctrine\DBAL\Types\Types::STRING, |
81
|
|
|
'geometryCollection' => \Doctrine\DBAL\Types\Types::STRING, |
82
|
|
|
|
83
|
|
|
\Doctrine\DBAL\Types\Types::BIGINT => 'bigInteger', |
84
|
|
|
\Doctrine\DBAL\Types\Types::SMALLINT => 'smallInteger', |
85
|
|
|
\Doctrine\DBAL\Types\Types::BLOB => 'binary', |
86
|
|
|
\Doctrine\DBAL\Types\Types::DATE_IMMUTABLE => 'date', |
87
|
|
|
\Doctrine\DBAL\Types\Types::DATETIME_IMMUTABLE => 'dateTime', |
88
|
|
|
\Doctrine\DBAL\Types\Types::DATETIMETZ_IMMUTABLE => 'dateTimeTz', |
89
|
|
|
\Doctrine\DBAL\Types\Types::TIME_IMMUTABLE => 'time', |
90
|
|
|
\Doctrine\DBAL\Types\Types::SIMPLE_ARRAY => 'array' |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Generates and store the database schema. |
96
|
|
|
*/ |
97
|
|
|
private static function generateDatabaseSchema(string $connection) |
98
|
|
|
{ |
99
|
|
|
if (! isset(self::$schema[$connection])) { |
100
|
|
|
self::$schema[$connection] = self::mapTables($connection); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Map the tables from raw db values into an usable array. |
106
|
|
|
* |
107
|
|
|
* @param string $connection |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
private static function mapTables(string $connection) |
111
|
|
|
{ |
112
|
|
|
return LazyCollection::make(self::getCreateSchema($connection)->getTables())->mapWithKeys(function ($table, $key) use ($connection) { |
|
|
|
|
113
|
|
|
$tableName = is_array($table) ? $table['name'] : $table->getName(); |
114
|
|
|
|
115
|
|
|
if (self::$schema[$connection][$tableName] ?? false) { |
116
|
|
|
return [$tableName => self::$schema[$connection][$tableName]]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (is_array($table)) { |
120
|
|
|
$table = new Table($tableName, self::mapTableColumns($connection, $tableName)); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return [$tableName => $table]; |
124
|
|
|
})->toArray(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private static function getIndexColumnNames(string $connection, string $table) |
128
|
|
|
{ |
129
|
|
|
$schemaManager = self::getSchemaManager($connection); |
130
|
|
|
$indexes = method_exists($schemaManager, 'listTableIndexes') ? $schemaManager->listTableIndexes($table) : $schemaManager->getIndexes($table); |
|
|
|
|
131
|
|
|
|
132
|
|
|
$indexes = array_map(function ($index) { |
133
|
|
|
return is_array($index) ? $index['columns'] : $index->getColumns(); |
134
|
|
|
}, $indexes); |
135
|
|
|
|
136
|
|
|
$indexes = \Illuminate\Support\Arr::flatten($indexes); |
137
|
|
|
|
138
|
|
|
return array_unique($indexes); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
private static function mapTableColumns(string $connection, string $table) |
142
|
|
|
{ |
143
|
|
|
$indexedColumns = self::getIndexColumnNames($connection, $table); |
144
|
|
|
|
145
|
|
|
return LazyCollection::make(self::getSchemaManager($connection)->getColumns($table))->mapWithKeys(function ($column, $key) use ($indexedColumns) { |
|
|
|
|
146
|
|
|
$column['index'] = array_key_exists($column['name'], $indexedColumns) ? true : false; |
147
|
|
|
|
148
|
|
|
return [$column['name'] => $column]; |
149
|
|
|
})->toArray(); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private static function getCreateSchema(string $connection) |
153
|
|
|
{ |
154
|
|
|
$schemaManager = self::getSchemaManager($connection); |
155
|
|
|
|
156
|
|
|
return method_exists($schemaManager, 'createSchema') ? $schemaManager->createSchema() : $schemaManager; |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
private static function getSchemaManager(string $connection) |
160
|
|
|
{ |
161
|
|
|
$connection = DB::connection($connection); |
162
|
|
|
|
163
|
|
|
return method_exists($connection, 'getDoctrineSchemaManager') ? $connection->getDoctrineSchemaManager() : $connection->getSchemaBuilder(); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.