77use Closure ;
88use MongoDB \Collection ;
99use MongoDB \Driver \Exception \ServerException ;
10+ use MongoDB \Laravel \Connection ;
1011use MongoDB \Model \CollectionInfo ;
1112use MongoDB \Model \IndexInfo ;
1213
1617use function array_keys ;
1718use function array_map ;
1819use function array_merge ;
20+ use function array_values ;
1921use function assert ;
2022use function count ;
2123use function current ;
2224use function implode ;
2325use function in_array ;
26+ use function is_array ;
27+ use function is_string ;
2428use function iterator_to_array ;
2529use function sort ;
2630use function sprintf ;
2731use function str_ends_with ;
2832use function substr ;
2933use function usort ;
3034
35+ /** @property Connection $connection */
3136class Builder extends \Illuminate \Database \Schema \Builder
3237{
3338 /**
@@ -137,9 +142,10 @@ public function dropAllTables()
137142 }
138143 }
139144
140- public function getTables ()
145+ /** @param string|null $schema Database name */
146+ public function getTables ($ schema = null )
141147 {
142- $ db = $ this ->connection ->getMongoDB ( );
148+ $ db = $ this ->connection ->getDatabase ( $ schema );
143149 $ collections = [];
144150
145151 foreach ($ db ->listCollectionNames () as $ collectionName ) {
@@ -165,9 +171,23 @@ public function getTables()
165171 return $ collections ;
166172 }
167173
168- public function getTableListing ()
174+ public function getTableListing ($ schema = null , $ schemaQualified = true )
169175 {
170- $ collections = iterator_to_array ($ this ->connection ->getMongoDB ()->listCollectionNames ());
176+ $ collections = [];
177+
178+ if ($ schema === null || is_string ($ schema )) {
179+ $ collections [$ schema ?? '' ] = iterator_to_array ($ this ->connection ->getDatabase ($ schema )->listCollectionNames ());
180+ } elseif (is_array ($ schema )) {
181+ foreach ($ schema as $ db ) {
182+ $ collections [$ db ] = iterator_to_array ($ this ->connection ->getDatabase ($ db )->listCollectionNames ());
183+ }
184+ }
185+
186+ if ($ schema && $ schemaQualified ) {
187+ $ collections = array_map (fn ($ db , $ collections ) => array_map (static fn ($ collection ) => $ db . '. ' . $ collection , $ collections ), array_keys ($ collections ), $ collections );
188+ }
189+
190+ $ collections = array_merge (...array_values ($ collections ));
171191
172192 sort ($ collections );
173193
0 commit comments