22
33namespace Backpack \CRUD \app \Library \Database ;
44
5+ use Illuminate \Support \Arr ;
56use Illuminate \Support \Facades \DB ;
67use Illuminate \Support \LazyCollection ;
78
@@ -12,28 +13,39 @@ final class DatabaseSchema
1213 /**
1314 * Return the schema for the table.
1415 */
15- public static function getForTable (string $ connection , string $ table )
16+ public static function getForTable (string $ table , string $ connection )
1617 {
1718 $ connection = $ connection ?: config ('database.default ' );
1819
19- self ::generateDatabaseSchema ($ connection );
20+ self ::generateDatabaseSchema ($ connection, $ table );
2021
2122 return self ::$ schema [$ connection ][$ table ] ?? null ;
2223 }
2324
2425 public static function getTables (string $ connection = null ): array
2526 {
2627 $ connection = $ connection ?: config ('database.default ' );
27- self ::generateDatabaseSchema ($ connection );
2828
29- return self ::$ schema [$ connection ] ?? [];
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 ];
3042 }
3143
3244 public function listTableColumnsNames (string $ connection , string $ table )
3345 {
34- $ table = self ::getForTable ($ connection , $ table );
46+ $ table = self ::getForTable ($ table , $ connection );
3547
36- return array_keys ($ table ->getColumns ());
48+ return $ table ? array_keys ($ table ->getColumns ()) : [] ;
3749 }
3850
3951 public function listTableIndexes (string $ connection , string $ table )
@@ -51,59 +63,53 @@ public function getManager(string $connection = null)
5163 /**
5264 * Generates and store the database schema.
5365 */
54- private static function generateDatabaseSchema (string $ connection )
66+ private static function generateDatabaseSchema (string $ connection, string $ table )
5567 {
56- if (! isset (self ::$ schema [$ connection ])) {
57- self ::$ schema [$ connection ] = self ::mapTables ($ connection );
68+ if (! isset (self ::$ schema [$ connection ][ $ table ] )) {
69+ self ::$ schema [$ connection ][ $ table ] = self ::mapTable ($ connection, $ table );
5870 }
5971 }
6072
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 )
73+ private static function mapTable (string $ connection , string $ tableName )
6874 {
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- }
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+ }
7982
80- return [ $ tableName => $ table];
81- })-> toArray () ;
82- }
83+ if (! is_array ( $ table ) || empty ( $ table)) {
84+ return $ table ;
85+ }
8386
84- private static function getIndexColumnNames (string $ connection , string $ table )
85- {
8687 $ schemaManager = self ::getSchemaManager ($ connection );
87- $ indexes = method_exists ( $ schemaManager, ' listTableIndexes ' ) ? $ schemaManager -> listTableIndexes ( $ table ) : $ schemaManager -> getIndexes ($ table );
88+ $ indexes = $ schemaManager-> getIndexes ($ tableName );
8889
8990 $ indexes = array_map (function ($ index ) {
90- return is_array ( $ index) ? $ index ['columns ' ] : $ index -> getColumns () ;
91+ return $ index ['columns ' ];
9192 }, $ indexes );
9293
93- $ indexes = \ Illuminate \ Support \Arr:: flatten ( $ indexes );
94+ $ table = new Table ( $ tableName , $ table );
9495
95- return array_unique ($ indexes );
96+ $ indexes = Arr::flatten ($ indexes );
97+ $ table ->setIndexes (array_unique ($ indexes ));
98+
99+ return $ table ;
96100 }
97101
98- private static function mapTableColumns (string $ connection , string $ table )
102+ private static function getIndexColumnNames (string $ connection , string $ table )
99103 {
100- $ indexedColumns = self ::getIndexColumnNames ($ connection , $ table );
104+ self ::generateDatabaseSchema ($ connection , $ table );
101105
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 ;
106+ $ indexes = self ::$ schema [$ connection ][$ table ]->getIndexes ();
104107
105- return [$ column ['name ' ] => $ column ];
106- })->toArray ();
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 );
107113 }
108114
109115 private static function getCreateSchema (string $ connection )
0 commit comments